json-sort-cli 3.1.0 → 3.1.2

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/cli.js +36 -14
  3. package/package.json +5 -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
+ ## [3.1.1](https://github.com/codsen/codsen/compare/json-sort-cli@3.1.0...json-sort-cli@3.1.1) (2023-04-18)
7
+
8
+ ### Bug Fixes
9
+
10
+ - fix `-c` flag to correctly recognise different EOLs as per `-l` ([5af3ba1](https://github.com/codsen/codsen/commit/5af3ba16962c2dd001005780a3843ecf675389f4)), closes [#73](https://github.com/codsen/codsen/issues/73)
11
+
6
12
  # [3.1.0](https://github.com/codsen/codsen/compare/json-sort-cli@3.0.12...json-sort-cli@3.1.0) (2023-04-16)
7
13
 
8
14
  ### Features
package/cli.js CHANGED
@@ -86,56 +86,56 @@ const cli = meow(
86
86
  flags: {
87
87
  nodemodules: {
88
88
  type: "boolean",
89
- alias: "n",
89
+ shortFlag: "n",
90
90
  default: false,
91
91
  },
92
92
  tabs: {
93
93
  type: "boolean",
94
- alias: "t",
94
+ shortFlag: "t",
95
95
  default: false,
96
96
  },
97
97
  silent: {
98
98
  type: "boolean",
99
- alias: "s",
99
+ shortFlag: "s",
100
100
  default: false,
101
101
  },
102
102
  arrays: {
103
103
  type: "boolean",
104
- alias: "a",
104
+ shortFlag: "a",
105
105
  default: false,
106
106
  },
107
107
  pack: {
108
108
  type: "boolean",
109
- alias: "p",
109
+ shortFlag: "p",
110
110
  default: false,
111
111
  },
112
112
  dry: {
113
113
  type: "boolean",
114
- alias: "d",
114
+ shortFlag: "d",
115
115
  default: false,
116
116
  },
117
117
  ci: {
118
118
  type: "boolean",
119
- alias: "c",
119
+ shortFlag: "c",
120
120
  default: false,
121
121
  },
122
122
  help: {
123
123
  type: "boolean",
124
- alias: "h",
124
+ shortFlag: "h",
125
125
  default: false,
126
126
  },
127
127
  version: {
128
128
  type: "boolean",
129
- alias: "v",
129
+ shortFlag: "v",
130
130
  default: false,
131
131
  },
132
132
  indentationCount: {
133
133
  type: "number",
134
- alias: "i",
134
+ shortFlag: "i",
135
135
  },
136
136
  lineEnding: {
137
137
  type: "string",
138
- alias: "l",
138
+ shortFlag: "l",
139
139
  },
140
140
  },
141
141
  }
@@ -171,6 +171,8 @@ function readSortAndWriteOverFile(oneOfPaths) {
171
171
  return fs
172
172
  .readFile(oneOfPaths, "utf8")
173
173
  .then((filesContent) => {
174
+ let eolChar = resolveEolSetting(filesContent, cli.flags.lineEnding);
175
+
174
176
  let parsedJson;
175
177
  try {
176
178
  // try to parse JSON
@@ -200,7 +202,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
200
202
  spaces: cli.flags.tabs
201
203
  ? "\t".repeat(indentationCount)
202
204
  : indentationCount,
203
- EOL: resolveEolSetting(filesContent, cli.flags.lineEnding),
205
+ EOL: eolChar,
204
206
  }
205
207
  );
206
208
  } else {
@@ -219,6 +221,8 @@ function readSortAndWriteOverFile(oneOfPaths) {
219
221
  // we read it, now we return true if result differs after processing
220
222
 
221
223
  let stringified = JSON.stringify(
224
+ // The traversal below will mutate, not just traverse, -
225
+ // whatever you return from the callback below, gets written
222
226
  traverse(obj, (key, val) => {
223
227
  let current = val !== undefined ? val : key;
224
228
  if (isPlainObject(current)) {
@@ -230,7 +234,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
230
234
  current.length > 1 &&
231
235
  current.every(isStr)
232
236
  ) {
233
- // alphabetical sort
237
+ // alphabetical sort, this value gets written
234
238
  return current.sort((a, b) => a.localeCompare(b));
235
239
  }
236
240
  return current;
@@ -238,7 +242,25 @@ function readSortAndWriteOverFile(oneOfPaths) {
238
242
  null,
239
243
  cli.flags.tabs ? "\t".repeat(indentationCount) : indentationCount
240
244
  );
241
- return stringified.trimEnd() !== filesContent.trimEnd();
245
+
246
+ if (eolChar === "\r\n") {
247
+ // CRLF
248
+ stringified = stringified
249
+ .replaceAll(/(?<!\r)\n/g, "\r\n")
250
+ .replaceAll(/\r(?!\n)/g, "\n");
251
+ } else {
252
+ // LF or CR
253
+ stringified = stringified.replaceAll(/(?:\r?\n)|\r/g, eolChar);
254
+ }
255
+
256
+ return (
257
+ stringified
258
+ .replaceAll(
259
+ /\r\n/g,
260
+ resolveEolSetting(filesContent, cli.flags.lineEnding)
261
+ )
262
+ .trimEnd() !== filesContent.trimEnd()
263
+ );
242
264
  }
243
265
 
244
266
  // ELSE,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-sort-cli",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
4
4
  "description": "Command line app to deep sort JSON files, retains package.json special key order",
5
5
  "keywords": [
6
6
  "app",
@@ -67,19 +67,19 @@
67
67
  }
68
68
  },
69
69
  "dependencies": {
70
- "ast-monkey-traverse": "^4.0.9",
70
+ "ast-monkey-traverse": "^4.0.10",
71
71
  "chalk": "^5.2.0",
72
- "codsen-utils": "^1.4.0",
72
+ "codsen-utils": "^1.5.0",
73
73
  "fs-extra": "^11.1.1",
74
74
  "globby": "^13.1.4",
75
75
  "is-d": "^1.0.0",
76
- "meow": "^11.0.0",
76
+ "meow": "^12.0.1",
77
77
  "p-filter": "^3.0.0",
78
78
  "p-reduce": "^3.0.0",
79
79
  "sort-package-json": "^2.4.1",
80
80
  "update-notifier": "^6.0.2"
81
81
  },
82
82
  "devDependencies": {
83
- "p-map": "^5.5.0"
83
+ "p-map": "^6.0.0"
84
84
  }
85
85
  }