Last active
November 6, 2022 11:44
-
-
Save cagcak/726e2ee8cd294d1072f7b69aed304d2a to your computer and use it in GitHub Desktop.
Print a console message from a npm script cross-platform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Print a console message from a npm script by only one long signal flag passing at a time | |
* --start prints outputs.start | |
*/ | |
const signal = "--"; | |
const outputs = { | |
start: `Print a message for passed flag start`, | |
build: `Print a message for passed flag build`, | |
}; | |
const flag = (process.argv || []) | |
.filter((arg) => arg.includes(signal)) | |
.reduce((_, curr) => curr, []) | |
.replace(signal, ""); | |
const output = outputs[flag]; | |
if (!output) { | |
throw new Error( | |
`No output defined for ${flag}. Define one as a key-value pair` | |
); | |
} | |
console.warn("\x1b[33m%s\x1b[0m", output); | |
process.exit(2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment