@delucis/if-env
v1.1.2
Published
Run an npm script if an environment variable matches a pattern
Downloads
2,651
Readme
@delucis/if-env
Run an npm script if an environment variable matches a pattern
This is a fork of Eric Clemmons’s if-env
. It allows you to match environment variables using wildcards (*
) rather than using simple string equality. See Sindre Sorhus’s matcher
for details about how the matching works.
Installation
npm install --save @delucis/if-env
Usage
This package installs if-env
, a script which lets you easily match environment variable values against a pattern using wildcards. It also includes if-env-cs
, which is case sensitive.
If you want to to run an NPM script conditionally, depending on the value of an environment variable, you can use if-env
in your package.json
. In this example, we only want to run our test
script when the SOME_VAR
variable starts with new
:
"scripts": {
"test": "if-env SOME_VAR=new* && npm run test-suite"
}
Multiple conditions
If you want several conditions to be met you can pass them all to if-env
:
"scripts": {
"yay": "if-env VAR1=woo* VAR2=*hoo && echo yay"
}
If you want to run a script if either one or another condition is met, you can use the ||
operator:
"scripts": {
"moo": "if-env ANIMAL=cow || if-env ANIMAL=bull* && echo moo"
}
If you want to do different things depending on the value of a variable, you can also use the ||
operator:
"scripts": {
"joy": "if-env MOOD=happy && echo 😄 || echo 😭"
}
Negating conditions
If you want to do something if a variable does not match a pattern, you have two options. Note the single quotes in the first example; they escape !
for your shell.
"scripts": {
"ifenv": "if-env MOOD='!happy' && echo 😭",
"shell": "if-env MOOD=happy || echo 😭"
}
Escaping characters
If you need to have spaces in your pattern, make sure you wrap it in quotes:
"scripts": {
"spacious": "if-env SENTENCE='I want * spaces.' && echo You get spaces!"
}
Case sensitivity
if-env
is case insensitive, which means if-env VAR=foo
will match both foo
and FOO
. If you need to test a variable and case is important, use the if-env-cs
command:
"scripts": {
"case": "if-env-cs WORD=lower && echo nice! || echo STOP SHOUTING!"
}