Last active
November 13, 2020 21:17
-
-
Save dsandor/04eac83b8692ca88237976443050b508 to your computer and use it in GitHub Desktop.
test
This file contains 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 | |
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(); |
This file contains 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
{"name": "stackery-template-token-replace", "version": "0.0.0", "bin": "./index.js"} |
This file contains 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
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