-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild-tpl.js
50 lines (42 loc) · 1.32 KB
/
build-tpl.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
const fs = require('fs').promises;
const config = require('./config');
config.fcmEnabled = !!(config.fcmPublicVapidKey && config.firebaseConfig.apiKey);
// Do not change array order
const tpls = [
'./fcm-tpl.html',
'./global-variables-tpl.html',
'./firebase-messaging-sw-tpl.js',
'./index-tpl.html',
'./service-worker-tpl.js'
];
function insertData(content) {
return content.replace(/{{(.*?)}}/g, ($0, val) => {
if (val in config) {
if (typeof config[val] === 'object') {
return JSON.stringify(config[val]);
}
return config[val] + '';
}
return '';
});
}
async function main() {
// load all tpls
const result = await Promise.all(tpls.map(file => fs.readFile(file, { encoding: 'utf8' })));
// index.html
const fcmTpl = insertData(result[0]);
const globalVariablesTpl = insertData(result[1]);
const indexContent = result[3]
.replace(/<!-- insert global variables here -->/, globalVariablesTpl)
.replace(/<!-- insert fcm here -->/, fcmTpl);
// firebase-messaging-sw.js
const fcmContent = insertData(result[2]);
// service-worker.js
const swContent = insertData(result[4]);
await Promise.all([
fs.writeFile('./index.html', indexContent),
fs.writeFile('./firebase-messaging-sw.js', fcmContent),
fs.writeFile('./service-worker.js', swContent)
]);
}
main();