-
Notifications
You must be signed in to change notification settings - Fork 15
/
buildEnv.js
112 lines (107 loc) · 3.25 KB
/
buildEnv.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
102
103
104
105
106
107
108
109
110
111
112
const fs = require("fs-extra")
const { exec } = require("child_process")
const file = ".env"
const debug = false
var isDev = process.argv[2] === "dev"
function execShellCommand(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(error)
reject(error)
}
if (stdout) {
resolve(stdout.replace(/(\r\n|\n|\r)/gm, "")) //remove all line breaks
} else {
reject(stderr)
}
})
})
}
function prepareDestination() {
return new Promise((resolve, reject) => {
fs.pathExists(file).then((exists) => {
if (exists) {
fs.remove(file, (err) => {
if (err) {
console.error(`error removing existing ${file} file`)
reject(err)
} else {
debug && console.log(`removed existing ${file} file`)
resolve(true)
}
})
} else {
debug && console.log(`${file} file does not exist`)
resolve(true)
}
})
}).catch((err) => {
console.error(`error removing existing ${file} file`)
reject(err)
})
}
function writeFile() {
return new Promise((resolve, reject) => {
debug && console.log(`writing ${file} file`)
execShellCommand("git rev-list --count HEAD")
.then((headCount) => {
execShellCommand("git describe --tags --exact-match --abbrev=0 || echo dev")
.then((description) => {
execShellCommand("git rev-parse HEAD ")
.then((latestCommit) => {
execShellCommand("git rev-list --tags --max-count=1")
.then((prodCommit) => {
const latest = latestCommit === prodCommit
debug && console.log({ headCount, description })
fs.outputFile(
file,
[
"SKIP_PREFLIGHT_CHECK=true",
"NODE_ENV=production",
`REACT_APP_GIT_NUM=${headCount}`,
`REACT_APP_GIT_SHA=${description}`,
`REACT_APP_LATEST_LAMP=${latest}`,
isDev ? "BROWSER=none" : "CI=false",
].join("\r\n")
)
.then(() => resolve(true))
.catch((err) => {
console.error(`error writing ${file} file`)
reject(err)
})
.catch((err) => {
reject(err)
})
})
.catch((err) => resolve(err))
})
.catch((err) => {
resolve(err)
})
}).catch((err) => reject(err))
}).catch((err) => reject(err))
}).catch((err) => reject(err))
}
prepareDestination()
.then((success) => {
if (success) {
writeFile()
.then(() => {
console.log(`${file} file built successfully`)
process.exit(0) // Success
})
.catch((err) => {
console.error(`error building ${file} file`)
process.exit(1) // Failure
})
} else {
console.error(`${file} file preparation failed`)
process.exit(1) // Failure
}
})
.catch((err) => {
console.error(`${file} file preparation failed`)
console.error(err)
process.exit(1) // Failure
})