Skip to content

Instantly share code, notes, and snippets.

@cagcak
Last active November 6, 2022 11:44
Show Gist options
  • Save cagcak/726e2ee8cd294d1072f7b69aed304d2a to your computer and use it in GitHub Desktop.
Save cagcak/726e2ee8cd294d1072f7b69aed304d2a to your computer and use it in GitHub Desktop.
Print a console message from a npm script cross-platform
/**
* 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