shelljs / shx

Portable Shell Commands for Node
MIT License
1.73k stars 45 forks source link

--silent exists with code 1 #210

Closed its-dibo closed 1 year ago

its-dibo commented 1 year ago

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
nfischer commented 1 year ago

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.

nfischer commented 1 year ago

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 (https://github.com/fish-shell/fish-shell/issues/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).

its-dibo commented 1 year ago

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