@zthun/janitor-lint 19.2.1 → 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.
@@ -0,0 +1,1358 @@
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
+
17
+ function $require(id) {
18
+ const require = createRequire(import.meta.url);
19
+ return require(id);
20
+ }
21
+ function $resolve(id, options) {
22
+ const require = createRequire(import.meta.url);
23
+ return require.resolve(id, options);
24
+ }
25
+
26
+ function asyncGeneratorStep$d(gen, resolve, reject, _next, _throw, key, arg) {
27
+ try {
28
+ var info = gen[key](arg);
29
+ var value = info.value;
30
+ } catch (error) {
31
+ reject(error);
32
+ return;
33
+ }
34
+ if (info.done) {
35
+ resolve(value);
36
+ } else {
37
+ Promise.resolve(value).then(_next, _throw);
38
+ }
39
+ }
40
+ function _async_to_generator$d(fn) {
41
+ return function() {
42
+ var self = this, args = arguments;
43
+ return new Promise(function(resolve, reject) {
44
+ var gen = fn.apply(self, args);
45
+ function _next(value) {
46
+ asyncGeneratorStep$d(gen, resolve, reject, _next, _throw, "next", value);
47
+ }
48
+ function _throw(err) {
49
+ asyncGeneratorStep$d(gen, resolve, reject, _next, _throw, "throw", err);
50
+ }
51
+ _next(undefined);
52
+ });
53
+ };
54
+ }
55
+ function _define_property$9(obj, key, value) {
56
+ if (key in obj) {
57
+ Object.defineProperty(obj, key, {
58
+ value: value,
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true
62
+ });
63
+ } else {
64
+ obj[key] = value;
65
+ }
66
+ return obj;
67
+ }
68
+ /**
69
+ * Represents the standard extender.
70
+ *
71
+ * This extender works the same as how eslint extends their
72
+ * configuration. You can pass a key that acts as the extends key which holds paths
73
+ * to the extendable files/modules. Note that this class, unlike eslint, does not assume any naming
74
+ * conventions and ONLY shared configurations are supported. You must pass the full path of the
75
+ * configs when extending, and plugin based syntax is not recognized.
76
+ *
77
+ * It is always better to just use the extension methods of the actual
78
+ * applications if those are available, but this can be used as a fallback or an
79
+ * extension of behavior for child linters that do not support extendable configs.
80
+ */ class ZConfigExtender {
81
+ /**
82
+ * Extends the configuration value.
83
+ *
84
+ * This is similar to how eslint works. This will do an
85
+ * asynchronous require on each configuration under the key in the
86
+ * config. If the key in the config is falsy, then the config is returned.
87
+ *
88
+ * The actual key in the config is deleted.
89
+ *
90
+ * @param config
91
+ * - The config to extend.
92
+ *
93
+ * @returns
94
+ * A promise that resolves the extended configuration.
95
+ */ extend(config) {
96
+ return _async_to_generator$d(function*() {
97
+ if (config == null || !Object.hasOwnProperty.call(config, this.key)) {
98
+ return config;
99
+ }
100
+ const extensions = config[this.key];
101
+ const modules = Array.isArray(extensions) ? extensions : [
102
+ extensions
103
+ ];
104
+ const resolved = yield Promise.all(modules.map((m)=>this._read(m)));
105
+ let updated = resolved.reduce((last, current)=>Object.assign({}, last, current), {});
106
+ updated = Object.assign({}, updated, config);
107
+ delete updated[this.key];
108
+ return updated;
109
+ }).call(this);
110
+ }
111
+ /**
112
+ * Reads a module recursively.
113
+ *
114
+ * @param module
115
+ * - The module to read.
116
+ *
117
+ * @returns
118
+ * A promise that resolves the extended inner module.
119
+ */ _read(module) {
120
+ return _async_to_generator$d(function*() {
121
+ const data = $require(module);
122
+ return yield this.extend(data);
123
+ }).call(this);
124
+ }
125
+ /**
126
+ * Initializes a new instance of this object.
127
+ *
128
+ * @param key
129
+ * - The key to extend.
130
+ */ constructor(key = "extends"){
131
+ _define_property$9(this, "key", void 0);
132
+ this.key = key;
133
+ }
134
+ }
135
+
136
+ function asyncGeneratorStep$c(gen, resolve, reject, _next, _throw, key, arg) {
137
+ try {
138
+ var info = gen[key](arg);
139
+ var value = info.value;
140
+ } catch (error) {
141
+ reject(error);
142
+ return;
143
+ }
144
+ if (info.done) {
145
+ resolve(value);
146
+ } else {
147
+ Promise.resolve(value).then(_next, _throw);
148
+ }
149
+ }
150
+ function _async_to_generator$c(fn) {
151
+ return function() {
152
+ var self = this, args = arguments;
153
+ return new Promise(function(resolve, reject) {
154
+ var gen = fn.apply(self, args);
155
+ function _next(value) {
156
+ asyncGeneratorStep$c(gen, resolve, reject, _next, _throw, "next", value);
157
+ }
158
+ function _throw(err) {
159
+ asyncGeneratorStep$c(gen, resolve, reject, _next, _throw, "throw", err);
160
+ }
161
+ _next(undefined);
162
+ });
163
+ };
164
+ }
165
+ function _define_property$8(obj, key, value) {
166
+ if (key in obj) {
167
+ Object.defineProperty(obj, key, {
168
+ value: value,
169
+ enumerable: true,
170
+ configurable: true,
171
+ writable: true
172
+ });
173
+ } else {
174
+ obj[key] = value;
175
+ }
176
+ return obj;
177
+ }
178
+ /**
179
+ * Represents a reader that uses the cosmiconfig standard for files.
180
+ */ class ZConfigReaderCosmic {
181
+ /**
182
+ * Runs a search for the appropriate configuration file.
183
+ *
184
+ * The extension keyword is deleted from the config.
185
+ *
186
+ * @returns
187
+ * A promise that resolves with the expanded configuration.
188
+ */ search() {
189
+ return _async_to_generator$c(function*() {
190
+ const explorer = cosmiconfig(this.name, {
191
+ searchStrategy: "project"
192
+ });
193
+ // The first step is the standard cosmiconfig
194
+ // search to see if any of these paths exists.
195
+ // These are highest priority.
196
+ const searched = yield explorer.search();
197
+ if (searched) {
198
+ return searched.filepath;
199
+ }
200
+ // Try our additional paths, if any.
201
+ for (const path of this.paths){
202
+ const full = resolve(path);
203
+ const result = yield explorer.load(full).catch(()=>null);
204
+ if (result) {
205
+ return result.filepath;
206
+ }
207
+ }
208
+ return null;
209
+ }).call(this);
210
+ }
211
+ /**
212
+ * Reads the config file.
213
+ *
214
+ * @param config -
215
+ * The optional configuration file. If this is null then the cosmiconfig path is searched on the name.
216
+ *
217
+ * @returns
218
+ * A promise that resolves the json object that represents the config.
219
+ */ read(config) {
220
+ return _async_to_generator$c(function*() {
221
+ const configLoad = config ? Promise.resolve(config) : this.search();
222
+ const configFile = yield configLoad;
223
+ if (!configFile) {
224
+ return {};
225
+ }
226
+ const path = $resolve(configFile, {
227
+ paths: [
228
+ process.cwd()
229
+ ]
230
+ });
231
+ const buffer = yield cosmiconfig(this.name).load(path);
232
+ return yield this.extender.extend(buffer.config);
233
+ }).call(this);
234
+ }
235
+ /**
236
+ * Initializes a new instance of this object.
237
+ *
238
+ * @param name -
239
+ * The name of the application to load.
240
+ * @param extender -
241
+ * The extender to expand upon the read configuration.
242
+ * @param paths -
243
+ * The additional paths to read if cosmic config does not find a valid config. Remember that these
244
+ * are paths, not modules in this case, so you can't load things from the node modules directory
245
+ * using these values. These only affect the search for the config file, not the actual
246
+ * read of the config.
247
+ */ constructor(name, extender, paths = []){
248
+ _define_property$8(this, "name", void 0);
249
+ _define_property$8(this, "extender", void 0);
250
+ _define_property$8(this, "paths", void 0);
251
+ this.name = name;
252
+ this.extender = extender;
253
+ this.paths = paths;
254
+ }
255
+ }
256
+
257
+ function asyncGeneratorStep$b(gen, resolve, reject, _next, _throw, key, arg) {
258
+ try {
259
+ var info = gen[key](arg);
260
+ var value = info.value;
261
+ } catch (error) {
262
+ reject(error);
263
+ return;
264
+ }
265
+ if (info.done) {
266
+ resolve(value);
267
+ } else {
268
+ Promise.resolve(value).then(_next, _throw);
269
+ }
270
+ }
271
+ function _async_to_generator$b(fn) {
272
+ return function() {
273
+ var self = this, args = arguments;
274
+ return new Promise(function(resolve, reject) {
275
+ var gen = fn.apply(self, args);
276
+ function _next(value) {
277
+ asyncGeneratorStep$b(gen, resolve, reject, _next, _throw, "next", value);
278
+ }
279
+ function _throw(err) {
280
+ asyncGeneratorStep$b(gen, resolve, reject, _next, _throw, "throw", err);
281
+ }
282
+ _next(undefined);
283
+ });
284
+ };
285
+ }
286
+ /**
287
+ * Represents a config reader that returns the empty options.
288
+ */ class ZConfigReaderNull {
289
+ /**
290
+ * Returns a null resolved promise.
291
+ *
292
+ * @returns
293
+ * A promise that resolves to null.
294
+ */ read() {
295
+ return _async_to_generator$b(function*() {
296
+ return null;
297
+ })();
298
+ }
299
+ }
300
+
301
+ function asyncGeneratorStep$a(gen, resolve, reject, _next, _throw, key, arg) {
302
+ try {
303
+ var info = gen[key](arg);
304
+ var value = info.value;
305
+ } catch (error) {
306
+ reject(error);
307
+ return;
308
+ }
309
+ if (info.done) {
310
+ resolve(value);
311
+ } else {
312
+ Promise.resolve(value).then(_next, _throw);
313
+ }
314
+ }
315
+ function _async_to_generator$a(fn) {
316
+ return function() {
317
+ var self = this, args = arguments;
318
+ return new Promise(function(resolve, reject) {
319
+ var gen = fn.apply(self, args);
320
+ function _next(value) {
321
+ asyncGeneratorStep$a(gen, resolve, reject, _next, _throw, "next", value);
322
+ }
323
+ function _throw(err) {
324
+ asyncGeneratorStep$a(gen, resolve, reject, _next, _throw, "throw", err);
325
+ }
326
+ _next(undefined);
327
+ });
328
+ };
329
+ }
330
+ /**
331
+ * Represents a configuration reader for prettier.
332
+ */ class ZConfigReaderPrettier {
333
+ /**
334
+ * Reads the configuration file.
335
+ *
336
+ * @param config -
337
+ * The config module to load. If this value is falsy,
338
+ * then prettier will be used to retrieve the configuration.
339
+ *
340
+ * @returns
341
+ * The options for the config file.
342
+ */ read(config) {
343
+ return _async_to_generator$a(function*() {
344
+ const cwd = process.cwd();
345
+ const configFile = config ? $resolve(config, {
346
+ paths: [
347
+ cwd
348
+ ]
349
+ }) : undefined;
350
+ const ops = {
351
+ config: configFile
352
+ };
353
+ // This function seems to have a slight issue in that it needs one directory deeper than
354
+ // the directory you want to start the search in. The documentation on this isn't exactly correct,
355
+ // and it was this change, https://github.com/prettier/prettier/pull/15363/files#diff-6569a6bfe16237da3c47f035a6f3325a79e958507cf866a1bd703ae9210129b3,
356
+ // that broke it.
357
+ const options = yield resolveConfig(resolve(process.cwd(), "some-prettier-config"), ops);
358
+ return options || {};
359
+ })();
360
+ }
361
+ }
362
+
363
+ function _define_property$7(obj, key, value) {
364
+ if (key in obj) {
365
+ Object.defineProperty(obj, key, {
366
+ value: value,
367
+ enumerable: true,
368
+ configurable: true,
369
+ writable: true
370
+ });
371
+ } else {
372
+ obj[key] = value;
373
+ }
374
+ return obj;
375
+ }
376
+ /**
377
+ * Represents an object that can be used to hint html files.
378
+ */ class ZContentLinterHtml {
379
+ /**
380
+ * Lints the content.
381
+ *
382
+ * @param content -
383
+ * The content to check.
384
+ * @param contentPath -
385
+ * The path of the content data.
386
+ * @param options -
387
+ * The htmlhint options.
388
+ *
389
+ * @returns
390
+ * A promise that resolves if the content is lint free, and rejects if it has lint errors.
391
+ */ lint(content, contentPath, options) {
392
+ const messages = HTMLHint.verify(content, options);
393
+ if (messages.length > 0) {
394
+ const logs = HTMLHint.format(messages, this._formatOptions);
395
+ return Promise.reject(logs);
396
+ }
397
+ return Promise.resolve(`${contentPath} is lint free.`);
398
+ }
399
+ constructor(){
400
+ _define_property$7(this, "_formatOptions", {
401
+ colors: true
402
+ });
403
+ }
404
+ }
405
+
406
+ function asyncGeneratorStep$9(gen, resolve, reject, _next, _throw, key, arg) {
407
+ try {
408
+ var info = gen[key](arg);
409
+ var value = info.value;
410
+ } catch (error) {
411
+ reject(error);
412
+ return;
413
+ }
414
+ if (info.done) {
415
+ resolve(value);
416
+ } else {
417
+ Promise.resolve(value).then(_next, _throw);
418
+ }
419
+ }
420
+ function _async_to_generator$9(fn) {
421
+ return function() {
422
+ var self = this, args = arguments;
423
+ return new Promise(function(resolve, reject) {
424
+ var gen = fn.apply(self, args);
425
+ function _next(value) {
426
+ asyncGeneratorStep$9(gen, resolve, reject, _next, _throw, "next", value);
427
+ }
428
+ function _throw(err) {
429
+ asyncGeneratorStep$9(gen, resolve, reject, _next, _throw, "throw", err);
430
+ }
431
+ _next(undefined);
432
+ });
433
+ };
434
+ }
435
+ /**
436
+ * Represents the linter for json files.
437
+ */ class ZContentLinterJson {
438
+ /**
439
+ * Lints the collection of json files.
440
+ *
441
+ * @param contents -
442
+ * The json file contents.
443
+ */ lint(contents) {
444
+ return _async_to_generator$9(function*() {
445
+ return JSON.parse(contents);
446
+ })();
447
+ }
448
+ }
449
+
450
+ function asyncGeneratorStep$8(gen, resolve, reject, _next, _throw, key, arg) {
451
+ try {
452
+ var info = gen[key](arg);
453
+ var value = info.value;
454
+ } catch (error) {
455
+ reject(error);
456
+ return;
457
+ }
458
+ if (info.done) {
459
+ resolve(value);
460
+ } else {
461
+ Promise.resolve(value).then(_next, _throw);
462
+ }
463
+ }
464
+ function _async_to_generator$8(fn) {
465
+ return function() {
466
+ var self = this, args = arguments;
467
+ return new Promise(function(resolve, reject) {
468
+ var gen = fn.apply(self, args);
469
+ function _next(value) {
470
+ asyncGeneratorStep$8(gen, resolve, reject, _next, _throw, "next", value);
471
+ }
472
+ function _throw(err) {
473
+ asyncGeneratorStep$8(gen, resolve, reject, _next, _throw, "throw", err);
474
+ }
475
+ _next(undefined);
476
+ });
477
+ };
478
+ }
479
+ /**
480
+ * Represents an object that can be used to perform prettier checks on files.
481
+ */ class ZContentLinterPretty {
482
+ /**
483
+ * Lints the content.
484
+ *
485
+ * @param content -
486
+ * The content to check.
487
+ * @param contentPath -
488
+ * The path of the content data.
489
+ * @param options -
490
+ * The htmlhint options.
491
+ *
492
+ * @returns
493
+ * A promise that resolves if the content is lint free, and rejects if it has lint errors.
494
+ */ lint(content, contentPath, options) {
495
+ return _async_to_generator$8(function*() {
496
+ const file = yield getFileInfo(contentPath);
497
+ const finalOptions = Object.assign({}, {
498
+ parser: file.inferredParser
499
+ }, options);
500
+ const formatted = yield check(content, finalOptions);
501
+ if (!formatted) {
502
+ return Promise.reject(`${contentPath} is not formatted.`);
503
+ }
504
+ return Promise.resolve(`${contentPath} is properly formatted.`);
505
+ })();
506
+ }
507
+ }
508
+
509
+ function asyncGeneratorStep$7(gen, resolve, reject, _next, _throw, key, arg) {
510
+ try {
511
+ var info = gen[key](arg);
512
+ var value = info.value;
513
+ } catch (error) {
514
+ reject(error);
515
+ return;
516
+ }
517
+ if (info.done) {
518
+ resolve(value);
519
+ } else {
520
+ Promise.resolve(value).then(_next, _throw);
521
+ }
522
+ }
523
+ function _async_to_generator$7(fn) {
524
+ return function() {
525
+ var self = this, args = arguments;
526
+ return new Promise(function(resolve, reject) {
527
+ var gen = fn.apply(self, args);
528
+ function _next(value) {
529
+ asyncGeneratorStep$7(gen, resolve, reject, _next, _throw, "next", value);
530
+ }
531
+ function _throw(err) {
532
+ asyncGeneratorStep$7(gen, resolve, reject, _next, _throw, "throw", err);
533
+ }
534
+ _next(undefined);
535
+ });
536
+ };
537
+ }
538
+ /**
539
+ * Represents a linter for yml files.
540
+ */ class ZContentLinterYaml {
541
+ /**
542
+ * Lints yml files.
543
+ *
544
+ * @param contents -
545
+ * Yaml formatted string.
546
+ *
547
+ * @returns
548
+ * A promise that resolves if successful, or rejects if failed.
549
+ */ lint(contents) {
550
+ return _async_to_generator$7(function*() {
551
+ return load(contents);
552
+ })();
553
+ }
554
+ }
555
+
556
+ function asyncGeneratorStep$6(gen, resolve, reject, _next, _throw, key, arg) {
557
+ try {
558
+ var info = gen[key](arg);
559
+ var value = info.value;
560
+ } catch (error) {
561
+ reject(error);
562
+ return;
563
+ }
564
+ if (info.done) {
565
+ resolve(value);
566
+ } else {
567
+ Promise.resolve(value).then(_next, _throw);
568
+ }
569
+ }
570
+ function _async_to_generator$6(fn) {
571
+ return function() {
572
+ var self = this, args = arguments;
573
+ return new Promise(function(resolve, reject) {
574
+ var gen = fn.apply(self, args);
575
+ function _next(value) {
576
+ asyncGeneratorStep$6(gen, resolve, reject, _next, _throw, "next", value);
577
+ }
578
+ function _throw(err) {
579
+ asyncGeneratorStep$6(gen, resolve, reject, _next, _throw, "throw", err);
580
+ }
581
+ _next(undefined);
582
+ });
583
+ };
584
+ }
585
+ function _define_property$6(obj, key, value) {
586
+ if (key in obj) {
587
+ Object.defineProperty(obj, key, {
588
+ value: value,
589
+ enumerable: true,
590
+ configurable: true,
591
+ writable: true
592
+ });
593
+ } else {
594
+ obj[key] = value;
595
+ }
596
+ return obj;
597
+ }
598
+ /**
599
+ * Represents an object that can be used to perform eslint on javascript files.
600
+ */ class ZLinterEs {
601
+ /**
602
+ * Runs the lint given the specified config and source files.
603
+ *
604
+ * @param src -
605
+ * The list of files globs to lint.
606
+ * @param config -
607
+ * The optional lint config file.
608
+ *
609
+ * @returns
610
+ * A promise that resolves to true if the lint is
611
+ * fully successful, and false if the lint
612
+ * has errors.
613
+ */ lint(src, config) {
614
+ return _async_to_generator$6(function*() {
615
+ const esOptions = {
616
+ errorOnUnmatchedPattern: false
617
+ };
618
+ if (config) {
619
+ esOptions.overrideConfigFile = $resolve(config, {
620
+ paths: [
621
+ process.cwd()
622
+ ]
623
+ });
624
+ }
625
+ try {
626
+ const engine = this.engineFactory(esOptions);
627
+ const formatter = yield engine.loadFormatter();
628
+ const report = yield engine.lintFiles(src);
629
+ const output = formatter.format(report);
630
+ this._logger.log(output);
631
+ return every(report, (r)=>r.errorCount === 0);
632
+ } catch (err) {
633
+ this._logger.log(err);
634
+ return false;
635
+ }
636
+ }).call(this);
637
+ }
638
+ /**
639
+ * Initializes a new instance of this object.
640
+ *
641
+ * @param _logger -
642
+ * The logger to output to.
643
+ */ constructor(_logger){
644
+ _define_property$6(this, "_logger", void 0);
645
+ /**
646
+ * The factory that constructs a CLIEngine object.
647
+ *
648
+ * @param options -
649
+ * The engine options.
650
+ *
651
+ * @returns
652
+ * The engine that can be used to perform eslint.
653
+ */ _define_property$6(this, "engineFactory", void 0);
654
+ this._logger = _logger;
655
+ this.engineFactory = (options)=>new ESLint(options);
656
+ }
657
+ }
658
+
659
+ function asyncGeneratorStep$5(gen, resolve, reject, _next, _throw, key, arg) {
660
+ try {
661
+ var info = gen[key](arg);
662
+ var value = info.value;
663
+ } catch (error) {
664
+ reject(error);
665
+ return;
666
+ }
667
+ if (info.done) {
668
+ resolve(value);
669
+ } else {
670
+ Promise.resolve(value).then(_next, _throw);
671
+ }
672
+ }
673
+ function _async_to_generator$5(fn) {
674
+ return function() {
675
+ var self = this, args = arguments;
676
+ return new Promise(function(resolve, reject) {
677
+ var gen = fn.apply(self, args);
678
+ function _next(value) {
679
+ asyncGeneratorStep$5(gen, resolve, reject, _next, _throw, "next", value);
680
+ }
681
+ function _throw(err) {
682
+ asyncGeneratorStep$5(gen, resolve, reject, _next, _throw, "throw", err);
683
+ }
684
+ _next(undefined);
685
+ });
686
+ };
687
+ }
688
+ function _define_property$5(obj, key, value) {
689
+ if (key in obj) {
690
+ Object.defineProperty(obj, key, {
691
+ value: value,
692
+ enumerable: true,
693
+ configurable: true,
694
+ writable: true
695
+ });
696
+ } else {
697
+ obj[key] = value;
698
+ }
699
+ return obj;
700
+ }
701
+ /**
702
+ * Represents an object that can lint files one at a time.
703
+ */ class ZLinterFile {
704
+ /**
705
+ * Lints the collection of json files.
706
+ *
707
+ * @param src -
708
+ * The file list of blobs to lint.
709
+ * @param config -
710
+ * The optional path to the config file.
711
+ * @param exclude -
712
+ * The list of globs to exclude.
713
+ */ lint(src, config, exclude) {
714
+ return _async_to_generator$5(function*() {
715
+ const readFileAsync = promisify(readFile);
716
+ let options = {};
717
+ const globOptions = {
718
+ dot: true,
719
+ ignore: exclude
720
+ };
721
+ let files = [];
722
+ src.forEach((pattern)=>files = files.concat(sync(pattern, globOptions)));
723
+ if (files.length === 0) {
724
+ this._logger.log(chalk.yellow.italic("No globs matched any files."));
725
+ return true;
726
+ }
727
+ try {
728
+ options = yield this._configReader.read(config);
729
+ } catch (err) {
730
+ this._logger.error(chalk.red(err));
731
+ return false;
732
+ }
733
+ this._logger.log(chalk.green.italic(`Checking syntax for ${files.length} ${this._type} files.`));
734
+ this._logger.log();
735
+ let result = true;
736
+ for (const file of files){
737
+ const fullFilePath = resolve(file);
738
+ try {
739
+ const content = yield readFileAsync(fullFilePath, "utf-8");
740
+ yield this._contentLint.lint(content, fullFilePath, options, config);
741
+ } catch (err) {
742
+ result = false;
743
+ this._format(fullFilePath, err);
744
+ }
745
+ }
746
+ return result;
747
+ }).call(this);
748
+ }
749
+ /**
750
+ * Formats a file error to the logger.
751
+ *
752
+ * @param file -
753
+ * The file that failed to parse.
754
+ * @param err -
755
+ * The error that occurred.Ø
756
+ */ _format(file, err) {
757
+ const fileFormat = `Errors in ${file}`;
758
+ this._logger.error(chalk.green.underline(fileFormat));
759
+ if (Array.isArray(err)) {
760
+ err.forEach((log)=>this._logger.error(chalk.red(log)));
761
+ } else {
762
+ this._logger.error(chalk.red(err));
763
+ }
764
+ }
765
+ /**
766
+ * Initializes a new instance of this object.
767
+ *
768
+ * @param _contentLint -
769
+ * The linter for an individual file.
770
+ * @param _configReader -
771
+ * The config reader.
772
+ * @param _logger -
773
+ * The logger to use.
774
+ * @param _type -
775
+ * The file type.
776
+ */ constructor(_contentLint, _configReader, _logger, _type){
777
+ _define_property$5(this, "_contentLint", void 0);
778
+ _define_property$5(this, "_configReader", void 0);
779
+ _define_property$5(this, "_logger", void 0);
780
+ _define_property$5(this, "_type", void 0);
781
+ this._contentLint = _contentLint;
782
+ this._configReader = _configReader;
783
+ this._logger = _logger;
784
+ this._type = _type;
785
+ }
786
+ }
787
+
788
+ function asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, key, arg) {
789
+ try {
790
+ var info = gen[key](arg);
791
+ var value = info.value;
792
+ } catch (error) {
793
+ reject(error);
794
+ return;
795
+ }
796
+ if (info.done) {
797
+ resolve(value);
798
+ } else {
799
+ Promise.resolve(value).then(_next, _throw);
800
+ }
801
+ }
802
+ function _async_to_generator$4(fn) {
803
+ return function() {
804
+ var self = this, args = arguments;
805
+ return new Promise(function(resolve, reject) {
806
+ var gen = fn.apply(self, args);
807
+ function _next(value) {
808
+ asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, "next", value);
809
+ }
810
+ function _throw(err) {
811
+ asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, "throw", err);
812
+ }
813
+ _next(undefined);
814
+ });
815
+ };
816
+ }
817
+ function _define_property$4(obj, key, value) {
818
+ if (key in obj) {
819
+ Object.defineProperty(obj, key, {
820
+ value: value,
821
+ enumerable: true,
822
+ configurable: true,
823
+ writable: true
824
+ });
825
+ } else {
826
+ obj[key] = value;
827
+ }
828
+ return obj;
829
+ }
830
+ /**
831
+ * Represents a linter object that checks markdown.
832
+ */ class ZLinterMarkdown {
833
+ /**
834
+ * Lints all files matched by the specified glob pattern.
835
+ *
836
+ * @param src -
837
+ * The glob patterns to match and lint.
838
+ * @param cfg -
839
+ * The optional config for the linter.
840
+ * @param exclude -
841
+ * The glob patterns to exclude.
842
+ *
843
+ * @returns A promise that resolves to true if the linting is ok, and false if the linting fails.
844
+ */ lint(_0, _1) {
845
+ return _async_to_generator$4(function*(src, cfg, exclude = []) {
846
+ let config;
847
+ try {
848
+ config = yield this._reader.read(cfg);
849
+ } catch (err) {
850
+ this._logger.error(chalk.red(err));
851
+ return false;
852
+ }
853
+ const globOptions = {
854
+ dot: true,
855
+ ignore: exclude
856
+ };
857
+ let files = [];
858
+ src.forEach((pattern)=>files = files.concat(sync(pattern, globOptions)));
859
+ const options = {
860
+ files,
861
+ config
862
+ };
863
+ const result = yield lint(options);
864
+ this._logger.log(`${result.toString().trim()}`);
865
+ return !some(values(result), (val)=>val.length > 0);
866
+ }).apply(this, arguments);
867
+ }
868
+ /**
869
+ * Initializes a new instance of this object.
870
+ *
871
+ * @param _logger -
872
+ * The logger to write messages to.
873
+ * @param _reader -
874
+ * The configuration reader.
875
+ */ constructor(_logger, _reader){
876
+ _define_property$4(this, "_logger", void 0);
877
+ _define_property$4(this, "_reader", void 0);
878
+ this._logger = _logger;
879
+ this._reader = _reader;
880
+ }
881
+ }
882
+
883
+ function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
884
+ try {
885
+ var info = gen[key](arg);
886
+ var value = info.value;
887
+ } catch (error) {
888
+ reject(error);
889
+ return;
890
+ }
891
+ if (info.done) {
892
+ resolve(value);
893
+ } else {
894
+ Promise.resolve(value).then(_next, _throw);
895
+ }
896
+ }
897
+ function _async_to_generator$3(fn) {
898
+ return function() {
899
+ var self = this, args = arguments;
900
+ return new Promise(function(resolve, reject) {
901
+ var gen = fn.apply(self, args);
902
+ function _next(value) {
903
+ asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value);
904
+ }
905
+ function _throw(err) {
906
+ asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err);
907
+ }
908
+ _next(undefined);
909
+ });
910
+ };
911
+ }
912
+ function _define_property$3(obj, key, value) {
913
+ if (key in obj) {
914
+ Object.defineProperty(obj, key, {
915
+ value: value,
916
+ enumerable: true,
917
+ configurable: true,
918
+ writable: true
919
+ });
920
+ } else {
921
+ obj[key] = value;
922
+ }
923
+ return obj;
924
+ }
925
+ /**
926
+ * Represents an object that will report on file globs, but will
927
+ * pass the actual linting job to another linter.
928
+ */ class ZLinterReport {
929
+ /**
930
+ * Lints the collection of json files.
931
+ *
932
+ * @param src -
933
+ * The file list of blobs to lint.
934
+ * @param config -
935
+ * The optional path to the config file.
936
+ * @param exclude -
937
+ * The list of globs to exclude.
938
+ */ lint(src, config, exclude) {
939
+ return _async_to_generator$3(function*() {
940
+ const globOptions = {
941
+ dot: true,
942
+ ignore: exclude
943
+ };
944
+ let files = [];
945
+ src.forEach((pattern)=>files = files.concat(sync(pattern, globOptions)));
946
+ files = uniq(files);
947
+ if (files.length === 0) {
948
+ this._logger.log(chalk.yellow.italic("No globs matched any files."));
949
+ return true;
950
+ }
951
+ this._logger.log(chalk.green.italic(`Checking syntax for ${files.length} ${this._type} files.`));
952
+ return this._child.lint(src, config, exclude);
953
+ }).call(this);
954
+ }
955
+ /**
956
+ * Initializes a new instance of this object.
957
+ *
958
+ * @param _child -
959
+ * The child linter to pass the operation off to.
960
+ * @param _logger -
961
+ * The logger to use.
962
+ * @param _type -
963
+ * The file type.
964
+ */ constructor(_child, _logger, _type){
965
+ _define_property$3(this, "_child", void 0);
966
+ _define_property$3(this, "_logger", void 0);
967
+ _define_property$3(this, "_type", void 0);
968
+ this._child = _child;
969
+ this._logger = _logger;
970
+ this._type = _type;
971
+ }
972
+ }
973
+
974
+ function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
975
+ try {
976
+ var info = gen[key](arg);
977
+ var value = info.value;
978
+ } catch (error) {
979
+ reject(error);
980
+ return;
981
+ }
982
+ if (info.done) {
983
+ resolve(value);
984
+ } else {
985
+ Promise.resolve(value).then(_next, _throw);
986
+ }
987
+ }
988
+ function _async_to_generator$2(fn) {
989
+ return function() {
990
+ var self = this, args = arguments;
991
+ return new Promise(function(resolve, reject) {
992
+ var gen = fn.apply(self, args);
993
+ function _next(value) {
994
+ asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value);
995
+ }
996
+ function _throw(err) {
997
+ asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err);
998
+ }
999
+ _next(undefined);
1000
+ });
1001
+ };
1002
+ }
1003
+ function _define_property$2(obj, key, value) {
1004
+ if (key in obj) {
1005
+ Object.defineProperty(obj, key, {
1006
+ value: value,
1007
+ enumerable: true,
1008
+ configurable: true,
1009
+ writable: true
1010
+ });
1011
+ } else {
1012
+ obj[key] = value;
1013
+ }
1014
+ return obj;
1015
+ }
1016
+ /**
1017
+ * Represents an object that can be used to perform cspell on files.
1018
+ */ class ZLinterSpelling {
1019
+ /**
1020
+ * Runs the lint given the specified config and source files.
1021
+ *
1022
+ * @param src -
1023
+ * The list of files globs to lint.
1024
+ * @param config -
1025
+ * The optional lint config file.
1026
+ * @param exclude -
1027
+ * The list of file globs to exclude.
1028
+ *
1029
+ * @returns
1030
+ * A promise that resolves to true if the
1031
+ * lint is fully successful, and false if the lint
1032
+ * has errors.
1033
+ */ lint(src, config, exclude) {
1034
+ return _async_to_generator$2(function*() {
1035
+ const options = {
1036
+ exclude
1037
+ };
1038
+ if (config) {
1039
+ options.config = $resolve(config, {
1040
+ paths: [
1041
+ process.cwd()
1042
+ ]
1043
+ });
1044
+ }
1045
+ const info = noop;
1046
+ const debug = noop;
1047
+ const error = noop;
1048
+ const progress = noop;
1049
+ const result = noop;
1050
+ const issue = (issue)=>{
1051
+ const position = `${issue.row}:${issue.col}`;
1052
+ this._logger.log(`${chalk.green(issue.uri)}:${chalk.yellow(position)} - Unknown word (${chalk.red(issue.text)})`);
1053
+ };
1054
+ const emitters = {
1055
+ info,
1056
+ debug,
1057
+ error,
1058
+ progress,
1059
+ issue,
1060
+ result
1061
+ };
1062
+ const runResult = yield lint$1(src, options, emitters);
1063
+ if (runResult.errors > 0 || runResult.issues > 0) {
1064
+ return false;
1065
+ }
1066
+ this._logger.log();
1067
+ return true;
1068
+ }).call(this);
1069
+ }
1070
+ /**
1071
+ * Initializes a new instance of this object.
1072
+ *
1073
+ * @param _logger -
1074
+ * The logger to output to.
1075
+ */ constructor(_logger){
1076
+ _define_property$2(this, "_logger", void 0);
1077
+ this._logger = _logger;
1078
+ }
1079
+ }
1080
+
1081
+ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
1082
+ try {
1083
+ var info = gen[key](arg);
1084
+ var value = info.value;
1085
+ } catch (error) {
1086
+ reject(error);
1087
+ return;
1088
+ }
1089
+ if (info.done) {
1090
+ resolve(value);
1091
+ } else {
1092
+ Promise.resolve(value).then(_next, _throw);
1093
+ }
1094
+ }
1095
+ function _async_to_generator$1(fn) {
1096
+ return function() {
1097
+ var self = this, args = arguments;
1098
+ return new Promise(function(resolve, reject) {
1099
+ var gen = fn.apply(self, args);
1100
+ function _next(value) {
1101
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
1102
+ }
1103
+ function _throw(err) {
1104
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
1105
+ }
1106
+ _next(undefined);
1107
+ });
1108
+ };
1109
+ }
1110
+ function _define_property$1(obj, key, value) {
1111
+ if (key in obj) {
1112
+ Object.defineProperty(obj, key, {
1113
+ value: value,
1114
+ enumerable: true,
1115
+ configurable: true,
1116
+ writable: true
1117
+ });
1118
+ } else {
1119
+ obj[key] = value;
1120
+ }
1121
+ return obj;
1122
+ }
1123
+ /**
1124
+ * Represents a linter for stylelint.
1125
+ */ class ZLinterStyle {
1126
+ /**
1127
+ * Runs the file globs through the stylelint application.
1128
+ *
1129
+ * @param content -
1130
+ * The list of globs to lint.
1131
+ * @param config -
1132
+ * The linter config file.
1133
+ * @param exclude
1134
+ * The globs to exclude.
1135
+ *
1136
+ * @returns
1137
+ * A promise that, when resolved, returns true
1138
+ * if there are no lint errors, or
1139
+ * false if errors are present.
1140
+ */ lint(content, config, exclude) {
1141
+ return _async_to_generator$1(function*() {
1142
+ const options = {
1143
+ files: content,
1144
+ ignorePattern: exclude
1145
+ };
1146
+ if (config) {
1147
+ options.configFile = $resolve(config, {
1148
+ paths: [
1149
+ process.cwd()
1150
+ ]
1151
+ });
1152
+ }
1153
+ const result = yield stylelint.lint(options);
1154
+ const verbose = yield stylelint.formatters.verbose;
1155
+ if (result.errored) {
1156
+ const output = verbose(result.results, result);
1157
+ this._logger.log(output);
1158
+ return false;
1159
+ }
1160
+ this._logger.log("");
1161
+ return true;
1162
+ }).call(this);
1163
+ }
1164
+ /**
1165
+ * Initializes a new instance of this object.
1166
+ *
1167
+ * @param _logger -
1168
+ * The logger to log the output to.
1169
+ */ constructor(_logger){
1170
+ _define_property$1(this, "_logger", void 0);
1171
+ this._logger = _logger;
1172
+ }
1173
+ }
1174
+
1175
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
1176
+ try {
1177
+ var info = gen[key](arg);
1178
+ var value = info.value;
1179
+ } catch (error) {
1180
+ reject(error);
1181
+ return;
1182
+ }
1183
+ if (info.done) {
1184
+ resolve(value);
1185
+ } else {
1186
+ Promise.resolve(value).then(_next, _throw);
1187
+ }
1188
+ }
1189
+ function _async_to_generator(fn) {
1190
+ return function() {
1191
+ var self = this, args = arguments;
1192
+ return new Promise(function(resolve, reject) {
1193
+ var gen = fn.apply(self, args);
1194
+ function _next(value) {
1195
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
1196
+ }
1197
+ function _throw(err) {
1198
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
1199
+ }
1200
+ _next(undefined);
1201
+ });
1202
+ };
1203
+ }
1204
+ function _define_property(obj, key, value) {
1205
+ if (key in obj) {
1206
+ Object.defineProperty(obj, key, {
1207
+ value: value,
1208
+ enumerable: true,
1209
+ configurable: true,
1210
+ writable: true
1211
+ });
1212
+ } else {
1213
+ obj[key] = value;
1214
+ }
1215
+ return obj;
1216
+ }
1217
+ /**
1218
+ * Represents the main entry point object for the application.
1219
+ */ class ZJanitorLint {
1220
+ /**
1221
+ * Runs the lint given the required options.
1222
+ *
1223
+ * @param options -
1224
+ * The lint options.
1225
+ *
1226
+ * @returns
1227
+ * A promise that returns 0 if all linting was successful,
1228
+ * and 1 if any of the linting failed.
1229
+ */ lint(options) {
1230
+ return _async_to_generator(function*() {
1231
+ let current = true;
1232
+ let result = true;
1233
+ const { lint = {} } = options;
1234
+ const { jsonFiles, jsonFilesExclude, yamlFiles, yamlFilesExclude, markdownConfig, markdownFiles, markdownFilesExclude, esConfig, esFiles, styleConfig, styleFiles, styleFilesExclude, htmlConfig, htmlFiles, htmlFilesExclude, spellingConfig, spellingFiles, spellingFilesExclude, prettyConfig, prettyFiles, prettyFilesExclude } = lint;
1235
+ if (jsonFiles) {
1236
+ this._logger.log(chalk.magenta.underline(`Linting json files from ${jsonFiles.length} globs.`));
1237
+ current = yield this.jsonLint.lint(jsonFiles, undefined, jsonFilesExclude);
1238
+ result = result && current;
1239
+ }
1240
+ if (yamlFiles) {
1241
+ this._logger.log(chalk.magenta.underline(`Linting yaml files from ${yamlFiles.length} globs.`));
1242
+ current = yield this.yamlLint.lint(yamlFiles, undefined, yamlFilesExclude);
1243
+ result = result && current;
1244
+ }
1245
+ if (markdownFiles) {
1246
+ this._logger.log(chalk.magenta.underline(`Linting markdown files from ${markdownFiles.length} globs.`));
1247
+ current = yield this.markdownLint.lint(markdownFiles, markdownConfig, markdownFilesExclude);
1248
+ result = result && current;
1249
+ }
1250
+ if (esFiles) {
1251
+ this._logger.log(chalk.magenta.underline(`Linting ecmaScript files from ${esFiles.length} globs.`));
1252
+ current = yield this.esLint.lint(esFiles, esConfig, undefined);
1253
+ result = result && current;
1254
+ }
1255
+ if (styleFiles) {
1256
+ this._logger.log(chalk.magenta.underline(`Linting style files from ${styleFiles.length} globs.`));
1257
+ current = yield this.styleLint.lint(styleFiles, styleConfig, styleFilesExclude);
1258
+ result = result && current;
1259
+ }
1260
+ if (htmlFiles) {
1261
+ this._logger.log(chalk.magenta.underline(`Linting html files from ${htmlFiles.length} globs.`));
1262
+ current = yield this.htmlHint.lint(htmlFiles, htmlConfig, htmlFilesExclude);
1263
+ result = result && current;
1264
+ }
1265
+ if (spellingFiles) {
1266
+ this._logger.log(chalk.magenta.underline(`Checking spelling for ${spellingFiles.length} globs.`));
1267
+ current = yield this.spellLint.lint(spellingFiles, spellingConfig, spellingFilesExclude);
1268
+ result = result && current;
1269
+ }
1270
+ if (prettyFiles) {
1271
+ this._logger.log(chalk.magenta.underline(`Checking formatting for ${prettyFiles.length} globs.`));
1272
+ current = yield this.prettyLint.lint(prettyFiles, prettyConfig, prettyFilesExclude);
1273
+ result = result && current;
1274
+ }
1275
+ return result ? 0 : 1;
1276
+ }).call(this);
1277
+ }
1278
+ /**
1279
+ * Runs the application.
1280
+ *
1281
+ * @param args -
1282
+ * The command line arguments.
1283
+ *
1284
+ * @returns
1285
+ * A promise that returns 0 if all linting was
1286
+ * successful, and 1 if any of the linting failed.
1287
+ */ run(args) {
1288
+ return _async_to_generator(function*() {
1289
+ try {
1290
+ const options = yield this.config.read(args.config);
1291
+ return this.lint(options);
1292
+ } catch (err) {
1293
+ this._logger.error(err);
1294
+ return 1;
1295
+ }
1296
+ }).call(this);
1297
+ }
1298
+ /**
1299
+ * Initializes a new instance of this object.
1300
+ *
1301
+ * @param _logger -
1302
+ * The logger to use when formatting output.
1303
+ */ constructor(_logger){
1304
+ _define_property(this, "_logger", void 0);
1305
+ /**
1306
+ * The linter for js files.
1307
+ */ _define_property(this, "esLint", void 0);
1308
+ /**
1309
+ * The linter for cspell. Useful for multiple file types.
1310
+ */ _define_property(this, "spellLint", void 0);
1311
+ /**
1312
+ * The linter for prettier formatting checks.
1313
+ */ _define_property(this, "prettyLint", void 0);
1314
+ /**
1315
+ * The linter for style files.
1316
+ */ _define_property(this, "styleLint", void 0);
1317
+ /**
1318
+ * The linter for html files.
1319
+ *
1320
+ * Currently, htmlhint has no support for cosmiconfig based paths, so we're going to
1321
+ * add them here.
1322
+ */ _define_property(this, "htmlHint", void 0);
1323
+ /**
1324
+ * The linter for json files.
1325
+ */ _define_property(this, "jsonLint", void 0);
1326
+ /**
1327
+ * The linter for yaml files.
1328
+ */ _define_property(this, "yamlLint", void 0);
1329
+ /**
1330
+ * The linter for markdown files.
1331
+ *
1332
+ * Markdownlint is a bit annoying with this. They
1333
+ * don't really fully support the cosmiconfig standard,
1334
+ * and they only support the config files that are named
1335
+ * .markdownlint.yaml, .markdownlint.json, and .markdownlint.cjs
1336
+ */ _define_property(this, "markdownLint", void 0);
1337
+ /**
1338
+ * The configuration reader.
1339
+ */ _define_property(this, "config", void 0);
1340
+ this._logger = _logger;
1341
+ this.esLint = new ZLinterReport(new ZLinterEs(this._logger), this._logger, "es");
1342
+ this.spellLint = new ZLinterReport(new ZLinterSpelling(this._logger), this._logger, "various");
1343
+ this.prettyLint = new ZLinterFile(new ZContentLinterPretty(), new ZConfigReaderPrettier(), this._logger, "pretty");
1344
+ this.styleLint = new ZLinterReport(new ZLinterStyle(this._logger), this._logger, "style");
1345
+ this.htmlHint = new ZLinterFile(new ZContentLinterHtml(), new ZConfigReaderCosmic("htmlhint", new ZConfigExtender()), this._logger, "html");
1346
+ this.jsonLint = new ZLinterFile(new ZContentLinterJson(), new ZConfigReaderNull(), this._logger, "json");
1347
+ this.yamlLint = new ZLinterFile(new ZContentLinterYaml(), new ZConfigReaderNull(), this._logger, "yaml");
1348
+ this.markdownLint = new ZLinterReport(new ZLinterMarkdown(this._logger, new ZConfigReaderCosmic("markdownlint", new ZConfigExtender(), [
1349
+ ".markdownlint.json",
1350
+ ".markdownlint.yaml",
1351
+ ".markdownlint.cjs"
1352
+ ])), this._logger, "markdown");
1353
+ this.config = new ZConfigReaderCosmic("janitor", new ZConfigExtender());
1354
+ }
1355
+ }
1356
+
1357
+ export { ZJanitorLint as Z, ZConfigExtender as a, ZConfigReaderCosmic as b, ZConfigReaderNull as c, ZConfigReaderPrettier as d, ZContentLinterHtml as e, ZContentLinterJson as f, ZContentLinterPretty as g, ZContentLinterYaml as h, ZLinterEs as i, ZLinterFile as j, ZLinterMarkdown as k, ZLinterReport as l, ZLinterSpelling as m, ZLinterStyle as n };
1358
+ //# sourceMappingURL=janitor-lint-Caz9LdHb.js.map