-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostReactBuild.js
101 lines (83 loc) · 2.46 KB
/
postReactBuild.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const filesToReplace = [
{ regexString: "css/main" },
{ regexString: "js/main" },
// { regexString: "js/\\d\\." },
]
const copyDestinations = [
// "../gullibot.github.io/static-root/static",
"../reasonscore.github.io/static"
]
const fs = require('fs')
const Path = require('path');
function copyDir(src, dest) {
const entries = fs.readdirSync(src, { withFileTypes: true });
fs.mkdirSync(dest);
for (let entry of entries) {
const srcPath = Path.join(src, entry.name);
const destPath = Path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
// Gather up the cache busting file names
let indexHtml
try {
indexHtml = fs.readFileSync('build/index.html', 'utf8')
} catch (err) {
console.error(err)
}
// indexHtml = indexHtml.replace(/"\/static\//g, '"static/');
for (const fileInfo of filesToReplace) {
const newFileName = indexHtml.match(
new RegExp(
'"(/static/' +
fileInfo.regexString +
'[^"]*)'
)
)
if (newFileName) {
fileInfo.newfileName = newFileName[1]
}
}
// Replace the file names in the javascript file
let ReasonScoreFull2Js
try {
ReasonScoreFull2Js = fs.readFileSync('build/static/js/ReasonScoreFull2.js', 'utf8')
} catch (err) {
console.error(err)
}
for (const fileInfo of filesToReplace) {
fileInfo.oldfileName = ReasonScoreFull2Js.match(
new RegExp(
'(/static/' +
fileInfo.regexString +
'[^"]*)'
)
)[1]
ReasonScoreFull2Js = ReasonScoreFull2Js.replace(
new RegExp(
'(/static/' +
fileInfo.regexString +
'[^"]*)'
),
fileInfo.newfileName // TODO: simplify all the quotes
)
}
ReasonScoreFull2Js = ReasonScoreFull2Js.replace('rootAddress + "/static/js/bundle.js",', '');
fs.writeFileSync('build/static/js/ReasonScoreFull2.js', ReasonScoreFull2Js);
// Delete Unused Files
fs.unlinkSync(`build/index.html`);
if (fs.existsSync(`build/service-worker.js`)) {
fs.unlinkSync(`build/service-worker.js`);
}
fs.unlinkSync(`build/asset-manifest.json`);
//fs.unlinkSync(`precache-manifest`); //TODO: Figure out deletion of manifest
// Copy the fils to other projects
for (const dest of copyDestinations) {
fs.rmdirSync(dest, { recursive: true });
copyDir("./build/static", dest);
}