I have a script in my package.json with this code:
"scripts": {
"build": "postcss tailwind/tailwind.css -o css/cenic.tailwind.css",
"watch": "postcss tailwind/tailwind.css -o css/cenic.tailwind.css --watch"
},
It works fine - but how do I get it to output text to the command line, something like
script ran at {{ date(now) }}
In other words, I want to see a notification when a script has run.
To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.
You can easily run scripts using npm by adding them to the "scripts" field in package. json and run them with npm run <script-name> . Run npm run to see available scripts. Binaries of locally install packages are made available in the PATH , so you can run them by name instead of pointing to node_modules/.
npm run sets the NODE environment variable to the node executable with which npm is executed. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install , just in case you've forgotten.
You can do the following to log the date/time when the script begins its task:
"build": "node -e \"console.log('script started at: %s', Date())\" && postcss tailwind/tailwind.css -o css/cenic.tailwind.css"
The part on the left side of the &&
operator that reads;
node -e \"console.log('script started at: %s', Date())\"
-e
to evaluate the inline JavaScript.console.log(...)
and Date()
.The commands on the right side of the &&
operator are whatever command(s) you want to run, in your scenario it's the postcss
command.
To color the log you could add some ANSI/VT100 Control sequences. For instance:
"build": "node -e \"console.log('%sscript started at: %s%s', '\\x1b[42;30m', Date(), '\\x1b[0m' )\" && postcss tailwind/tailwind.css -o css/cenic.tailwind.css"
To log when the npm script completed instead of started you can switch the order of the commands. For example:
"build": "postcss tailwind/tailwind.css -o css/cenic.tailwind.css && node -e \"console.log('script completed at: %s', Date())\""
If you only want a solution that runs on *nix platforms, then you can do the following instead:
"build": "echo \"script started at: $(date)\" && postcss tailwind/tailwind.css -o css/cenic.tailwind.css"
The part on the left side of the &&
operator that reads:
echo \"script started at: $(date)\"
echo
command to print the message to the command line.$(...)
part, is utilized to obtain the output from the shells date
command.The commands on the right side of the &&
operator are whatever command(s) you want to run, in your scenario it's the postcss
command.
If you want to apply visual styling to the echo
command please refer to my answer here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With