Skip to content

Instantly share code, notes, and snippets.

@jgermade
Last active February 3, 2021 17:59
Show Gist options
  • Save jgermade/aa4276998290d08cb17a6374f036aa14 to your computer and use it in GitHub Desktop.
Save jgermade/aa4276998290d08cb17a6374f036aa14 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
process.stdin.resume()
process.stdin.setEncoding('utf8')
function envsubst (str) {
return str
.replace(/\${ *([^}]+?) *}/g, (matched, var_name) => {
return process.env[var_name] || matched
})
}
var input = ''
process.stdin.on('data', function(chunk) {
input += chunk
if (/\n/.test(input)) {
let lines = input.split('\n')
input = lines.pop()
lines.forEach(line => process.stdout.write(envsubst(line) + '\n'))
}
})
process.stdin.on('end', ()=> {
process.stdout.write(envsubst(input) + '\n')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment