@cspell/cspell-json-reporter 6.20.1 → 6.22.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/README.md CHANGED
@@ -37,7 +37,52 @@ or `cspell.json`
37
37
  - `info` - CSpell execution logs if `settings.verbose` is enabled
38
38
  - `debug` - CSpell debug logs if `settings.debug` is enabled
39
39
 
40
- See [CSpellJSONReporterOutput](https://github.com/streetsidesoftware/cspell/blob/main/packages/cspell-json-reporter/src/CSpellJSONReporterOutput.ts) for more info.
40
+ <details>
41
+ <summary>JSON Output Definition</summary>
42
+
43
+ <!--- @@inject: src/CSpellJSONReporterOutput.ts --->
44
+
45
+ ```ts
46
+ import type {
47
+ ErrorLike,
48
+ Issue,
49
+ MessageType,
50
+ ProgressFileComplete,
51
+ ProgressItem,
52
+ RunResult,
53
+ } from '@cspell/cspell-types';
54
+
55
+ export type CSpellJSONReporterOutput = {
56
+ /**
57
+ * Found spelling issues
58
+ */
59
+ issues: Array<Issue>;
60
+ /**
61
+ * CSpell execution logs
62
+ */
63
+ info?: Array<{ message: string; msgType: MessageType }>;
64
+ /**
65
+ * CSpell debug logs
66
+ */
67
+ debug?: Array<{ message: string }>;
68
+ /**
69
+ * CSpell error logs
70
+ */
71
+ error?: Array<{ message: string; error: ErrorLike }>;
72
+ /**
73
+ * CSpell file progress logs
74
+ */
75
+ progress?: Array<ProgressItem | ProgressFileComplete>;
76
+ /**
77
+ * Execution result
78
+ */
79
+ result: RunResult;
80
+ };
81
+ ```
82
+
83
+ <!--- @@inject-end: src/CSpellJSONReporterOutput.ts --->
84
+
85
+ </details>
41
86
 
42
87
  ## Settings
43
88
 
@@ -48,4 +93,46 @@ Possible settings:
48
93
  - `debug` (default: false) - enable saving of debug logs
49
94
  - `progress` (default: false) - enable saving of file progress logs
50
95
 
51
- See [CSpellJSONReporterSettings](https://github.com/streetsidesoftware/cspell/blob/main/packages/cspell-json-reporter/src/CSpellJSONReporterSettings.ts) for more info.
96
+ <details>
97
+ <summary>Reporter Settings</summary>
98
+
99
+ <!--- @@inject: src/CSpellJSONReporterSettings.ts --->
100
+
101
+ ```ts
102
+ /**
103
+ * CSpell-json-reporter settings type definition
104
+ */
105
+ export type CSpellJSONReporterSettings = {
106
+ /**
107
+ * Path to the output file.
108
+ *
109
+ * Relative paths are relative to the current working directory.
110
+ *
111
+ * Special values:
112
+ * - `stdout` - write the JSON to `stdout`.
113
+ * - `stderr` - write the JSON to `stderr`.
114
+ *
115
+ * @default stdout
116
+ */
117
+ outFile?: string;
118
+ /**
119
+ * Add more information about the files being checked and the configuration
120
+ * @default false
121
+ */
122
+ verbose?: boolean;
123
+ /**
124
+ * Add information useful for debugging cspell.json files
125
+ * @default false
126
+ */
127
+ debug?: boolean;
128
+ /**
129
+ * Add progress messages
130
+ * @default false
131
+ */
132
+ progress?: boolean;
133
+ };
134
+ ```
135
+
136
+ <!--- @@inject-end: src/CSpellJSONReporterSettings.ts --->
137
+
138
+ </details>
@@ -7,27 +7,27 @@ export type CSpellJSONReporterOutput = {
7
7
  /**
8
8
  * CSpell execution logs
9
9
  */
10
- info: Array<{
10
+ info?: Array<{
11
11
  message: string;
12
12
  msgType: MessageType;
13
13
  }>;
14
14
  /**
15
15
  * CSpell debug logs
16
16
  */
17
- debug: Array<{
17
+ debug?: Array<{
18
18
  message: string;
19
19
  }>;
20
20
  /**
21
21
  * CSpell error logs
22
22
  */
23
- error: Array<{
23
+ error?: Array<{
24
24
  message: string;
25
25
  error: ErrorLike;
26
26
  }>;
27
27
  /**
28
28
  * CSpell file progress logs
29
29
  */
30
- progress: Array<ProgressItem | ProgressFileComplete>;
30
+ progress?: Array<ProgressItem | ProgressFileComplete>;
31
31
  /**
32
32
  * Execution result
33
33
  */
@@ -3,9 +3,17 @@
3
3
  */
4
4
  export type CSpellJSONReporterSettings = {
5
5
  /**
6
- * Output JSON file path
6
+ * Path to the output file.
7
+ *
8
+ * Relative paths are relative to the current working directory.
9
+ *
10
+ * Special values:
11
+ * - `stdout` - write the JSON to `stdout`.
12
+ * - `stderr` - write the JSON to `stderr`.
13
+ *
14
+ * @default stdout
7
15
  */
8
- outFile: string;
16
+ outFile?: string;
9
17
  /**
10
18
  * Add more information about the files being checked and the configuration
11
19
  * @default false
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CSpellReporter } from '@cspell/cspell-types';
1
+ import type { CSpellReporter, ReporterConfiguration } from '@cspell/cspell-types';
2
2
  import type { CSpellJSONReporterSettings } from './CSpellJSONReporterSettings';
3
- export declare function getReporter(settings: unknown | CSpellJSONReporterSettings): CSpellReporter;
3
+ export declare function getReporter(settings: unknown | CSpellJSONReporterSettings, cliOptions?: ReporterConfiguration): Required<CSpellReporter>;
4
4
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -33,51 +33,70 @@ function mkdirp(p) {
33
33
  return fs_1.promises.mkdir(p, { recursive: true });
34
34
  }
35
35
  const noopReporter = () => undefined;
36
- function getReporter(settings) {
37
- (0, validateSettings_1.validateSettings)(settings);
38
- const reportData = {
39
- issues: [],
40
- info: [],
41
- debug: [],
42
- error: [],
43
- progress: [],
44
- };
36
+ const STDOUT = 'stdout';
37
+ const STDERR = 'stderr';
38
+ function getReporter(settings, cliOptions) {
39
+ const useSettings = normalizeSettings(settings);
40
+ const reportData = { issues: [], info: [], debug: [], error: [], progress: [] };
45
41
  return {
46
42
  issue: (issue) => {
47
43
  reportData.issues.push(issue);
48
44
  },
49
45
  info: (message, msgType) => {
50
- if (msgType === cspell_types_1.MessageTypes.Debug && !settings.debug) {
46
+ if (msgType === cspell_types_1.MessageTypes.Debug && !useSettings.debug) {
51
47
  return;
52
48
  }
53
- if (msgType === cspell_types_1.MessageTypes.Info && !settings.verbose) {
49
+ if (msgType === cspell_types_1.MessageTypes.Info && !useSettings.verbose) {
54
50
  return;
55
51
  }
56
- reportData.info.push({ message, msgType });
52
+ reportData.info = push(reportData.info, { message, msgType });
57
53
  },
58
- debug: settings.debug
54
+ debug: useSettings.debug
59
55
  ? (message) => {
60
- reportData.debug.push({ message });
56
+ reportData.debug = push(reportData.debug, { message });
61
57
  }
62
58
  : noopReporter,
63
59
  error: (message, error) => {
64
- reportData.error.push({ message, error });
60
+ reportData.error = push(reportData.error, { message, error });
65
61
  },
66
- progress: settings.progress
62
+ progress: useSettings.progress
67
63
  ? (item) => {
68
- reportData.progress.push(item);
64
+ reportData.progress = push(reportData.progress, item);
69
65
  }
70
66
  : noopReporter,
71
67
  result: async (result) => {
72
- const outFilePath = path.join(process.cwd(), settings.outFile);
68
+ const outFile = useSettings.outFile || STDOUT;
73
69
  const output = {
74
70
  ...reportData,
75
71
  result,
76
72
  };
73
+ const jsonData = JSON.stringify(output, setToJSONReplacer_1.setToJSONReplacer, 4);
74
+ if (outFile === STDOUT) {
75
+ console.log(jsonData);
76
+ return;
77
+ }
78
+ if (outFile === STDERR) {
79
+ console.error(jsonData);
80
+ return;
81
+ }
82
+ const outFilePath = path.join(cliOptions?.root ?? process.cwd(), outFile);
77
83
  await mkdirp(path.dirname(outFilePath));
78
- return fs_1.promises.writeFile(outFilePath, JSON.stringify(output, setToJSONReplacer_1.setToJSONReplacer, 4));
84
+ return fs_1.promises.writeFile(outFilePath, jsonData);
79
85
  },
80
86
  };
81
87
  }
82
88
  exports.getReporter = getReporter;
89
+ function normalizeSettings(settings) {
90
+ if (settings === undefined)
91
+ return { outFile: STDOUT };
92
+ (0, validateSettings_1.validateSettings)(settings);
93
+ return settings;
94
+ }
95
+ function push(src, value) {
96
+ if (src) {
97
+ src.push(value);
98
+ return src;
99
+ }
100
+ return [value];
101
+ }
83
102
  //# sourceMappingURL=index.js.map
@@ -15,14 +15,14 @@ function assertBooleanOrUndefined(key, value) {
15
15
  * Throws an error if passed cspell-json-reporter settings are invalid
16
16
  */
17
17
  function validateSettings(settings) {
18
- if (!settings || typeof settings !== 'object') {
18
+ if (!settings || typeof settings !== 'object' || Array.isArray(settings)) {
19
19
  throw new assert_1.AssertionError({
20
20
  message: 'cspell-json-reporter settings must be an object',
21
21
  actual: typeof settings,
22
22
  expected: 'object',
23
23
  });
24
24
  }
25
- const { outFile, debug, verbose, progress } = settings;
25
+ const { outFile = 'stdout', debug, verbose, progress } = settings;
26
26
  if (typeof outFile !== 'string') {
27
27
  throw new assert_1.AssertionError({
28
28
  message: 'cspell-json-reporter settings.outFile must be a string',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cspell/cspell-json-reporter",
3
- "version": "6.20.1",
3
+ "version": "6.22.0",
4
4
  "description": "JSON reporter for CSpell",
5
5
  "author": "Jason Dent",
6
6
  "license": "MIT",
@@ -38,10 +38,10 @@
38
38
  "update-snapshot": "jest --updateSnapshot"
39
39
  },
40
40
  "dependencies": {
41
- "@cspell/cspell-types": "6.20.1"
41
+ "@cspell/cspell-types": "6.22.0"
42
42
  },
43
43
  "engines": {
44
44
  "node": ">=14"
45
45
  },
46
- "gitHead": "508a06d32dd59d881d07cb60fe8c0a7c0bddf8eb"
46
+ "gitHead": "a133874ed7590cbe140f5067cfa80db84b644a5d"
47
47
  }