@conformetry/configuration 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,988 @@
1
+ import { z as e } from "zod";
2
+ import t from "node:path";
3
+ import { Injectable as n, Module as r } from "@nestjs/common";
4
+ import i from "lodash";
5
+ import a from "prompts";
6
+ import o from "mustache";
7
+ import s, { existsSync as c } from "node:fs";
8
+ import { access as l, readFile as u } from "node:fs/promises";
9
+ import { fileURLToPath as d } from "node:url";
10
+ import { createJiti as f } from "jiti";
11
+ import { parse as p } from "jsonc-parser";
12
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/input/input.constants.ts
13
+ var m = /* @__PURE__ */ new Set([
14
+ "config",
15
+ "help",
16
+ "instancePath",
17
+ "template"
18
+ ]), h = "all", g = [
19
+ "instancePath",
20
+ "directory",
21
+ "outputPath"
22
+ ], _ = class extends Error {
23
+ constructor(e) {
24
+ super(e), this.name = "InputError";
25
+ }
26
+ }, v = (e) => new _(`${e} is required, and stdin is not a terminal so it cannot be asked for. Pass --${e}.`);
27
+ //#endregion
28
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/configuration/configuration.utilities.ts
29
+ function y(e, t) {
30
+ let n = [
31
+ ...b({
32
+ definitions: e,
33
+ describe: (e, t) => `"${e}" is the name of more than one generator: ${t.join(", ")}. A host resolves the first match, leaving the others unreachable.`,
34
+ keysOf: (e) => [e.name]
35
+ }),
36
+ ...b({
37
+ definitions: e,
38
+ describe: (e, t) => `${e} is the template of more than one generator: ${t.join(", ")}. Validation cannot tell which one a matching instance belongs to.`,
39
+ keysOf: (e) => [e.templatePath]
40
+ }),
41
+ ...S(e),
42
+ ...x(e)
43
+ ];
44
+ for (let e of n) t.addIssue({
45
+ code: "custom",
46
+ message: e.message,
47
+ path: e.path
48
+ });
49
+ }
50
+ function b(e) {
51
+ let t = /* @__PURE__ */ new Map();
52
+ for (let [n, r] of e.definitions.entries()) for (let i of e.keysOf(r)) {
53
+ let e = t.get(i);
54
+ t.set(i, {
55
+ lastIndex: n,
56
+ names: [...e?.names ?? [], r.name]
57
+ });
58
+ }
59
+ return [...t.entries()].filter(([, e]) => e.names.length > 1).map(([t, n]) => ({
60
+ message: e.describe(t, n.names),
61
+ path: [n.lastIndex]
62
+ }));
63
+ }
64
+ function x(e) {
65
+ return e.map((e, t) => ({
66
+ index: t,
67
+ name: e.name
68
+ })).filter(({ name: e }) => e === "all").map(({ index: e, name: t }) => ({
69
+ message: `Generator "${t}" is named after a selection that is reserved: \`validate --templates all\` already means every template.`,
70
+ path: [e]
71
+ }));
72
+ }
73
+ function S(e) {
74
+ return e.map((e, t) => ({
75
+ index: t,
76
+ name: e.name
77
+ })).filter(({ name: e }) => e === "" || e !== t.basename(e)).map(({ index: e, name: t }) => ({
78
+ message: `Generator "${t}" is addressed by its name and emitted to a file named after it, so that name cannot contain a path separator.`,
79
+ path: [e]
80
+ }));
81
+ }
82
+ //#endregion
83
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/configuration/configuration.constants.ts
84
+ var C = class extends Error {
85
+ constructor(e) {
86
+ super(`Unsupported configuration file type: ${e}`), this.name = "UnknownConfigurationFileTypeError";
87
+ }
88
+ }, w = "pnpm-workspace.yaml", T = /* @__PURE__ */ new Set([
89
+ ".cjs",
90
+ ".cts",
91
+ ".js",
92
+ ".json",
93
+ ".jsonc",
94
+ ".mjs",
95
+ ".mts",
96
+ ".ts"
97
+ ]), E = e.lazy(() => e.union([
98
+ e.string(),
99
+ e.number(),
100
+ e.boolean(),
101
+ e.null(),
102
+ e.array(E),
103
+ e.record(e.string(), E)
104
+ ])), D = e.array(e.strictObject({
105
+ description: e.string().optional(),
106
+ inputs: e.record(e.string(), e.record(e.string(), E)).optional(),
107
+ instances: e.array(e.object({
108
+ patterns: e.array(e.string()).optional(),
109
+ substitutions: e.record(e.string(), e.string()).optional(),
110
+ tags: e.array(e.string()).optional(),
111
+ threshold: e.number().min(0).max(1).optional()
112
+ })).optional(),
113
+ name: e.string(),
114
+ templatePath: e.string(),
115
+ threshold: e.number().min(0).max(1).optional()
116
+ })).superRefine(y);
117
+ //#endregion
118
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorateMetadata.js
119
+ function O(e, t) {
120
+ if (typeof Reflect == "object" && typeof Reflect.metadata == "function") return Reflect.metadata(e, t);
121
+ }
122
+ //#endregion
123
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorate.js
124
+ function k(e, t, n, r) {
125
+ var i = arguments.length, a = i < 3 ? t : r === null ? r = Object.getOwnPropertyDescriptor(t, n) : r, o;
126
+ if (typeof Reflect == "object" && typeof Reflect.decorate == "function") a = Reflect.decorate(e, t, n, r);
127
+ else for (var s = e.length - 1; s >= 0; s--) (o = e[s]) && (a = (i < 3 ? o(a) : i > 3 ? o(t, n, a) : o(t, n)) || a);
128
+ return i > 3 && a && Object.defineProperty(t, n, a), a;
129
+ }
130
+ //#endregion
131
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/input/input-options.service.ts
132
+ var A = class {
133
+ constructor() {}
134
+ collectOneInput(e) {
135
+ let t = e.commandArguments[e.argumentIndex];
136
+ if (t?.startsWith("--") !== !0) return 1;
137
+ let n = this.readOptionName(t), r = this.resolvePropertyName({
138
+ optionName: n,
139
+ schemaPropertyNames: e.schemaPropertyNames
140
+ }), i = this.readOptionValue({
141
+ argumentIndex: e.argumentIndex,
142
+ commandArguments: e.commandArguments
143
+ });
144
+ return r === void 0 || i === void 0 || m.has(n) ? 1 : (e.generatorInputs[r] = i, t.includes("=") ? 1 : 2);
145
+ }
146
+ readOptionName(e) {
147
+ return e.includes("=") ? e.slice(2).split("=", 1)[0] ?? "" : e.slice(2);
148
+ }
149
+ readOptionValue(e) {
150
+ let t = e.commandArguments[e.argumentIndex] ?? "";
151
+ if (t.includes("=")) return t.slice(t.indexOf("=") + 1);
152
+ let n = e.commandArguments[e.argumentIndex + 1];
153
+ return n === void 0 || n.startsWith("--") ? void 0 : n;
154
+ }
155
+ resolvePropertyName(e) {
156
+ return e.schemaPropertyNames.find((t) => e.optionName === t || e.optionName === i.kebabCase(t));
157
+ }
158
+ collectGeneratorInputs(e) {
159
+ let t = e.rawArguments[0] === "generate" ? e.rawArguments.slice(1) : e.rawArguments, n = Object.keys(e.schema.properties ?? {}), r = {}, i = 0;
160
+ for (; i < t.length;) i += this.collectOneInput({
161
+ argumentIndex: i,
162
+ commandArguments: t,
163
+ generatorInputs: r,
164
+ schemaPropertyNames: n
165
+ });
166
+ return r;
167
+ }
168
+ normalizeRuntimeOptions(e) {
169
+ let t = {};
170
+ for (let [n, r] of Object.entries(e)) t[n] = r === void 0 ? void 0 : typeof r == "string" ? r : typeof r == "number" || typeof r == "boolean" ? `${r}` : JSON.stringify(r);
171
+ return t;
172
+ }
173
+ resolveTargetDirectoryPath(e) {
174
+ for (let t of g) {
175
+ let n = e.options[t];
176
+ if (typeof n == "string") return n;
177
+ }
178
+ let n = e.options.projectName ?? e.options.project;
179
+ if (typeof n == "string") {
180
+ let t = e.resolveProjectRootPath?.(n);
181
+ if (typeof t == "string") return t;
182
+ }
183
+ return t.join(e.defaultGeneratedOutputDirectory ?? "generated", e.generatorName);
184
+ }
185
+ };
186
+ A = k([n(), O("design:paramtypes", [])], A);
187
+ //#endregion
188
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/input/input-schema.service.ts
189
+ var j = class {
190
+ constructor() {}
191
+ readSchemaProperty(e, t) {
192
+ if (typeof e == "object" && e) return Object.entries(e).find(([e]) => e === t)?.[1];
193
+ }
194
+ validateEnum(e) {
195
+ let t = this.readEnumValues(e.input.propertySchema);
196
+ return t.length === 0 || t.includes(e.value) ? !0 : `${e.input.inputName} must be one of: ${t.join(", ")}`;
197
+ }
198
+ validateLength(e) {
199
+ let t = this.readSchemaProperty(e.input.propertySchema, "minLength");
200
+ if (typeof t == "number" && e.value.length < t) return `${e.input.inputName} must be at least ${String(t)} characters`;
201
+ let n = this.readSchemaProperty(e.input.propertySchema, "maxLength");
202
+ return typeof n == "number" && e.value.length > n ? `${e.input.inputName} must be at most ${String(n)} characters` : !0;
203
+ }
204
+ validatePattern(e) {
205
+ let t = this.readSchemaProperty(e.input.propertySchema, "pattern");
206
+ return typeof t == "string" ? new RegExp(t, "u").test(e.value) ? !0 : `${e.input.inputName} does not match pattern ${t}` : !0;
207
+ }
208
+ describeInput(e) {
209
+ let t = e.schema.required, n = Array.isArray(t) ? t : [];
210
+ return {
211
+ inputName: e.inputName,
212
+ isRequired: n.includes(e.inputName),
213
+ propertySchema: e.schema.properties?.[e.inputName]
214
+ };
215
+ }
216
+ readEnumValues(e) {
217
+ let t = this.readSchemaProperty(e, "enum");
218
+ return Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
219
+ }
220
+ readPromptMessage(e) {
221
+ let t = this.readSchemaProperty(e.propertySchema, "description");
222
+ return typeof t == "string" && t.trim().length > 0 ? t : `Enter ${e.inputName}`;
223
+ }
224
+ readPropertyNames(e) {
225
+ return Object.keys(e.properties ?? {});
226
+ }
227
+ validateValue(e) {
228
+ if (typeof e.value != "string" || e.value.trim().length === 0) return !e.input.isRequired || `${e.input.inputName} is required`;
229
+ let t = e.value, n = this.validateEnum({
230
+ input: e.input,
231
+ value: t
232
+ });
233
+ if (n !== !0) return n;
234
+ let r = this.validateLength({
235
+ input: e.input,
236
+ value: t
237
+ });
238
+ return r === !0 ? this.validatePattern({
239
+ input: e.input,
240
+ value: t
241
+ }) : r;
242
+ }
243
+ };
244
+ j = k([n(), O("design:paramtypes", [])], j);
245
+ //#endregion
246
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/input/input-prompting.service.ts
247
+ var M = class {
248
+ inputSchemaService;
249
+ constructor(e) {
250
+ this.inputSchemaService = e;
251
+ }
252
+ promptRunner = a;
253
+ assertCanPrompt(e) {
254
+ if (!this.isAtTerminal()) throw v(e);
255
+ }
256
+ describeChoice(e) {
257
+ return {
258
+ ...e.description === void 0 ? {} : { description: e.description },
259
+ title: e.name,
260
+ value: e.name
261
+ };
262
+ }
263
+ isAtTerminal() {
264
+ return process.stdin.isTTY;
265
+ }
266
+ async promptForInput(e) {
267
+ this.assertCanPrompt(e.inputName);
268
+ let t = this.inputSchemaService.readEnumValues(e.propertySchema), n = (await this.promptRunner({
269
+ ...t.length > 0 ? {
270
+ choices: t.map((e) => ({
271
+ title: e,
272
+ value: e
273
+ })),
274
+ type: "select"
275
+ } : { type: "text" },
276
+ message: this.inputSchemaService.readPromptMessage(e),
277
+ name: "value",
278
+ validate: (t) => this.inputSchemaService.validateValue({
279
+ input: e,
280
+ value: t
281
+ })
282
+ })).value;
283
+ return typeof n == "string" ? n : void 0;
284
+ }
285
+ async promptForTemplate(e) {
286
+ let t = (await this.promptRunner({
287
+ choices: e.map((e) => this.describeChoice(e)),
288
+ message: "Which template should be rendered?",
289
+ name: "template",
290
+ type: "autocomplete"
291
+ })).template;
292
+ return typeof t == "string" ? t : void 0;
293
+ }
294
+ async promptForTemplates(e) {
295
+ let t = (await this.promptRunner({
296
+ choices: [{
297
+ description: "Every configured template",
298
+ title: "all",
299
+ value: "all"
300
+ }, ...e.map((e) => this.describeChoice(e))],
301
+ message: "Which templates should be validated?",
302
+ name: "templates",
303
+ type: "autocompleteMultiselect"
304
+ })).templates;
305
+ if (!Array.isArray(t)) return;
306
+ let n = t.filter((e) => typeof e == "string");
307
+ return n.length === 0 ? void 0 : n;
308
+ }
309
+ };
310
+ M = k([n(), O("design:paramtypes", [j === void 0 ? Object : j])], M);
311
+ //#endregion
312
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/input/input.service.ts
313
+ var N = class {
314
+ inputOptionsService;
315
+ inputPromptingService;
316
+ inputSchemaService;
317
+ constructor(e, t, n) {
318
+ this.inputOptionsService = e, this.inputPromptingService = t, this.inputSchemaService = n;
319
+ }
320
+ acceptProvidedValue(e) {
321
+ let t = this.inputSchemaService.validateValue(e);
322
+ if (t !== !0) throw Error(t);
323
+ return e.value;
324
+ }
325
+ async resolveInputs(e) {
326
+ let t = {};
327
+ for (let n of this.inputSchemaService.readPropertyNames(e.schema)) {
328
+ let r = this.inputSchemaService.describeInput({
329
+ inputName: n,
330
+ schema: e.schema
331
+ }), i = e.valueResolver(n);
332
+ if (i !== void 0) {
333
+ t[n] = this.acceptProvidedValue({
334
+ input: r,
335
+ value: i
336
+ });
337
+ continue;
338
+ }
339
+ let a = await this.resolveMissingValue(r);
340
+ a !== void 0 && a.trim() !== "" && (t[n] = a);
341
+ }
342
+ return t;
343
+ }
344
+ async resolveMissingValue(e) {
345
+ if (!this.inputPromptingService.isAtTerminal()) {
346
+ if (e.isRequired) throw v(e.inputName);
347
+ return;
348
+ }
349
+ let t = await this.inputPromptingService.promptForInput(e), n = this.inputSchemaService.validateValue({
350
+ input: e,
351
+ value: t
352
+ });
353
+ if (n !== !0) throw Error(n);
354
+ return t;
355
+ }
356
+ parseCommaDelimitedOption(e) {
357
+ if (e === void 0) return;
358
+ let t = e.split(",").map((e) => e.trim()).filter((e) => e.length > 0);
359
+ return t.length > 0 ? t : void 0;
360
+ }
361
+ parseOptionalOption(e) {
362
+ let t = e?.trim();
363
+ return t === void 0 || t.length === 0 ? void 0 : t;
364
+ }
365
+ parseRequiredOption(e) {
366
+ let t = e.value.trim();
367
+ if (t.length === 0) throw Error(`${e.optionName} must not be empty`);
368
+ return t;
369
+ }
370
+ parseThresholdOption(e) {
371
+ let t = this.parseOptionalOption(e);
372
+ if (t === void 0) return;
373
+ let n = Number(t);
374
+ if (!Number.isFinite(n) || n < 0 || n > 1) throw Error(`Invalid --threshold ${t}: expected a ratio between 0 and 1, such as 0.9.`);
375
+ return n;
376
+ }
377
+ async resolveGeneratorInputs(e) {
378
+ let t = this.inputOptionsService.collectGeneratorInputs({
379
+ rawArguments: e.rawArguments,
380
+ schema: e.schema
381
+ });
382
+ return this.resolveInputs({
383
+ schema: e.schema,
384
+ valueResolver: (e) => t[e]
385
+ });
386
+ }
387
+ async resolveInputsFromValues(e) {
388
+ return this.resolveInputs({
389
+ schema: e.schema,
390
+ valueResolver: (t) => e.providedInputs[t]
391
+ });
392
+ }
393
+ };
394
+ N = k([n(), O("design:paramtypes", [
395
+ A === void 0 ? Object : A,
396
+ M === void 0 ? Object : M,
397
+ j === void 0 ? Object : j
398
+ ])], N);
399
+ //#endregion
400
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/input/input.module.ts
401
+ var P = class {};
402
+ P = k([r({
403
+ controllers: [],
404
+ exports: [
405
+ A,
406
+ M,
407
+ N
408
+ ],
409
+ imports: [],
410
+ providers: [
411
+ A,
412
+ M,
413
+ j,
414
+ N
415
+ ]
416
+ })], P);
417
+ //#endregion
418
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/instance-group/instance-group.service.ts
419
+ var F = class {
420
+ constructor() {}
421
+ isProjectScoped(e) {
422
+ return e.tags !== void 0 && e.tags.length > 0;
423
+ }
424
+ };
425
+ F = k([n(), O("design:paramtypes", [])], F);
426
+ //#endregion
427
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/instance-group/instance-group.module.ts
428
+ var I = class {};
429
+ I = k([r({
430
+ controllers: [],
431
+ exports: [F],
432
+ imports: [],
433
+ providers: [F]
434
+ })], I);
435
+ //#endregion
436
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/rendering/rendering.constants.ts
437
+ var L = { escape: String }, R = class extends Error {
438
+ constructor(e) {
439
+ let t = e.placeholders.map((e) => `{{${e}}}`).join(", ");
440
+ super(`No value was supplied for ${t} while rendering ${e.subject}. Declare each one as an input of the generator, and in the matching instance group's substitutions.`), this.name = "MissingSubstitutionError";
441
+ }
442
+ }, z = class {
443
+ constructor() {}
444
+ assertEverySubstitutionSupplied(e) {
445
+ let t = this.collectInterpolatedNames(e.template).filter((t) => !Object.hasOwn(e.substitutions, t));
446
+ if (t.length > 0) throw new R({
447
+ placeholders: t,
448
+ subject: e.subject
449
+ });
450
+ }
451
+ collectInterpolatedNames(e) {
452
+ let t = /* @__PURE__ */ new Set(), n = (e) => {
453
+ for (let r of e) {
454
+ let e = r[0], i = r[1], a = r[4];
455
+ (e === "name" || e === "&") && i !== "." && t.add(i), Array.isArray(a) && n(a);
456
+ }
457
+ };
458
+ return n(o.parse(e)), [...t];
459
+ }
460
+ buildNameSubstitutions(e) {
461
+ let t = i.camelCase(e);
462
+ return {
463
+ nameCamelCase: t,
464
+ nameKebabCase: i.kebabCase(e),
465
+ namePascalCase: i.upperFirst(t),
466
+ nameSnakeCase: i.snakeCase(e)
467
+ };
468
+ }
469
+ renderContent(e) {
470
+ return this.assertEverySubstitutionSupplied({
471
+ subject: e.subject ?? "a template file",
472
+ substitutions: e.substitutions,
473
+ template: e.templateContent
474
+ }), o.render(e.templateContent, e.substitutions, {}, L);
475
+ }
476
+ renderPath(e) {
477
+ return this.assertEverySubstitutionSupplied({
478
+ subject: e.subject ?? e.templatePath,
479
+ substitutions: e.substitutions,
480
+ template: e.templatePath
481
+ }), o.render(e.templatePath, e.substitutions, {}, L);
482
+ }
483
+ };
484
+ z = k([n(), O("design:paramtypes", [])], z);
485
+ //#endregion
486
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/rendering/rendering.module.ts
487
+ var B = class {};
488
+ B = k([r({
489
+ controllers: [],
490
+ exports: [z],
491
+ imports: [],
492
+ providers: [z]
493
+ })], B);
494
+ //#endregion
495
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/template-discovery/template-discovery.service.ts
496
+ var V = class {
497
+ renderingService;
498
+ constructor(e) {
499
+ this.renderingService = e;
500
+ }
501
+ collectFilePaths(e) {
502
+ if (!s.existsSync(e)) return [];
503
+ let n = [], r = [e];
504
+ for (; r.length > 0;) {
505
+ let e = r.pop();
506
+ if (e !== void 0) for (let i of s.readdirSync(e, { withFileTypes: !0 })) {
507
+ let a = t.join(e, i.name);
508
+ i.isDirectory() ? r.push(a) : i.isFile() && n.push(a);
509
+ }
510
+ }
511
+ return n.toSorted();
512
+ }
513
+ collectTemplate(e) {
514
+ return {
515
+ directoryPath: e.templatePath,
516
+ filePaths: this.collectFilePaths(e.templatePath),
517
+ name: e.name,
518
+ ...e.threshold === void 0 ? {} : { threshold: e.threshold }
519
+ };
520
+ }
521
+ collectTemplates(e) {
522
+ return e.configuration.map((n) => this.collectTemplate({
523
+ name: n.name,
524
+ templatePath: t.resolve(e.workingDirectory, n.templatePath),
525
+ threshold: n.threshold
526
+ }));
527
+ }
528
+ countMatchingFiles(e) {
529
+ let t = e.fileScope === void 0 ? void 0 : new Set(e.fileScope);
530
+ return e.template.filePaths.filter((n) => {
531
+ let r = this.resolveInstanceFilePath({
532
+ instancePath: e.instancePath,
533
+ substitutions: e.substitutions,
534
+ templateDirectoryPath: e.template.directoryPath,
535
+ templateFilePath: n
536
+ });
537
+ return t !== void 0 && !t.has(r) ? !1 : s.existsSync(r);
538
+ }).length;
539
+ }
540
+ prepareDocument(e) {
541
+ let n = this.resolveInstanceFilePath(e);
542
+ if (s.existsSync(n)) return {
543
+ filename: t.basename(n),
544
+ instance: s.readFileSync(n, "utf8"),
545
+ instanceFilePath: n,
546
+ renderedTemplate: this.renderingService.renderContent({
547
+ subject: e.templateFilePath,
548
+ substitutions: e.substitutions,
549
+ templateContent: s.readFileSync(e.templateFilePath, "utf8")
550
+ }),
551
+ templateFilePath: e.templateFilePath
552
+ };
553
+ }
554
+ resolveInstanceFilePath(e) {
555
+ return t.join(e.instancePath, this.renderingService.renderPath({
556
+ subject: e.templateFilePath,
557
+ substitutions: e.substitutions,
558
+ templatePath: t.relative(e.templateDirectoryPath, e.templateFilePath)
559
+ }));
560
+ }
561
+ };
562
+ V = k([n(), O("design:paramtypes", [z === void 0 ? Object : z])], V);
563
+ //#endregion
564
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/template-discovery/template-discovery.module.ts
565
+ var H = class {};
566
+ H = k([r({
567
+ controllers: [],
568
+ exports: [V],
569
+ imports: [B],
570
+ providers: [V]
571
+ })], H);
572
+ //#endregion
573
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/instance-discovery/instance-discovery.constants.ts
574
+ var U = /\.[^.]+$/u, W = [
575
+ "*",
576
+ "?",
577
+ "]"
578
+ ], G = class {
579
+ constructor() {}
580
+ deriveLocationSubstitutions(e) {
581
+ let [n = ""] = t.relative(e.workingDirectory, t.join(e.instancePath, e.nameStem)).split(t.sep);
582
+ return n === "" || n === ".." ? {} : { type: n };
583
+ }
584
+ resolveGlobSuffix(e) {
585
+ let n = t.basename(e), r = Math.max(...W.map((e) => n.lastIndexOf(e)));
586
+ return r === -1 ? "" : n.slice(r + 1);
587
+ }
588
+ resolveNameStem(e) {
589
+ return e.isDirectory ? e.entryName : e.suffix !== "" && e.entryName.endsWith(e.suffix) ? e.entryName.slice(0, -e.suffix.length) : e.entryName.replace(U, "");
590
+ }
591
+ findInstances(e) {
592
+ let n = /* @__PURE__ */ new Map();
593
+ for (let r of e.patterns) {
594
+ let i = this.resolveGlobSuffix(r);
595
+ for (let a of s.globSync(r, {
596
+ cwd: e.workingDirectory,
597
+ withFileTypes: !0
598
+ })) {
599
+ let e = a.isDirectory(), r = t.join(a.parentPath, a.name), o = [
600
+ a.parentPath,
601
+ this.resolveNameStem({
602
+ entryName: a.name,
603
+ isDirectory: e,
604
+ suffix: i
605
+ }),
606
+ e ? "directory" : "file"
607
+ ].join("\0");
608
+ if (e) {
609
+ n.set(o, void 0);
610
+ continue;
611
+ }
612
+ let s = n.get(o) ?? /* @__PURE__ */ new Set();
613
+ s.add(r), n.set(o, s);
614
+ }
615
+ }
616
+ return [...n.entries()].toSorted(([e], [t]) => e.localeCompare(t)).map(([t, n]) => {
617
+ let [r = "", i = ""] = t.split("\0");
618
+ return {
619
+ ...n === void 0 ? {} : { fileScope: [...n].toSorted() },
620
+ nameStem: i,
621
+ path: r,
622
+ substitutions: {
623
+ ...this.deriveLocationSubstitutions({
624
+ instancePath: r,
625
+ nameStem: i,
626
+ workingDirectory: e.workingDirectory
627
+ }),
628
+ ...e.substitutions
629
+ },
630
+ ...e.threshold === void 0 ? {} : { threshold: e.threshold }
631
+ };
632
+ });
633
+ }
634
+ };
635
+ G = k([n(), O("design:paramtypes", [])], G);
636
+ //#endregion
637
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/instance-discovery/instance-discovery-matching.service.ts
638
+ var K = class {
639
+ templateDiscoveryService;
640
+ renderingService;
641
+ constructor(e, t) {
642
+ this.templateDiscoveryService = e, this.renderingService = t;
643
+ }
644
+ compareMatches(e, t) {
645
+ return e.matchRatio === t.matchRatio ? e.matchedFileCount === t.matchedFileCount ? e.template.name.localeCompare(t.template.name) : t.matchedFileCount - e.matchedFileCount : t.matchRatio - e.matchRatio;
646
+ }
647
+ buildSubstitutions(e) {
648
+ return {
649
+ ...this.renderingService.buildNameSubstitutions(e.nameStem),
650
+ name: e.nameStem,
651
+ ...e.substitutions
652
+ };
653
+ }
654
+ matchInstances(e) {
655
+ let t = [], n = [];
656
+ for (let r of e.instances) {
657
+ let i = this.buildSubstitutions(r), a = this.matchTemplates({
658
+ instance: r,
659
+ substitutions: i,
660
+ templates: e.templates
661
+ }), o = a[0];
662
+ if (o === void 0) {
663
+ n.push({
664
+ instance: r,
665
+ reason: "no-match",
666
+ tiedTemplateNames: []
667
+ });
668
+ continue;
669
+ }
670
+ let s = a.filter((e) => e.matchRatio === o.matchRatio && e.matchedFileCount === o.matchedFileCount);
671
+ if (s.length > 1 && o.matchRatio < 1) {
672
+ n.push({
673
+ instance: r,
674
+ reason: "ambiguous",
675
+ tiedTemplateNames: s.map((e) => e.template.name)
676
+ });
677
+ continue;
678
+ }
679
+ t.push(...s.map((e) => ({
680
+ instance: r,
681
+ matchedFileCount: e.matchedFileCount,
682
+ substitutions: i,
683
+ template: e.template
684
+ })));
685
+ }
686
+ return {
687
+ matched: t,
688
+ unmatched: n
689
+ };
690
+ }
691
+ matchTemplates(e) {
692
+ return e.templates.map((t) => {
693
+ let n = this.templateDiscoveryService.countMatchingFiles({
694
+ fileScope: e.instance.fileScope,
695
+ instancePath: e.instance.path,
696
+ substitutions: e.substitutions,
697
+ template: t
698
+ });
699
+ return {
700
+ matchedFileCount: n,
701
+ matchRatio: n / t.filePaths.length,
702
+ template: t
703
+ };
704
+ }).filter((e) => e.matchedFileCount > 0 && e.matchRatio > 0).toSorted((e, t) => this.compareMatches(e, t));
705
+ }
706
+ };
707
+ K = k([n(), O("design:paramtypes", [V === void 0 ? Object : V, z === void 0 ? Object : z])], K);
708
+ //#endregion
709
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/instance-discovery/instance-discovery.service.ts
710
+ var q = class {
711
+ instanceDiscoveryLocatingService;
712
+ instanceDiscoveryMatchingService;
713
+ instanceGroupService;
714
+ templateDiscoveryService;
715
+ constructor(e, t, n, r) {
716
+ this.instanceDiscoveryLocatingService = e, this.instanceDiscoveryMatchingService = t, this.instanceGroupService = n, this.templateDiscoveryService = r;
717
+ }
718
+ weighInstance(e) {
719
+ let t = this.instanceDiscoveryMatchingService.matchTemplates({
720
+ instance: e.instance,
721
+ substitutions: this.instanceDiscoveryMatchingService.buildSubstitutions(e.instance),
722
+ templates: e.templates
723
+ }).map((e) => ({
724
+ matchedFileCount: e.matchedFileCount,
725
+ matchRatio: e.matchRatio,
726
+ name: e.template.name,
727
+ templateFileCount: e.template.filePaths.length
728
+ }));
729
+ return {
730
+ instance: e.instance,
731
+ pairings: t
732
+ };
733
+ }
734
+ buildSubstitutions(e) {
735
+ return this.instanceDiscoveryMatchingService.buildSubstitutions(e);
736
+ }
737
+ findInstances(e) {
738
+ return this.instanceDiscoveryLocatingService.findInstances(e);
739
+ }
740
+ matchInstances(e) {
741
+ return this.instanceDiscoveryMatchingService.matchInstances(e);
742
+ }
743
+ matchTemplates(e) {
744
+ return this.instanceDiscoveryMatchingService.matchTemplates(e);
745
+ }
746
+ prepareDocuments(e) {
747
+ let n = new Set(e.fileExtensions);
748
+ return e.instances.map((e) => ({
749
+ documents: e.template.filePaths.filter((e) => n.has(t.extname(e))).map((t) => this.templateDiscoveryService.prepareDocument({
750
+ instancePath: e.instance.path,
751
+ substitutions: e.substitutions,
752
+ templateDirectoryPath: e.template.directoryPath,
753
+ templateFilePath: t
754
+ })).filter((e) => e !== void 0),
755
+ instance: e
756
+ }));
757
+ }
758
+ readWorkspaceGroups(e) {
759
+ return e.filter((e) => !this.instanceGroupService.isProjectScoped(e));
760
+ }
761
+ resolveInstanceFiles(e) {
762
+ return e.flatMap((e) => e.template.filePaths.map((t) => ({
763
+ instance: e,
764
+ instanceFilePath: this.templateDiscoveryService.resolveInstanceFilePath({
765
+ instancePath: e.instance.path,
766
+ substitutions: e.substitutions,
767
+ templateDirectoryPath: e.template.directoryPath,
768
+ templateFilePath: t
769
+ }),
770
+ templateFilePath: t
771
+ })));
772
+ }
773
+ resolveInventoriedInstances(e) {
774
+ let { templates: n, weighed: r } = this.takeInventory(e), i = new Set(e.templateNames ?? n.map((e) => e.name));
775
+ return r.map(({ instance: e, pairings: n }) => ({
776
+ path: t.join(e.path, e.nameStem),
777
+ templates: n.filter((e) => i.has(e.name))
778
+ })).filter((e) => e.templates.length > 0);
779
+ }
780
+ resolveInventoriedTemplates(e) {
781
+ let { weighed: n } = this.takeInventory(e), r = e.instancePatterns !== void 0;
782
+ return e.configuration.filter((t) => e.templateNames === void 0 || e.templateNames.includes(t.name)).map((e) => {
783
+ let r = n.flatMap(({ instance: n, pairings: r }) => {
784
+ let i = r.find((t) => t.name === e.name);
785
+ return i === void 0 ? [] : [{
786
+ ...i,
787
+ name: t.join(n.path, n.nameStem)
788
+ }];
789
+ });
790
+ return {
791
+ description: e.description ?? "",
792
+ instances: r,
793
+ name: e.name,
794
+ templatePath: e.templatePath
795
+ };
796
+ }).filter((e) => !r || e.instances.length > 0);
797
+ }
798
+ takeInventory(e) {
799
+ let t = this.templateDiscoveryService.collectTemplates({
800
+ configuration: e.configuration,
801
+ workingDirectory: e.workingDirectory
802
+ }), n = e.instancePatterns ?? e.configuration.flatMap((e) => this.readWorkspaceGroups(e.instances).flatMap((e) => e.patterns ?? []));
803
+ return {
804
+ templates: t,
805
+ weighed: this.findInstances({
806
+ patterns: n,
807
+ workingDirectory: e.workingDirectory
808
+ }).map((e) => this.weighInstance({
809
+ instance: e,
810
+ templates: t
811
+ }))
812
+ };
813
+ }
814
+ };
815
+ q = k([n(), O("design:paramtypes", [
816
+ G === void 0 ? Object : G,
817
+ K === void 0 ? Object : K,
818
+ F === void 0 ? Object : F,
819
+ V === void 0 ? Object : V
820
+ ])], q);
821
+ //#endregion
822
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/instance-discovery/instance-discovery.module.ts
823
+ var J = class {};
824
+ J = k([r({
825
+ controllers: [],
826
+ exports: [
827
+ G,
828
+ K,
829
+ q,
830
+ H
831
+ ],
832
+ imports: [
833
+ I,
834
+ B,
835
+ H
836
+ ],
837
+ providers: [
838
+ G,
839
+ K,
840
+ q
841
+ ]
842
+ })], J);
843
+ //#endregion
844
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/configuration/configuration.service.ts
845
+ var Y = class {
846
+ inputPromptingService;
847
+ inputService;
848
+ instanceDiscoveryService;
849
+ instanceGroupService;
850
+ renderingService;
851
+ templateDiscoveryService;
852
+ constructor(e, t, n, r, i, a) {
853
+ this.inputPromptingService = e, this.inputService = t, this.instanceDiscoveryService = n, this.instanceGroupService = r, this.renderingService = i, this.templateDiscoveryService = a;
854
+ }
855
+ applyGeneratorDefaults(e) {
856
+ return {
857
+ ...e.description === void 0 ? {} : { description: e.description },
858
+ inputs: e.inputs ?? {},
859
+ instances: e.instances ?? [],
860
+ name: e.name,
861
+ templatePath: e.templatePath,
862
+ ...e.threshold === void 0 ? {} : { threshold: e.threshold }
863
+ };
864
+ }
865
+ async findWorkspaceRoot() {
866
+ let e = t.resolve(process.cwd());
867
+ for (;;) try {
868
+ return await l(t.join(e, w)), e;
869
+ } catch {
870
+ let n = t.dirname(e);
871
+ if (n === e) return;
872
+ e = n;
873
+ }
874
+ }
875
+ async loadConfigurationModule(e) {
876
+ if (e.extension === ".json" || e.extension === ".jsonc") return this.loadJsonConfiguration(e);
877
+ let t = await f(d(import.meta.url)).import(e.configurationPath, { default: !0 });
878
+ if (typeof t != "object" || !t) return [];
879
+ let n = t.default;
880
+ return typeof n == "object" && n ? n : t;
881
+ }
882
+ async loadJsonConfiguration(e) {
883
+ let t = await u(e.configurationPath, "utf8");
884
+ return e.extension === ".jsonc" ? p(t) : JSON.parse(t);
885
+ }
886
+ async resolveConfigurationPath(e) {
887
+ let n = t.resolve(e);
888
+ if (c(n)) return n;
889
+ let r = await this.findWorkspaceRoot();
890
+ if (r === void 0) return n;
891
+ let i = t.resolve(r, e);
892
+ return c(i) ? i : n;
893
+ }
894
+ buildNameSubstitutions(e) {
895
+ return this.renderingService.buildNameSubstitutions(e);
896
+ }
897
+ collectTemplate(e) {
898
+ return this.templateDiscoveryService.collectTemplate(e);
899
+ }
900
+ collectTemplates(e) {
901
+ return this.templateDiscoveryService.collectTemplates(e);
902
+ }
903
+ findInstances(e) {
904
+ return this.instanceDiscoveryService.findInstances(e);
905
+ }
906
+ isAtTerminal() {
907
+ return this.inputPromptingService.isAtTerminal();
908
+ }
909
+ isProjectScoped(e) {
910
+ return this.instanceGroupService.isProjectScoped(e);
911
+ }
912
+ async loadConformetryConfiguration(e) {
913
+ let n = await this.resolveConfigurationPath(e), r = t.extname(n).toLowerCase();
914
+ if (!T.has(r)) throw new C(n);
915
+ let i = await this.loadConfigurationModule({
916
+ configurationPath: n,
917
+ extension: r
918
+ });
919
+ return D.parse(i).map((e) => this.applyGeneratorDefaults(e));
920
+ }
921
+ matchInstances(e) {
922
+ return this.instanceDiscoveryService.matchInstances(e);
923
+ }
924
+ parseCommaDelimitedOption(e) {
925
+ return this.inputService.parseCommaDelimitedOption(e);
926
+ }
927
+ parseOptionalOption(e) {
928
+ return this.inputService.parseOptionalOption(e);
929
+ }
930
+ parseThresholdOption(e) {
931
+ return this.inputService.parseThresholdOption(e);
932
+ }
933
+ prepareDocuments(e) {
934
+ return this.instanceDiscoveryService.prepareDocuments(e);
935
+ }
936
+ async promptForTemplate(e) {
937
+ return this.inputPromptingService.promptForTemplate(e);
938
+ }
939
+ async promptForTemplates(e) {
940
+ return this.inputPromptingService.promptForTemplates(e);
941
+ }
942
+ readWorkspaceGroups(e) {
943
+ return this.instanceDiscoveryService.readWorkspaceGroups(e);
944
+ }
945
+ renderContent(e) {
946
+ return this.renderingService.renderContent(e);
947
+ }
948
+ renderPath(e) {
949
+ return this.renderingService.renderPath(e);
950
+ }
951
+ async resolveGeneratorInputs(e) {
952
+ return this.inputService.resolveGeneratorInputs(e);
953
+ }
954
+ resolveInstanceFiles(e) {
955
+ return this.instanceDiscoveryService.resolveInstanceFiles(e);
956
+ }
957
+ resolveInventoriedInstances(e) {
958
+ return this.instanceDiscoveryService.resolveInventoriedInstances(e);
959
+ }
960
+ resolveInventoriedTemplates(e) {
961
+ return this.instanceDiscoveryService.resolveInventoriedTemplates(e);
962
+ }
963
+ };
964
+ Y = k([n(), O("design:paramtypes", [
965
+ M === void 0 ? Object : M,
966
+ N === void 0 ? Object : N,
967
+ q === void 0 ? Object : q,
968
+ F === void 0 ? Object : F,
969
+ z === void 0 ? Object : z,
970
+ V === void 0 ? Object : V
971
+ ])], Y);
972
+ //#endregion
973
+ //#region packages/ic-suite/conformetry/conformetry-configuration/src/modules/configuration/configuration.module.ts
974
+ var X = class {};
975
+ X = k([r({
976
+ controllers: [],
977
+ exports: [Y],
978
+ imports: [
979
+ P,
980
+ J,
981
+ I,
982
+ B,
983
+ H
984
+ ],
985
+ providers: [Y]
986
+ })], X);
987
+ //#endregion
988
+ export { h as ALL_TEMPLATES_SELECTION, X as ConfigurationModule, Y as ConfigurationService, _ as InputError, R as MissingSubstitutionError, C as UnknownConfigurationFileTypeError, v as missingInputError };