@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/package.json
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lntvow/sort-package-json",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Sort an Object or package.json based on the well-known package.json keys",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"keys",
|
|
7
|
+
"object",
|
|
8
|
+
"sort"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/keithamus/sort-package-json#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/keithamus/sort-package-json/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+ssh://git@github.com/keithamus/sort-package-json.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Keith Cirkel <npm@keithcirkel.co.uk> (http://keithcirkel.co.uk/)",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./index.d.ts",
|
|
25
|
+
"default": "./index.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./index.d.ts",
|
|
29
|
+
"default": "./index.cjs"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"types": "index.d.ts",
|
|
34
|
+
"bin": "cli.js",
|
|
35
|
+
"files": [
|
|
36
|
+
"index.js",
|
|
37
|
+
"index.d.ts",
|
|
38
|
+
"cli.js",
|
|
39
|
+
"reporter.js",
|
|
40
|
+
"index.cjs"
|
|
41
|
+
],
|
|
42
|
+
"commitlint": {
|
|
43
|
+
"extends": [
|
|
44
|
+
"@commitlint/config-conventional"
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"lint-staged": {
|
|
48
|
+
"package.json": [
|
|
49
|
+
"node cli.js"
|
|
50
|
+
],
|
|
51
|
+
"*.js": [
|
|
52
|
+
"eslint --fix"
|
|
53
|
+
],
|
|
54
|
+
"**/*": [
|
|
55
|
+
"prettier --write --ignore-unknown"
|
|
56
|
+
]
|
|
57
|
+
},
|
|
58
|
+
"prettier": {
|
|
59
|
+
"semi": false,
|
|
60
|
+
"singleQuote": true
|
|
61
|
+
},
|
|
62
|
+
"ava": {
|
|
63
|
+
"verbose": true
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"detect-indent": "^7.0.2",
|
|
67
|
+
"detect-newline": "^4.0.1",
|
|
68
|
+
"git-hooks-list": "^4.1.1",
|
|
69
|
+
"is-plain-obj": "^4.1.0",
|
|
70
|
+
"semver": "^7.7.3",
|
|
71
|
+
"sort-object-keys": "^2.0.1",
|
|
72
|
+
"tinyglobby": "^0.2.15"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@commitlint/cli": "^20.1.0",
|
|
76
|
+
"@commitlint/config-conventional": "^20.0.0",
|
|
77
|
+
"@eslint/js": "^9.39.1",
|
|
78
|
+
"ava": "^6.4.1",
|
|
79
|
+
"bumpp": "^11.0.1",
|
|
80
|
+
"c8": "^10.1.3",
|
|
81
|
+
"dot-prop": "^10.1.0",
|
|
82
|
+
"esbuild": "^0.27.0",
|
|
83
|
+
"eslint": "^9.39.1",
|
|
84
|
+
"eslint-config-prettier": "^10.1.8",
|
|
85
|
+
"eslint-plugin-n": "^17.23.1",
|
|
86
|
+
"eslint-plugin-promise": "^7.2.1",
|
|
87
|
+
"globals": "^16.5.0",
|
|
88
|
+
"husky": "^9.1.7",
|
|
89
|
+
"lint-staged": "^16.2.7",
|
|
90
|
+
"prettier": "^3.7.1",
|
|
91
|
+
"semantic-release": "^25.0.2",
|
|
92
|
+
"tempy": "^3.1.0",
|
|
93
|
+
"tstyche": "^5.0.1"
|
|
94
|
+
},
|
|
95
|
+
"engines": {
|
|
96
|
+
"node": ">=20"
|
|
97
|
+
},
|
|
98
|
+
"scripts": {
|
|
99
|
+
"build": "esbuild index.js --bundle --platform=node --outfile=index.cjs --external:semver --external:git-hooks-list",
|
|
100
|
+
"fix": "eslint . --fix && prettier . --write && node cli.js \"package.json\"",
|
|
101
|
+
"lint": "eslint . && prettier . \"!**/*.js\" --check && node cli.js \"package.json\" --check",
|
|
102
|
+
"semantic-release": "semantic-release",
|
|
103
|
+
"test": "ava && tstyche",
|
|
104
|
+
"test-coverage": "c8 npm run test",
|
|
105
|
+
"update-snapshots": "ava --update-snapshots",
|
|
106
|
+
"release": "git add . && bumpp package.json --all --no-tag --no-print-commits"
|
|
107
|
+
}
|
|
108
|
+
}
|
package/reporter.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const getFilesCountText = (count) => (count === 1 ? '1 file' : `${count} files`)
|
|
2
|
+
|
|
3
|
+
class Reporter {
|
|
4
|
+
#hasPrinted = false
|
|
5
|
+
#options
|
|
6
|
+
#status
|
|
7
|
+
#logger
|
|
8
|
+
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.#options = options
|
|
11
|
+
this.#status = {
|
|
12
|
+
matchedFilesCount: 0,
|
|
13
|
+
failedFilesCount: 0,
|
|
14
|
+
wellSortedFilesCount: 0,
|
|
15
|
+
changedFilesCount: 0,
|
|
16
|
+
changedFiles: [],
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this.#logger = options.shouldBeQuiet
|
|
20
|
+
? { log() {}, error() {} }
|
|
21
|
+
: {
|
|
22
|
+
log: (...args) => {
|
|
23
|
+
this.#hasPrinted = true
|
|
24
|
+
console.log(...args)
|
|
25
|
+
},
|
|
26
|
+
error: (...args) => {
|
|
27
|
+
this.#hasPrinted = true
|
|
28
|
+
console.error(...args)
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
reportFound(/* file */) {
|
|
34
|
+
this.#status.matchedFilesCount++
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// The file is well-sorted
|
|
38
|
+
reportNotChanged(/* file */) {
|
|
39
|
+
this.#status.wellSortedFilesCount++
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
reportChanged(file) {
|
|
43
|
+
this.#status.changedFilesCount++
|
|
44
|
+
if (this.#options.isCheck) {
|
|
45
|
+
this.#status.changedFiles.push(file)
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.#logger.log(`${file} is sorted!`)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
reportFailed(file, error) {
|
|
53
|
+
this.#status.failedFilesCount++
|
|
54
|
+
|
|
55
|
+
console.error('Error on: ' + file)
|
|
56
|
+
this.#logger.error(error.message)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
printSummary() {
|
|
60
|
+
const {
|
|
61
|
+
matchedFilesCount,
|
|
62
|
+
failedFilesCount,
|
|
63
|
+
changedFilesCount,
|
|
64
|
+
wellSortedFilesCount,
|
|
65
|
+
changedFiles,
|
|
66
|
+
} = this.#status
|
|
67
|
+
|
|
68
|
+
if (matchedFilesCount === 0) {
|
|
69
|
+
console.error('No matching files.')
|
|
70
|
+
process.exitCode = 2
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { isCheck, shouldBeQuiet } = this.#options
|
|
75
|
+
|
|
76
|
+
if (isCheck && changedFilesCount) {
|
|
77
|
+
process.exitCode = 1
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (failedFilesCount) {
|
|
81
|
+
process.exitCode = 2
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (shouldBeQuiet) {
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const { log } = this.#logger
|
|
89
|
+
|
|
90
|
+
if (isCheck && changedFiles.length > 0) {
|
|
91
|
+
for (const file of changedFiles.toSorted((a, b) =>
|
|
92
|
+
a.localeCompare(b, 'en'),
|
|
93
|
+
)) {
|
|
94
|
+
log(file)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Print an empty line.
|
|
99
|
+
if (this.#hasPrinted) {
|
|
100
|
+
log()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Matched files
|
|
104
|
+
log('Found %s.', getFilesCountText(matchedFilesCount))
|
|
105
|
+
|
|
106
|
+
// Failed files
|
|
107
|
+
if (failedFilesCount) {
|
|
108
|
+
log(
|
|
109
|
+
'%s could not be %s.',
|
|
110
|
+
getFilesCountText(failedFilesCount),
|
|
111
|
+
isCheck ? 'checked' : 'sorted',
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Changed files
|
|
116
|
+
if (changedFilesCount) {
|
|
117
|
+
if (isCheck) {
|
|
118
|
+
log(
|
|
119
|
+
'%s %s not sorted.',
|
|
120
|
+
getFilesCountText(changedFilesCount),
|
|
121
|
+
changedFilesCount === 1 ? 'was' : 'were',
|
|
122
|
+
)
|
|
123
|
+
} else {
|
|
124
|
+
log('%s successfully sorted.', getFilesCountText(changedFilesCount))
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Well-sorted files
|
|
129
|
+
if (wellSortedFilesCount) {
|
|
130
|
+
log(
|
|
131
|
+
'%s %s already sorted.',
|
|
132
|
+
getFilesCountText(wellSortedFilesCount),
|
|
133
|
+
wellSortedFilesCount === 1 ? 'was' : 'were',
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default Reporter
|