-
Notifications
You must be signed in to change notification settings - Fork 6
/
processManifest.js
50 lines (42 loc) · 1.32 KB
/
processManifest.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
const _ = require('lodash')
const dotenv = require('dotenv')
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')
module.exports = async function processManifest(manifestData) {
const stageName = Object.keys(manifestData)
const { outputs } = manifestData[stageName]
const getOutputValue = (key) => {
console.log(`loading output value for [${key}]`)
const output = _.find(outputs, x => x.OutputKey === key)
if (!output) {
throw new Error(`No output found for ${key}`)
}
return output.OutputValue
}
const dotEnvFile = path.resolve('.env')
await updateDotEnv(dotEnvFile, {
API_URL: getOutputValue('TwitterGraphQlApiUrl'),
})
}
/* Utils, typically this would be a package includes from NPM */
async function updateDotEnv(filePath, env) {
// Merge with existing values
try {
const existing = dotenv.parse(await promisify(fs.readFile)(filePath, 'utf-8'))
env = Object.assign(existing, env)
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
const contents = Object.keys(env).map(key => format(key, env[key])).join('\n')
await promisify(fs.writeFile)(filePath, contents)
return env
}
function escapeNewlines (str) {
return str.replace(/\n/g, '\\n')
}
function format (key, value) {
return `${key}=${escapeNewlines(value)}`
}