Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Last active October 2, 2020 11:02
Show Gist options
  • Save stefanocudini/2e6675b145e8938e8967d4f036959c93 to your computer and use it in GitHub Desktop.
Save stefanocudini/2e6675b145e8938e8967d4f036959c93 to your computer and use it in GitHub Desktop.
environment vars to template to file
/*
usage:
cat config.template.yml | VAR1=test node envtmpl.js > config.valued.yml
*/
const fs = require('fs');
//const tmplReg = /\$\{(.+?)\}/g
const tmplReg = /\$\{([\w_\-]+)\}/g
//const tmplReg = /\{ *([\w_\-]+) *\}/g
function tmpl(str, data, double) {
return str.replace(tmplReg, function (str, key) {
var value = data[key];
if (value === undefined)
value = "${"+key+"}";
return value;
});
}
try {
process.stdout.write( tmpl(fs.readFileSync(0, "utf-8"), process.env) );
} catch (e) {
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment