-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildWranglerToml.js
41 lines (28 loc) · 1.06 KB
/
buildWranglerToml.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
const fs = require('fs')
const { config } = require('dotenv')
const uniq = require('lodash/uniq')
const pkg = require('./package.json')
const isDev = process.env.NODE_ENV === 'development'
const path = isDev ? '.env' : '.env.production'
const { parsed } = config({ path })
parsed.RELEASE_VERSION = pkg.version
const tpl = fs.readFileSync('./wrangler.template.toml').toString('utf-8')
const envVarsRegex = /\(\$([A-Z0-9_]+)\)/g
const matches = [...tpl.matchAll(envVarsRegex)]
const uniqueMatches = uniq(matches.map((match) => match[1]))
let res = tpl
uniqueMatches.forEach((token) => {
const envVar = process.env[token]
console.log(`Replacing ($${token}) with ${envVar}`)
delete parsed[token]
res = res.replace(new RegExp(`\\(\\$${token}\\)`, 'g'), envVar)
})
const otherVarsToml = Object.keys(parsed)
.map((key) => {
console.log(`Adding ${key}`)
return `${key} = "${parsed[key]}"`
})
.join('\n')
res = res.replace(new RegExp('\\[vars\\]', 'g'), `[vars]\n${otherVarsToml}`)
fs.writeFileSync('./wrangler.toml', res)
console.log('Wrote wrangler.toml')