Lint-staged aborts check with the first failing file; "Error: Unknown argument" - lint-staged

With "lint-staged": "^13.0.3" and the following .lintstagedrc config:
{
"src/**/*.{ts,html}": [
"ng lint"
],
"src/**/*.scss": [
"npx stylelint **/*.scss"
]
}
I'm trying to run npx lint-staged -q on the following files (all of them have lint problems that would come up with the individual lint command):
Running ng lint and npx stylelint **/*.scss directly will bring up the problems (detailed) and would not fail on the first file:
ng lint
npx stylelint **/*.scss
I'd like lint-staged's output to show all of the problems from the individual lint commands. What's wrong here?
Thanks

Ok, after further investigation;
The partial output problems seem to be introduced to lint-staged on versions >= 12.2.0, see on github. I've downgraded to "lint-staged": "12.1.7" and the problem seems to be gone.
Regarding the Unknown argument thing, it resolves by calling eslint instead of ng lint on the configuration file.

Related

husky / lint-staged not running custom command or printing errors

I am trying to upgrade from husky#4.8 and lint-staged#11.2 to husky#8.0 and lint-staged##13.1 for my Nx repo. It seems that the only configuration I used to need was the following in package.json:
"scripts: {
"lint:fix": "ng lint -- --fix && npm run prettier:fix",
}
"husky": {
"hooks": {
"commit-msg": "commitlint -e $HUSKY_GIT_PARAMS"
}
},
"lint-staged": {
"*.{ts,js,css,md,html}": "npm run lint:fix",
"relative": true
}
This no longer works with the new husky and lint-staged. I followed this tutorial and installed husky: npx husky-init && npm install, installed lint-staged: npm install --save-dev lint-staged, modified the .husky/pre-commit file:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
and added the .lintstagedrc.json file:
{
"*.ts": [
"prettier --write",
"eslint"
],
"*.html": [
"eslint",
"prettier --write"
],
"*.scss": "prettier --write"
}
When I make a new commit it seems to run. However it looks like this command either runs on every library or every staged file (I have a lot of libraries). When I change the file contents to:
{
"*.{js,ts,json,scss,md}": ["npm run lint:fix"]
}
it takes a while to run (though it seems to run and fix the linting) AND I don't get the output of my ng lint -- --fix command. If I go into the .husky/pre-commit file and change npx lint-staged to npm run lint:fix, it runs the ng lint -- --fix command, fixes what it can and prints out linting errors. However, the error doesn't prevent the commit and I have to commit any new changes that the linter fixed. With the old versions I didn't need to do this. Also I didn't need to commit a .husky/pre-commit file into my repo. (I see some workarounds that have a postinstall script that generates this file for the user).
How can I utilize lint-stage and husky newest versions to run my custom npm command so I can call ng lint like in the older versions and block commits on errors? Should I just use the older versions? Also what benefit do the newer versions provide?

Yarn script fails when invoking other Yarn-scripts

I've got an issue when invoking other Yarn scripts from a Yarn-script. What I want to achieve is having one yarn develop task that first invokes TypeScript and some other preprocessors before starting a couple of watchers and running my application. I'd like to use npm-run-all (or something similar) to invoke multiple targets as that would allow me to define separate targets that I can manually run as well without having duplicate script-entries in my package.json.
I've made a minimal reproduction case which has the following package.json:
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"task:one": "echo one",
"task:two": "echo two",
"run-all": "npm-run-all task:one task:two",
"run-all-yarn": "yarn task:one && yarn task:two"
},
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
Invoking yarn task:one results in an echo one as expected, as does yarn task:two. If I invoke yarn run-all though, I get an error:
$-> yarn run-all
yarn run v1.22.10
$ npm-run-all task:one task:two
$ echo one
error Couldn't find the binary echo one
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: "task:one" exited with 1.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
yarn run-all-yarn fails with (almost exactly, sans the command being executed on the second line) the same error.
If instead of Yarn I use NPM (npm run run-all) it all works as expected:
$-> npm run run-all
> test#1.0.0 run-all /Users/basdalenoord/Projects/52DN/psyflix/test
> npm-run-all task:one task:two
> test#1.0.0 task:one /Users/basdalenoord/Projects/52DN/psyflix/test
> echo one
one
> test#1.0.0 task:two /Users/basdalenoord/Projects/52DN/psyflix/test
> echo two
two
I'm using Yarn version 1.22.17 provided by my node-version from nvm: ~/.nvm/versions/node/v14.18.1/bin/yarn
I'm a bit lost as to why running individual targets would be fine but combining them fails for yarn, whereas the same script works for npm.

