-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswGenerator.js
44 lines (33 loc) · 1013 Bytes
/
swGenerator.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
const path = require('path');
const fs = require('fs');
const readDir = (dir) => {
const directoryPath = path.join(__dirname, dir);
return new Promise((acc) => fs.readdir(directoryPath, (err, files) => acc(files)));
}
const writeFile = (file, content) => {
const filePath = path.join(__dirname, file);
return new Promise( acc => {
fs.writeFile(filePath,content,acc);
});
}
const readFile = (file) => {
const filePath = path.join(__dirname, file);
return new Promise( (acc,rej) => {
fs.readFile(filePath,'utf8', (err, data) => {
if (err) {
rej(err)
}
acc(data)
});
});
}
const found = ['/', '/library', '/discover', '/settings', '/podcast'];
readDir('dist')
.then(files => files.forEach((file) => found.push(`/${file}`)))
.then(()=> {
readFile('/dist/service-worker.js')
.then( data => {
const readyToWrite = data.replace("addAll([])",`addAll(${JSON.stringify(found)})`);
writeFile("/dist/service-worker.js", readyToWrite)
})
});