Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yarn script produces different output than running command directly

I have a script defined in package.json:

"scripts": {
  "prettierCheck": "./node_modules/.bin/prettier --check ./app/javascript/**/*.js"
}

If I run this script using yarn run prettierCheck, Prettier does not find any formatting issues with my files. However, if I run the Prettier command directly, it finds violating files.

Output of yarn run prettierCheck:

~/Projects/tome $ yarn run prettierCheck
yarn run v1.19.0
$ ./node_modules/.bin/prettier --check ./app/javascript/**/*.js
Checking formatting...
All matched files use Prettier code style!
Done in 0.20s

Output of ./node_modules/.bin/prettier --check ./app/javascript/**/*.js:

~/Projects/tome $ ./node_modules/.bin/prettier --check ./app/javascript/**/*.js
Checking formatting...
{... several files listed here ...}
Code style issues found in the above file(s). Forgot to run Prettier?

Why does this happen? What is the difference between running the command directly vs. through a Yarn script?

like image 305
tverghis Avatar asked Nov 07 '22 13:11

tverghis


1 Answers

I had the same issue or at least I thought I had the same issue. I tried a few things like adding quotes to the file/dir/glob in my command, changing my paths/globs & it started working for me because now I could see some useful output. Try these & maybe it'll work for you guys as well.

This is what prettier CLI documentation has to say about paths & file/dir/globs.

Don't forget the quotes around the globs! The quotes make sure that Prettier CLI expands the globs rather than your shell, which is important for cross-platform usage.

So try something like this in your scripts (Note: -c is short for --check)

scripts": {
"pretty-check": "prettier -c 'src/**/*.ts'",
}

Notice the quotes around 'src/**/*.ts' I'm not sure if this would work for everyone but I'm hoping that it does as it did for me.

And if you are wondering What is file/dir/glob
It's basically the part where you define the regex for your files like the part 'src/**/*.ts' in the command prettier -c 'src/**/*.ts'

like image 147
Junaid Avatar answered Nov 28 '22 21:11

Junaid