forked from keithamus/sort-package-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·57 lines (47 loc) · 1.33 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
#!/usr/bin/env node
const fs = require('fs')
const globby = require('globby')
const sortPackageJson = require('.')
const isCheckFlag = (argument) => argument === '--check' || argument === '-c'
const cliArguments = process.argv.slice(2)
const isCheck = cliArguments.some(isCheckFlag)
const patterns = cliArguments.filter((argument) => !isCheckFlag(argument))
if (!patterns.length) {
patterns[0] = 'package.json'
}
const files = globby.sync(patterns)
if (files.length === 0) {
console.log('No matching files.')
process.exit(1)
}
let notSortedFiles = 0
files.forEach((file) => {
const packageJson = fs.readFileSync(file, 'utf8')
const sorted = sortPackageJson(packageJson)
if (sorted !== packageJson) {
if (isCheck) {
notSortedFiles++
console.log(file)
} else {
fs.writeFileSync(file, sorted, 'utf8')
console.log(`${file} is sorted!`)
}
}
})
if (isCheck) {
console.log()
if (notSortedFiles) {
console.log(
notSortedFiles === 1
? `${notSortedFiles} of ${files.length} matched file is not sorted.`
: `${notSortedFiles} of ${files.length} matched files are not sorted.`,
)
} else {
console.log(
files.length === 1
? `${files.length} matched file is sorted.`
: `${files.length} matched files are sorted.`,
)
}
process.exit(notSortedFiles)
}