forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateRedirects.js
More file actions
43 lines (34 loc) · 1.11 KB
/
generateRedirects.js
File metadata and controls
43 lines (34 loc) · 1.11 KB
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
const { resolve } = require('path');
const { readFileSync, writeFileSync } = require('fs');
const safeJSON = require('./safeJSON');
const updateRedirects = redirects => {
const firebaseConfig = resolve(__dirname, '../firebase.json');
const firebaseJSON = safeJSON.parse(readFileSync(firebaseConfig));
// Map data as firebase expects it to be
const firebaseRedirects = {};
Object.entries(redirects).forEach(([key, value]) => {
firebaseRedirects[key] = {
source: key,
destination: value,
type: '302',
};
});
// Delete keys that already exists in the firebase.json
Object.entries(firebaseJSON.redirects).forEach(([key]) => {
delete firebaseRedirects[key];
});
// Serialize the redirects as firebase expects it to be
const newRedirects = [];
Object.keys(firebaseRedirects).forEach(value =>
newRedirects.push({
source: value,
destination: firebaseRedirects[value].destination,
type: '301',
})
);
writeFileSync(
firebaseConfig,
safeJSON.toString({ ...firebaseJSON, redirects: newRedirects }, null, 2)
);
};
module.exports = updateRedirects;