Missing node_modules bin on PATH

I have run the command
yarn add -D jest to install jest to my project.
This does successfully add jest to my node_modules
> find . -name jest
./node_modules/.bin/jest
./node_modules/jest
When I use iterm2 to run jest however I get the following output
> jest
zsh: command not found: jest
FWIW When I use the IntelliJ terminal it does work
> jest
Determining test suites to run...^C
What am I missing in the iterm environment to be able to have node_modules bin in my classpath depending on the current repo?
An OS shell doesn't know about your locally installed node_modules, but IntelliJ terminal does. So if you want to run jest from outside of an IDE you should perform several additional steps.
The most common way to run locally installed packages is to define a separate script in the "scripts" section of your package.json file. Then you will be able to run it using the yarn/npm itself from any terminal. You can find an exact example in the Yarn docs.
{
"name": "my-package",
"scripts": {
"test": "jest"
}
}
yarn run test
Or you could install jest globally so it will be accessible from anywhere, but it's not a best practice.

What does " yarn build " command do? Are " npm build " and "yarn build" similar commands?

What does yarn build command do ?
Are yarn build and npm build the same? If not what's the difference?
yarn build and npm build are not existing commands by default. I think you mean yarn run build or npm run build.
build is a command which can be specified in your package.json file on the scripts property. See the example below.
{
"name": "mypackage",
"version": "0.1.0",
"scripts": {
"build": "webpack --config webpack.dev.js"
}
}
In this example, build is a shortcut for launching command webpack --config webpack.dev.js. You can use every keyword you want to define some shortcuts to launch commands.
And the only difference between the two commands it's the JS dependency manager you're using, yarn or npm.
More infos :
https://yarnpkg.com/lang/en/
https://www.npmjs.com/
"yarn build
Bundles the app into static files for production." from Create React App by MAD9135.
https://mad9135.github.io/F2020/modules/week3/02-react-tooling/07-create-react-app.html
yarn build is a shortcut of yarn run build
from original documentation:
Reference: https://classic.yarnpkg.com/lang/en/docs/cli/run/#toc-yarn-run-script
its the same i guess the real difference between yarn and npm is the performance and security that yarn provides.
yarn actually installing packages in parallelly and Npm only install one package at a time
And Yarn have more secure dependency.
Yarn is a package manager for your code. Yarn build makes a bundle of apps in one format and shows errors if the app has any problem that will make an error on the server.

ESLint producing different output when run with npm run-script

When I lint my code with eslint scripts/**/*.js I see two linting errors:
» eslint scripts/**/*.js
/Users/user/dev/scripts/application.js
3:8 error "React" is defined but never used no-unused-vars
/Users/user/dev/scripts/components/Header.js
24:2 error Unnecessary semicolon no-extra-semi
✖ 2 problem (2 error, 0 warnings)
That's fine. When I put that command into "scripts" in my package.json then I only get one error.
// package.json
// ...
"scripts": {
"lint": "eslint scripts/**/*.js"
}
// ...
» npm run lint
/Users/david.tuite/dev/ui/scripts/components/Header.js
24:2 error Unnecessary semicolon no-extra-semi
✖ 2 problems (2 errors, 0 warnings)
What's happening to the other linting error?
edit
I'm starting to suspect this is a globbing problem. The missing linting error is in a file which isn't in a subdirectory of scripts.
Globs work differently in the package.json file.
The trick is to wrap the path matchers in single quotes to have them expanded at the shell level before they're passed to eslint.
// package.json
// ...
"scripts": {
"lint": "eslint 'scripts/**/*.js'"
}
// ...
Sounds like David's answer solved the problem he was encountering. However, I had an identical symptom to the problem described.
In my case the problem was that npm was using the node_modules in the project directory which had a different version of the plugin than the global npm folder had.
Running eslint from command line was using the global version. Reconciling those versions resolved the issue in my case.