shelljs/shx

--silent exists with code 1

its-dibo opened this issue · 3 comments

if the --silent option provided, the script hides the error but still exit with code 1, we just need to totally ignore the error.
otherwise provide another option --ignore to totally ignore errors and exit with code 0

shx --ignore cp -r non-exists dist

If you just want to ignore the error, I suggest appending || shx true to the command. Here's an example:

$ shx --silent cp -r does-not-exist/ path/to/destination/ || shx true

In that example, cp -r does-not-exist/ path/to/destination/ represents a command which might fail. shx --silent at the beginning means it will not print the error message and || shx true at the end means the full command will always have exit code 0.

Let me know if this works for you.

I forgot to mention: the || shx true part should be portable across most operating systems. This relies on your shell to evaluate the && or || syntax, but this is a fairly safe assumption because it's supported by sh, bash, zsh, and even on Windows (cmd.exe). This is even supported in new versions (v3+) of the fish shell (fish-shell/fish-shell#4620).

This should definitely be good enough for npm package scripts because npm executes those in either sh or cmd.exe depending on the operating system version (source: https://docs.npmjs.com/cli/v9/commands/npm-run-script#script-shell).

nice suggestion but it easer and more readable to achieve it by --ignore, especially if the command has multiple sub commands like this

shx cmd | cmd2 && shx cmd3

@nfischer