shelljs/shx

Add a command to negate the exit code of some other command

Shayan-To opened this issue · 1 comments

I couldn't find any way to do this in a cross-platform way. POSIX shell has the ! operator that can do that, but I couldn't find a way to do it on Windows. It could work like an exec command that negates the exit code of the execution.

Sample use case: Stop a commit using husky if any file contains string DO_NOT_COMMIT_THIS. "pre-commit": "shx not grep -rq DO_NOT_COMMIT_THIS ."

I don't think this is currently possible in shx. We support shx true and shx false (and the OS will support && and ||), but I believe these are not enough to implement negation.

We could support a shx --negate flag to logically negate exit codes. Would you be interested in sending a PR? We would want to check for the flag value here:

shx/src/shx.js

Lines 92 to 93 in 7e7bf7f

// Set shell.config with parsed options
Object.assign(shell.config, parsedArgs);

And then invert the exit code here:

shx/src/shx.js

Lines 118 to 125 in 7e7bf7f

if (typeof code === 'number') {
return code;
} else if (shell.error()) {
/* istanbul ignore next */
return EXIT_CODES.CMD_FAILED;
}
return EXIT_CODES.SUCCESS;