Created
April 28, 2018 15:32
-
-
Save andrewthauer/14ffe2ac112e626b45d1e901cf184d63 to your computer and use it in GitHub Desktop.
Node Script - Replace Values in File
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
#!/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