json-sort-cli 3.0.12 → 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 +12 -0
  2. package/cli.js +36 -6
  3. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
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
+
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)
13
+
14
+ ### Features
15
+
16
+ - `-l` or `--lineEnding` flag to override the default - the EOL format found in file (or LF) ([1788a77](https://github.com/codsen/codsen/commit/1788a7701629713a1b9ad9b992ad01aa7d456068))
17
+
6
18
  ## 3.0.7 (2023-01-06)
7
19
 
8
20
  ### Bug Fixes
package/cli.js CHANGED
@@ -12,7 +12,7 @@ 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 { isPlainObject } from "codsen-utils";
15
+ import { isPlainObject, resolveEolSetting } from "codsen-utils";
16
16
  import updateNotifier from "update-notifier";
17
17
  import { traverse } from "ast-monkey-traverse";
18
18
  import sortPackageJson, { sortOrder } from "sort-package-json";
@@ -66,7 +66,7 @@ const cli = meow(
66
66
  Options
67
67
  -n, --nodemodules Don't ignore any node_modules folders
68
68
  -t, --tabs Use tabs for JSON file indentation
69
- -i, --indentationCount How many spaces or tabs to use (default = 2 spaces or 1 tab)
69
+ -i, --indentationCount How many spaces/tabs to use (default: 2 for spaces or 1 for tabs)
70
70
  -s, --silent Does not show the result per-file, only totals in the end
71
71
  -h, --help Shows this help
72
72
  -v, --version Shows the current version
@@ -74,6 +74,8 @@ const cli = meow(
74
74
  -d, --dry Only list all the files about to be processed
75
75
  -p, --pack Exclude all package.json files
76
76
  -c, --ci Only exits with non-zero code if files COULD BE sorted
77
+ -l, --lineEnding Set to "cr", "crlf" or "lf" to override the default
78
+ (which is either EOL format used in the file, or Mac LF)
77
79
 
78
80
  Example
79
81
  Call anywhere using glob patterns. If you put them as string, this library
@@ -131,6 +133,10 @@ const cli = meow(
131
133
  type: "number",
132
134
  alias: "i",
133
135
  },
136
+ lineEnding: {
137
+ type: "string",
138
+ alias: "l",
139
+ },
134
140
  },
135
141
  }
136
142
  );
@@ -165,6 +171,8 @@ function readSortAndWriteOverFile(oneOfPaths) {
165
171
  return fs
166
172
  .readFile(oneOfPaths, "utf8")
167
173
  .then((filesContent) => {
174
+ let eolChar = resolveEolSetting(filesContent, cli.flags.lineEnding);
175
+
168
176
  let parsedJson;
169
177
  try {
170
178
  // try to parse JSON
@@ -194,6 +202,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
194
202
  spaces: cli.flags.tabs
195
203
  ? "\t".repeat(indentationCount)
196
204
  : indentationCount,
205
+ EOL: eolChar,
197
206
  }
198
207
  );
199
208
  } else {
@@ -212,6 +221,8 @@ function readSortAndWriteOverFile(oneOfPaths) {
212
221
  // we read it, now we return true if result differs after processing
213
222
 
214
223
  let stringified = JSON.stringify(
224
+ // The traversal below will mutate, not just traverse, -
225
+ // whatever you return from the callback below, gets written
215
226
  traverse(obj, (key, val) => {
216
227
  let current = val !== undefined ? val : key;
217
228
  if (isPlainObject(current)) {
@@ -223,7 +234,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
223
234
  current.length > 1 &&
224
235
  current.every(isStr)
225
236
  ) {
226
- // alphabetical sort
237
+ // alphabetical sort, this value gets written
227
238
  return current.sort((a, b) => a.localeCompare(b));
228
239
  }
229
240
  return current;
@@ -231,7 +242,25 @@ function readSortAndWriteOverFile(oneOfPaths) {
231
242
  null,
232
243
  cli.flags.tabs ? "\t".repeat(indentationCount) : indentationCount
233
244
  );
234
- 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
+ );
235
264
  }
236
265
 
237
266
  // ELSE,
@@ -258,6 +287,7 @@ function readSortAndWriteOverFile(oneOfPaths) {
258
287
  spaces: cli.flags.tabs
259
288
  ? "\t".repeat(indentationCount)
260
289
  : indentationCount,
290
+ EOL: resolveEolSetting(filesContent, cli.flags.lineEnding),
261
291
  }
262
292
  )
263
293
  .then(() => {
@@ -318,8 +348,8 @@ globby(input, { dot: true })
318
348
  .then((paths) =>
319
349
  pReduce(
320
350
  paths,
321
- (concattedTotal, singleDirOrFilePath) =>
322
- concattedTotal.concat(
351
+ (concatTotal, singleDirOrFilePath) =>
352
+ concatTotal.concat(
323
353
  isDirectory(singleDirOrFilePath).then((bool) =>
324
354
  bool
325
355
  ? globby(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-sort-cli",
3
- "version": "3.0.12",
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",
@@ -67,9 +67,9 @@
67
67
  }
68
68
  },
69
69
  "dependencies": {
70
- "ast-monkey-traverse": "^4.0.8",
70
+ "ast-monkey-traverse": "^4.0.9",
71
71
  "chalk": "^5.2.0",
72
- "codsen-utils": "^1.3.1",
72
+ "codsen-utils": "^1.4.0",
73
73
  "fs-extra": "^11.1.1",
74
74
  "globby": "^13.1.4",
75
75
  "is-d": "^1.0.0",