Skip to content

Commit 4b70ee6

Browse files
committed
Merge branch 'main' into cli_improvments
# Conflicts: # cli.js # tests/snapshots/cli.js.snap
2 parents baaa560 + ec74b4c commit 4b70ee6

32 files changed

+3451
-2595
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/pr.yml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,29 @@ jobs:
88
fail-fast: false
99
matrix:
1010
os:
11-
- "ubuntu-latest"
12-
# - "macos-latest"
13-
# - "windows-latest"
11+
- 'ubuntu-latest'
12+
# - 'macos-latest'
13+
# - 'windows-latest'
1414
node_version:
15-
- "18"
16-
- "16"
17-
- "14"
18-
- "12"
15+
- '22'
16+
- '20'
1917
name: Node.js ${{ matrix.node_version }} on ${{ matrix.os }}
2018
runs-on: ${{ matrix.os }}
2119
steps:
2220
- name: Checkout
23-
uses: actions/checkout@v3
21+
uses: actions/checkout@v4
2422

2523
- name: Setup Node.js
26-
uses: actions/setup-node@v3
24+
uses: actions/setup-node@v4
2725
with:
2826
node-version: ${{ matrix.node_version }}
2927

30-
# https://github.com/bahmutov/npm-install/issues/103#issuecomment-931226602
31-
- name: Update NPM
32-
run: npm install --global npm@8
33-
3428
- name: Install Dependencies
3529
run: npm ci
3630

37-
- name: Run ESLint
38-
if: matrix.node_version == '18'
31+
- name: Lint
32+
if: matrix.node_version == '22'
3933
run: npm run lint
4034

41-
- name: Check package.json
42-
run: npm run sort-package-json
43-
4435
- name: Run Tests
4536
run: npm run test-coverage

.github/workflows/release.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Checkout
12-
uses: actions/checkout@v3
12+
uses: actions/checkout@v4
1313

1414
- name: Setup Node.js
15-
uses: actions/setup-node@v3
16-
17-
# https://github.com/bahmutov/npm-install/issues/103#issuecomment-931226602
18-
- name: Update NPM
19-
run: npm install --global npm
15+
uses: actions/setup-node@v4
2016

2117
- name: Install dependencies
22-
run: npm ci
18+
run: npm i
2319

2420
- name: Release
2521
env:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
/.nyc_output
3-
/coverage
3+
/cjs
4+
/coverage

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/snapshots/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

