@code-pushup/eslint-plugin 0.1.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/bin.js ADDED
@@ -0,0 +1,806 @@
1
+ // packages/plugin-eslint/src/lib/runner/index.ts
2
+ import { mkdir as mkdir2, writeFile } from "fs/promises";
3
+ import { dirname, join as join2 } from "path";
4
+
5
+ // packages/models/src/lib/category-config.ts
6
+ import { z as z2 } from "zod";
7
+
8
+ // packages/models/src/lib/implementation/schemas.ts
9
+ import { z } from "zod";
10
+ import { MATERIAL_ICONS } from "@code-pushup/portal-client";
11
+
12
+ // packages/models/src/lib/implementation/utils.ts
13
+ var slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
14
+ var filenameRegex = /^(?!.*[ \\/:*?"<>|]).+$/;
15
+ function hasDuplicateStrings(strings) {
16
+ const uniqueStrings = Array.from(new Set(strings));
17
+ const duplicatedStrings = strings.filter(
18
+ /* @__PURE__ */ ((i) => (v) => uniqueStrings[i] !== v || !++i)(0)
19
+ );
20
+ return duplicatedStrings.length === 0 ? false : duplicatedStrings;
21
+ }
22
+ function hasMissingStrings(toCheck, existing) {
23
+ const nonExisting = toCheck.filter((s) => !existing.includes(s));
24
+ return nonExisting.length === 0 ? false : nonExisting;
25
+ }
26
+ function errorItems(items, transform = (items2) => items2.join(", ")) {
27
+ const paredItems = items ? items : [];
28
+ return transform(paredItems);
29
+ }
30
+ function exists(value) {
31
+ return value != null;
32
+ }
33
+
34
+ // packages/models/src/lib/implementation/schemas.ts
35
+ function executionMetaSchema(options = {
36
+ descriptionDate: "Execution start date and time",
37
+ descriptionDuration: "Execution duration in ms"
38
+ }) {
39
+ return z.object({
40
+ date: z.string({ description: options.descriptionDate }),
41
+ duration: z.number({ description: options.descriptionDuration })
42
+ });
43
+ }
44
+ function slugSchema(description = "Unique ID (human-readable, URL-safe)") {
45
+ return z.string({ description }).regex(slugRegex, {
46
+ message: "The slug has to follow the pattern [0-9a-z] followed by multiple optional groups of -[0-9a-z]. e.g. my-slug"
47
+ }).max(128, {
48
+ message: "slug can be max 128 characters long"
49
+ });
50
+ }
51
+ function descriptionSchema(description = "Description (markdown)") {
52
+ return z.string({ description }).max(65536).optional();
53
+ }
54
+ function docsUrlSchema(description = "Documentation site") {
55
+ return urlSchema(description).optional().or(z.string().max(0));
56
+ }
57
+ function urlSchema(description) {
58
+ return z.string({ description }).url();
59
+ }
60
+ function titleSchema(description = "Descriptive name") {
61
+ return z.string({ description }).max(256);
62
+ }
63
+ function metaSchema(options) {
64
+ const {
65
+ descriptionDescription,
66
+ titleDescription,
67
+ docsUrlDescription,
68
+ description
69
+ } = options || {};
70
+ return z.object(
71
+ {
72
+ title: titleSchema(titleDescription),
73
+ description: descriptionSchema(descriptionDescription),
74
+ docsUrl: docsUrlSchema(docsUrlDescription)
75
+ },
76
+ { description }
77
+ );
78
+ }
79
+ function filePathSchema(description) {
80
+ return z.string({ description }).trim().min(1, { message: "path is invalid" });
81
+ }
82
+ function fileNameSchema(description) {
83
+ return z.string({ description }).trim().regex(filenameRegex, {
84
+ message: `The filename has to be valid`
85
+ }).min(1, { message: "file name is invalid" });
86
+ }
87
+ function positiveIntSchema(description) {
88
+ return z.number({ description }).int().nonnegative();
89
+ }
90
+ function packageVersionSchema(options) {
91
+ let { versionDescription, optional } = options || {};
92
+ versionDescription = versionDescription || "NPM version of the package";
93
+ optional = !!optional;
94
+ const packageSchema = z.string({ description: "NPM package name" });
95
+ const versionSchema = z.string({ description: versionDescription });
96
+ return z.object(
97
+ {
98
+ packageName: optional ? packageSchema.optional() : packageSchema,
99
+ version: optional ? versionSchema.optional() : versionSchema
100
+ },
101
+ { description: "NPM package name and version of a published package" }
102
+ );
103
+ }
104
+ function weightSchema(description = "Coefficient for the given score (use weight 0 if only for display)") {
105
+ return positiveIntSchema(description);
106
+ }
107
+ function weightedRefSchema(description, slugDescription) {
108
+ return z.object(
109
+ {
110
+ slug: slugSchema(slugDescription),
111
+ weight: weightSchema("Weight used to calculate score")
112
+ },
113
+ { description }
114
+ );
115
+ }
116
+ function scorableSchema(description, refSchema, duplicateCheckFn, duplicateMessageFn) {
117
+ return z.object(
118
+ {
119
+ slug: slugSchema('Human-readable unique ID, e.g. "performance"'),
120
+ refs: z.array(refSchema).refine(
121
+ (refs) => !duplicateCheckFn(refs),
122
+ (refs) => ({
123
+ message: duplicateMessageFn(refs)
124
+ })
125
+ )
126
+ },
127
+ { description }
128
+ );
129
+ }
130
+ var materialIconSchema = z.enum(
131
+ MATERIAL_ICONS,
132
+ { description: "Icon from VSCode Material Icons extension" }
133
+ );
134
+
135
+ // packages/models/src/lib/category-config.ts
136
+ var categoryRefSchema = weightedRefSchema(
137
+ "Weighted references to audits and/or groups for the category",
138
+ "Slug of an audit or group (depending on `type`)"
139
+ ).merge(
140
+ z2.object({
141
+ type: z2.enum(["audit", "group"], {
142
+ description: "Discriminant for reference kind, affects where `slug` is looked up"
143
+ }),
144
+ plugin: slugSchema(
145
+ "Plugin slug (plugin should contain referenced audit or group)"
146
+ )
147
+ })
148
+ );
149
+ var categoryConfigSchema = scorableSchema(
150
+ "Category with a score calculated from audits and groups from various plugins",
151
+ categoryRefSchema,
152
+ getDuplicateRefsInCategoryMetrics,
153
+ duplicateRefsInCategoryMetricsErrorMsg
154
+ ).merge(
155
+ metaSchema({
156
+ titleDescription: "Category Title",
157
+ docsUrlDescription: "Category docs URL",
158
+ descriptionDescription: "Category description",
159
+ description: "Meta info for category"
160
+ })
161
+ ).merge(
162
+ z2.object({
163
+ isBinary: z2.boolean({
164
+ description: 'Is this a binary category (i.e. only a perfect score considered a "pass")?'
165
+ }).optional()
166
+ })
167
+ );
168
+ function duplicateRefsInCategoryMetricsErrorMsg(metrics) {
169
+ const duplicateRefs = getDuplicateRefsInCategoryMetrics(metrics);
170
+ return `In the categories, the following audit or group refs are duplicates: ${errorItems(
171
+ duplicateRefs
172
+ )}`;
173
+ }
174
+ function getDuplicateRefsInCategoryMetrics(metrics) {
175
+ return hasDuplicateStrings(
176
+ metrics.map(({ slug, type, plugin }) => `${type} :: ${plugin} / ${slug}`)
177
+ );
178
+ }
179
+
180
+ // packages/models/src/lib/core-config.ts
181
+ import { z as z11 } from "zod";
182
+
183
+ // packages/models/src/lib/persist-config.ts
184
+ import { z as z3 } from "zod";
185
+ var formatSchema = z3.enum(["json", "md"]);
186
+ var persistConfigSchema = z3.object({
187
+ outputDir: filePathSchema("Artifacts folder"),
188
+ filename: fileNameSchema("Artifacts file name (without extension)").default(
189
+ "report"
190
+ ),
191
+ format: z3.array(formatSchema).default(["json"]).optional()
192
+ // @TODO remove default or optional value and otherwise it will not set defaults.
193
+ });
194
+
195
+ // packages/models/src/lib/plugin-config.ts
196
+ import { z as z9 } from "zod";
197
+
198
+ // packages/models/src/lib/plugin-config-audits.ts
199
+ import { z as z4 } from "zod";
200
+ var auditSchema = z4.object({
201
+ slug: slugSchema("ID (unique within plugin)")
202
+ }).merge(
203
+ metaSchema({
204
+ titleDescription: "Descriptive name",
205
+ descriptionDescription: "Description (markdown)",
206
+ docsUrlDescription: "Link to documentation (rationale)",
207
+ description: "List of scorable metrics for the given plugin"
208
+ })
209
+ );
210
+ var pluginAuditsSchema = z4.array(auditSchema, {
211
+ description: "List of audits maintained in a plugin"
212
+ }).refine(
213
+ (auditMetadata) => !getDuplicateSlugsInAudits(auditMetadata),
214
+ (auditMetadata) => ({
215
+ message: duplicateSlugsInAuditsErrorMsg(auditMetadata)
216
+ })
217
+ );
218
+ function duplicateSlugsInAuditsErrorMsg(audits) {
219
+ const duplicateRefs = getDuplicateSlugsInAudits(audits);
220
+ return `In plugin audits the slugs are not unique: ${errorItems(
221
+ duplicateRefs
222
+ )}`;
223
+ }
224
+ function getDuplicateSlugsInAudits(audits) {
225
+ return hasDuplicateStrings(audits.map(({ slug }) => slug));
226
+ }
227
+
228
+ // packages/models/src/lib/plugin-config-groups.ts
229
+ import { z as z5 } from "zod";
230
+ var auditGroupRefSchema = weightedRefSchema(
231
+ "Weighted references to audits",
232
+ "Reference slug to an audit within this plugin (e.g. 'max-lines')"
233
+ );
234
+ var auditGroupSchema = scorableSchema(
235
+ 'An audit group aggregates a set of audits into a single score which can be referenced from a category. E.g. the group slug "performance" groups audits and can be referenced in a category',
236
+ auditGroupRefSchema,
237
+ getDuplicateRefsInGroups,
238
+ duplicateRefsInGroupsErrorMsg
239
+ ).merge(
240
+ metaSchema({
241
+ titleDescription: "Descriptive name for the group",
242
+ descriptionDescription: "Description of the group (markdown)",
243
+ docsUrlDescription: "Group documentation site",
244
+ description: "Group metadata"
245
+ })
246
+ );
247
+ var auditGroupsSchema = z5.array(auditGroupSchema, {
248
+ description: "List of groups"
249
+ }).optional().refine(
250
+ (groups) => !getDuplicateSlugsInGroups(groups),
251
+ (groups) => ({
252
+ message: duplicateSlugsInGroupsErrorMsg(groups)
253
+ })
254
+ );
255
+ function duplicateRefsInGroupsErrorMsg(groupAudits) {
256
+ const duplicateRefs = getDuplicateRefsInGroups(groupAudits);
257
+ return `In plugin groups the audit refs are not unique: ${errorItems(
258
+ duplicateRefs
259
+ )}`;
260
+ }
261
+ function getDuplicateRefsInGroups(groupAudits) {
262
+ return hasDuplicateStrings(
263
+ groupAudits.map(({ slug: ref }) => ref).filter(exists)
264
+ );
265
+ }
266
+ function duplicateSlugsInGroupsErrorMsg(groups) {
267
+ const duplicateRefs = getDuplicateSlugsInGroups(groups);
268
+ return `In groups the slugs are not unique: ${errorItems(duplicateRefs)}`;
269
+ }
270
+ function getDuplicateSlugsInGroups(groups) {
271
+ return Array.isArray(groups) ? hasDuplicateStrings(groups.map(({ slug }) => slug)) : false;
272
+ }
273
+
274
+ // packages/models/src/lib/plugin-config-runner.ts
275
+ import { z as z8 } from "zod";
276
+
277
+ // packages/models/src/lib/plugin-process-output.ts
278
+ import { z as z7 } from "zod";
279
+
280
+ // packages/models/src/lib/plugin-process-output-audit-issue.ts
281
+ import { z as z6 } from "zod";
282
+ var sourceFileLocationSchema = z6.object(
283
+ {
284
+ file: filePathSchema("Relative path to source file in Git repo"),
285
+ position: z6.object(
286
+ {
287
+ startLine: positiveIntSchema("Start line"),
288
+ startColumn: positiveIntSchema("Start column").optional(),
289
+ endLine: positiveIntSchema("End line").optional(),
290
+ endColumn: positiveIntSchema("End column").optional()
291
+ },
292
+ { description: "Location in file" }
293
+ ).optional()
294
+ },
295
+ { description: "Source file location" }
296
+ );
297
+ var issueSeveritySchema = z6.enum(["info", "warning", "error"], {
298
+ description: "Severity level"
299
+ });
300
+ var issueSchema = z6.object(
301
+ {
302
+ message: z6.string({ description: "Descriptive error message" }).max(512),
303
+ severity: issueSeveritySchema,
304
+ source: sourceFileLocationSchema.optional()
305
+ },
306
+ { description: "Issue information" }
307
+ );
308
+
309
+ // packages/models/src/lib/plugin-process-output.ts
310
+ var auditOutputSchema = z7.object(
311
+ {
312
+ slug: slugSchema("Reference to audit"),
313
+ displayValue: z7.string({ description: "Formatted value (e.g. '0.9 s', '2.1 MB')" }).optional(),
314
+ value: positiveIntSchema("Raw numeric value"),
315
+ score: z7.number({
316
+ description: "Value between 0 and 1"
317
+ }).min(0).max(1),
318
+ details: z7.object(
319
+ {
320
+ issues: z7.array(issueSchema, { description: "List of findings" })
321
+ },
322
+ { description: "Detailed information" }
323
+ ).optional()
324
+ },
325
+ { description: "Audit information" }
326
+ );
327
+ var auditOutputsSchema = z7.array(auditOutputSchema, {
328
+ description: "List of JSON formatted audit output emitted by the runner process of a plugin"
329
+ }).refine(
330
+ (audits) => !getDuplicateSlugsInAudits2(audits),
331
+ (audits) => ({ message: duplicateSlugsInAuditsErrorMsg2(audits) })
332
+ );
333
+ function duplicateSlugsInAuditsErrorMsg2(audits) {
334
+ const duplicateRefs = getDuplicateSlugsInAudits2(audits);
335
+ return `In plugin audits the slugs are not unique: ${errorItems(
336
+ duplicateRefs
337
+ )}`;
338
+ }
339
+ function getDuplicateSlugsInAudits2(audits) {
340
+ return hasDuplicateStrings(audits.map(({ slug }) => slug));
341
+ }
342
+
343
+ // packages/models/src/lib/plugin-config-runner.ts
344
+ var outputTransformSchema = z8.function().args(z8.unknown()).returns(z8.union([auditOutputsSchema, z8.promise(auditOutputsSchema)]));
345
+ var runnerConfigSchema = z8.object(
346
+ {
347
+ command: z8.string({
348
+ description: "Shell command to execute"
349
+ }),
350
+ args: z8.array(z8.string({ description: "Command arguments" })).optional(),
351
+ outputFile: filePathSchema("Output path"),
352
+ outputTransform: outputTransformSchema.optional()
353
+ },
354
+ {
355
+ description: "How to execute runner"
356
+ }
357
+ );
358
+ var onProgressSchema = z8.function().args(z8.unknown()).returns(z8.void());
359
+ var runnerFunctionSchema = z8.function().args(onProgressSchema.optional()).returns(z8.union([auditOutputsSchema, z8.promise(auditOutputsSchema)]));
360
+
361
+ // packages/models/src/lib/plugin-config.ts
362
+ var pluginMetaSchema = packageVersionSchema({
363
+ optional: true
364
+ }).merge(
365
+ metaSchema({
366
+ titleDescription: "Descriptive name",
367
+ descriptionDescription: "Description (markdown)",
368
+ docsUrlDescription: "Plugin documentation site",
369
+ description: "Plugin metadata"
370
+ })
371
+ ).merge(
372
+ z9.object({
373
+ slug: slugSchema("References plugin. ID (unique within core config)"),
374
+ icon: materialIconSchema
375
+ })
376
+ );
377
+ var pluginDataSchema = z9.object({
378
+ runner: z9.union([runnerConfigSchema, runnerFunctionSchema]),
379
+ audits: pluginAuditsSchema,
380
+ groups: auditGroupsSchema
381
+ });
382
+ var pluginConfigSchema = pluginMetaSchema.merge(pluginDataSchema).refine(
383
+ (pluginCfg) => !getMissingRefsFromGroups(pluginCfg),
384
+ (pluginCfg) => ({
385
+ message: missingRefsFromGroupsErrorMsg(pluginCfg)
386
+ })
387
+ );
388
+ function missingRefsFromGroupsErrorMsg(pluginCfg) {
389
+ const missingRefs = getMissingRefsFromGroups(pluginCfg);
390
+ return `In the groups, the following audit ref's needs to point to a audit in this plugin config: ${errorItems(
391
+ missingRefs
392
+ )}`;
393
+ }
394
+ function getMissingRefsFromGroups(pluginCfg) {
395
+ if (pluginCfg?.groups?.length && pluginCfg?.audits?.length) {
396
+ const groups = pluginCfg?.groups || [];
397
+ const audits = pluginCfg?.audits || [];
398
+ return hasMissingStrings(
399
+ groups.flatMap(({ refs: audits2 }) => audits2.map(({ slug: ref }) => ref)),
400
+ audits.map(({ slug }) => slug)
401
+ );
402
+ }
403
+ return false;
404
+ }
405
+
406
+ // packages/models/src/lib/upload-config.ts
407
+ import { z as z10 } from "zod";
408
+ var uploadConfigSchema = z10.object({
409
+ server: urlSchema("URL of deployed portal API"),
410
+ apiKey: z10.string({
411
+ description: "API key with write access to portal (use `process.env` for security)"
412
+ }),
413
+ organization: z10.string({
414
+ description: "Organization in code versioning system"
415
+ }),
416
+ project: z10.string({
417
+ description: "Project in code versioning system"
418
+ })
419
+ });
420
+
421
+ // packages/models/src/lib/core-config.ts
422
+ var unrefinedCoreConfigSchema = z11.object({
423
+ plugins: z11.array(pluginConfigSchema, {
424
+ description: "List of plugins to be used (official, community-provided, or custom)"
425
+ }),
426
+ /** portal configuration for persisting results */
427
+ persist: persistConfigSchema,
428
+ /** portal configuration for uploading results */
429
+ upload: uploadConfigSchema.optional(),
430
+ categories: z11.array(categoryConfigSchema, {
431
+ description: "Categorization of individual audits"
432
+ }).refine(
433
+ (categoryCfg) => !getDuplicateSlugCategories(categoryCfg),
434
+ (categoryCfg) => ({
435
+ message: duplicateSlugCategoriesErrorMsg(categoryCfg)
436
+ })
437
+ )
438
+ });
439
+ var coreConfigSchema = refineCoreConfig(unrefinedCoreConfigSchema);
440
+ function refineCoreConfig(schema) {
441
+ return schema.refine(
442
+ (coreCfg) => !getMissingRefsForCategories(coreCfg),
443
+ (coreCfg) => ({
444
+ message: missingRefsForCategoriesErrorMsg(coreCfg)
445
+ })
446
+ );
447
+ }
448
+ function missingRefsForCategoriesErrorMsg(coreCfg) {
449
+ const missingRefs = getMissingRefsForCategories(coreCfg);
450
+ return `In the categories, the following plugin refs do not exist in the provided plugins: ${errorItems(
451
+ missingRefs
452
+ )}`;
453
+ }
454
+ function getMissingRefsForCategories(coreCfg) {
455
+ const missingRefs = [];
456
+ const auditRefsFromCategory = coreCfg.categories.flatMap(
457
+ ({ refs }) => refs.filter(({ type }) => type === "audit").map(({ plugin, slug }) => `${plugin}/${slug}`)
458
+ );
459
+ const auditRefsFromPlugins = coreCfg.plugins.flatMap(
460
+ ({ audits, slug: pluginSlug }) => {
461
+ return audits.map(({ slug }) => `${pluginSlug}/${slug}`);
462
+ }
463
+ );
464
+ const missingAuditRefs = hasMissingStrings(
465
+ auditRefsFromCategory,
466
+ auditRefsFromPlugins
467
+ );
468
+ if (Array.isArray(missingAuditRefs) && missingAuditRefs.length > 0) {
469
+ missingRefs.push(...missingAuditRefs);
470
+ }
471
+ const groupRefsFromCategory = coreCfg.categories.flatMap(
472
+ ({ refs }) => refs.filter(({ type }) => type === "group").map(({ plugin, slug }) => `${plugin}#${slug} (group)`)
473
+ );
474
+ const groupRefsFromPlugins = coreCfg.plugins.flatMap(
475
+ ({ groups, slug: pluginSlug }) => {
476
+ return Array.isArray(groups) ? groups.map(({ slug }) => `${pluginSlug}#${slug} (group)`) : [];
477
+ }
478
+ );
479
+ const missingGroupRefs = hasMissingStrings(
480
+ groupRefsFromCategory,
481
+ groupRefsFromPlugins
482
+ );
483
+ if (Array.isArray(missingGroupRefs) && missingGroupRefs.length > 0) {
484
+ missingRefs.push(...missingGroupRefs);
485
+ }
486
+ return missingRefs.length ? missingRefs : false;
487
+ }
488
+ function duplicateSlugCategoriesErrorMsg(categories) {
489
+ const duplicateStringSlugs = getDuplicateSlugCategories(categories);
490
+ return `In the categories, the following slugs are duplicated: ${errorItems(
491
+ duplicateStringSlugs
492
+ )}`;
493
+ }
494
+ function getDuplicateSlugCategories(categories) {
495
+ return hasDuplicateStrings(categories.map(({ slug }) => slug));
496
+ }
497
+
498
+ // packages/models/src/lib/report.ts
499
+ import { z as z12 } from "zod";
500
+ var auditReportSchema = auditSchema.merge(auditOutputSchema);
501
+ var pluginReportSchema = pluginMetaSchema.merge(
502
+ executionMetaSchema({
503
+ descriptionDate: "Start date and time of plugin run",
504
+ descriptionDuration: "Duration of the plugin run in ms"
505
+ })
506
+ ).merge(
507
+ z12.object({
508
+ audits: z12.array(auditReportSchema),
509
+ groups: z12.array(auditGroupSchema).optional()
510
+ })
511
+ );
512
+ var reportSchema = packageVersionSchema({
513
+ versionDescription: "NPM version of the CLI"
514
+ }).merge(
515
+ executionMetaSchema({
516
+ descriptionDate: "Start date and time of the collect run",
517
+ descriptionDuration: "Duration of the collect run in ms"
518
+ })
519
+ ).merge(
520
+ z12.object(
521
+ {
522
+ categories: z12.array(categoryConfigSchema),
523
+ plugins: z12.array(pluginReportSchema)
524
+ },
525
+ { description: "Collect output data" }
526
+ )
527
+ );
528
+
529
+ // packages/utils/src/lib/file-system.ts
530
+ import { bundleRequire } from "bundle-require";
531
+ import chalk from "chalk";
532
+ import { mkdir, readFile, readdir, stat } from "fs/promises";
533
+ import { join } from "path";
534
+
535
+ // packages/utils/src/lib/formatting.ts
536
+ function slugify(text) {
537
+ return text.trim().toLowerCase().replace(/\s+|\//g, "-").replace(/[^a-z0-9-]/g, "");
538
+ }
539
+ function pluralize(text) {
540
+ if (text.endsWith("y")) {
541
+ return text.slice(0, -1) + "ies";
542
+ }
543
+ if (text.endsWith("s")) {
544
+ return `${text}es`;
545
+ }
546
+ return `${text}s`;
547
+ }
548
+ function pluralizeToken(token, times = 0) {
549
+ return `${times} ${Math.abs(times) === 1 ? token : pluralize(token)}`;
550
+ }
551
+
552
+ // packages/utils/src/lib/file-system.ts
553
+ function toUnixPath(path, options) {
554
+ const unixPath = path.replace(/\\/g, "/");
555
+ if (options?.toRelative) {
556
+ return unixPath.replace(process.cwd().replace(/\\/g, "/") + "/", "");
557
+ }
558
+ return unixPath;
559
+ }
560
+ async function readTextFile(path) {
561
+ const buffer = await readFile(path);
562
+ return buffer.toString();
563
+ }
564
+ async function readJsonFile(path) {
565
+ const text = await readTextFile(path);
566
+ return JSON.parse(text);
567
+ }
568
+ function pluginWorkDir(slug) {
569
+ return join("node_modules", ".code-pushup", slug);
570
+ }
571
+
572
+ // packages/utils/src/lib/report.ts
573
+ function compareIssueSeverity(severity1, severity2) {
574
+ const levels = {
575
+ info: 0,
576
+ warning: 1,
577
+ error: 2
578
+ };
579
+ return levels[severity1] - levels[severity2];
580
+ }
581
+
582
+ // packages/utils/src/lib/execute-process.ts
583
+ function objectToCliArgs(params) {
584
+ if (!params) {
585
+ return [];
586
+ }
587
+ return Object.entries(params).flatMap(([key, value]) => {
588
+ if (key === "_") {
589
+ if (Array.isArray(value)) {
590
+ return value;
591
+ } else {
592
+ return [value + ""];
593
+ }
594
+ }
595
+ const prefix = key.length === 1 ? "-" : "--";
596
+ if (Array.isArray(value)) {
597
+ return value.map((v) => `${prefix}${key}="${v}"`);
598
+ }
599
+ if (Array.isArray(value)) {
600
+ return value.map((v) => `${prefix}${key}="${v}"`);
601
+ }
602
+ if (typeof value === "string") {
603
+ return [`${prefix}${key}="${value}"`];
604
+ }
605
+ if (typeof value === "number") {
606
+ return [`${prefix}${key}=${value}`];
607
+ }
608
+ if (typeof value === "boolean") {
609
+ return [`${prefix}${value ? "" : "no-"}${key}`];
610
+ }
611
+ throw new Error(`Unsupported type ${typeof value} for key ${key}`);
612
+ });
613
+ }
614
+ objectToCliArgs({ z: 5 });
615
+
616
+ // packages/utils/src/lib/git.ts
617
+ import simpleGit from "simple-git";
618
+ var git = simpleGit();
619
+
620
+ // packages/utils/src/lib/progress.ts
621
+ import chalk2 from "chalk";
622
+ import { MultiProgressBars } from "multi-progress-bars";
623
+
624
+ // packages/utils/src/lib/report-to-stdout.ts
625
+ import cliui from "@isaacs/cliui";
626
+ import chalk3 from "chalk";
627
+ import Table from "cli-table3";
628
+
629
+ // packages/utils/src/lib/transformation.ts
630
+ function toArray(val) {
631
+ return Array.isArray(val) ? val : [val];
632
+ }
633
+ function objectToEntries(obj) {
634
+ return Object.entries(obj);
635
+ }
636
+ function countOccurrences(values) {
637
+ return values.reduce(
638
+ (acc, value) => ({ ...acc, [value]: (acc[value] ?? 0) + 1 }),
639
+ {}
640
+ );
641
+ }
642
+ function distinct(array) {
643
+ return Array.from(new Set(array));
644
+ }
645
+
646
+ // packages/plugin-eslint/src/lib/setup.ts
647
+ import { ESLint } from "eslint";
648
+ function setupESLint(eslintrc) {
649
+ return new ESLint({
650
+ ...typeof eslintrc === "string" ? { overrideConfigFile: eslintrc } : { baseConfig: eslintrc },
651
+ useEslintrc: false,
652
+ errorOnUnmatchedPattern: false
653
+ });
654
+ }
655
+
656
+ // packages/plugin-eslint/src/lib/runner/lint.ts
657
+ async function lint({
658
+ eslintrc,
659
+ patterns
660
+ }) {
661
+ const eslint = setupESLint(eslintrc);
662
+ const lintResults = await eslint.lintFiles(patterns);
663
+ const results = lintResults.map(
664
+ (result) => ({
665
+ ...result,
666
+ relativeFilePath: toUnixPath(result.filePath, { toRelative: true })
667
+ })
668
+ );
669
+ const ruleOptionsPerFile = await results.reduce(
670
+ async (acc, { filePath, relativeFilePath, messages }) => {
671
+ const filesMap = await acc;
672
+ const config = await eslint.calculateConfigForFile(
673
+ filePath
674
+ );
675
+ const ruleIds = distinct(
676
+ messages.map(({ ruleId }) => ruleId).filter((ruleId) => ruleId != null)
677
+ );
678
+ const rulesMap = Object.fromEntries(
679
+ ruleIds.map((ruleId) => [
680
+ ruleId,
681
+ toArray(config.rules?.[ruleId] ?? []).slice(1)
682
+ ])
683
+ );
684
+ return {
685
+ ...filesMap,
686
+ [relativeFilePath]: {
687
+ ...filesMap[relativeFilePath],
688
+ ...rulesMap
689
+ }
690
+ };
691
+ },
692
+ Promise.resolve({})
693
+ );
694
+ return { results, ruleOptionsPerFile };
695
+ }
696
+
697
+ // packages/plugin-eslint/src/lib/meta/hash.ts
698
+ import { createHash } from "crypto";
699
+ function ruleIdToSlug(ruleId, options) {
700
+ const slug = slugify(ruleId);
701
+ if (!options?.length) {
702
+ return slug;
703
+ }
704
+ return `${slug}-${jsonHash(options)}`;
705
+ }
706
+ function jsonHash(data, bytes = 8) {
707
+ return createHash("shake256", { outputLength: bytes }).update(JSON.stringify(data) ?? "null").digest("hex");
708
+ }
709
+
710
+ // packages/plugin-eslint/src/lib/runner/transform.ts
711
+ function lintResultsToAudits({
712
+ results,
713
+ ruleOptionsPerFile
714
+ }) {
715
+ const issuesPerAudit = results.flatMap(
716
+ ({ messages, relativeFilePath }) => messages.map((message) => ({ ...message, relativeFilePath }))
717
+ ).reduce((acc, issue) => {
718
+ const { ruleId, message, relativeFilePath } = issue;
719
+ if (!ruleId) {
720
+ console.warn(`ESLint core error - ${message}`);
721
+ return acc;
722
+ }
723
+ const options = ruleOptionsPerFile[relativeFilePath]?.[ruleId] ?? [];
724
+ const auditSlug = ruleIdToSlug(ruleId, options);
725
+ return { ...acc, [auditSlug]: [...acc[auditSlug] ?? [], issue] };
726
+ }, {});
727
+ return Object.entries(issuesPerAudit).map((entry) => toAudit(...entry));
728
+ }
729
+ function toAudit(slug, issues) {
730
+ const auditIssues = issues.map(convertIssue);
731
+ const severityCounts = countOccurrences(
732
+ auditIssues.map(({ severity }) => severity)
733
+ );
734
+ const summaryText = objectToEntries(severityCounts).sort((a, b) => -compareIssueSeverity(a[0], b[0])).map(([severity, count = 0]) => pluralizeToken(severity, count)).join(", ");
735
+ return {
736
+ slug,
737
+ score: Number(auditIssues.length === 0),
738
+ value: auditIssues.length,
739
+ displayValue: summaryText,
740
+ details: {
741
+ issues: auditIssues
742
+ }
743
+ };
744
+ }
745
+ function convertIssue(issue) {
746
+ return {
747
+ message: issue.message,
748
+ severity: convertSeverity(issue.severity),
749
+ source: {
750
+ file: issue.relativeFilePath,
751
+ position: {
752
+ startLine: issue.line,
753
+ startColumn: issue.column,
754
+ endLine: issue.endLine,
755
+ endColumn: issue.endColumn
756
+ }
757
+ }
758
+ };
759
+ }
760
+ function convertSeverity(severity) {
761
+ switch (severity) {
762
+ case 2:
763
+ return "error";
764
+ case 1:
765
+ return "warning";
766
+ case 0:
767
+ throw new Error(`Unexpected severity ${severity} in ESLint results`);
768
+ }
769
+ }
770
+
771
+ // packages/plugin-eslint/src/lib/runner/index.ts
772
+ var WORKDIR = pluginWorkDir("eslint");
773
+ var RUNNER_OUTPUT_PATH = join2(WORKDIR, "runner-output.json");
774
+ var ESLINTRC_PATH = join2(process.cwd(), WORKDIR, ".eslintrc.json");
775
+ var AUDIT_SLUGS_SEP = ",";
776
+ async function executeRunner(argv = process.argv) {
777
+ const [slugs, eslintrc, ...patterns] = argv.slice(2);
778
+ if (!slugs) {
779
+ throw new Error("Invalid runner args - missing slugs argument");
780
+ }
781
+ if (!eslintrc) {
782
+ throw new Error("Invalid runner args - missing eslintrc argument");
783
+ }
784
+ if (!patterns.length) {
785
+ throw new Error("Invalid runner args - missing patterns argument");
786
+ }
787
+ const lintResults = await lint({
788
+ // if file created from inline object, provide inline to preserve relative links
789
+ eslintrc: eslintrc === ESLINTRC_PATH ? await readJsonFile(eslintrc) : eslintrc,
790
+ patterns
791
+ });
792
+ const failedAudits = lintResultsToAudits(lintResults);
793
+ const audits = slugs.split(AUDIT_SLUGS_SEP).map(
794
+ (slug) => failedAudits.find((audit) => audit.slug === slug) ?? {
795
+ slug,
796
+ score: 1,
797
+ value: 0,
798
+ details: { issues: [] }
799
+ }
800
+ );
801
+ await mkdir2(dirname(RUNNER_OUTPUT_PATH), { recursive: true });
802
+ await writeFile(RUNNER_OUTPUT_PATH, JSON.stringify(audits));
803
+ }
804
+
805
+ // packages/plugin-eslint/src/bin.ts
806
+ executeRunner().catch(console.error);