Socket
Socket
Sign inDemoInstall

sort-package-json

Package Overview
Dependencies
44
Maintainers
1
Versions
96
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.51.0 to 1.57.0

46

index.js

@@ -9,4 +9,7 @@ const sortObjectKeys = require('sort-object-keys')

Object.prototype.hasOwnProperty.call(object, property)
const pipe = (fns) => (x) => fns.reduce((result, fn) => fn(result), x)
const onArray = (fn) => (x) => (Array.isArray(x) ? fn(x) : x)
const pipe =
(fns) =>
(x, ...args) =>
fns.reduce((result, fn) => fn(result, ...args), x)
const onArray = (fn) => (x) => Array.isArray(x) ? fn(x) : x
const onStringArray = (fn) => (x) =>

@@ -17,3 +20,6 @@ Array.isArray(x) && x.every((item) => typeof item === 'string') ? fn(x) : x

const uniqAndSortArray = pipe([uniq, sortArray])
const onObject = (fn) => (x) => (isPlainObject(x) ? fn(x) : x)
const onObject =
(fn) =>
(x, ...args) =>
isPlainObject(x) ? fn(x, ...args) : x
const sortObjectBy = (comparator, deep) => {

@@ -43,6 +49,8 @@ const over = onObject((object) => {

])
const overProperty = (property, over) => (object) =>
hasOwnProperty(object, property)
? Object.assign(object, { [property]: over(object[property]) })
: object
const overProperty =
(property, over) =>
(object, ...args) =>
hasOwnProperty(object, property)
? Object.assign(object, { [property]: over(object[property], ...args) })
: object
const sortGitHooks = sortObjectBy(gitHooks)

@@ -122,2 +130,4 @@

const sortVolta = sortObjectBy(['node', 'npm', 'yarn'])
// See https://docs.npmjs.com/misc/scripts

@@ -138,3 +148,10 @@ const defaultNpmScripts = new Set([

const sortScripts = onObject((scripts) => {
const hasDevDependency = (dependency, packageJson) => {
return (
'devDependencies' in packageJson &&
!!packageJson.devDependencies[dependency]
)
}
const sortScripts = onObject((scripts, packageJson) => {
const names = Object.keys(scripts)

@@ -152,2 +169,6 @@ const prefixable = new Set()

if (!hasDevDependency('npm-run-all', packageJson)) {
keys.sort()
}
const order = keys.reduce(

@@ -203,2 +224,3 @@ (order, key) =>

{ key: 'main' },
{ key: 'svelte' },
{ key: 'umd:main' },

@@ -211,2 +233,3 @@ { key: 'jsdelivr' },

{ key: 'browser' },
{ key: 'react-native' },
{ key: 'types' },

@@ -254,2 +277,5 @@ { key: 'typesVersions' },

{ key: 'npmpkgjsonlint', over: sortObject },
{ key: 'npmPackageJsonLintConfig', over: sortObject },
{ key: 'npmpackagejsonlint', over: sortObject },
{ key: 'release', over: sortObject },
{ key: 'remarkConfig', over: sortObject },

@@ -276,4 +302,6 @@ { key: 'stylelint' },

{ key: 'flat' },
{ key: 'packageManager' },
{ key: 'engines', over: sortObject },
{ key: 'engineStrict', over: sortObject },
{ key: 'volta', over: sortVolta },
{ key: 'languageName' },

@@ -347,3 +375,3 @@ { key: 'os' },

return overFields(sortObjectKeys(json, sortOrder))
return overFields(sortObjectKeys(json, sortOrder), json)
}),

@@ -350,0 +378,0 @@ )

24

package.json
{
"name": "sort-package-json",
"version": "1.51.0",
"version": "1.57.0",
"description": "Sort an Object or package.json based on the well-known package.json keys",

@@ -66,4 +66,3 @@ "keywords": [

"standard",
"plugin:prettier/recommended",
"prettier/standard"
"plugin:prettier/recommended"
]

@@ -85,14 +84,13 @@ },

"@commitlint/config-conventional": "8.3.4",
"ava": "3.5.1",
"ava": "4.1.0",
"del": "5.1.0",
"dot-prop": "^5.2.0",
"dtslint": "^4.0.5",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"eslint": "^8.12.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0-1",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-n": "^14.0.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"husky": "^4.2.3",

@@ -102,3 +100,3 @@ "lint-staged": "^10.0.8",

"nyc": "^15.0.0",
"prettier": "^2.0.4",
"prettier": "^2.6.2",
"semantic-release": "17.0.5",

@@ -105,0 +103,0 @@ "tempy": "0.4.0",

@@ -187,2 +187,3 @@ # Sort Package.json

- [remark](https://remark.js.org/)
- [semantic-release](https://github.com/semantic-release/semantic-release)
- [stylelint](https://github.com/stylelint/stylelint)

@@ -229,4 +230,11 @@ - [Tap](https://node-tap.org/)

### I would like this tool to be configurable with a config file or command line arguments.
The lack of configuration here is a feature, not a bug. The intent of this tool is that a user can open a package json and always expect to see keys in a particular order. If we add a configuration for this tool, then that promise is broken, as users will first need to look at the configuration for each project to learn the ways in which this tool will change the `package.json`. The structure of the `package.json` should always be predictable & deterministic from project to project. I think the _reason_ why this project is well used is because it is not another "tool" you have to set up with yet another JSON file and more cruft in your project to support it. You run a command and it does what it says on the tin.
A lot of people who ask for configuration cite the use case that they simply don't like the given order that exists and want to make sweeping changes. To me this seems far better suited to simply making a fork of this project as then you can go far further than specifying configuration.
### What?! Why would you want to do this?!
Well, it's nice to have the keys of a package.json in a well sorted order. Almost everyone would agree having "name" at the top of a package.json is sensible (rather than sorted alphabetically or somewhere silly like the bottom), so why not the rest of the package.json?
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc