Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Format package.json with Prettier after npm install #17973

Closed
1 of 4 tasks
tunnckoCore opened this issue Jul 28, 2017 · 6 comments
Closed
1 of 4 tasks

Format package.json with Prettier after npm install #17973

tunnckoCore opened this issue Jul 28, 2017 · 6 comments

Comments

@tunnckoCore
Copy link

I'm opening this issue because:

  • npm is crashing.
  • npm is producing an incorrect install.
  • npm is doing something I don't understand.
  • Other (see below for feature requests):

What's the feature?

Format package.json with Prettier

What problem is the feature intended to solve?

git diffs would be more pretty. The whole problem is that when npm install it formats it, then when update it from the editor which, of course uses some integration plugins (eslint + prettier), it is changed again, then commit. After that in next turn, for example just run some npm install or update the deps with some tool, the package.json again is formated, then commit and there is needless diff.

Probably, workaround is to run prettier formatting on precommit on package.json, but it again won't be good.

Is the absence of this feature blocking you or your team? If so, how?

No

Is this feature similar to an existing feature in another tool?

No

Is this a feature you're prepared to implement, with support from the npm CLI team?

Yes, probably. I believe it is in normalize-package-data?

@zkat
Copy link
Contributor

zkat commented Jul 28, 2017

We already take a lot of care to make sure package.json is sorted consistently, and that the existing indentation scheme is preserved. If you want a specific format beyond that for your package.json, you'll have to do it yourself. We're unlikely to go back to forcing the file to one particular format after so many folks wanting us to preserve things.

@zkat zkat closed this as completed Jul 28, 2017
@tunnckoCore
Copy link
Author

I understand that, but Prettier does nothing to sorting and ordering. You can just pass the string of JSON, after the whole normalization and validation, and before passing it to writeFile.

@rgbkrk
Copy link

rgbkrk commented Aug 30, 2017

When I contribute to projects that use prettier and apply it to json, when I run npm install I always end up with changes on the package.json. I then run prettier --write package.json so that I've not made any changes. Even with the precommit hook, it's still awkward because it ends up making an empty commit rather than just stopping from committing all together.

@jaydenseric
Copy link

@zkat

We already take a lot of care to make sure package.json is sorted consistently

Not a problem with Prettier, it respects ordering.

existing indentation scheme is preserved

We could do the same with Prettier: Detect the indentation scheme to set --tabs or --tab-width.

npm should respect the project's Prettier config, or else use defaults tweaked to preserve indentation.

The current forced formatting in package.json has terrible legibility; single item arrays are expanded to cover 3 lines! Most would say the carefully considered Prettier format, which has consensus, looks better. Prettier is fast becoming the standard for formatting .js, .graphql, .md, and more.

If this must be a wontfix, at least can we have an npm script to hook on so that tidying up after npm can be automated? E.g.

{
  "scripts": {
    "postpackageformat": "prettier --write package.json"
  }
}

@tunnckoCore
Copy link
Author

@jaydenseric good point! I'm tired about that.

@rafaelrinaldi
Copy link

It would be nice if postinstall was only called after both installation and formatting happens.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants