Last active
June 21, 2019 13:14
-
-
Save sbinlondon/b8dfaa22e4ed1a7ec63353c2e631ea4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
const { execSync } = require('child_process'); | |
const fs = require('fs'); | |
const cmd = "grep -r --exclude-dir=node_modules 'process.env' ./" | |
const stdout = execSync(cmd, { encoding: 'UTF-8' }); | |
// scrape envs from code | |
function getEnvs(input) { | |
const arr = input.toString().split('\n'); | |
const newArr = []; | |
arr.forEach((line) => { | |
const re = new RegExp('(?<=\\bprocess.env.\\b)([A-Z]+[_]*)*', 'g'); | |
const env = line.match(re); | |
if (env !== null) | |
newArr.push(env); | |
}); | |
var flatArray = newArr.reduce((a, b) => { | |
return a.concat(b); | |
}); | |
const unique = (envs) => envs.filter((a, b) => envs.indexOf(a) === b); | |
const envs = unique(flatArray).sort(); | |
return envs | |
} | |
const envs = getEnvs(stdout); | |
// turn envs into object to be used by assert-envs | |
function envsToJson(array) { | |
let obj = {} | |
array.forEach((env) => { | |
const entry = {}; | |
entry[`${env}`] = { 'required': true } | |
Object.assign(obj, entry) | |
}); | |
let json = { "env": obj } | |
return json | |
} | |
const envsObject = envsToJson(envs); | |
// create app.json file in the root | |
function writeJsonFile(object) { | |
object = JSON.stringify(object); | |
if (object !== undefined) { | |
try { | |
fs.writeFileSync('app.json', object, (err) => { | |
if (err) throw err | |
console.log('App.json written successfully'); | |
}); | |
} catch (err) { | |
console.warn('Error writing env vars to app.json', err); | |
} | |
} | |
}; | |
writeJsonFile(envsObject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment