Skip to content

Instantly share code, notes, and snippets.

@andrewthauer
Created April 28, 2018 15:32
Show Gist options
  • Save andrewthauer/14ffe2ac112e626b45d1e901cf184d63 to your computer and use it in GitHub Desktop.
Save andrewthauer/14ffe2ac112e626b45d1e901cf184d63 to your computer and use it in GitHub Desktop.
Node Script - Replace Values in File
#!/usr/bin/env node
/* eslint-env node */
/* eslint-disable no-console */
const path = require('path');
const replace = require('replace');
const argv = require('yargs').argv;
const targetPath = path.join(__dirname, '../dist/');
if (!argv.env || !argv.env.path) {
throw Error('The `env.path` is not set');
}
const envPath = argv.env.path;
const dotenvConfig = require('dotenv').config({ path: envPath });
const config = dotenvConfig.parsed;
for (const key in config) {
const value = config[key];
console.log(`Replacing - ${key}: ${value}`);
replace({
regex: `__${key}__`,
replacement: value,
paths: [targetPath],
include: '*.js',
recursive: true,
silent: false,
});
}
// All done
console.log('Done replacing values');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment