Skip to content

Instantly share code, notes, and snippets.

@beaulac
Last active May 1, 2018 21:47
Show Gist options
  • Save beaulac/9c387f2eb609950026e265b7201a8e78 to your computer and use it in GitHub Desktop.
Save beaulac/9c387f2eb609950026e265b7201a8e78 to your computer and use it in GitHub Desktop.
Hacky script to turn JSON into a .env file and spit out corresponding JS object to clipboard (mac only for now)
'use strict';
const path = require('path');
const fs = require('fs');
const argv = process.argv.slice(2);
let [filename, prefix = ''] = argv;
if (prefix) {
prefix = `${prefix}_`;
}
const jsonPath = path.resolve(__dirname, filename);
const outputPath = '.env_generated';
const envObj = require(jsonPath);
let envString = '{';
const envGetter = envKey => `process.env["${envKey}"]`;
const envFile = Object.keys(envObj).map(k => {
const envKey = `${prefix}${k.toUpperCase()}`;
envString += `\n\t"${k}": ${envGetter(envKey)},`;
const envVal = envObj[k];
const envValue = `"${envVal.replace(/\r?\n/g, '\\n')}"`;
return `${envKey}=${envValue}`;
}).join('\n');
envString += '\n}';
fs.writeFileSync(outputPath, envFile);
const cp = require('child_process');
const clip = cp.exec('pbcopy', (_err, _stdout, _stderr) => {/* optionally do something */});
clip.stdin.write(envString);
clip.stdin.end(() => process.stdout.write(`\rCopied ${envString.length} characters.\n`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment