Skip to content

Instantly share code, notes, and snippets.

@dsandor
Last active November 13, 2020 21:17
Show Gist options
  • Save dsandor/04eac83b8692ca88237976443050b508 to your computer and use it in GitHub Desktop.
Save dsandor/04eac83b8692ca88237976443050b508 to your computer and use it in GitHub Desktop.
test
#!/usr/bin/env node
const fs = require('fs');
const { processTemplate } = require('./processor')
function run() {
const templateFilePath = '../.stackery/template.yaml';
const templateExists = fs.existsSync(templateFilePath);
if (!templateExists) {
console.log('Could not find template file:', templateFilePath, 'cwd:', process.cwd());
process.exit(1);
}
processTemplate();
}
console.log('node arguments:', process.argv);
console.log('env:', process.env);
run();
{"name": "stackery-template-token-replace", "version": "0.0.0", "bin": "./index.js"}
const fs = require('fs');
const tokenEnvNamePrefix = 'STACKERY_TOKEN_';
const templateFilePath = '../.stackery/template.yaml';
function processTemplate() {
const templateExists = fs.existsSync(templateFilePath);
if (!templateExists) {
console.log('Could not find template file:', templateFilePath, 'cwd:', process.cwd());
process.exit(1);
}
const templateOriginal = fs.readFileSync(templateFilePath, 'utf8');
const tokens = getTokens(process.env);
console.log('Found tokens:', tokens);
let templateUpdated = templateOriginal;
for (const [name, value] of tokens) {
const tokenRegex = new RegExp(`<${name}>`, 'ig');
templateUpdated = templateUpdated.replace(tokenRegex, 'dev');
}
console.log('Updated template:\n', templateUpdated);
fs.writeFileSync(templateFilePath, templateUpdated);
console.log('Wrote new template.');
}
function getTokens(processEnv) {
const tokenMapArray = Object
.keys(process.env)
.filter(p => p.toUpperCase().indexOf(tokenEnvNamePrefix) >= 0)
.map(name => [name.substring(tokenEnvNamePrefix.length), process.env[name]]);
return new Map(tokenMapArray);
}
module.exports = {
processTemplate,
getTokens,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment