-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
36 lines (31 loc) · 1.27 KB
/
gatsby-node.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
const fs = require("fs-extra");
const path = require("path");
exports.onPostBuild = ({ store }) => {
// Only run if we're in a firebase build context
if (!process.env.IS_FIREBASE_BUILD) return;
const { redirects, program } = store.getState();
// The path to write the file `firebase.json`
const firebaseConfigPath = path.join(program.directory, "./firebase.json");
// Return a promise chain
return (
fs
.readFile(firebaseConfigPath)
.then((data) => {
const firebaseConfig = JSON.parse(data)
if (!firebaseConfig.hosting.redirects)
firebaseConfig.hosting.redirects = []
redirects.map(r => {
firebaseConfig.hosting.redirects.push({
source: r.fromPath,
destination: r.toPath,
type: r.isPermanent ? 301 : 302
})
})
fs.writeFile(firebaseConfigPath, JSON.stringify(firebaseConfig));
})
.catch(e => {
// Log any errors thrown, e.g. the file doesn't exist
console.error("onPostBuild error #hq0Kxa", JSON.stringify(e));
})
);
};