npm tslint don't give error - npm

Hey all got a issue with npm and tslint I was hoping you could help me with.
Ok here comes my situation and code:
package.json
"scripts": {
"lint": "tslint -c tslint.json 'src/app/**/*.ts'",
"pretest": "npm run lint ",
"test": "echo 'No test are made'",
...
},
When I run command npm test this is the output:
input terminal
$ npm test
output terminal
> keoom#1.0.0 pretest c:\Users\Hjorth\Documents\github\keoom-angular
> npm run lint
> keoom#1.0.0 lint c:\Users\Hjorth\Documents\github\keoom-angular
> tslint -c tslint.json 'src/app/**/*.ts'
> keoom#1.0.0 test c:\Users\Hjorth\Documents\github\keoom-angular
> echo 'No test are made'
'No test are made'
If I only run command tslint -c tslint.json 'src/app/**/*.ts' I on the other hand see the linting error.
input terminal
$ tslint -c tslint.json 'src/app/**/*.ts'
output terminal
src/app/app.component.ts[1, 27]: " should be '
So as you can see there is a linting error in my code, but if I am not running the script directly It will not show. What I am expecting is this:
When I run npm test the script pretest will run, and that script will run the script lint, it will then exit with exit 0 before test script is run as it will find the linting error.
Anyone that can be of assistance.

It's the quotes around the file spec in the tslint command that are the problem:
"lint": "tslint -c tslint.json 'src/app/**/*.ts'"
^ ^
If you remove those, you should see the expected error reported by tslint.

Related

What should be NPM command for solving this question?

I tried various solutions for this question but not able to pass 2 test cases amongst 4. Please help me crackdown this problem
Perform the below mentioned steps by creating package.json file named npm_commands in the maxbot directory
Create a file named index.js.
Write a js code in index.js file to create a string named myVar and value as node package manager
and convert it into uppercase.
NOTE: Please use console.log to display the output of myVar in index.js file
Configure scripts in package.json
(a) to check the versions of npm and node by using npm run release | tee output1.txt
(b) to execute index.js by using npm run build | tee output.txt
npm init - to initialize new package
give npm_commands as package name
give index.js as start file
Create new file named as index.js
declare : let myVar = "node package manager"
print var in uppercase: console.log(myVar.toUpperCase());
Add these scripts in package.json
to check npm and node version: "release": "npm -v && node -v",
to execute index.js file: "build": "node index.js"
Save and then run these commands to get the results:
npm run release | tee output1.txt
npm run build | tee output.txt
Step 1: run the command to initialize new package
npm init
Step 2: Command will start package initialization process
Set the package name:
package name: (challange) npm_commands
Step3: Press enter till time it ask for "entry point", by default it set to index.js verify and press enter key. Until the initialization process ends.
Step 4: Go to editor project explorer add create new file index.js, and add following code in it.
let myVar = 'node package manager';
console.log(myVar.toUpperCase());
Step 5: Add following lines into script block of package.json file
"release" : "npm -v && node -v",
"build" : "node index.js",
Script block looks like as below after adding above lines
"scripts": {
"release" : "npm -v && node -v",
"build" : "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Step 6: Now open the command prompt and run the following commands
npm run release | tee output1.txt
npm run build | tee output.txt
Above commands create the output1.txt, and output.txt files in project explorer containing respective commands output.
Step 7: That's it! now run the test and check your FS_SCORE for 100% and submit the test.

How to use commander.js command through npm command

I'm using commander.js command like this ./index.js --project mono --type item --title newInvoice --comments 'Creates an invoice' --write, Now I'm using the command through npm run item newInvoice by setting some options in package.json file like this
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"snapshot": "node --max-old-space-size=10240 ./scripts/snapshot.js",
"item": "./index.js --project mono --type item --title",
"config": "./index.js --project mono --type config --title"
}
But whenever I'm trying to get the --write option with npm using npm run item newInvoice --write it's showing undefined for --write
Source Code:
#!/usr/bin/env node
const fs = require('fs');
const program = require('commander');
require('colors');
program
.version('0.1.0')
.option('--project [project]', 'Specifies the project name', 'mono')
.option('--type [type]', 'Type of code to generate, either "item" or "config"', /^(config|item)$/, 'config')
.option('--title [title]', 'Title of the item or config', 'untitled')
.option('--comments [comments]', 'Configs: describe the config', '#todo description/comments')
.option('--write', 'Write the source code to a new file in the expected path')
.option('--read', 'To see what would be written the source code to a new file in the expected path')
.parse(process.argv);
console.log(program.write, program.read); //=> undefined undefined
Can anyone help me how to use commander js command with npm?
When you run your npm run command you need to utilize the special -- option to demarcate the end of any option(s) that may belong to the npm run command itself (e.g. --silent), and the beginning of the argument(s) that are to be passed to the end of the npm script.
Run the following command instead:
npm run item -- newInvoice --write
Given the aforementioned command and the command currently defined your npm script named item they essentially form the following compound command prior to execution:
./index.js --project mono --type item --title newInvoice --write
^ ^
The npm run documentation states the following:
As of npm#2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script.
and it's usage syntax is defined in the Synopsis section as:
npm run-script <command> [--silent] [-- <args>...]
^^
Note: It's not possible to add the -- option to the npm script itself.

