I am testing a larger library of NPM packages, that consists of private packages, altered forks of public packages or downstreams of public packages.
lib
|-package_1
|-package_2
|-package_N
So I am running a shell script through my package lib, that runs in each directory the npm test
command.
for D in *; do
if [ -d "${D}" ]; then
echo "================================="
echo "${D}" # PRINT DIRECTORY NAME
echo "================================="
cd $D
npm run tests
cd ../ # LEAVE PACKAGE DIR
fi
done
Unfortunately there is not a unique pattern for naming the tests-script in the package's JSON files. Some package are running under test
a script with watch-mode and have a different name for their cli script (mostly named testcli
).
What I would like to do is something like the following pseudocode:
if has-testcli-script then
npm run testcli
else
npm run test
I assume for now, that only those two options exist. I am rather interested in the way of knowing if the script exists, without installing an additional global NPM package.
To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.
Package. json has various sections, scripts is one of them, which allows you to write npm script which we can run using npm run <script-name>. Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH , so you can run them by name instead of pointing to node_modules/.
If you plan to publish your package, the most important things in your package. json are the name and version fields as they will be required. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version.
To identify the unused package, just run npx depcheck in the project root directory. Next step is to uninstall the npm packages using npm uninstall command. The post Remove unused npm modules from package. json appeared first on Poopcode.
Since npm version 2.11.4 at least, calling npm run
with no arguments will list all runable scripts. Using that you can check to see if your script is present. So something like:
has_testcli_script () {
[[ $(npm run | grep "^ testcli" | wc -l) > 0 ]]
}
if has_testcli_script; then
npm run testcli
else
npm test
fi
Or alternatively, just check to see if your script is in the package.json file directly:
has_testcli_script () {
[[ $(cat package.json | grep "^ \"testcli\":" | wc -l) > 0 ]]
}
In my case that approach didn't work and I had to implement something using jq instead.
has_script (script) {
[[ ! -z "$(jq -rc --arg key $script '.scripts | to_entries | .[] | select(.key == \$key)' package.json)" ]]
}
then use it as:
if has_script('testcli'); then
// Do something
else
// Do nothing
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With