This is my package.json:
"scripts": {
 "start": "bash do_something.sh && jest",
 "...": "..."
}
In my package, there is the package.json and do_something.sh files at the package root, and a test_file.spec.ts module in ./src.
So doing npm run start from the CLI first runs the shell script with bash, then when the shell script finishes, it runs jest. This works fine so far.
However, there is a hardcoded constant, say FOO, inside do_something.sh (as FOO="bar") and inside the test module (as const FOO = "bar";). Instead of having to change the constant in both files upon each run, I would like to be able to set it from the CLI when calling the script, by doing something like:
npm run start --FOO=bar
I would then want to intercept that CLI argument from within the npm script definition in the package.json, and set it as an environment variable accessible within the do_something.sh shell script, and within the test_file.spec.ts module.
So in the package.json I would do something like:
"scripts": {
 "start": "env FOO=$npm_config_FOO bash do_something.sh && jest",
 "...": "..."
}
(I am using the npm_config_ approach from here)
Then from within do_something.sh:
echo $FOO
And within test_file.spec.ts:
console.log(process.env.FOO);
I think I am close to getting it working, just a few missing steps I think.
1 Answer 1
I think it's very close to what you have
"scripts": {
 "start": "export FOO=\"$npm_config_FOO\"; bash do_something.sh && jest"
 ...
}
which is going to export FOO and it will be available in bash and jest environments.
1 Comment
Explore related questions
See similar questions with these tags.