Output to command line within npm scripts?

I have a script in my package.json with the following 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.
Cross Platform (Windows/Linux/macOS...)
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"
Explanation:
The part on the left side of the && operator that reads;
node -e \"console.log('script started at: %s', Date())\"
utilizes Node.js command line option -e to evaluate the inline JavaScript.
The inline script utilizes JavaScript's 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.
Variations:
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())\""
Nix platforms only (Linux/MacOS...)
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"
Explanation:
The part on the left side of the && operator that reads:
echo \"script started at: $(date)\"
utilizes the shells echo command to print the message to the command line.
Command substitution, i.e. the $(...) 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

npm sub script with command line parameters

I have some npm scripts that look like this:
"scripts": {
"exec:dev": "export NODE_ENV=development && npm run exec",
"exec:stage": "export NODE_ENV=stage && npm run exec",
"exec:prod": "export NODE_ENV=production && npm run exec",
"exec": "node myapp.js"
}
I'd like to pass some command line arguments to myapp.js but this does not work:
npm run exec:prod -- -a 123 -b 456
This is apparently due to the fact that the exec:prod script is calling the exec script and apparently not passing along the command line parameters. Is there any way to achieve this while keeping the nested script calls?
To explicitly tell the exec script to pass along the arguments it gets, include another --.
Instead of:
npm run exec:prod -- -a 123 -b 456
try:
npm run exec:prod -- -- -a 123 -b 456
The first double dash tells the exec:dev script, "these args
aren't for you, pass them along to the exec script".
The second double dash tells the exec script, "these args aren't
for you, pass them along to node myapp.js".
If you want to keep the npm run command that you enter via the CLI the same as per your question. (i.e. avoid adding yet another npm special option (--) to it, as per #MikePatrick 's answer).
Change your npm-scripts to the following instead:
"scripts": {
"exec:dev": "export NODE_ENV=development && npm run exec --",
"exec:stage": "export NODE_ENV=stage && npm run exec --",
"exec:prod": "export NODE_ENV=production && npm run exec --",
"exec": "node myapp.js"
}
Note: the npm special option (--) added to the end of the first three scripts.
Demo
For demonstration purposes, lets say myapp.js is as follows:
myapp.js
const args = (process.argv.slice(2));
const nodeEnv = process.env.NODE_ENV;
console.log(nodeEnv);
console.log(args);
Testing:
Running npm run exec:dev -- -a 123 -b 456 prints:
development
[ '-a', '123', '-b', '456' ]
Running npm run exec:stage -- -a 123 -b 456 prints:
stage
[ '-a', '123', '-b', '456' ]
Running npm run exec:prod -- -a 123 -b 456 prints:
production
[ '-a', '123', '-b', '456' ]
Further info
The docs for npm-run-script describe the npm special option (--) as follows:
... The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script ... The arguments will only be passed to the script specified after npm run and not to any pre or post script.

npm seems to be messing up my shell script which invokes uglifyjs

I have webpack use babel to transpile es6 files into es5. I want to compress these using uglifyjs. The method I used which worked till today was to have this script file be invoked with npm:
scripts/uglifyjs:
#!/usr/bin/env zsh
for file in ./public/assets/js/*.js
do
echo "uglifying `basename $file`"
uglifyjs --verbose --compress --source-map content=${file:2}.map,url=`basename $file`.map,filename=${file:2}.map,includeSources=true --output $file $file
done
And in my package.json:
"scripts": {
...
"uglifyjs": "scripts/uglifyjs",
Take the first file that gets invoked, links.js. If I manually type the command is works:
$ uglifyjs --verbose --compress --source-map content=public/assets/js/links.js.map,url=links.js.map,filename=public/assets/js/links.js.map,includeSources=true --output ./public/assets/js/links.js ./public/assets/js/links.js
INFO: Using input source map: public/assets/js/links.js.map
When running npm run uglifyjs I get the following error:
$ npm run uglifyjs
> jbuk-frontend#0.0.1 uglifyjs /home/jonny/git/jonnybarnes.uk
> scripts/uglifyjs
uglifying links.js
fs.js:651
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open 'content=public/assets/js/links.js.map,url=links.js.map,filename=public/assets/js/links.js.map,includeSources=true'
at Object.fs.openSync (fs.js:651:18)
at Object.fs.writeFileSync (fs.js:1300:33)
at done (/home/jonny/git/jonnybarnes.uk/node_modules/uglify-js/bin/uglifyjs:516:20)
at cb (/home/jonny/git/jonnybarnes.uk/node_modules/uglify-js/bin/uglifyjs:324:39)
at /home/jonny/git/jonnybarnes.uk/node_modules/uglify-js/bin/uglifyjs:391:9
at tryToString (fs.js:512:3)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:500:12)
I don’t know why I can run the command manually, but not via npm, it worked before. Is this a known change with npm v5?