Lines changed: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ CLI also supports multi file paths or [`glob`](https://github.com/sindresorhus/g
5959
$ sort-package-json "my-package/package.json" "other-package/package.json"
6060

6161
$ sort-package-json "package.json" "packages/*/package.json"
62+
63+
$ sort-package-json "package.json" "packages/*/package.json" --ignore "packages/one-package"
6264
```
6365

6466
#### `--check` flag
@@ -89,6 +91,20 @@ $ sort-package-json "**/package.json" --check --quiet
8991
$ sort-package-json "**/package.json" --quiet
9092
```
9193

94+
#### `--stdin` flag
95+
96+
To read from `stdin` and output the result to `stdout` use the `--stdin` flag.
97+
98+
```bash
99+
$ cat package.json | sort-package-json --stdin
100+
```
101+
102+
This can, for instance, be used to generate a diff before changing `package.json`.
103+
104+
```bash
105+
$ ( PKG="./package.json" ; cat "${PKG?}" | sort-package-json --stdin | diff "${PKG?}" - ; )
106+
```
107+
92108
## API
93109

94110
### Install
@@ -157,7 +173,7 @@ If an array, sort keys in ordering of `options.sortOrder`.
157173
158174
```js
159175
const sorted = sortPackageJson(packageJsonObject, {
160-
sortOrder: ['version']
176+
sortOrder: ['version'],
161177
})
162178

163179
console.log(Object.keys(sorted))
@@ -173,7 +189,7 @@ If a function, sort fields by [Array#sort(options.sortOrder)](https://developer.
173189
const sorted = sortPackageJson(packageJsonObject, {
174190
sortOrder(left, right) {
175191
return left.localeCompare(right)
176-
}
192+
},
177193
})
178194

179195
console.log(Object.keys(sorted))
@@ -202,6 +218,8 @@ console.log(Object.keys(sorted))
202218
- [Mocha](https://mochajs.org/)
203219
- [node-pre-gyp](https://github.com/mapbox/node-pre-gyp)
204220
- [npm-package-json-lint](https://npmpackagejsonlint.org/)
221+
- [oclif](https://oclif.io/)
222+
- [pnpm](https://pnpm.io/)
205223
- [Prettier](https://prettier.io/)
206224
- [remark](https://remark.js.org/)
207225
- [semantic-release](https://github.com/semantic-release/semantic-release)
@@ -258,6 +276,152 @@ The lack of configuration here is a feature, not a bug. The intent of this tool
258276
259277
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.
260278
279+
### What is the order this package defaults to?
280+
281+
The default order is exported as a `sortOrder` object.
282+
283+
<details>
284+
<summary>Properties mentioned in the npm docs</summary>
285+
286+
1. `name`
287+
1. `version`
288+
1. `private`
289+
1. `description`
290+
1. `keywords`
291+
1. `homepage`
292+
1. `bugs`
293+
1. `repository`
294+
1. `funding`
295+
1. `license`
296+
1. `author`
297+
1. `contributors`
298+
1. `main`
299+
1. `browser`
300+
1. `bin`
301+
1. `man`
302+
1. `directories`
303+
1. `files`
304+
1. `workspaces`
305+
1. `scripts`
306+
1. `config`
307+
1. `dependencies`
308+
1. `engines`
309+
1. `os`
310+
1. `cpu`
311+
312+
</details>
313+
314+
<details>
315+
<summary>Full list of recognized properties</summary>
316+
317+
1. `$schema`
318+
1. `name`
319+
1. `displayName`
320+
1. `version`
321+
1. `private`
322+
1. `description`
323+
1. `categories`
324+
1. `keywords`
325+
1. `homepage`
326+
1. `bugs`
327+
1. `repository`
328+
1. `funding`
329+
1. `license`
330+
1. `qna`
331+
1. `author`
332+
1. `maintainers`
333+
1. `contributors`
334+
1. `publisher`
335+
1. `sideEffects`
336+
1. `type`
337+
1. `imports`
338+
1. `exports`
339+
1. `main`
340+
1. `svelte`
341+
1. `umd:main`
342+
1. `jsdelivr`
343+
1. `unpkg`
344+
1. `module`
345+
1. `source`
346+
1. `jsnext:main`
347+
1. `browser`
348+
1. `react-native`
349+
1. `types`
350+
1. `typesVersions`
351+
1. `typings`
352+
1. `style`
353+
1. `example`
354+
1. `examplestyle`
355+
1. `assets`
356+
1. `bin`
357+
1. `man`
358+
1. `directories`
359+
1. `files`
360+
1. `workspaces`
361+
1. `binary`
362+
1. `scripts`
363+
1. `betterScripts`
364+
1. `contributes`
365+
1. `activationEvents`
366+
1. `husky`
367+
1. `simple-git-hooks`
368+
1. `pre-commit`
369+
1. `commitlint`
370+
1. `lint-staged`
371+
1. `nano-staged`
372+
1. `config`
373+
1. `nodemonConfig`
374+
1. `browserify`
375+
1. `babel`
376+
1. `browserslist`
377+
1. `xo`
378+
1. `prettier`
379+
1. `eslintConfig`
380+
1. `eslintIgnore`
381+
1. `npmpkgjsonlint`
382+
1. `npmPackageJsonLintConfig`
383+
1. `npmpackagejsonlint`
384+
1. `release`
385+
1. `remarkConfig`
386+
1. `stylelint`
387+
1. `ava`
388+
1. `jest`
389+
1. `jest-junit`
390+
1. `jest-stare`
391+
1. `mocha`
392+
1. `nyc`
393+
1. `c8`
394+
1. `tap`
395+
1. `resolutions`
396+
1. `dependencies`
397+
1. `devDependencies`
398+
1. `dependenciesMeta`
399+
1. `peerDependencies`
400+
1. `peerDependenciesMeta`
401+
1. `optionalDependencies`
402+
1. `bundledDependencies`
403+
1. `bundleDependencies`
404+
1. `extensionPack`
405+
1. `extensionDependencies`
406+
1. `flat`
407+
1. `packageManager`
408+
1. `engines`
409+
1. `engineStrict`
410+
1. `volta`
411+
1. `languageName`
412+
1. `os`
413+
1. `cpu`
414+
1. `preferGlobal`
415+
1. `publishConfig`
416+
1. `icon`
417+
1. `badges`
418+
1. `galleryBanner`
419+
1. `preview`
420+
1. `markdown`
421+
1. `pnpm`
422+
423+
</details>
424+
261425
### What?! Why would you want to do this?!
262426
263427
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?

cli.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env node
2-
import { globbySync } from 'globby'
2+
import { globSync } from 'tinyglobby'
33
import fs from 'node:fs'
4+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
45
import { parseArgs } from 'node:util'
6+
import getStdin from 'get-stdin'
57
import sortPackageJson from './index.js'
68
import Reporter from './reporter.js'
79

@@ -23,7 +25,9 @@ If file/glob is omitted, './package.json' file will be processed.
2325
-c, --check Check if files are sorted
2426
-q, --quiet Don't output success messages
2527
-h, --help Display this help
28+
-i, --ignore An array of glob patterns to ignore
2629
-v, --version Display the package version
30+
--stdin Read package.json from stdin
2731
`,
2832
)
2933
}
@@ -33,14 +37,21 @@ function parseCliArguments() {
3337
options: {
3438
check: { type: 'boolean', short: 'c', default: false },
3539
quiet: { type: 'boolean', short: 'q', default: false },
40+
stdin: { type: 'boolean', default: false },
41+
ignore: {
42+
type: 'string',
43+
short: 'i',
44+
multiple: true,
45+
default: ['node_modules/**'],
46+
},
3647
version: { type: 'boolean', short: 'v', default: false },
3748
help: { type: 'boolean', short: 'h', default: false },
3849
},
3950
allowPositionals: true,
4051
strict: true,
4152
})
4253

