Bunyan is displaying /n in the output .! - node.js

I am using bunyan logs to display my messages . Its displaying \n in the output after every key-value pair . How to remove it ? Following is the screen shot of the output .
here is the image

This is just formatting. The text has line-changes which is displayed by the newline character "\n". Everything after the newline character should be displayed on the next line. Since this is just a string the terminal doesn't know how to display it.
If you have bunyan installed globally, you can pipe that response through bunyan cli.
node runsomething.js | bunyan
OR you don't have to have it globally installed if you run it through an npm script.
package.json
{
"scripts": {
"start": "node runsomething.js | bunyan",
}
}
and run it via the start command
npm start
Relevant documentation: https://github.com/trentm/node-bunyan#cli-usage

Related

-print printing out string via npm but actually evaluating through powershell

The following script is defined in my package.json:
"abc": "node -p 'p=require(\"./package\");p.main=\"lib\";p.scripts=p.devDependencies=undefined;JSON.stringify(p,null,2)'",
If I run 'npm run-script abc' I end up with a package.json with the string
'p=require(\"./package\");p.main=\"lib\";p.scripts=p.devDependencies=undefined;JSON.stringify(p,null,2)'
If I run the command:
node -p 'p=require(\"./package\");p.main=\"lib\";p.scripts=p.devDependencies=undefined;JSON.stringify(p,null,2)'
Directly in powershell I actually get the json output I'm looking for.
Not sure why the difference? I tried using -e/-eval as well to no avail, it seems to think that it just prints out the script?
Change your npm script to the following instead:
"abc": "node -p \"p=require('./package');p.main='lib';p.scripts=p.devDependencies=undefined;JSON.stringify(p,null,2)\"",
Your npm script will now run succesfully via Powershell, Command Prompt (cmd.exe), Linux, and MacOS.
Changes are as follows:
The node/js code has been wrapped in JSON escaped double quotes \"...\" instead of single quotes.
The actual node/js code itself utilizes single quotes '...' instead of JSON escaped double quotes \"...\" because using escaped double quotes in JavaScript is invalid syntax.
"abc": "node -p \"p=require('./package');p.main='lib';p.scripts=p.devDependencies=undefined;JSON.stringify(p,null,2)\"",
^^ ^ ^ ^ ^ ^^
It's a quoting issue.
The problem is that running npm run-script abc is not interpreted in powershell (which properly supports both single and double quotes), but in cmd.
You can either:
Replace single quotes with double quotes, and use proper escaping (although the result may be a bit ugly):
"abc": "node -p \"p=require(\\\"./package\\\");p.main=\\\"lib\\\";p.scripts=p.devDependencies=undefined;JSON.stringify(p,null,2)\""
Use single quotes inside the javascript code and double qoutes to surround the script
"abc": "node -p \"p=require('./package');p.main='lib';p.scripts=p.devDependencies=undefined;JSON.stringify(p,null,2)\""

Why does npm start ignore escaped backticks from argument?

