Skip to content

[RRFC] format package.json based on config values #423

Description

@lukekarrys

Motivation ("The Why")

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="bar
npm 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?

References

  • This should cover the use cases mentioned in [RRFC] Don't expand tabs by default #420
  • Prior art: fixpack
    • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions