Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phani1kumar/2a1f59ec427b0c37dc0153e9813d9a6d to your computer and use it in GitHub Desktop.
Save phani1kumar/2a1f59ec427b0c37dc0153e9813d9a6d to your computer and use it in GitHub Desktop.
Include Server Push headers in firebase.json from push-manifest.json generated by Polymer Build
#!/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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment