@zthun/janitor-lint 19.2.0 → 19.2.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.
@@ -1,819 +0,0 @@
1
- import chalk from "chalk";
2
- import { createRequire } from "node:module";
3
- import { cosmiconfig } from "cosmiconfig";
4
- import { resolve } from "path";
5
- import { resolveConfig, getFileInfo, check } from "prettier";
6
- import { HTMLHint } from "htmlhint";
7
- import { load } from "js-yaml";
8
- import { ESLint } from "eslint";
9
- import { every, some, values, uniq, noop } from "lodash-es";
10
- import { readFile } from "fs";
11
- import { sync } from "glob";
12
- import { promisify } from "util";
13
- import { lint } from "markdownlint/promise";
14
- import { lint as lint$1 } from "cspell";
15
- import stylelint from "stylelint";
16
- function $require(id) {
17
- const require2 = createRequire(import.meta.url);
18
- return require2(id);
19
- }
20
- function $resolve(id, options) {
21
- const require2 = createRequire(import.meta.url);
22
- return require2.resolve(id, options);
23
- }
24
- class ZConfigExtender {
25
- /**
26
- * Initializes a new instance of this object.
27
- *
28
- * @param key
29
- * - The key to extend.
30
- */
31
- constructor(key = "extends") {
32
- this.key = key;
33
- }
34
- /**
35
- * Extends the configuration value.
36
- *
37
- * This is similar to how eslint works. This will do an
38
- * asynchronous require on each configuration under the key in the
39
- * config. If the key in the config is falsy, then the config is returned.
40
- *
41
- * The actual key in the config is deleted.
42
- *
43
- * @param config
44
- * - The config to extend.
45
- *
46
- * @returns
47
- * A promise that resolves the extended configuration.
48
- */
49
- async extend(config) {
50
- if (config == null || !Object.hasOwnProperty.call(config, this.key)) {
51
- return config;
52
- }
53
- const extensions = config[this.key];
54
- const modules = Array.isArray(extensions) ? extensions : [extensions];
55
- const resolved = await Promise.all(modules.map((m) => this._read(m)));
56
- let updated = resolved.reduce(
57
- (last, current) => Object.assign({}, last, current),
58
- {}
59
- );
60
- updated = Object.assign({}, updated, config);
61
- delete updated[this.key];
62
- return updated;
63
- }
64
- /**
65
- * Reads a module recursively.
66
- *
67
- * @param module
68
- * - The module to read.
69
- *
70
- * @returns
71
- * A promise that resolves the extended inner module.
72
- */
73
- async _read(module) {
74
- const data = $require(module);
75
- return await this.extend(data);
76
- }
77
- }
78
- class ZConfigReaderCosmic {
79
- /**
80
- * Initializes a new instance of this object.
81
- *
82
- * @param name -
83
- * The name of the application to load.
84
- * @param extender -
85
- * The extender to expand upon the read configuration.
86
- * @param paths -
87
- * The additional paths to read if cosmic config does not find a valid config. Remember that these
88
- * are paths, not modules in this case, so you can't load things from the node modules directory
89
- * using these values. These only affect the search for the config file, not the actual
90
- * read of the config.
91
- */
92
- constructor(name, extender, paths = []) {
93
- this.name = name;
94
- this.extender = extender;
95
- this.paths = paths;
96
- }
97
- /**
98
- * Runs a search for the appropriate configuration file.
99
- *
100
- * The extension keyword is deleted from the config.
101
- *
102
- * @returns
103
- * A promise that resolves with the expanded configuration.
104
- */
105
- async search() {
106
- const explorer = cosmiconfig(this.name, { searchStrategy: "project" });
107
- const searched = await explorer.search();
108
- if (searched) {
109
- return searched.filepath;
110
- }
111
- for (const path of this.paths) {
112
- const full = resolve(path);
113
- const result = await explorer.load(full).catch(() => null);
114
- if (result) {
115
- return result.filepath;
116
- }
117
- }
118
- return null;
119
- }
120
- /**
121
- * Reads the config file.
122
- *
123
- * @param config -
124
- * The optional configuration file. If this is null then the cosmiconfig path is searched on the name.
125
- *
126
- * @returns
127
- * A promise that resolves the json object that represents the config.
128
- */
129
- async read(config) {
130
- const configLoad = config ? Promise.resolve(config) : this.search();
131
- const configFile = await configLoad;
132
- if (!configFile) {
133
- return {};
134
- }
135
- const path = $resolve(configFile, { paths: [process.cwd()] });
136
- const buffer = await cosmiconfig(this.name).load(path);
137
- return await this.extender.extend(buffer.config);
138
- }
139
- }
140
- class ZConfigReaderNull {
141
- /**
142
- * Returns a null resolved promise.
143
- *
144
- * @returns
145
- * A promise that resolves to null.
146
- */
147
- async read() {
148
- return null;
149
- }
150
- }
151
- class ZConfigReaderPrettier {
152
- /**
153
- * Reads the configuration file.
154
- *
155
- * @param config -
156
- * The config module to load. If this value is falsy,
157
- * then prettier will be used to retrieve the configuration.
158
- *
159
- * @returns
160
- * The options for the config file.
161
- */
162
- async read(config) {
163
- const cwd = process.cwd();
164
- const configFile = config ? $resolve(config, { paths: [cwd] }) : void 0;
165
- const ops = { config: configFile };
166
- const options = await resolveConfig(
167
- resolve(process.cwd(), "some-prettier-config"),
168
- ops
169
- );
170
- return options || {};
171
- }
172
- }
173
- class ZContentLinterHtml {
174
- constructor() {
175
- this._formatOptions = { colors: true };
176
- }
177
- /**
178
- * Lints the content.
179
- *
180
- * @param content -
181
- * The content to check.
182
- * @param contentPath -
183
- * The path of the content data.
184
- * @param options -
185
- * The htmlhint options.
186
- *
187
- * @returns
188
- * A promise that resolves if the content is lint free, and rejects if it has lint errors.
189
- */
190
- lint(content, contentPath, options) {
191
- const messages = HTMLHint.verify(content, options);
192
- if (messages.length > 0) {
193
- const logs = HTMLHint.format(messages, this._formatOptions);
194
- return Promise.reject(logs);
195
- }
196
- return Promise.resolve(`${contentPath} is lint free.`);
197
- }
198
- }
199
- class ZContentLinterJson {
200
- /**
201
- * Lints the collection of json files.
202
- *
203
- * @param contents -
204
- * The json file contents.
205
- */
206
- async lint(contents) {
207
- return JSON.parse(contents);
208
- }
209
- }
210
- class ZContentLinterPretty {
211
- /**
212
- * Lints the content.
213
- *
214
- * @param content -
215
- * The content to check.
216
- * @param contentPath -
217
- * The path of the content data.
218
- * @param options -
219
- * The htmlhint options.
220
- *
221
- * @returns
222
- * A promise that resolves if the content is lint free, and rejects if it has lint errors.
223
- */
224
- async lint(content, contentPath, options) {
225
- const file = await getFileInfo(contentPath);
226
- const finalOptions = Object.assign(
227
- {},
228
- { parser: file.inferredParser },
229
- options
230
- );
231
- const formatted = await check(content, finalOptions);
232
- if (!formatted) {
233
- return Promise.reject(`${contentPath} is not formatted.`);
234
- }
235
- return Promise.resolve(`${contentPath} is properly formatted.`);
236
- }
237
- }
238
- class ZContentLinterYaml {
239
- /**
240
- * Lints yml files.
241
- *
242
- * @param contents -
243
- * Yaml formatted string.
244
- *
245
- * @returns
246
- * A promise that resolves if successful, or rejects if failed.
247
- */
248
- async lint(contents) {
249
- return load(contents);
250
- }
251
- }
252
- class ZLinterEs {
253
- /**
254
- * Initializes a new instance of this object.
255
- *
256
- * @param _logger -
257
- * The logger to output to.
258
- */
259
- constructor(_logger) {
260
- this._logger = _logger;
261
- this.engineFactory = (options) => new ESLint(options);
262
- }
263
- /**
264
- * Runs the lint given the specified config and source files.
265
- *
266
- * @param src -
267
- * The list of files globs to lint.
268
- * @param config -
269
- * The optional lint config file.
270
- *
271
- * @returns
272
- * A promise that resolves to true if the lint is
273
- * fully successful, and false if the lint
274
- * has errors.
275
- */
276
- async lint(src, config) {
277
- const esOptions = {
278
- errorOnUnmatchedPattern: false
279
- };
280
- if (config) {
281
- esOptions.overrideConfigFile = $resolve(config, {
282
- paths: [process.cwd()]
283
- });
284
- }
285
- try {
286
- const engine = this.engineFactory(esOptions);
287
- const formatter = await engine.loadFormatter();
288
- const report = await engine.lintFiles(src);
289
- const output = formatter.format(report);
290
- this._logger.log(output);
291
- return every(report, (r) => r.errorCount === 0);
292
- } catch (err) {
293
- this._logger.log(err);
294
- return false;
295
- }
296
- }
297
- }
298
- class ZLinterFile {
299
- /**
300
- * Initializes a new instance of this object.
301
- *
302
- * @param _contentLint -
303
- * The linter for an individual file.
304
- * @param _configReader -
305
- * The config reader.
306
- * @param _logger -
307
- * The logger to use.
308
- * @param _type -
309
- * The file type.
310
- */
311
- constructor(_contentLint, _configReader, _logger, _type) {
312
- this._contentLint = _contentLint;
313
- this._configReader = _configReader;
314
- this._logger = _logger;
315
- this._type = _type;
316
- }
317
- /**
318
- * Lints the collection of json files.
319
- *
320
- * @param src -
321
- * The file list of blobs to lint.
322
- * @param config -
323
- * The optional path to the config file.
324
- * @param exclude -
325
- * The list of globs to exclude.
326
- */
327
- async lint(src, config, exclude) {
328
- const readFileAsync = promisify(readFile);
329
- let options = {};
330
- const globOptions = {
331
- dot: true,
332
- ignore: exclude
333
- };
334
- let files = [];
335
- src.forEach(
336
- (pattern) => files = files.concat(sync(pattern, globOptions))
337
- );
338
- if (files.length === 0) {
339
- this._logger.log(chalk.yellow.italic("No globs matched any files."));
340
- return true;
341
- }
342
- try {
343
- options = await this._configReader.read(config);
344
- } catch (err) {
345
- this._logger.error(chalk.red(err));
346
- return false;
347
- }
348
- this._logger.log(
349
- chalk.green.italic(
350
- `Checking syntax for ${files.length} ${this._type} files.`
351
- )
352
- );
353
- this._logger.log();
354
- let result = true;
355
- for (const file of files) {
356
- const fullFilePath = resolve(file);
357
- try {
358
- const content = await readFileAsync(fullFilePath, "utf-8");
359
- await this._contentLint.lint(content, fullFilePath, options, config);
360
- } catch (err) {
361
- result = false;
362
- this._format(fullFilePath, err);
363
- }
364
- }
365
- return result;
366
- }
367
- /**
368
- * Formats a file error to the logger.
369
- *
370
- * @param file -
371
- * The file that failed to parse.
372
- * @param err -
373
- * The error that occurred.Ø
374
- */
375
- _format(file, err) {
376
- const fileFormat = `Errors in ${file}`;
377
- this._logger.error(chalk.green.underline(fileFormat));
378
- if (Array.isArray(err)) {
379
- err.forEach((log) => this._logger.error(chalk.red(log)));
380
- } else {
381
- this._logger.error(chalk.red(err));
382
- }
383
- }
384
- }
385
- class ZLinterMarkdown {
386
- /**
387
- * Initializes a new instance of this object.
388
- *
389
- * @param _logger -
390
- * The logger to write messages to.
391
- * @param _reader -
392
- * The configuration reader.
393
- */
394
- constructor(_logger, _reader) {
395
- this._logger = _logger;
396
- this._reader = _reader;
397
- }
398
- /**
399
- * Lints all files matched by the specified glob pattern.
400
- *
401
- * @param src -
402
- * The glob patterns to match and lint.
403
- * @param cfg -
404
- * The optional config for the linter.
405
- * @param exclude -
406
- * The glob patterns to exclude.
407
- *
408
- * @returns A promise that resolves to true if the linting is ok, and false if the linting fails.
409
- */
410
- async lint(src, cfg, exclude = []) {
411
- let config;
412
- try {
413
- config = await this._reader.read(cfg);
414
- } catch (err) {
415
- this._logger.error(chalk.red(err));
416
- return false;
417
- }
418
- const globOptions = {
419
- dot: true,
420
- ignore: exclude
421
- };
422
- let files = [];
423
- src.forEach(
424
- (pattern) => files = files.concat(sync(pattern, globOptions))
425
- );
426
- const options = { files, config };
427
- const result = await lint(options);
428
- this._logger.log(`${result.toString().trim()}`);
429
- return !some(values(result), (val) => val.length > 0);
430
- }
431
- }
432
- class ZLinterReport {
433
- /**
434
- * Initializes a new instance of this object.
435
- *
436
- * @param _child -
437
- * The child linter to pass the operation off to.
438
- * @param _logger -
439
- * The logger to use.
440
- * @param _type -
441
- * The file type.
442
- */
443
- constructor(_child, _logger, _type) {
444
- this._child = _child;
445
- this._logger = _logger;
446
- this._type = _type;
447
- }
448
- /**
449
- * Lints the collection of json files.
450
- *
451
- * @param src -
452
- * The file list of blobs to lint.
453
- * @param config -
454
- * The optional path to the config file.
455
- * @param exclude -
456
- * The list of globs to exclude.
457
- */
458
- async lint(src, config, exclude) {
459
- const globOptions = {
460
- dot: true,
461
- ignore: exclude
462
- };
463
- let files = [];
464
- src.forEach(
465
- (pattern) => files = files.concat(sync(pattern, globOptions))
466
- );
467
- files = uniq(files);
468
- if (files.length === 0) {
469
- this._logger.log(chalk.yellow.italic("No globs matched any files."));
470
- return true;
471
- }
472
- this._logger.log(
473
- chalk.green.italic(
474
- `Checking syntax for ${files.length} ${this._type} files.`
475
- )
476
- );
477
- return this._child.lint(src, config, exclude);
478
- }
479
- }
480
- class ZLinterSpelling {
481
- /**
482
- * Initializes a new instance of this object.
483
- *
484
- * @param _logger -
485
- * The logger to output to.
486
- */
487
- constructor(_logger) {
488
- this._logger = _logger;
489
- }
490
- /**
491
- * Runs the lint given the specified config and source files.
492
- *
493
- * @param src -
494
- * The list of files globs to lint.
495
- * @param config -
496
- * The optional lint config file.
497
- * @param exclude -
498
- * The list of file globs to exclude.
499
- *
500
- * @returns
501
- * A promise that resolves to true if the
502
- * lint is fully successful, and false if the lint
503
- * has errors.
504
- */
505
- async lint(src, config, exclude) {
506
- const options = { exclude };
507
- if (config) {
508
- options.config = $resolve(config, { paths: [process.cwd()] });
509
- }
510
- const info = noop;
511
- const debug = noop;
512
- const error = noop;
513
- const progress = noop;
514
- const result = noop;
515
- const issue = (issue2) => {
516
- const position = `${issue2.row}:${issue2.col}`;
517
- this._logger.log(
518
- `${chalk.green(issue2.uri)}:${chalk.yellow(position)} - Unknown word (${chalk.red(issue2.text)})`
519
- );
520
- };
521
- const emitters = {
522
- info,
523
- debug,
524
- error,
525
- progress,
526
- issue,
527
- result
528
- };
529
- const runResult = await lint$1(src, options, emitters);
530
- if (runResult.errors > 0 || runResult.issues > 0) {
531
- return false;
532
- }
533
- this._logger.log();
534
- return true;
535
- }
536
- }
537
- class ZLinterStyle {
538
- /**
539
- * Initializes a new instance of this object.
540
- *
541
- * @param _logger -
542
- * The logger to log the output to.
543
- */
544
- constructor(_logger) {
545
- this._logger = _logger;
546
- }
547
- /**
548
- * Runs the file globs through the stylelint application.
549
- *
550
- * @param content -
551
- * The list of globs to lint.
552
- * @param config -
553
- * The linter config file.
554
- * @param exclude
555
- * The globs to exclude.
556
- *
557
- * @returns
558
- * A promise that, when resolved, returns true
559
- * if there are no lint errors, or
560
- * false if errors are present.
561
- */
562
- async lint(content, config, exclude) {
563
- const options = {
564
- files: content,
565
- ignorePattern: exclude
566
- };
567
- if (config) {
568
- options.configFile = $resolve(config, { paths: [process.cwd()] });
569
- }
570
- const result = await stylelint.lint(options);
571
- const verbose = await stylelint.formatters.verbose;
572
- if (result.errored) {
573
- const output = verbose(result.results, result);
574
- this._logger.log(output);
575
- return false;
576
- }
577
- this._logger.log("");
578
- return true;
579
- }
580
- }
581
- class ZJanitorLint {
582
- /**
583
- * Initializes a new instance of this object.
584
- *
585
- * @param _logger -
586
- * The logger to use when formatting output.
587
- */
588
- constructor(_logger) {
589
- this._logger = _logger;
590
- this.esLint = new ZLinterReport(
591
- new ZLinterEs(this._logger),
592
- this._logger,
593
- "es"
594
- );
595
- this.spellLint = new ZLinterReport(
596
- new ZLinterSpelling(this._logger),
597
- this._logger,
598
- "various"
599
- );
600
- this.prettyLint = new ZLinterFile(
601
- new ZContentLinterPretty(),
602
- new ZConfigReaderPrettier(),
603
- this._logger,
604
- "pretty"
605
- );
606
- this.styleLint = new ZLinterReport(
607
- new ZLinterStyle(this._logger),
608
- this._logger,
609
- "style"
610
- );
611
- this.htmlHint = new ZLinterFile(
612
- new ZContentLinterHtml(),
613
- new ZConfigReaderCosmic("htmlhint", new ZConfigExtender()),
614
- this._logger,
615
- "html"
616
- );
617
- this.jsonLint = new ZLinterFile(
618
- new ZContentLinterJson(),
619
- new ZConfigReaderNull(),
620
- this._logger,
621
- "json"
622
- );
623
- this.yamlLint = new ZLinterFile(
624
- new ZContentLinterYaml(),
625
- new ZConfigReaderNull(),
626
- this._logger,
627
- "yaml"
628
- );
629
- this.markdownLint = new ZLinterReport(
630
- new ZLinterMarkdown(
631
- this._logger,
632
- new ZConfigReaderCosmic("markdownlint", new ZConfigExtender(), [
633
- ".markdownlint.json",
634
- ".markdownlint.yaml",
635
- ".markdownlint.cjs"
636
- ])
637
- ),
638
- this._logger,
639
- "markdown"
640
- );
641
- this.config = new ZConfigReaderCosmic("janitor", new ZConfigExtender());
642
- }
643
- /**
644
- * Runs the lint given the required options.
645
- *
646
- * @param options -
647
- * The lint options.
648
- *
649
- * @returns
650
- * A promise that returns 0 if all linting was successful,
651
- * and 1 if any of the linting failed.
652
- */
653
- async lint(options) {
654
- let current = true;
655
- let result = true;
656
- const { lint: lint2 = {} } = options;
657
- const {
658
- jsonFiles,
659
- jsonFilesExclude,
660
- yamlFiles,
661
- yamlFilesExclude,
662
- markdownConfig,
663
- markdownFiles,
664
- markdownFilesExclude,
665
- esConfig,
666
- esFiles,
667
- styleConfig,
668
- styleFiles,
669
- styleFilesExclude,
670
- htmlConfig,
671
- htmlFiles,
672
- htmlFilesExclude,
673
- spellingConfig,
674
- spellingFiles,
675
- spellingFilesExclude,
676
- prettyConfig,
677
- prettyFiles,
678
- prettyFilesExclude
679
- } = lint2;
680
- if (jsonFiles) {
681
- this._logger.log(
682
- chalk.magenta.underline(
683
- `Linting json files from ${jsonFiles.length} globs.`
684
- )
685
- );
686
- current = await this.jsonLint.lint(
687
- jsonFiles,
688
- void 0,
689
- jsonFilesExclude
690
- );
691
- result = result && current;
692
- }
693
- if (yamlFiles) {
694
- this._logger.log(
695
- chalk.magenta.underline(
696
- `Linting yaml files from ${yamlFiles.length} globs.`
697
- )
698
- );
699
- current = await this.yamlLint.lint(
700
- yamlFiles,
701
- void 0,
702
- yamlFilesExclude
703
- );
704
- result = result && current;
705
- }
706
- if (markdownFiles) {
707
- this._logger.log(
708
- chalk.magenta.underline(
709
- `Linting markdown files from ${markdownFiles.length} globs.`
710
- )
711
- );
712
- current = await this.markdownLint.lint(
713
- markdownFiles,
714
- markdownConfig,
715
- markdownFilesExclude
716
- );
717
- result = result && current;
718
- }
719
- if (esFiles) {
720
- this._logger.log(
721
- chalk.magenta.underline(
722
- `Linting ecmaScript files from ${esFiles.length} globs.`
723
- )
724
- );
725
- current = await this.esLint.lint(esFiles, esConfig, void 0);
726
- result = result && current;
727
- }
728
- if (styleFiles) {
729
- this._logger.log(
730
- chalk.magenta.underline(
731
- `Linting style files from ${styleFiles.length} globs.`
732
- )
733
- );
734
- current = await this.styleLint.lint(
735
- styleFiles,
736
- styleConfig,
737
- styleFilesExclude
738
- );
739
- result = result && current;
740
- }
741
- if (htmlFiles) {
742
- this._logger.log(
743
- chalk.magenta.underline(
744
- `Linting html files from ${htmlFiles.length} globs.`
745
- )
746
- );
747
- current = await this.htmlHint.lint(
748
- htmlFiles,
749
- htmlConfig,
750
- htmlFilesExclude
751
- );
752
- result = result && current;
753
- }
754
- if (spellingFiles) {
755
- this._logger.log(
756
- chalk.magenta.underline(
757
- `Checking spelling for ${spellingFiles.length} globs.`
758
- )
759
- );
760
- current = await this.spellLint.lint(
761
- spellingFiles,
762
- spellingConfig,
763
- spellingFilesExclude
764
- );
765
- result = result && current;
766
- }
767
- if (prettyFiles) {
768
- this._logger.log(
769
- chalk.magenta.underline(
770
- `Checking formatting for ${prettyFiles.length} globs.`
771
- )
772
- );
773
- current = await this.prettyLint.lint(
774
- prettyFiles,
775
- prettyConfig,
776
- prettyFilesExclude
777
- );
778
- result = result && current;
779
- }
780
- return result ? 0 : 1;
781
- }
782
- /**
783
- * Runs the application.
784
- *
785
- * @param args -
786
- * The command line arguments.
787
- *
788
- * @returns
789
- * A promise that returns 0 if all linting was
790
- * successful, and 1 if any of the linting failed.
791
- */
792
- async run(args) {
793
- try {
794
- const options = await this.config.read(args.config);
795
- return this.lint(options);
796
- } catch (err) {
797
- this._logger.error(err);
798
- return 1;
799
- }
800
- }
801
- }
802
- export {
803
- ZJanitorLint as Z,
804
- ZConfigExtender as a,
805
- ZConfigReaderCosmic as b,
806
- ZConfigReaderNull as c,
807
- ZConfigReaderPrettier as d,
808
- ZContentLinterHtml as e,
809
- ZContentLinterJson as f,
810
- ZContentLinterPretty as g,
811
- ZContentLinterYaml as h,
812
- ZLinterEs as i,
813
- ZLinterFile as j,
814
- ZLinterMarkdown as k,
815
- ZLinterReport as l,
816
- ZLinterSpelling as m,
817
- ZLinterStyle as n
818
- };
819
- //# sourceMappingURL=janitor-lint-qkTWaEzl.js.map