json-sort-cli 2.1.1 → 2.1.3
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/CHANGELOG.md +6 -0
- package/cli.js +21 -17
- package/package.json +4 -5
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [2.1.3](https://github.com/codsen/codsen/compare/json-sort-cli@2.1.2...json-sort-cli@2.1.3) (2022-10-13)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- update dependencies ([a3834e7](https://github.com/codsen/codsen/commit/a3834e75d77cbdb238cfa8ec392829dfa95cb962))
|
|
11
|
+
|
|
6
12
|
# 2.1.0 (2022-08-12)
|
|
7
13
|
|
|
8
14
|
### Features
|
package/cli.js
CHANGED
|
@@ -12,11 +12,10 @@ import pReduce from "p-reduce";
|
|
|
12
12
|
import pFilter from "p-filter";
|
|
13
13
|
import { globby } from "globby";
|
|
14
14
|
import { createRequire } from "module";
|
|
15
|
-
import sortObject from "sorted-object";
|
|
16
15
|
import isObj from "lodash.isplainobject";
|
|
17
16
|
import updateNotifier from "update-notifier";
|
|
18
17
|
import { traverse } from "ast-monkey-traverse";
|
|
19
|
-
import sortPackageJson from "sort-package-json";
|
|
18
|
+
import sortPackageJson, { sortOrder } from "sort-package-json";
|
|
20
19
|
|
|
21
20
|
const require1 = createRequire(import.meta.url);
|
|
22
21
|
const pkg = require1("./package.json");
|
|
@@ -25,28 +24,34 @@ function isStr(something) {
|
|
|
25
24
|
return typeof something === "string";
|
|
26
25
|
}
|
|
27
26
|
function format(obj) {
|
|
28
|
-
/* istanbul ignore next */
|
|
29
27
|
if (typeof obj !== "object") {
|
|
30
28
|
return obj;
|
|
31
29
|
}
|
|
32
|
-
let
|
|
30
|
+
let newSortOrder = sortOrder
|
|
33
31
|
// 1. delete tap and lect fields
|
|
34
32
|
.filter((field) => !["lect", "tap"].includes(field));
|
|
35
33
|
|
|
36
34
|
// 2. then, insert both after resolutions, first tap then lect
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let idxOfResolutions = sortOrder.indexOf("resolutions");
|
|
35
|
+
let idxOfResolutions = newSortOrder.indexOf("resolutions");
|
|
40
36
|
// console.log(idxOfResolutions);
|
|
41
37
|
// => 63
|
|
42
38
|
|
|
43
|
-
|
|
39
|
+
newSortOrder.splice(idxOfResolutions, 0, "tap", "lect");
|
|
44
40
|
|
|
45
41
|
// use custom array for sorting order:
|
|
46
42
|
return sortPackageJson(obj, {
|
|
47
|
-
sortOrder,
|
|
43
|
+
sortOrder: newSortOrder,
|
|
48
44
|
});
|
|
49
45
|
}
|
|
46
|
+
function sortObj(obj) {
|
|
47
|
+
let res = {};
|
|
48
|
+
Object.keys(obj)
|
|
49
|
+
.sort()
|
|
50
|
+
.forEach((key) => {
|
|
51
|
+
res[key] = obj[key];
|
|
52
|
+
});
|
|
53
|
+
return res;
|
|
54
|
+
}
|
|
50
55
|
|
|
51
56
|
const prefix = "✨ json-sort-cli: ";
|
|
52
57
|
const { log } = console;
|
|
@@ -174,7 +179,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
|
|
|
174
179
|
let result;
|
|
175
180
|
|
|
176
181
|
if (isObj(parsedJson)) {
|
|
177
|
-
result =
|
|
182
|
+
result = sortObj(parsedJson);
|
|
178
183
|
} else if (
|
|
179
184
|
cli.flags.arrays &&
|
|
180
185
|
Array.isArray(parsedJson) &&
|
|
@@ -210,7 +215,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
|
|
|
210
215
|
traverse(obj, (key, val) => {
|
|
211
216
|
let current = val !== undefined ? val : key;
|
|
212
217
|
if (isObj(current)) {
|
|
213
|
-
return
|
|
218
|
+
return sortObj(current);
|
|
214
219
|
}
|
|
215
220
|
if (
|
|
216
221
|
cli.flags.arrays &&
|
|
@@ -228,6 +233,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
|
|
|
228
233
|
);
|
|
229
234
|
return stringified.trimEnd() !== filesContent.trimEnd();
|
|
230
235
|
}
|
|
236
|
+
|
|
231
237
|
// ELSE,
|
|
232
238
|
return fs
|
|
233
239
|
.writeJson(
|
|
@@ -235,7 +241,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
|
|
|
235
241
|
traverse(obj, (key, val) => {
|
|
236
242
|
let current = val !== undefined ? val : key;
|
|
237
243
|
if (isObj(current)) {
|
|
238
|
-
return
|
|
244
|
+
return sortObj(current);
|
|
239
245
|
}
|
|
240
246
|
if (
|
|
241
247
|
cli.flags.arrays &&
|
|
@@ -267,7 +273,6 @@ function readSortAndWriteOverFile(oneOfPaths) {
|
|
|
267
273
|
});
|
|
268
274
|
})
|
|
269
275
|
.catch((err) => {
|
|
270
|
-
/* istanbul ignore next */
|
|
271
276
|
console.log(
|
|
272
277
|
`${oneOfPaths} - ${`\u001b[${31}m${`BAD`}\u001b[${39}m`} - ${err}`
|
|
273
278
|
);
|
|
@@ -289,6 +294,9 @@ if (cli.flags.version) {
|
|
|
289
294
|
// -----------------------------------------------------------------------------
|
|
290
295
|
|
|
291
296
|
const { input } = cli;
|
|
297
|
+
if (Array.isArray(input) && !input.length) {
|
|
298
|
+
input.push("**/*.json");
|
|
299
|
+
}
|
|
292
300
|
|
|
293
301
|
// Step #2. query the glob and follow the pipeline
|
|
294
302
|
// -----------------------------------------------------------------------------
|
|
@@ -313,7 +321,6 @@ globby(input, { dot: true })
|
|
|
313
321
|
(concattedTotal, singleDirOrFilePath) =>
|
|
314
322
|
concattedTotal.concat(
|
|
315
323
|
isDirectory(singleDirOrFilePath).then((bool) =>
|
|
316
|
-
/* istanbul ignore next */
|
|
317
324
|
bool
|
|
318
325
|
? globby(
|
|
319
326
|
cli.flags.nodemodules
|
|
@@ -380,7 +387,6 @@ globby(input, { dot: true })
|
|
|
380
387
|
return pFilter(paths, (currentPath) =>
|
|
381
388
|
readSortAndWriteOverFile(currentPath)
|
|
382
389
|
).then((received2) => {
|
|
383
|
-
/* istanbul ignore else */
|
|
384
390
|
if (received2.length && !cli.flags.silent) {
|
|
385
391
|
log(
|
|
386
392
|
`${chalk.grey(prefix)}${chalk.red(
|
|
@@ -415,7 +421,6 @@ globby(input, { dot: true })
|
|
|
415
421
|
}
|
|
416
422
|
)
|
|
417
423
|
.catch((err) => {
|
|
418
|
-
/* istanbul ignore next */
|
|
419
424
|
if (!cli.flags.silent) {
|
|
420
425
|
log(
|
|
421
426
|
`${chalk.grey(prefix)}${chalk.red(
|
|
@@ -449,7 +454,6 @@ globby(input, { dot: true })
|
|
|
449
454
|
}
|
|
450
455
|
})
|
|
451
456
|
.catch((err) => {
|
|
452
|
-
/* istanbul ignore next */
|
|
453
457
|
if (!cli.flags.silent) {
|
|
454
458
|
log(`${chalk.grey(prefix)}${chalk.red("Oops!")} ${err}`);
|
|
455
459
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-sort-cli",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
4
4
|
"description": "Command line app to deep sort JSON files, retains package.json special key order",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -60,16 +60,15 @@
|
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"ast-monkey-traverse": "^3.1.1",
|
|
63
|
-
"chalk": "^5.
|
|
63
|
+
"chalk": "^5.1.1",
|
|
64
64
|
"fs-extra": "^10.1.0",
|
|
65
65
|
"globby": "^13.1.2",
|
|
66
66
|
"is-d": "^1.0.0",
|
|
67
67
|
"lodash.isplainobject": "^4.0.6",
|
|
68
|
-
"meow": "^
|
|
68
|
+
"meow": "^11.0.0",
|
|
69
69
|
"p-filter": "^3.0.0",
|
|
70
70
|
"p-reduce": "^3.0.0",
|
|
71
|
-
"sort-package-json": "^
|
|
72
|
-
"sorted-object": "^2.0.1",
|
|
71
|
+
"sort-package-json": "^2.0.0",
|
|
73
72
|
"update-notifier": "^6.0.2"
|
|
74
73
|
},
|
|
75
74
|
"devDependencies": {
|