@lntvow/sort-package-json 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +456 -0
- package/cli.js +138 -0
- package/index.cjs +704 -0
- package/index.d.ts +34 -0
- package/index.js +636 -0
- package/package.json +108 -0
- package/reporter.js +139 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Keith Cirkel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
> Fork Notice
|
|
2
|
+
>
|
|
3
|
+
> This package is a fork of [keithamus/sort-package-json](https://github.com/keithamus/sort-package-json).
|
|
4
|
+
> The original author attribution and project content are preserved.
|
|
5
|
+
> The fork introduces one behavioral change only: by default, scripts and
|
|
6
|
+
> betterScripts are not sorted. Pass --sort-scripts to enable scripts sorting.
|
|
7
|
+
|
|
8
|
+
# Sort Package.json
|
|
9
|
+
|
|
10
|
+
[![Build Status][github_actions_badge]][github_actions_link]
|
|
11
|
+
[![NPM Version][package_version_badge]][package_link]
|
|
12
|
+
|
|
13
|
+
[package_version_badge]: https://img.shields.io/npm/v/sort-package-json.svg
|
|
14
|
+
[package_link]: https://www.npmjs.com/package/sort-package-json
|
|
15
|
+
[github_actions_badge]: https://img.shields.io/github/actions/workflow/status/keithamus/sort-package-json/pr.yml
|
|
16
|
+
[github_actions_link]: https://github.com/keithamus/sort-package-json/actions?query=workflow%3ACI+branch%3Amain
|
|
17
|
+
|
|
18
|
+
## CLI
|
|
19
|
+
|
|
20
|
+
### Run via npx (npm@5.2+ required)
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npx sort-package-json
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install --global sort-package-json
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Usage
|
|
33
|
+
|
|
34
|
+
```console
|
|
35
|
+
$ cd my-project
|
|
36
|
+
$ cat package.json
|
|
37
|
+
{
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"sort-package-json": "1.0.0",
|
|
40
|
+
"sort-object-keys": "1.0.0"
|
|
41
|
+
},
|
|
42
|
+
"version": "1.0.0",
|
|
43
|
+
"name": "my-awesome-project"
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
$ npx sort-package-json
|
|
47
|
+
package.json is sorted!
|
|
48
|
+
|
|
49
|
+
Found 1 file.
|
|
50
|
+
1 file successfully sorted.
|
|
51
|
+
|
|
52
|
+
$ cat package.json
|
|
53
|
+
{
|
|
54
|
+
"name": "my-awesome-project",
|
|
55
|
+
"version": "1.0.0",
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"sort-object-keys": "1.0.0",
|
|
58
|
+
"sort-package-json": "1.0.0"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
CLI also supports multi file paths or [`glob`](https://github.com/sindresorhus/globby) - so you can give it a bunch of `package.json` file(s) to sort.
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
$ sort-package-json "my-package/package.json" "other-package/package.json"
|
|
67
|
+
|
|
68
|
+
$ sort-package-json "package.json" "packages/*/package.json"
|
|
69
|
+
|
|
70
|
+
$ sort-package-json "package.json" "packages/*/package.json" --ignore "packages/one-package"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### `--check` flag
|
|
74
|
+
|
|
75
|
+
When you want to check if your files are sorted, you can run CLI with the `--check` flag (or `-c`). This will output a list of not sorted files, if any.
|
|
76
|
+
|
|
77
|
+
```console
|
|
78
|
+
$ sort-package-json "**/package.json" --check
|
|
79
|
+
|
|
80
|
+
Found 5 files.
|
|
81
|
+
5 files were already sorted.
|
|
82
|
+
|
|
83
|
+
$ sort-package-json "**/package.json" --check
|
|
84
|
+
foo/package.json
|
|
85
|
+
bar/package.json
|
|
86
|
+
|
|
87
|
+
Found 5 files.
|
|
88
|
+
3 files were not sorted.
|
|
89
|
+
2 files were already sorted.
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### `--quiet` flag
|
|
93
|
+
|
|
94
|
+
In order to silence any successful output, you can run CLI with the `--quiet` flag (or `-q`). This will stop the CLI from outputting if it runs successfully, but won't effect error messages and the exit code.
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
$ sort-package-json "**/package.json" --check --quiet
|
|
98
|
+
$ sort-package-json "**/package.json" --quiet
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### `--stdin` flag
|
|
102
|
+
|
|
103
|
+
To read from `stdin` and output the result to `stdout` use the `--stdin` flag.
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
$ cat package.json | sort-package-json --stdin
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### `--sort-scripts` flag
|
|
110
|
+
|
|
111
|
+
By default, CLI keeps `scripts` and `betterScripts` key order unchanged while sorting the rest of the file. Use `--sort-scripts` to enable sorting for those fields.
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
$ sort-package-json package.json --sort-scripts
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
This can, for instance, be used to generate a diff before changing `package.json`.
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
$ ( PKG="./package.json" ; cat "${PKG?}" | sort-package-json --stdin | diff "${PKG?}" - ; )
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## API
|
|
124
|
+
|
|
125
|
+
### Install
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
npm install --save-dev sort-package-json
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Usage
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
sortPackageJson(packageJson, options?)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Pass a JSON string, return a new sorted JSON string.\
|
|
138
|
+
Pass a JSON object, return a new sorted JSON object.
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
import sortPackageJson from 'sort-package-json'
|
|
142
|
+
|
|
143
|
+
const packageJsonString = `{
|
|
144
|
+
"dependencies": {
|
|
145
|
+
"sort-package-json": "1.0.0",
|
|
146
|
+
"sort-object-keys": "1.0.0"
|
|
147
|
+
},
|
|
148
|
+
"version": "1.0.0",
|
|
149
|
+
"name": "my-awesome-project"
|
|
150
|
+
}`
|
|
151
|
+
|
|
152
|
+
console.log(sortPackageJson(packageJsonString))
|
|
153
|
+
/* => string:
|
|
154
|
+
{
|
|
155
|
+
"name": "my-awesome-project",
|
|
156
|
+
"version": "1.0.0",
|
|
157
|
+
"dependencies": {
|
|
158
|
+
"sort-object-keys": "1.0.0",
|
|
159
|
+
"sort-package-json": "1.0.0"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
*/
|
|
163
|
+
|
|
164
|
+
const packageJsonObject = JSON.parse(packageJsonString)
|
|
165
|
+
console.log(sortPackageJson(packageJsonObject))
|
|
166
|
+
/* => object:
|
|
167
|
+
{
|
|
168
|
+
name: 'my-awesome-project',
|
|
169
|
+
version: '1.0.0',
|
|
170
|
+
dependencies: {
|
|
171
|
+
'sort-object-keys': '1.0.0',
|
|
172
|
+
'sort-package-json': '1.0.0'
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
*/
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
#### options.sortOrder
|
|
179
|
+
|
|
180
|
+
Type: `string[] | Function`\
|
|
181
|
+
Default: `sortPackageJson.sortOrder`
|
|
182
|
+
|
|
183
|
+
Custom ordering array or comparator function.
|
|
184
|
+
|
|
185
|
+
If an array, sort keys in ordering of `options.sortOrder`.
|
|
186
|
+
|
|
187
|
+
**Notice**: fields not in this array, will still sort by `defaultSortOrder`
|
|
188
|
+
|
|
189
|
+
```js
|
|
190
|
+
const sorted = sortPackageJson(packageJsonObject, {
|
|
191
|
+
sortOrder: ['version'],
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
console.log(Object.keys(sorted))
|
|
195
|
+
|
|
196
|
+
// -> [ 'version', 'name', 'dependencies' ]
|
|
197
|
+
// ^^^^^^^^^^^^^^^^^^^^^^
|
|
198
|
+
// `name` and `dependencies` are sorted by defaultSortOrder
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
If a function, sort fields by [Array#sort(options.sortOrder)](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description)
|
|
202
|
+
|
|
203
|
+
```js
|
|
204
|
+
const sorted = sortPackageJson(packageJsonObject, {
|
|
205
|
+
sortOrder(left, right) {
|
|
206
|
+
return left.localeCompare(right)
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
console.log(Object.keys(sorted))
|
|
211
|
+
|
|
212
|
+
// -> [ 'dependencies', 'name', 'version' ]
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### options.sortScripts
|
|
216
|
+
|
|
217
|
+
Type: `boolean`\
|
|
218
|
+
Default: `false`
|
|
219
|
+
|
|
220
|
+
Controls whether `scripts` and `betterScripts` are sorted.
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
const sorted = sortPackageJson(packageJsonObject, {
|
|
224
|
+
sortScripts: false,
|
|
225
|
+
})
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Related tools
|
|
229
|
+
|
|
230
|
+
- [ESLint Rule with Autofix](https://github.com/kellyselden/eslint-plugin-json-files#supported-rules)
|
|
231
|
+
- [Prettier Plugin](https://github.com/matzkoh/prettier-plugin-packagejson)
|
|
232
|
+
- [Visual Studio Code Extension](https://github.com/unional/vscode-sort-package-json)
|
|
233
|
+
|
|
234
|
+
## Supported Libraries
|
|
235
|
+
|
|
236
|
+
- [AVA](https://github.com/avajs/ava)
|
|
237
|
+
- [Babel](https://babeljs.io/)
|
|
238
|
+
- [Browserify](http://browserify.org/)
|
|
239
|
+
- [c8](https://github.com/bcoe/c8)
|
|
240
|
+
- [commitlint](https://commitlint.js.org/)
|
|
241
|
+
- [ESLint](https://eslint.org/)
|
|
242
|
+
- [Husky](https://github.com/typicode/husky)
|
|
243
|
+
- [Istanbul](https://istanbul.js.org/)
|
|
244
|
+
- [Jest](https://jestjs.io/)
|
|
245
|
+
- [lint-staged](https://github.com/okonet/lint-staged)
|
|
246
|
+
- [Mocha](https://mochajs.org/)
|
|
247
|
+
- [node-pre-gyp](https://github.com/mapbox/node-pre-gyp)
|
|
248
|
+
- [npm-package-json-lint](https://npmpackagejsonlint.org/)
|
|
249
|
+
- [oclif](https://oclif.io/)
|
|
250
|
+
- [pnpm](https://pnpm.io/)
|
|
251
|
+
- [Prettier](https://prettier.io/)
|
|
252
|
+
- [remark](https://remark.js.org/)
|
|
253
|
+
- [semantic-release](https://github.com/semantic-release/semantic-release)
|
|
254
|
+
- [stylelint](https://github.com/stylelint/stylelint)
|
|
255
|
+
- [Tap](https://node-tap.org/)
|
|
256
|
+
- [xojs](https://github.com/xojs/xo)
|
|
257
|
+
|
|
258
|
+
_Alphabetically ordered._
|
|
259
|
+
|
|
260
|
+
## Automatically Sort
|
|
261
|
+
|
|
262
|
+
The package.json file can be sorted automatically before committing.
|
|
263
|
+
|
|
264
|
+
```bash
|
|
265
|
+
npm install husky lint-staged --save-dev
|
|
266
|
+
npm pkg set scripts.prepare="husky install"
|
|
267
|
+
npm run prepare
|
|
268
|
+
npx husky add .husky/pre-commit "npx lint-staged"
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Add the following to your `package.json` file
|
|
272
|
+
|
|
273
|
+
```json
|
|
274
|
+
{
|
|
275
|
+
"lint-staged": {
|
|
276
|
+
"package.json": "sort-package-json"
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
See [Husky](https://github.com/typicode/husky) and [lint-staged](https://github.com/okonet/lint-staged) for more information.
|
|
282
|
+
|
|
283
|
+
## PFAQ: Potential Frequently Asked Questions
|
|
284
|
+
|
|
285
|
+
### How does it sort?
|
|
286
|
+
|
|
287
|
+
It sorts using [`sort-object-keys`](http://github.com/keithamus/sort-object-keys). It sorts using the well-known keys of a package.json. For the full list check the [default rules](./defaultRules.md). It sorts sub-keys too - sometimes by a well-known order, other times alphabetically. The initial order was derived from the [package.json docs](https://docs.npmjs.com/files/package.json) with a few extras added for good measure.
|
|
288
|
+
|
|
289
|
+
### It doesn't sort X?
|
|
290
|
+
|
|
291
|
+
Cool. Send a PR! It might get denied if it is a specific vendor key of an unpopular project (e.g. `"my-super-unknown-project"`). We sort keys like "browserify" because it is a project with millions of users. If your project has, say, over 100 users, then we'll add it. Sound fair?
|
|
292
|
+
|
|
293
|
+
### Isn't this just like Project X?
|
|
294
|
+
|
|
295
|
+
Could be. I wanted this one because at the time of writing, nothing is:
|
|
296
|
+
|
|
297
|
+
- Zero config
|
|
298
|
+
- Able to be used in a library
|
|
299
|
+
- Quiet (i.e. not spitting out annoying log messages, when used in a library mode)
|
|
300
|
+
|
|
301
|
+
### I would like this tool to be configurable with a config file or command line arguments.
|
|
302
|
+
|
|
303
|
+
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.
|
|
304
|
+
|
|
305
|
+
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.
|
|
306
|
+
|
|
307
|
+
### What is the order this package defaults to?
|
|
308
|
+
|
|
309
|
+
The default order is exported as a `sortOrder` object.
|
|
310
|
+
|
|
311
|
+
<details>
|
|
312
|
+
<summary>Properties mentioned in the npm docs</summary>
|
|
313
|
+
|
|
314
|
+
1. `name`
|
|
315
|
+
1. `version`
|
|
316
|
+
1. `private`
|
|
317
|
+
1. `description`
|
|
318
|
+
1. `keywords`
|
|
319
|
+
1. `homepage`
|
|
320
|
+
1. `bugs`
|
|
321
|
+
1. `repository`
|
|
322
|
+
1. `funding`
|
|
323
|
+
1. `license`
|
|
324
|
+
1. `author`
|
|
325
|
+
1. `contributors`
|
|
326
|
+
1. `main`
|
|
327
|
+
1. `browser`
|
|
328
|
+
1. `bin`
|
|
329
|
+
1. `man`
|
|
330
|
+
1. `directories`
|
|
331
|
+
1. `files`
|
|
332
|
+
1. `workspaces`
|
|
333
|
+
1. `scripts`
|
|
334
|
+
1. `config`
|
|
335
|
+
1. `dependencies`
|
|
336
|
+
1. `engines`
|
|
337
|
+
1. `os`
|
|
338
|
+
1. `cpu`
|
|
339
|
+
|
|
340
|
+
</details>
|
|
341
|
+
|
|
342
|
+
<details>
|
|
343
|
+
<summary>Full list of recognized properties</summary>
|
|
344
|
+
|
|
345
|
+
1. `$schema`
|
|
346
|
+
1. `name`
|
|
347
|
+
1. `displayName`
|
|
348
|
+
1. `version`
|
|
349
|
+
1. `private`
|
|
350
|
+
1. `description`
|
|
351
|
+
1. `categories`
|
|
352
|
+
1. `keywords`
|
|
353
|
+
1. `homepage`
|
|
354
|
+
1. `bugs`
|
|
355
|
+
1. `repository`
|
|
356
|
+
1. `funding`
|
|
357
|
+
1. `license`
|
|
358
|
+
1. `qna`
|
|
359
|
+
1. `author`
|
|
360
|
+
1. `maintainers`
|
|
361
|
+
1. `contributors`
|
|
362
|
+
1. `publisher`
|
|
363
|
+
1. `sideEffects`
|
|
364
|
+
1. `type`
|
|
365
|
+
1. `imports`
|
|
366
|
+
1. `exports`
|
|
367
|
+
1. `main`
|
|
368
|
+
1. `svelte`
|
|
369
|
+
1. `umd:main`
|
|
370
|
+
1. `jsdelivr`
|
|
371
|
+
1. `unpkg`
|
|
372
|
+
1. `module`
|
|
373
|
+
1. `source`
|
|
374
|
+
1. `jsnext:main`
|
|
375
|
+
1. `browser`
|
|
376
|
+
1. `react-native`
|
|
377
|
+
1. `types`
|
|
378
|
+
1. `typesVersions`
|
|
379
|
+
1. `typings`
|
|
380
|
+
1. `style`
|
|
381
|
+
1. `example`
|
|
382
|
+
1. `examplestyle`
|
|
383
|
+
1. `assets`
|
|
384
|
+
1. `bin`
|
|
385
|
+
1. `man`
|
|
386
|
+
1. `directories`
|
|
387
|
+
1. `files`
|
|
388
|
+
1. `workspaces`
|
|
389
|
+
1. `binary`
|
|
390
|
+
1. `scripts`
|
|
391
|
+
1. `betterScripts`
|
|
392
|
+
1. `contributes`
|
|
393
|
+
1. `activationEvents`
|
|
394
|
+
1. `husky`
|
|
395
|
+
1. `simple-git-hooks`
|
|
396
|
+
1. `pre-commit`
|
|
397
|
+
1. `commitlint`
|
|
398
|
+
1. `lint-staged`
|
|
399
|
+
1. `nano-staged`
|
|
400
|
+
1. `config`
|
|
401
|
+
1. `nodemonConfig`
|
|
402
|
+
1. `browserify`
|
|
403
|
+
1. `babel`
|
|
404
|
+
1. `browserslist`
|
|
405
|
+
1. `xo`
|
|
406
|
+
1. `prettier`
|
|
407
|
+
1. `eslintConfig`
|
|
408
|
+
1. `eslintIgnore`
|
|
409
|
+
1. `npmpkgjsonlint`
|
|
410
|
+
1. `npmPackageJsonLintConfig`
|
|
411
|
+
1. `npmpackagejsonlint`
|
|
412
|
+
1. `release`
|
|
413
|
+
1. `remarkConfig`
|
|
414
|
+
1. `stylelint`
|
|
415
|
+
1. `ava`
|
|
416
|
+
1. `jest`
|
|
417
|
+
1. `jest-junit`
|
|
418
|
+
1. `jest-stare`
|
|
419
|
+
1. `mocha`
|
|
420
|
+
1. `nyc`
|
|
421
|
+
1. `c8`
|
|
422
|
+
1. `tap`
|
|
423
|
+
1. `resolutions`
|
|
424
|
+
1. `dependencies`
|
|
425
|
+
1. `devDependencies`
|
|
426
|
+
1. `dependenciesMeta`
|
|
427
|
+
1. `peerDependencies`
|
|
428
|
+
1. `peerDependenciesMeta`
|
|
429
|
+
1. `optionalDependencies`
|
|
430
|
+
1. `bundledDependencies`
|
|
431
|
+
1. `bundleDependencies`
|
|
432
|
+
1. `extensionPack`
|
|
433
|
+
1. `extensionDependencies`
|
|
434
|
+
1. `flat`
|
|
435
|
+
1. `packageManager`
|
|
436
|
+
1. `engines`
|
|
437
|
+
1. `engineStrict`
|
|
438
|
+
1. `devEngines`
|
|
439
|
+
1. `volta`
|
|
440
|
+
1. `languageName`
|
|
441
|
+
1. `os`
|
|
442
|
+
1. `cpu`
|
|
443
|
+
1. `preferGlobal`
|
|
444
|
+
1. `publishConfig`
|
|
445
|
+
1. `icon`
|
|
446
|
+
1. `badges`
|
|
447
|
+
1. `galleryBanner`
|
|
448
|
+
1. `preview`
|
|
449
|
+
1. `markdown`
|
|
450
|
+
1. `pnpm`
|
|
451
|
+
|
|
452
|
+
</details>
|
|
453
|
+
|
|
454
|
+
### What?! Why would you want to do this?!
|
|
455
|
+
|
|
456
|
+
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?
|
package/cli.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs/promises'
|
|
3
|
+
import streamConsumers from 'node:stream/consumers'
|
|
4
|
+
import { parseArgs } from 'node:util'
|
|
5
|
+
import { glob } from 'tinyglobby'
|
|
6
|
+
import sortPackageJson from './index.js'
|
|
7
|
+
import Reporter from './reporter.js'
|
|
8
|
+
import packageJson from './package.json' with { type: 'json' }
|
|
9
|
+
|
|
10
|
+
function showVersion() {
|
|
11
|
+
const { name, version } = packageJson
|
|
12
|
+
|
|
13
|
+
console.log(`${name} ${version}`)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function showHelpInformation() {
|
|
17
|
+
console.log(
|
|
18
|
+
`Usage: sort-package-json [options] [file/glob ...]
|
|
19
|
+
|
|
20
|
+
Sort package.json files.
|
|
21
|
+
If file/glob is omitted, './package.json' file will be processed.
|
|
22
|
+
|
|
23
|
+
-c, --check Check if files are sorted
|
|
24
|
+
-q, --quiet Don't output success messages
|
|
25
|
+
-h, --help Display this help
|
|
26
|
+
-i, --ignore An array of glob patterns to ignore
|
|
27
|
+
--sort-scripts Sort scripts and betterScripts fields
|
|
28
|
+
-v, --version Display the package version
|
|
29
|
+
--stdin Read package.json from stdin
|
|
30
|
+
`,
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseCliArguments() {
|
|
35
|
+
const { values: options, positionals: patterns } = parseArgs({
|
|
36
|
+
options: {
|
|
37
|
+
check: { type: 'boolean', short: 'c', default: false },
|
|
38
|
+
quiet: { type: 'boolean', short: 'q', default: false },
|
|
39
|
+
stdin: { type: 'boolean', default: false },
|
|
40
|
+
ignore: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
short: 'i',
|
|
43
|
+
multiple: true,
|
|
44
|
+
default: ['node_modules/**'],
|
|
45
|
+
},
|
|
46
|
+
'sort-scripts': { type: 'boolean', default: false },
|
|
47
|
+
version: { type: 'boolean', short: 'v', default: false },
|
|
48
|
+
help: { type: 'boolean', short: 'h', default: false },
|
|
49
|
+
},
|
|
50
|
+
allowPositionals: true,
|
|
51
|
+
strict: true,
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (patterns.length === 0) {
|
|
55
|
+
patterns[0] = 'package.json'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return { options, patterns }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function sortPackageJsonFile(file, reporter, isCheck, shouldSortScripts) {
|
|
62
|
+
const original = await fs.readFile(file, 'utf8')
|
|
63
|
+
const sorted = sortPackageJson(original, { sortScripts: shouldSortScripts })
|
|
64
|
+
if (sorted === original) {
|
|
65
|
+
return reporter.reportNotChanged(file)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!isCheck) {
|
|
69
|
+
await fs.writeFile(file, sorted)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
reporter.reportChanged(file)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function sortPackageJsonFiles(patterns, { ignore, ...options }) {
|
|
76
|
+
const files = (await glob(patterns, { ignore })).toSorted()
|
|
77
|
+
|
|
78
|
+
const reporter = new Reporter(options)
|
|
79
|
+
const { isCheck, shouldSortScripts } = options
|
|
80
|
+
|
|
81
|
+
for (const file of files) {
|
|
82
|
+
reporter.reportFound(file)
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
await sortPackageJsonFile(file, reporter, isCheck, shouldSortScripts)
|
|
86
|
+
} catch (error) {
|
|
87
|
+
reporter.reportFailed(file, error)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
reporter.printSummary()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function sortPackageJsonFromStdin(shouldSortScripts) {
|
|
95
|
+
process.stdout.write(
|
|
96
|
+
sortPackageJson(await streamConsumers.text(process.stdin), {
|
|
97
|
+
sortScripts: shouldSortScripts,
|
|
98
|
+
}),
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function run() {
|
|
103
|
+
let options, patterns
|
|
104
|
+
try {
|
|
105
|
+
;({ options, patterns } = parseCliArguments())
|
|
106
|
+
} catch (error) {
|
|
107
|
+
process.exitCode = 2
|
|
108
|
+
console.error(error.message)
|
|
109
|
+
if (
|
|
110
|
+
error.code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION' ||
|
|
111
|
+
error.code === 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
|
|
112
|
+
) {
|
|
113
|
+
console.error(`Try 'sort-package-json --help' for more information.`)
|
|
114
|
+
}
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (options.help) {
|
|
119
|
+
return showHelpInformation()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (options.version) {
|
|
123
|
+
return showVersion()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (options.stdin) {
|
|
127
|
+
return sortPackageJsonFromStdin(options['sort-scripts'])
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
await sortPackageJsonFiles(patterns, {
|
|
131
|
+
ignore: options.ignore,
|
|
132
|
+
isCheck: options.check,
|
|
133
|
+
shouldSortScripts: options['sort-scripts'],
|
|
134
|
+
shouldBeQuiet: options.quiet,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
await run()
|