This repository has been archived by the owner on Jan 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirebase-from-push.js
77 lines (69 loc) · 2.21 KB
/
firebase-from-push.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const util = require('util');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
function throwError(err) {
throw err;
}
function throwError(err) {
throw err;
}
function prependSlash(string) {
return string.charAt(0) === '/' ? string : '/' + string;
}
function generateFirebaseHeadersFromPushManifest(pushManifest) {
return Object.keys(pushManifest).map((source) => {
return {
source: prependSlash(source),
headers: [{
key: "Link",
value: Object.keys(pushManifest[source]).map((value) => {
return [
"<" + prependSlash(value) + ">",
"rel=preload",
"as=" + pushManifest[source][value].type
].join(";");
}).join(',')
}]
};
});
}
function readFilesFromPublicPath(publicPath) {
const firebasePath = path.join(publicPath, "firebase.json");
const readFirebaseFile = readFile(firebasePath).then(JSON.parse);
const pushManifestPath = path.join(publicPath, "push-manifest.json");
const generateHeaders = readFile(pushManifestPath)
.then(JSON.parse)
.then(generateFirebaseHeadersFromPushManifest);
return Promise.all([readFirebaseFile, generateHeaders])
}
function addNewHeadersToFirebase(firebase, newHeaders) {
debugger;
const headers = (firebase.hosting.headers || []).concat(newHeaders);
firebase.hosting.headers = headers;
return firebase;
}
function removeDuplicatedHeaders(firebase) {
debugger;
const headers = firebase.hosting.headers.filter((header, index, self) => {
return self.findIndex((t) => {
return t.source === header.source;
}) === index
});
firebase.hosting.headers = headers;
return firebase;
}
readFile("firebase.json")
.then(JSON.parse)
.then((json) => json.hosting.public)
.then((publicPath) => {
const firebasePath = path.join(publicPath, "firebase.json");
return readFilesFromPublicPath(publicPath)
.then((results) => addNewHeadersToFirebase.apply(this, results))
.then(removeDuplicatedHeaders)
.then((data) => JSON.stringify(data, null, ' '))
.then((data) => writeFile(firebasePath, data));
})
.catch(throwError);