43-
if (!patterns.length) {
54+
if (patterns.length === 0) {
4455
patterns[0] = 'package.json'
4556
}
4657

@@ -61,8 +72,9 @@ function sortPackageJsonFile(file, reporter, isCheck) {
6172
reporter.reportChanged(file)
6273
}
6374

64-
function sortPackageJsonFiles(patterns, options) {
65-
const files = globbySync(patterns)
75+
function sortPackageJsonFiles(patterns, { ignore, ...options }) {
76+
const files = globSync(patterns, { ignore })
77+
6678
const reporter = new Reporter(files, options)
6779
const { isCheck } = options
6880

@@ -73,10 +85,13 @@ function sortPackageJsonFiles(patterns, options) {
7385
reporter.reportFailed(file, error)
7486
}
7587
}
76-
7788
reporter.printSummary()
7889
}
7990

91+
async function sortPackageJsonFromStdin() {
92+
process.stdout.write(sortPackageJson(await getStdin()))
93+
}
94+
8095
function run() {
8196
let options, patterns
8297
try {
@@ -101,7 +116,12 @@ function run() {
101116
return showVersion()
102117
}
103118

119+
if (options.stdin) {
120+
return sortPackageJsonFromStdin()
121+
}
122+
104123
sortPackageJsonFiles(patterns, {
124+
ignore: options.ignore,
105125
isCheck: options.check,
106126
shouldBeQuiet: options.quiet,
107127
})

0 commit comments

Comments
 (0)