yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Run prettier over the package.json after editing if it's available

Open orta opened this issue 7 years ago • 9 comments

What is the current behavior?

Do: yarn add something

Then: git diff package.json

     "artsy-xapp": "^1.0.4",
+    "something": "^0.0.9",
     "babel-cli": "^6.24.1",
@@ -130,7 +131,14 @@
   "lint-staged": {
-    "*.json, *.md": ["yarn prettier --write", "git add"],
-    "*.js": ["eslint --fix", "yarn prettier --write", "git add"]
+    "*.json, *.md": [
+      "yarn prettier --write",
+      "git add"
+    ],
+    "*.js": [
+      "eslint --fix",
+      "yarn prettier --write",
+      "git add"
+    ]
   }

I see this pretty regularly, so I was wondering if it was possible to add something like:

// [edit & update package.json]
let newPackageJSON = "{ .. }"

try {
  const prettier = require('prettier')
  newPackageJSON = prettier.format(newPackageJSON)
}

If prettier is available to resolve in the CWD.

What is the expected behavior?

git diff package.json

     "artsy-xapp": "^1.0.4",
+    "something": "^0.0.9",
     "babel-cli": "^6.24.1",

Please mention your node.js, yarn and operating system version.

Node: 8.9 Yarn: 1.3.2 OS: macOS High Sierra

orta avatar Jan 03 '18 20:01 orta

Oh yeah, I'm happy to add this BTW

orta avatar Jan 04 '18 16:01 orta

Hey @orta! It would be the first case of Yarn doing something different depending on what packages are inside the cwd, but it looks reasonable to me. The main issue I have with doing this now is that it might cause a lot of unexpected changes on projects that use Yarn 🤔

Maybe we could add a settings in the Yarnrc file to toggle this behavior on and off? What do you think?

arcanis avatar Jan 18 '18 21:01 arcanis

I think this can be done via a postinstall hook in the package.json. Am I mistaken? If not, I'd prefer a note generic hook instead of hard-coding logic for a specific tool.

BYK avatar Jan 18 '18 22:01 BYK

@BYK it works fine when "adding", but does not works when "removing", because there is no such hooks in yarn (in fact there is but only for dependency, not for the root package), but thanks for the tips, it will do the job for now.

@arcanis I don't see a case where a package is using prettier would not want to format the package.json file with it. Maybe a setting to disable auto-formatting should be better.

jdeniau avatar Jan 22 '18 15:01 jdeniau

@BYK just a report after some tests: you can not really use the postinstall script because it will be triggered after installing in the same package, but also if you install this package as a dependency.

ie. foo contains the postinstall script with prettier, if you run yarn add foo, it will launch prettier --write package.json, but you may not have prettier there (and does not want to run prettier on the main package)

jdeniau avatar Feb 05 '18 13:02 jdeniau

It seems like the easiest alternative is to add package.json to .prettierignore, which will use Yarn's formatting rather than Prettier's, but still prevents conflicts between them.

nickserv avatar Jun 03 '19 07:06 nickserv

太好了

airenqingqing avatar Jun 03 '19 07:06 airenqingqing

It seems like the easiest alternative is to add package.json to .prettierignore, which will use Yarn's formatting rather than Prettier's, but still prevents conflicts between them.

This is what we're doing now but it means:

  • Manual edits (eg. adding a script) don't benefit from Prettier formatting and might produce a diff next time Yarn is run.
  • Can't use plugins such as prettier-package-json.
  • Can't use prettier --check on package.json.
  • Can't keep package.json formatted the same as other JSON files (eg. with custom indentation level).

None of which are a big deal, yet all of which would be nice to have.

I was thinking this could be implemented as a simple Yarn 2 plugin if there was a hook for it. Perhaps one could be added?

jscheid avatar Feb 04 '21 23:02 jscheid