I have a super simple index.js file that outputs the first argument it receives:
// Args 0 & 1 are /usr/bin/node and path to file
console.log(process.argv[2]);
When I run the following:
node index.js test``backticks
the output is testbackticks, and as expected, the backticks are ignored.
Calling this again, with escaping this time:
node index.js test\`\`backticks
the output is test``backticks.
My package.json has a start script like so:
"start": "node ./index.js"
Calling npm start -- test``backticks will result in them being ignored, just like with the node.js call.
However, calling this with escaping:
npm start -- test\`\`backticks
Also results in the same output, which is simply:
testbackticks
With the backticks ignored as well.
Escaping npm start with double or single quotes worked fine, it is just the backticks that are ignored.
Why does calling npm start and node produces different results and how come backticks can't be escaped in npm start ?
Lastly, how can I, if possible, pass a string with backticks as an argument to npm start ?

How do I conditionally output eslint results to a file using npm scripts?

I am trying to make a simple npm script to run eslint and check if it's in CI or not and output the results to a file if it is.
This works to output the results to the terminal:
"lint": "eslint src --cache --format $(if [ -z ${SOMEVAR} ]; then echo \"stylish\"; else echo \"checkstyle\"; fi)",
But I want to save them to a file if there is an ENV var present using > checkstyle.xml
Is there a way to tack this onto that command? I've tried several ways, but no luck getting the file to output.
Edit:
I was able to get this working by adding --color | tee checkstyle.xml which writes the xml file regardless of ENV var value and displays a colorized version to terminal. This is not ideal, but does work. Open to other ideas though.
I found this great chart that shows what combos of output you can use together to achieve this: https://askubuntu.com/a/731237/541276
Do you mean something like this?
if [ "$somevar" ]; then exec >checkstyle.xml; fi; eslint ...

Colored logging in terminal with NPM run script

I'm trying to setup a project exclusively with NPM as a build system (no Gulp or Grunt) so I'm a bit of a beginner, but so far it's working pretty nicely except for this little road block.
The scripts section of my package.json looks something like that :
"scripts": {
"clean:task": "rimraf dist/*",
"clean:notify": "notify --t 'Cleaning done.' --m 'dist/ has been cleaned successfully.",
"clean": "npm run clean:task -s && npm run clean:notify -s",
"serve": "browser-sync start --p 'xxx.dev/app' --host 'xxx.dev' --port '3000' --open 'external' --f 'app'",
"styles:task": "node-sass --output-style nested -o app/assets/css app/assets/css",
"styles:notify": "notify --t 'Styles compilation' --m 'Styles have been compiled successfully'",
"styles:build": "npm run styles:task && npm run autoprefixer",
"imagemin": "imagemin app/assets/img dist/img -p",
"scripts:lint": "jshint --reporter=node_modules/jshint-stylish app/assets/js/scripts.js"
}
I have notifications to announce successful tasks, but ideally I'd like some nice colored messages directly in the terminal. I know this can be done with Gulp via colored logging but I can't fin any NPM package that has a CLI that would be able to do that.
Any ideas ? Is it even possible ?
Thanks for your help.
Individual scripts defined in package.json can output a message to Mac's Terminal in color by following a syntax that boils down to echo + ANSI color reference + some text although you can get more complicated than this, as well.
Step 1: Define your scripts
"scripts": {
"greeting": "echo \"\\033[32mHello World\"",
"notification": "echo \"\\033[33mThe Server Has Started\"",
"timestamp": "echo \"\\033[31m--------\";date \"+%H:%M:%S\n--------\n\" && echo \"\\033[00m\""
{
Step 2: Run your scripts
npm run greeting
npm run notification
npm run timestamp
Some things to note
Escaping is inherently a part of this. A comment by James Lim on StackOverflow mentions the use of -e which apparently ensures that echo honors escaping slashes. I found this was necessary when running the command directly in Terminal but unnecessary when firing it as part of npm run <script>.
The ANSI code that sets the color is made up of eight characters that always begin with a backslash, like this \033[32m. Also, notice that the text I am outputting in my scripts above begins immediately after the last character in the ANSI code– this is so the message will be flush left in the Terminal.
In the timestamp script above, the semicolon separates the echo and date commands but maintains the same color we set at the beginning. In fact, this color will affect everything defined in this script and, in my case, even changed the color of a message output by a separate script that immediately followed this one. For this reason, I included another echo that simply resets the color to the Terminal's default color (in this case, black).
For reference, I could have used && in place of the semicolon and achieved the same results. If you do not already know the difference between these two operators, see this post.

nodejs bunyan order of elements

I'm using bunyan, and this is an example of what i'm writting in my log.
Is there a way to change the order of the fields printed? from this:
{"name":"appName","hostname":"ip","pid":5817,"level":30,"msg":"message","time":"2015-10-15T19:04:01.596Z","v":0}
To this:
{"time":"2015-10-15T19:04:01.596Z","msg":"message","name":"appName","hostname":"ip","pid":5817,"level":30,"v":0}
Use the bunyan cli to get a more human readable log.
One option is to pipe just bunyan when you start your app (assuming you are running this from your root directory)
$ node app.js | ./node_modules/.bin/bunyan
A really short version is to pipe
$ node app.js | ./node_modules/.bin/bunyan -o short
Search around, there is a lot of power in the bunyan CLI.
https://github.com/trentm/node-bunyan#cli-usage

Resources