danday74 / envsub Goto Github PK
View Code? Open in Web Editor NEWNPM - envsub is envsubst for Node.js
License: Other
NPM - envsub is envsubst for Node.js
License: Other
Currently if you use --env
or --env-file
, there is no way to also include system environment variables. For example:
# .env
ENV1=env1
ENV2=env2
# this will only substitute ENV3
$ export ENV3=env3 envsub file1 file2
# this will only substitute ENV1 and ENV2
$ export ENV3=env3 envsub file1 file2 --env-file .env
I think there are two possible ways to handle this:
--system
to work with --env-file
like it works with --env
--env-system
that works similar to --env-file
except it uses the system environment as well.Thanks for the good work though
This could be a very useful utility for us. Unfortunately, it doesn't support writing the result to stdout which prevents us from using it. Could you consider adding an option for it?
I am trying to validate the creation and substitution of both template and output file in a jest test.
It works perfectly outside of a jest test --> plain js script. Am I missing out on something or is this a bug?
code(node js):
constant:
module.exports = Object.freeze({
DEFAULT_ENVIRONMENT_SUBSTITUTION_OPTIONS: {
all: true,
diff: true,
system: true,
syntax: 'dollar-both', // env pattern: ${} and $
envs: [{ name: 'release_version' }, { name: 'repo_suffix' }],
}
});
function:
const substituteEnvForAllFiles = (filePathsArray, options=DEFAULT_ENVIRONMENT_SUBSTITUTION_OPTIONS) => {
let outputFiles = [];
filePathsArray.forEach((value) => {
if (value === '') return;
const output = value.replaceAll('.template', '');
envsub({
templateFile: value,
outputFile: value.replaceAll('.template', ''),
options,
});
outputFiles.push(output);
});
return outputFiles;
};
test:
const customEnvsubstOptions = {
all: true,
diff: true,
system: true,
syntax: 'dollar-both',
envs: [{ name: 'test_env' }, { name: 'test_version' }],
};
const templateFilePaths = [
"<cwd>/.gitlab/node/test/resource/someFileWithEnv-ci.template.yml",
"<cwd>/.gitlab/node/test/resource/sub-dir/someFileWithEnv-ci.template.yml"
];
const resolvedTemplateFilePaths= [
"<cwd>/.gitlab/node/test/resource/someFileWithEnv-ci.yml",
"<cwd>/.gitlab/node/test/resource/sub-dir/someFileWithEnv-ci.yml"
];
it('should substitute all given files and write it out to a new file', () => {
const outputFiles = substituteEnvForAllFiles(templateFilePaths, customEnvsubstOptions);
assert.deepEqual(outputFiles, resolvedTemplateFilePaths);
});
files:
The yml files to substitute look like this.
.test-template: &test-template
image: $test_env/bla/bla/image:$test_version
tags:
- test
- nodejs
Thanks already :)
If you have a .env
file like this:
NAME="Value"
running envsub --env-file .env
results in a substitution of ${NAME}
for "Value"
.
However, running NAME="Value" envsub
results in Value
(not wrapped in quotes).
I think that quotes should be ignored when setting values from a key=value file as you might expect when sourcing the file in a shell environment.
ie: given a string representing a .env
file and a template string, return another string with substituted variables.
ps: awesome library, thanks so much for creating it !
After looking through the code, it looks like envsub respects opts.envs, however envsubh just only uses the process.env for the handlebars templating, ignoring the provided option
Providing an asynchronous API that returns Promises forces an asynchronous approach instead of giving developers the flexibility to use the API in a synchronous or asynchronous way. I propose something like:
var result = envsub_sync(templateFile, outputFile, options);
Would it be possible to allow a default value to be specified for undefined env vars? I've used envtpl before, which has this feature, and it was really useful. I'm not sure what it would look like here, but here's how envtpl does it:
{{ MY_VAR | default(3) }}
Thanks for this awesome module!
When we add an '#' on quoted value this character is not escaped then the value we get it's not the right one.
MYVAR="any#notAcoment"
we get this value "any
instead of any#notAcomment
.
I already find the fix, here it's my pull request
Thanks in advance to make comment about my fix and merge it.
Great module.
I have a simple question - in order to be near compatible with envsubst, I was wondering if we could configure this to not replace template where variable is not found.
For example, if {{var1}}
is not defined in environment, it remains as {{var1}}
unlike the present behaviour, which simply removes this string.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.