Have recently put together a build tool with npm
and package.json
scripts, and I have a few echo
commands to state which parts of the pipeline are currently running.
For example (from my package.json
):
{
"scripts": {
"clean": "rimraf a-directory/",
"preclean": "echo \"\n[ Cleaning build directories ]\n\""
}
}
When I Bash: npm run clean
it prints my echo
message, and then cleans the appropriate directory.
I'd like to change colour, font weight, background text colour to make these echo
statements stand out and be more informative at a glance, but I've struggled even finding a starting point that can set me off being able to do this.
There's lots of info about doing this in regular CLI/Bash scripts, via grunt
and gulp
, or via JS scripts, but nothing I've found is attempting it from the scripts section of package.json
.
What am I missing? All help appreciated.
Many thanks.
Consoles/terminals typically provide support for ANSI/VT100 Control sequences, so it's possible to use these codes to control font colour, font weight, background colour, etc.
For a Bash only solution refer to the Bash (MacOS/Linux/ etc..) section below.
However, if cross platform support is required then follow the solution described in the Cross Platform section below.
Bash (MacOS/Linux/ etc..)
Important Note: The following will not work successfully on non-bash consoles, such as Windows Command Prompt (i.e. cmd.exe) or PowerShell.
This example npm-script below:
...will log the something like the following in your console when running
npm run clean
(i.e. white text with a blue colored background):Breakdown of the necessary syntax/codes:
Further examples:
The following example npm-scripts provide further examples:
Running
npm run bash-echo-all -s
using the scripts above will output the following to your console (the-s
option just makes npm log a bit less):A more comprehensive list of codes can be found at the link provided at the top of this post, (or see codes listed in Cross Platform section below), but remember not all ANSI/VT100 Control sequences are supported.
Cross Platform
For a cross-platform solution, (one which works successfully with Bash, Windows Command Prompt / cmd.exe, and PowerShell etc..), you'll need to create a nodejs utility script to handle the logging. This nodejs script can then be invoked via your
npm-scripts
.The following steps describe how this can be achieved:
Create a nodejs utility script as follows:
echo.js
Let's name the file
echo.js
and save it in the root of your project directory, i.e. in the same folder wherepackage.json
is stored.Then, given your example, let's add a
npm-script
topackage.json
as follows:When running
npm run clean
you'll see the same message logged to your console as before when using the bash only solution, i.e. white text with a blue colored background.Overview of usage syntax for invoking
echo.js
vianpm-scripts
node echo \"message\"
The
node echo \"message\"
part is mandatory. Themessage
is where you enter your message to be logged and it must be wrapped in escaped double quotes\"...\"
to prevent splitting.The remaining parts, which are for the purpose of formatting/styling, are all optional and can be defined in any order. However, when used, they must proceed after the initial
node echo \"message\"
part, and be separated by a single space.[
--set|-s
]The
--set
option, or it's shorthand equivalent-s
, followed by a single space, and one of the following ANSI codes can be used to specify general formatting:Note: Codes
1
and4
worked successfully with Bash, however they were not supported by Windows Command Prompt and Powershell. So if repeatability is important across platforms I recommend avoiding use of the--set
|-s
option entirely.[
--bg-color|-b
]The
--bg-color
option, or it's shorthand equivalent-b
, followed by a single space, and one of the following ANSI codes can be used to specify the background color:[
--font-color|-f
]The
--font-color
option, or it's shorthand equivalent-f
, followed by a single space, and one of the following ANSI codes can be used to specify the font color:Further examples:
The following example scripts provide further examples:
Running
npm run node-echo-all -s
using the scripts above will output the the same results as shown in the Bash (MacOS/Linux/ etc..) section above.For brevity these scripts (above) utilize the shorthand
-s
,-b
, and-f
options. However they can be substituted with their longhand equivalents--set
,--bg-color
, and--font-color
respectively if necessary to make your code more human readable.