-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·55 lines (55 loc) · 1.59 KB
/
index.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
#!/usr/bin/env node
require('colors');
const path = require("path")
const glob = require("glob")
const Promise = require('bluebird');
const Diff = require('diff');
const fs = Promise.promisifyAll(require('fs'));
const configRegex = /\"runtimeConfig\":({.*?})/g;
(async () => {
const nextConfigJsPath = path.resolve(process.cwd(), "next.config.js");
const nextConfig = require(nextConfigJsPath)
const {
publicRuntimeConfig
} = nextConfig
const nextBuildGlobPattern = path.resolve(process.cwd(), ".next", "**", "*.{js,html,json}")
const files = glob.sync(nextBuildGlobPattern);
let oldConfig
const promises = []
for(const filePath of files) {
promises.push((async () => {
try {
const isDirectory = fs.lstatSync(filePath).isDirectory()
if(isDirectory) {
return
}
const fileContent = await fs.readFileAsync(filePath, "utf8")
const match = configRegex.exec(fileContent)
if(!oldConfig && match?.length > 0) {
oldConfig = JSON.parse(match[1])
}
const newFileContent = fileContent
.replaceAll(
configRegex,
`\"runtimeConfig\":${JSON.stringify(publicRuntimeConfig)}`
)
await fs.writeFileAsync(filePath, newFileContent, "utf8")
}
catch(e) {
console.log("file", filePath)
console.log(e)
}
})())
}
await Promise.all(promises)
const diff = Diff.diffChars(
JSON.stringify(oldConfig, null, 2),
JSON.stringify(publicRuntimeConfig, null, 2)
)
process.stdout.write("Changes:\n");
diff.forEach((part) => {
const color = part.added ? 'green' :
part.removed ? 'red' : 'grey';
process.stdout.write(part.value[color]);
});
})()