You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow package.json to be formatted according to a set of config values. This will establish default config values for how package.json is written and add a command to rewrite an existing package.json files based on those config values. Since these config values will live in an .npmrc file, it will allow for users to format their package.json differently per project or globally.
Example
# New subcommand in `npm pkg` to format `package.json` based on config values
npm pkg format [--dry-run]
# Any command that writes to `package.json` will do so based on the config values# All of these could update any part of `package.json` if the relevant configs are set
npm install foo
npm pkg set foo="barnpm init --yes
How
In the initial implementation, default config values will be set to mirror current behavior where possible, with the goal being that formatting during commands that write to package.json will not be a breaking change.
Sorting will be done via localeCompare.
Configs
pkg-format-indent
Specifies how the file will be indented. If the value is an integer, spaces will be used.
Defaults to current (current|n|tabs)
Defaulting to current is new behavior based on the discussion in [RRFC] Don't expand tabs by default #420. To not have this change any behavior, the default would be 2.
pkg-format-eol
Specifies what the line endings in the file will be
Defaults to current (current|lf|cr|crlf)
Defaulting to current is new behavior based on the discussion in [RRFC] Don't expand tabs by default #420. To not have this change any behavior, the default would be lf.
pkg-format-sort-children
Specifies which top level items will have their children sorted
If the item is an object, it will be sorted by key. If the item is an array of strings, it will be sorted by string. Otherwise it will be ignored.
Defaults to dependencies,devDependencies,peerDependencies,optionalDependencies
pkg-format-keys-start
Specifies a list of top level keys that will always be moved to the start of package.json
Defaults to ``
pkg-format-keys-end
Specifies a list of top level keys that will always be moved to the end of package.json
Defaults to ``
pkg-format-keys-sort
Specifies whether other top level keys not specified by keys-start and keys-end will be sorted
Defaults to false (true|false)
Open Questions
How should the presence of indent_style|indent_size and end_of_line from EditorConfig affect the comparable config values? The EditorConfig spec relies on walking the directory tree to get the values.
Should pkg-format-indent and pkg-format-eol be applied to package-lock.json as well?
This RFC covers only the configs necessary to format a package.json file. fixpack also will warn/error on missing top level keys, which is not covered by this RFC.
Motivation ("The Why")
Allow
package.jsonto be formatted according to a set of config values. This will establish default config values for howpackage.jsonis written and add a command to rewrite an existingpackage.jsonfiles based on those config values. Since these config values will live in an.npmrcfile, it will allow for users to format theirpackage.jsondifferently per project or globally.Example
How
In the initial implementation, default config values will be set to mirror current behavior where possible, with the goal being that formatting during commands that write to
package.jsonwill not be a breaking change.Sorting will be done via
localeCompare.Configs
pkg-format-indentcurrent(current|n|tabs)currentis new behavior based on the discussion in [RRFC] Don't expand tabs by default #420. To not have this change any behavior, the default would be2.pkg-format-eolcurrent(current|lf|cr|crlf)currentis new behavior based on the discussion in [RRFC] Don't expand tabs by default #420. To not have this change any behavior, the default would belf.pkg-format-sort-childrendependencies,devDependencies,peerDependencies,optionalDependenciespkg-format-keys-startpackage.jsonpkg-format-keys-endpackage.jsonpkg-format-keys-sortkeys-startandkeys-endwill be sortedfalse(true|false)Open Questions
indent_style|indent_sizeandend_of_linefromEditorConfigaffect the comparable config values? The EditorConfig spec relies on walking the directory tree to get the values.pkg-format-indentandpkg-format-eolbe applied topackage-lock.jsonas well?References
package.jsonfile.fixpackalso will warn/error on missing top level keys, which is not covered by this RFC.