Skip to content

Commit 8f1aef9

Browse files
Shinigami92JounQin
andauthored
feat(pkg): add option packageSortOrderPreset (#475)
Co-authored-by: JounQin <admin@1stg.me>
1 parent c45e6d4 commit 8f1aef9

File tree

10 files changed

+600
-9
lines changed

10 files changed

+600
-9
lines changed

.changeset/silver-shirts-relax.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"prettier-plugin-pkg": patch
3+
---
4+
5+
feat(pkg): add option `packageSortOrderPreset`
6+
7+
This option allows you to specify a preset for sorting packages in the `package.json` file.
8+
9+
The available presets are: `npm`, `npm-plus`:
10+
11+
- `npm`: sorts by [`npm`'s document](https://docs.npmjs.com/cli/v11/configuring-npm/package-json)
12+
- `npm-plus`: sorts by [`sort-package-json`](https://github.com/keithamus/sort-package-json/blob/aa6774ad937feb83178c8bc981f08305e1d22b5c/defaultRules.md) and therefore is compatible to [`prettier-plugin-packagejson`](https://github.com/matzkoh/prettier-plugin-packagejson)

.github/workflows/size-limit.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ concurrency:
1111

1212
jobs:
1313
size-limit:
14-
if: ${{ github.repository_owner == 'un-ts' }}
1514
runs-on: ubuntu-latest
1615
steps:
1716
- name: Checkout Repo

.github/workflows/vercel.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ concurrency:
1212

1313
jobs:
1414
deploy:
15-
if: ${{ github.repository_owner == 'un-ts' }}
15+
if: ${{ !github.event.pull_request.head.repo.fork }}
1616
runs-on: ubuntu-latest
1717
steps:
1818
- name: Checkout Repo

.size-limit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
{
77
"path": "packages/pkg/lib/index.js",
8-
"limit": "600B"
8+
"limit": "800B"
99
},
1010
{
1111
"path": "packages/sh/lib/index.js",

packages/pkg/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ Top-level keys are sorted according to a style commonly seen in the packages of
161161

162162
Unknown keys, or keys not part of the list above, will be alphabetically sorted and added to the end of the file. Note that this list takes into account both `npm` and `yarn` keys.
163163

164+
You can also use the `packageSortOrder` option to specify a custom sort order, or use the `packageSortOrderPreset` option to use a preset sort order:
165+
166+
- `npm`: sorts by [`npm`'s document](https://docs.npmjs.com/cli/v11/configuring-npm/package-json)
167+
- `npm-plus`: sorts by [`sort-package-json`](https://github.com/keithamus/sort-package-json/blob/aa6774ad937feb83178c8bc981f08305e1d22b5c/defaultRules.md) and therefore is compatible to [`prettier-plugin-packagejson`](https://github.com/matzkoh/prettier-plugin-packagejson)
168+
164169
### Forthcoming
165170

166171
Forthcoming rules include:
@@ -176,6 +181,8 @@ interface FormatOptions {
176181
packageSortOrder?: string[]
177182
// An array of property names to ignore when sorting the package.json properties.
178183
packageIgnoreSort?: string[]
184+
// A preset for the package.json sort order.
185+
packageSortOrderPreset?: 'npm' | 'npm-plus'
179186
}
180187
```
181188

packages/pkg/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,23 @@ export default {
7575
description:
7676
'An array of property names to ignore when sorting the package.json properties.',
7777
},
78+
packageSortOrderPreset: {
79+
since: '0.21.1',
80+
category: 'Package',
81+
type: 'choice',
82+
choices: [
83+
{
84+
value: 'npm',
85+
description:
86+
'Sorted according to https://docs.npmjs.com/cli/v11/configuring-npm/package-json.',
87+
},
88+
{
89+
value: 'npm-plus',
90+
description:
91+
'Sorted according to https://github.com/keithamus/sort-package-json/blob/aa6774ad937feb83178c8bc981f08305e1d22b5c/defaultRules.md.',
92+
},
93+
],
94+
description: 'A preset for the package.json sort order.',
95+
},
7896
},
7997
} as Plugin

packages/pkg/src/rules/sort.ts

Lines changed: 173 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import type { FormatOptions, ObjectProperty } from '../types.js'
1111
import { alphabetSort, sortObject } from '../utils.js'
1212

13-
export const dependencyNames = [
13+
export const dependencyNames: readonly string[] = [
1414
'bundledDependencies',
1515
'peerDependencies',
1616
'peerDependenciesMeta',
@@ -23,9 +23,162 @@ export const dependencyNames = [
2323
]
2424

2525
/**
26-
* Reference: npm - https://docs.npmjs.com/files/package.json yarn -
27-
* https://yarnpkg.com/configuration/manifest vscode -
28-
* https://code.visualstudio.com/api/references/extension-manifest
26+
* Reference: `npm` -
27+
* https://docs.npmjs.com/cli/v11/configuring-npm/package-json
28+
*/
29+
const NPM_SORTS: readonly string[] = [
30+
'$schema',
31+
'name',
32+
'version',
33+
'description',
34+
'keywords',
35+
'homepage',
36+
'bugs',
37+
'license',
38+
'author',
39+
'contributors',
40+
'funding',
41+
'files',
42+
'exports',
43+
'main',
44+
'browser',
45+
'bin',
46+
'man',
47+
'directories',
48+
'repository',
49+
'scripts',
50+
'config',
51+
'dependencies',
52+
'devDependencies',
53+
'peerDependencies',
54+
'peerDependenciesMeta',
55+
'bundleDependencies',
56+
'optionalDependencies',
57+
'overrides',
58+
'engines',
59+
'os',
60+
'cpu',
61+
'libc',
62+
'devEngines',
63+
'private',
64+
'publishConfig',
65+
'workspaces',
66+
]
67+
68+
/**
69+
* Reference: `sort-package-json` -
70+
* https://github.com/keithamus/sort-package-json/blob/aa6774ad937feb83178c8bc981f08305e1d22b5c/defaultRules.md
71+
*/
72+
const NPM_PLUS_SORTS: readonly string[] = [
73+
'$schema',
74+
'name',
75+
'displayName',
76+
'version',
77+
'private',
78+
'description',
79+
'categories',
80+
'keywords',
81+
'homepage',
82+
'bugs',
83+
'repository',
84+
'funding',
85+
'license',
86+
'qna',
87+
'author',
88+
'maintainers',
89+
'contributors',
90+
'publisher',
91+
'sideEffects',
92+
'type',
93+
'imports',
94+
'exports',
95+
'main',
96+
'svelte',
97+
'umd:main',
98+
'jsdelivr',
99+
'unpkg',
100+
'module',
101+
'source',
102+
'jsnext:main',
103+
'browser',
104+
'react-native',
105+
'types',
106+
'typesVersions',
107+
'typings',
108+
'style',
109+
'example',
110+
'examplestyle',
111+
'assets',
112+
'bin',
113+
'man',
114+
'directories',
115+
'files',
116+
'workspaces',
117+
'binary',
118+
'scripts',
119+
'betterScripts',
120+
'contributes',
121+
'activationEvents',
122+
'husky',
123+
'simple-git-hooks',
124+
'pre-commit',
125+
'commitlint',
126+
'lint-staged',
127+
'nano-staged',
128+
'config',
129+
'nodemonConfig',
130+
'browserify',
131+
'babel',
132+
'browserslist',
133+
'xo',
134+
'prettier',
135+
'eslintConfig',
136+
'eslintIgnore',
137+
'npmpackagejsonlint',
138+
'release',
139+
'remarkConfig',
140+
'stylelint',
141+
'ava',
142+
'jest',
143+
'mocha',
144+
'nyc',
145+
'tap',
146+
'oclif',
147+
'resolutions',
148+
'dependencies',
149+
'devDependencies',
150+
'dependenciesMeta',
151+
'peerDependencies',
152+
'peerDependenciesMeta',
153+
'optionalDependencies',
154+
'bundledDependencies',
155+
'bundleDependencies',
156+
'extensionPack',
157+
'extensionDependencies',
158+
'flat',
159+
'packageManager',
160+
'engines',
161+
'engineStrict',
162+
'volta',
163+
'languageName',
164+
'os',
165+
'cpu',
166+
'preferGlobal',
167+
'publishConfig',
168+
'icon',
169+
'badges',
170+
'galleryBanner',
171+
'preview',
172+
'markdown',
173+
'pnpm',
174+
]
175+
176+
/**
177+
* Reference:
178+
*
179+
* 1. `npm` - https://docs.npmjs.com/files/package.json
180+
* 2. `yarn` - https://yarnpkg.com/configuration/manifest
181+
* 3. `vscode` - https://code.visualstudio.com/api/references/extension-manifest
29182
*/
30183
const primary: readonly string[] = [
31184
// schema definition
@@ -113,14 +266,28 @@ const primary: readonly string[] = [
113266
'extensionKind',
114267
]
115268

269+
const DEFAULT_SORT_ORDERS = {
270+
npm: NPM_SORTS,
271+
'npm-plus': NPM_PLUS_SORTS,
272+
}
273+
116274
const uniqueArray = <T>(arr: readonly T[]) => {
117275
return [...new Set(arr)]
118276
}
119277

120278
export const sort = (props: ObjectProperty[], options: FormatOptions) => {
121-
let { packageSortOrder } = options
279+
let { packageSortOrder, packageSortOrderPreset } = options
280+
281+
const defaultSortOrder =
282+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
283+
(packageSortOrderPreset && DEFAULT_SORT_ORDERS[packageSortOrderPreset]) ||
284+
[]
122285

123-
packageSortOrder = uniqueArray([...(packageSortOrder ?? []), ...primary])
286+
packageSortOrder = uniqueArray([
287+
...(packageSortOrder ?? []),
288+
...defaultSortOrder,
289+
...primary,
290+
])
124291

125292
const others: ObjectProperty[] = []
126293
const known = props.filter(prop => {

packages/pkg/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ export type { StringLiteral } from '@babel/types'
4747
export interface FormatOptions {
4848
packageSortOrder?: string[]
4949
packageIgnoreSort?: string[]
50+
packageSortOrderPreset?: 'npm-plus' | 'npm'
5051
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy