-
Notifications
You must be signed in to change notification settings - Fork 45
/
prebuild.js
42 lines (33 loc) · 965 Bytes
/
prebuild.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
const { execSync } = require("child_process");
const fs = require("fs");
const envLocalFile = ".env.local";
function cleanUpNewlines() {
data = data.replace(/[\n]{2,}/g, "\n");
}
function updateGitHash() {
const key = "REACT_APP_GIT_HASH";
const regex = new RegExp(`${key}=.*`);
const gitHash = execSync("git rev-parse HEAD").toString();
if (data.indexOf(key) === -1) {
data += `${key}=0`;
}
data = data.replace(regex, `${key}=${gitHash}\n`);
}
function updateBuildDate() {
const key = "REACT_APP_BUILD_DATE";
const regex = new RegExp(`${key}=.*`);
if (data.indexOf(key) === -1) {
data += `${key}=0`;
}
data = data.replace(regex, `${key}=${new Date().toISOString()}\n`);
}
function writeToEnvLocal() {
cleanUpNewlines();
fs.writeFileSync(envLocalFile, data);
}
let data =
fs.readFileSync(envLocalFile, { flag: "a+" }).toString().trim() + "\n";
cleanUpNewlines();
updateBuildDate();
updateGitHash();
writeToEnvLocal();