-
Notifications
You must be signed in to change notification settings - Fork 90
/
cli.js
executable file
·124 lines (103 loc) · 2.86 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
import { globSync } from 'tinyglobby'
import fs from 'node:fs'
import getStdin from 'get-stdin'
import sortPackageJson from './index.js'
import Reporter from './reporter.js'
function showVersion() {
const { name, version } = JSON.parse(
fs.readFileSync(new URL('package.json', import.meta.url)),
)
console.log(`${name} ${version}`)
}
function showHelpInformation() {
console.log(
`Usage: sort-package-json [options] [file/glob ...]
Sort package.json files.
If file/glob is omitted, './package.json' file will be processed.
-c, --check Check if files are sorted
-q, --quiet Don't output success messages
-h, --help Display this help
-i, --ignore An array of glob patterns to ignore
-v, --version Display the package version
--stdin Read package.json from stdin
`,
)
}
function sortPackageJsonFile(file, reporter, isCheck) {
const original = fs.readFileSync(file, 'utf8')
const sorted = sortPackageJson(original)
if (sorted === original) {
return reporter.reportNotChanged(file)
}
if (!isCheck) {
fs.writeFileSync(file, sorted)
}
reporter.reportChanged(file)
}
function sortPackageJsonFiles(patterns, { ignore, ...options }) {
const files = globSync(patterns, { ignore })
const reporter = new Reporter(files, options)
const { isCheck } = options
for (const file of files) {
try {
sortPackageJsonFile(file, reporter, isCheck)
} catch (error) {
reporter.reportFailed(file, error)
}
}
reporter.printSummary()
}
async function sortPackageJsonFromStdin() {
process.stdout.write(sortPackageJson(await getStdin()))
}
function run() {
const cliArguments = process.argv
.slice(2)
.map((arg) => arg.split('='))
.flat()
if (
cliArguments.some((argument) => argument === '--help' || argument === '-h')
) {
return showHelpInformation()
}
if (
cliArguments.some(
(argument) => argument === '--version' || argument === '-v',
)
) {
return showVersion()
}
if (cliArguments.some((argument) => argument === '--stdin')) {
return sortPackageJsonFromStdin()
}
const patterns = []
const ignore = []
let isCheck = false
let shouldBeQuiet = false
let lastArg
for (const argument of cliArguments) {
if (lastArg === '--ignore' || lastArg === '-i') {
ignore.push(argument)
lastArg = undefined
continue
}
if (argument === '--check' || argument === '-c') {
isCheck = true
} else if (argument === '--quiet' || argument === '-q') {
shouldBeQuiet = true
} else if (argument === '--ignore' || argument === '-i') {
lastArg = argument
} else {
patterns.push(argument)
}
}
if (!patterns.length) {
patterns[0] = 'package.json'
}
if (!ignore.length) {
ignore[0] = 'node_modules'
}
sortPackageJsonFiles(patterns, { ignore, isCheck, shouldBeQuiet })
}
run()