-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetenv.js
84 lines (67 loc) · 2.4 KB
/
setenv.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
'use strict'
const fs = require('fs')
const crypto = require('crypto')
const path = require('path')
const envFileRegEx = /env.([a-z0-9]*.)?js/g
const buildDir = './build'
// Generate the configuration script from environment variables
function generateConfigScript() {
const prefixRegexp = /^REACT_APP_/
const env = process.env
const config = Object.keys(env)
.filter(key => prefixRegexp.test(key))
.reduce((c, key) => Object.assign({}, c, { [key]: env[key] }), {})
console.log(config)
return `window.env=${JSON.stringify(config)};`
}
// Save the configuration script to disk
function saveConfigScript(scriptContents, scriptName) {
const scriptPath = path.join(buildDir, scriptName)
console.log('Saving file to: ' + scriptPath)
fs.writeFile(scriptPath, scriptContents, 'utf8', function (err) {
if (err) throw err
console.log('The file was saved!')
})
}
// Update configuration script name in index.html
function updateIndex(scriptName) {
const indexPath = path.join(buildDir, 'index.html')
fs.readFile(indexPath, 'utf8', function (err, data) {
if (err) throw err
let result = data.replace(envFileRegEx, scriptName)
fs.writeFile(indexPath, result, 'utf8', function (err) {
if (err) throw err
var indexHash = crypto.createHash('md5').update(result).digest('hex')
updateIndexInServiceWorker(indexHash)
})
})
}
// Update index hash in service-worker.js
function updateIndexInServiceWorker(indexHash) {
const swPath = path.join(buildDir, 'service-worker.js')
fs.readFile(swPath, 'utf8', function (err, data) {
if (err) throw err
let result = data.replace(/"\/index.html","(.*?)"/, `"/index.html","${indexHash}"`)
fs.writeFile(swPath, result, 'utf8', function (err) {
if (err) throw err
})
})
}
// Delete old configuration scripts from disk
function deleteOldScript(currentScriptName) {
const files = fs.readdirSync(buildDir).filter(fn => fn != currentScriptName && fn.match(envFileRegEx))
files.forEach(fn => {
let filePath = path.join(buildDir, fn)
fs.unlink(filePath, err => {
if (err) throw err
})
})
}
// MAIN
console.log('Setting runtime envoronment..')
let scriptContents = generateConfigScript()
let scriptHash = crypto.createHash('md5').update(scriptContents).digest('hex')
let scriptName = `env.${scriptHash}.js`
saveConfigScript(scriptContents, scriptName)
updateIndex(scriptName)
deleteOldScript(scriptName)