json-sort-cli 3.1.0 → 3.1.1

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 +25 -3
  3. package/package.json +1 -1
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
@@ -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.1",
4
4
  "description": "Command line app to deep sort JSON files, retains package.json special key order",
5
5
  "keywords": [
6
6
  "app",