Skip to content

Commit 156b3d7

Browse files
committed
Merge branch 'main' into sort-exports
# Conflicts: # index.js
2 parents 681a492 + b3be166 commit 156b3d7

14 files changed

Lines changed: 7175 additions & 8663 deletions

File tree

.github/workflows/pr.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ jobs:
1414
node_version:
1515
- '22'
1616
- '20'
17-
- '18'
18-
- '16'
19-
- '14'
20-
- '12'
2117
name: Node.js ${{ matrix.node_version }} on ${{ matrix.os }}
2218
runs-on: ${{ matrix.os }}
2319
steps:

.gitignore

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

.husky/pre-commit

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
3-
41
npm t && npx lint-staged

cli.js

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env node
2-
import { globSync } from 'tinyglobby'
32
import fs from 'node:fs'
4-
import getStdin from 'get-stdin'
3+
import streamConsumers from 'node:stream/consumers'
4+
import { parseArgs } from 'node:util'
5+
import { globSync } from 'tinyglobby'
56
import sortPackageJson from './index.js'
67
import Reporter from './reporter.js'
78

@@ -30,6 +31,32 @@ If file/glob is omitted, './package.json' file will be processed.
3031
)
3132
}
3233

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+
version: { type: 'boolean', short: 'v', default: false },
47+
help: { type: 'boolean', short: 'h', default: false },
48+
},
49+
allowPositionals: true,
50+
strict: true,
51+
})
52+
53+
if (patterns.length === 0) {
54+
patterns[0] = 'package.json'
55+
}
56+
57+
return { options, patterns }
58+
}
59+
3360
function sortPackageJsonFile(file, reporter, isCheck) {
3461
const original = fs.readFileSync(file, 'utf8')
3562
const sorted = sortPackageJson(original)
@@ -46,6 +73,7 @@ function sortPackageJsonFile(file, reporter, isCheck) {
4673

4774
function sortPackageJsonFiles(patterns, { ignore, ...options }) {
4875
const files = globSync(patterns, { ignore })
76+
4977
const reporter = new Reporter(files, options)
5078
const { isCheck } = options
5179

@@ -60,65 +88,44 @@ function sortPackageJsonFiles(patterns, { ignore, ...options }) {
6088
}
6189

6290
async function sortPackageJsonFromStdin() {
63-
process.stdout.write(sortPackageJson(await getStdin()))
91+
process.stdout.write(
92+
sortPackageJson(await streamConsumers.text(process.stdin)),
93+
)
6494
}
6595

6696
function run() {
67-
const cliArguments = process.argv
68-
.slice(2)
69-
.map((arg) => arg.split('='))
70-
.flat()
71-
72-
if (
73-
cliArguments.some((argument) => argument === '--help' || argument === '-h')
74-
) {
97+
let options, patterns
98+
try {
99+
;({ options, patterns } = parseCliArguments())
100+
} catch (error) {
101+
process.exitCode = 2
102+
console.error(error.message)
103+
if (
104+
error.code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION' ||
105+
error.code === 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
106+
) {
107+
console.error(`Try 'sort-package-json --help' for more information.`)
108+
}
109+
return
110+
}
111+
112+
if (options.help) {
75113
return showHelpInformation()
76114
}
77115

78-
if (
79-
cliArguments.some(
80-
(argument) => argument === '--version' || argument === '-v',
81-
)
82-
) {
116+
if (options.version) {
83117
return showVersion()
84118
}
85119

86-
if (cliArguments.some((argument) => argument === '--stdin')) {
120+
if (options.stdin) {
87121
return sortPackageJsonFromStdin()
88122
}
89123

90-
const patterns = []
91-
const ignore = []
92-
let isCheck = false
93-
let shouldBeQuiet = false
94-
95-
let lastArg
96-
for (const argument of cliArguments) {
97-
if (lastArg === '--ignore' || lastArg === '-i') {
98-
ignore.push(argument)
99-
lastArg = undefined
100-
continue
101-
}
102-
if (argument === '--check' || argument === '-c') {
103-
isCheck = true
104-
} else if (argument === '--quiet' || argument === '-q') {
105-
shouldBeQuiet = true
106-
} else if (argument === '--ignore' || argument === '-i') {
107-
lastArg = argument
108-
} else {
109-
patterns.push(argument)
110-
}
111-
}
112-
113-
if (!patterns.length) {
114-
patterns[0] = 'package.json'
115-
}
116-
117-
if (!ignore.length) {
118-
ignore[0] = 'node_modules'
119-
}
120-
121-
sortPackageJsonFiles(patterns, { ignore, isCheck, shouldBeQuiet })
124+
sortPackageJsonFiles(patterns, {
125+
ignore: options.ignore,
126+
isCheck: options.check,
127+
shouldBeQuiet: options.quiet,
128+
})
122129
}
123130

124131
run()

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default [
1313
languageOptions: {
1414
globals: { ...globals.builtin, ...globals.node },
1515
},
16+
settings: { node: { version: '20' } },
1617
},
1718
{ ignores: ['index.cjs'] },
1819
]

git-hooks-list_shim.cjs

Lines changed: 0 additions & 2 deletions
This file was deleted.

index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import gitHooks from 'git-hooks-list'
55
import isPlainObject from 'is-plain-obj'
66
import semver from 'semver'
77

8-
const hasOwn =
9-
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax -- Safe
10-
Object.hasOwn ||
11-
// TODO: Remove this when we drop supported for Node.js v20
12-
((object, property) => Object.prototype.hasOwnProperty.call(object, property))
138
const pipe =
149
(fns) =>
1510
(x, ...args) =>
@@ -38,8 +33,9 @@ const sortObjectBy = (comparator, deep) => {
3833
return over
3934
}
4035
const objectGroupBy =
41-
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax -- will enable later
36+
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax -- Safe
4237
Object.groupBy ||
38+
// Remove this when we drop support for Node.js 20
4339
((array, callback) => {
4440
const result = Object.create(null)
4541
for (const value of array) {
@@ -66,7 +62,7 @@ const sortDirectories = sortObjectBy([
6662
const overProperty =
6763
(property, over) =>
6864
(object, ...args) =>
69-
hasOwn(object, property)
65+
Object.hasOwn(object, property)
7066
? { ...object, [property]: over(object[property], ...args) }
7167
: object
7268
const sortGitHooks = sortObjectBy(gitHooks)
@@ -233,8 +229,8 @@ const defaultNpmScripts = new Set([
233229

234230
const hasDevDependency = (dependency, packageJson) => {
235231
return (
236-
hasOwn(packageJson, 'devDependencies') &&
237-
hasOwn(packageJson.devDependencies, dependency)
232+
Object.hasOwn(packageJson, 'devDependencies') &&
233+
Object.hasOwn(packageJson.devDependencies, dependency)
238234
)
239235
}
240236

0 commit comments

Comments
 (0)