bdy 1.26.3-stage → 1.26.5-beta

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.
Files changed (35) hide show
  1. package/distTs/package.json +4 -3
  2. package/distTs/src/api/client.js +40 -0
  3. package/distTs/src/cliIndex.js +2 -0
  4. package/distTs/src/command/login.js +1 -0
  5. package/distTs/src/command/ruleset/create.js +32 -0
  6. package/distTs/src/command/ruleset/delete.js +33 -0
  7. package/distTs/src/command/ruleset/get.js +67 -0
  8. package/distTs/src/command/ruleset/list.js +46 -0
  9. package/distTs/src/command/ruleset/ruleset.js +58 -0
  10. package/distTs/src/command/ruleset/update.js +35 -0
  11. package/distTs/src/command/ruleset.js +20 -0
  12. package/distTs/src/command/yaml/actions/info.js +8 -3
  13. package/distTs/src/command/yaml/actions/list.js +10 -11
  14. package/distTs/src/command/yaml/actions/schema.js +1 -0
  15. package/distTs/src/command/yaml/agent.js +13 -0
  16. package/distTs/src/command/yaml/customAction.js +13 -0
  17. package/distTs/src/command/yaml/distro.js +13 -0
  18. package/distTs/src/command/yaml/documentType.js +64 -0
  19. package/distTs/src/command/yaml/domain.js +13 -0
  20. package/distTs/src/command/yaml/environment.js +14 -0
  21. package/distTs/src/command/yaml/pipeline.js +49 -2
  22. package/distTs/src/command/yaml/render.js +30 -11
  23. package/distTs/src/command/yaml/rooted.js +48 -0
  24. package/distTs/src/command/yaml/sandbox.js +13 -0
  25. package/distTs/src/command/yaml/schemaUtils.js +45 -100
  26. package/distTs/src/command/yaml/targets/info.js +84 -0
  27. package/distTs/src/command/yaml/targets/list.js +60 -0
  28. package/distTs/src/command/yaml/targets/schema.js +29 -0
  29. package/distTs/src/command/yaml/targets.js +15 -0
  30. package/distTs/src/command/yaml/validate.js +238 -287
  31. package/distTs/src/command/yaml.js +22 -0
  32. package/distTs/src/output.js +43 -0
  33. package/distTs/src/texts.js +293 -49
  34. package/distTs/src/utils.js +2 -0
  35. package/package.json +4 -3
@@ -5,112 +5,61 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fs_1 = require("fs");
7
7
  const path_1 = require("path");
8
+ const commander_1 = require("commander");
8
9
  const utils_1 = require("../../utils");
9
10
  const output_1 = __importDefault(require("../../output"));
10
11
  const texts_1 = require("../../texts");
11
12
  const schemaUtils_1 = require("./schemaUtils");
13
+ const documentType_1 = require("./documentType");
12
14
  // Valid top-level keys the schema's PipelineYaml.properties omits (everything
13
15
  // else is derived from the schema, so this only covers genuine gaps).
14
16
  const SCHEMA_OMITTED_TOP_LEVEL_KEYS = ['git_config_ref'];
15
17
  const LABEL_KEYS = ['action', 'target', 'name', 'key', 'title', 'trigger_condition'];
16
- function stripRef(ref) {
17
- return ref.replace('#/$defs/', '');
18
- }
19
18
  function prettifyKey(key) {
20
19
  return key
21
20
  .split('_')
22
21
  .map((w) => w.charAt(0).toUpperCase() + w.slice(1))
23
22
  .join(' ');
24
23
  }
25
- function recordError(context, path, message) {
26
- context.errors.push({
27
- pipeline: context.pipeline,
28
- path: path.replace(/\.(\d+)(?=\.|$)/g, '[$1]'),
29
- message,
30
- });
24
+ function isObject(value) {
25
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
31
26
  }
32
- function outputErrors(result, indent, context, prefix = '') {
33
- for (const err of result.errors) {
34
- recordError(context, [prefix, err.path].filter(Boolean).join('.'), err.message);
35
- context.output.muted(`${indent}${err.path}: ${err.message}`);
36
- }
27
+ function isTopLevelKey(finding) {
28
+ return finding.code === 'unknownKey' && finding.at.length === 2;
37
29
  }
38
- function valueType(value) {
39
- if (value === null)
40
- return 'null';
41
- if (Array.isArray(value))
42
- return 'array';
43
- return typeof value;
30
+ function typeFindingAt(findings, depth) {
31
+ return findings.find((finding) => (finding.code === 'missingType' || finding.code === 'unknownType') &&
32
+ finding.at.length === depth);
44
33
  }
45
- function reportWrongContainer(header, expected, value, context, path) {
46
- recordError(context, path, (0, texts_1.ERR_YAML_MUST_BE)(expected, valueType(value)));
47
- context.output.error(header);
48
- context.output.muted(` ${(0, texts_1.ERR_YAML_MUST_BE)(expected, valueType(value))}`);
49
- return 1;
34
+ function applyPolicy(findings) {
35
+ return findings.filter((finding) => !(isTopLevelKey(finding) && SCHEMA_OMITTED_TOP_LEVEL_KEYS.includes(finding.key)));
50
36
  }
51
- // Array whose items reference a def — directly, or via a single $ref branch in
52
- // items.oneOf (how `targets` mixes a string ref with an object form).
53
- function itemRef(prop) {
54
- if (!prop || prop.type !== 'array' || !prop.items)
55
- return null;
56
- if (prop.items.$ref)
57
- return { defName: stripRef(prop.items.$ref), scalar: null };
58
- if (Array.isArray(prop.items.oneOf)) {
59
- const refs = prop.items.oneOf.filter((b) => b && b.$ref);
60
- const scalars = prop.items.oneOf.filter((b) => b && !b.$ref);
61
- if (refs.length === 1) {
62
- return {
63
- defName: stripRef(refs[0].$ref),
64
- scalar: scalars.length === 1 ? scalars[0] : null,
65
- };
66
- }
67
- }
68
- return null;
37
+ function pathOf(segments) {
38
+ return segments
39
+ .map((segment, index) => typeof segment === 'number' ? `[${segment}]` : index === 0 ? segment : `.${segment}`)
40
+ .join('');
69
41
  }
70
- function classifySections(props) {
71
- const itemSections = [];
72
- const objectSections = [];
73
- const settingsProps = {};
74
- for (const [key, prop] of Object.entries(props)) {
75
- const ref = itemRef(prop);
76
- if (ref) {
77
- itemSections.push({ key, ...ref });
78
- }
79
- else if (prop && prop.$ref) {
80
- objectSections.push({ key, defName: stripRef(prop.$ref), scalar: null });
81
- }
82
- else {
83
- settingsProps[key] = prop;
84
- }
85
- }
86
- return { itemSections, objectSections, settingsProps };
42
+ function messageOf(finding) {
43
+ if (finding.code === 'syntax')
44
+ return (0, texts_1.ERR_YAML_PARSE)(finding.message);
45
+ return finding.message;
87
46
  }
88
- async function unionMappings(defs) {
89
- const scope = (0, schemaUtils_1.pipelinesScope)();
90
- const unions = new Map();
91
- for (const [name, def] of Object.entries(defs)) {
92
- if (!def.oneOf || !def.discriminator)
93
- continue;
94
- const body = (await scope.def(name))?.body;
95
- if (body?.kind !== 'union')
96
- continue;
97
- const { property, members } = body.discriminator ?? {};
98
- if (property && members)
99
- unions.set(name, { property, members });
100
- }
101
- return unions;
47
+ function codeFrame(content, line, column) {
48
+ const lines = content.split(/\r?\n/);
49
+ const shown = [lines[line - 1] ?? ''];
50
+ if (line > 1 && shown[0].slice(0, column - 1).trim() === '')
51
+ shown.unshift(lines[line - 2]);
52
+ if (!shown.some((text) => text.trim() !== ''))
53
+ return '';
54
+ const indent = shown[shown.length - 1].slice(0, column - 1).replace(/[^\t]/g, ' ');
55
+ return `:\n\n${shown.join('\n')}\n${indent}^`;
102
56
  }
103
- // Resolve a discriminated def to its concrete branch by the item's `type`, so
104
- // AJV always validates against a single def (never a oneOf). Plain defs pass through.
105
- function resolveDef(unions, refName, value) {
106
- const union = unions.get(refName);
107
- if (!union)
108
- return { defName: refName };
109
- const dv = value?.[union.property];
110
- if (dv === undefined || dv === null)
111
- return { missingType: union.property };
112
- const branch = Object.hasOwn(union.members, dv) ? union.members[dv] : undefined;
113
- return branch ? { defName: branch } : { unknownType: String(dv), prop: union.property };
57
+ function issueOf(finding) {
58
+ return {
59
+ pipeline: Number(finding.at[0]) + 1,
60
+ path: pathOf(finding.at.slice(1)),
61
+ message: messageOf(finding),
62
+ };
114
63
  }
115
64
  function itemLabel(value, index, discProp) {
116
65
  let name;
@@ -126,230 +75,221 @@ function itemLabel(value, index, discProp) {
126
75
  name = `#${index + 1}`;
127
76
  return discProp && value && value[discProp] ? `${name} (${value[discProp]})` : name;
128
77
  }
129
- function validateScalarItem(item, section, context, path) {
130
- // Null errors even where the section takes a string: coerceTypes cannot fix a root scalar.
131
- if (item === null || !section.scalar) {
132
- recordError(context, path, (0, texts_1.ERR_YAML_MUST_BE)('object', valueType(item)));
133
- context.output.error(` ${String(item)}: ${(0, texts_1.ERR_YAML_MUST_BE)('object', valueType(item))}`);
134
- return 1;
78
+ function printLines(output, findings, depth, indent) {
79
+ for (const finding of findings) {
80
+ const path = pathOf(finding.at.slice(depth));
81
+ const line = `${indent}${path ? `${path}: ` : ''}${messageOf(finding)}`;
82
+ if (finding.severity === 'error')
83
+ output.muted(line);
84
+ else
85
+ output.yellow(line);
135
86
  }
136
- const result = (0, schemaUtils_1.validateAgainstSchema)({ ...section.scalar, $defs: (0, schemaUtils_1.loadRawSchema)().$defs }, item);
137
- if (result.valid) {
138
- context.output.green(` ${String(item)}`);
139
- return 0;
140
- }
141
- context.output.error(` ${String(item)}`);
142
- outputErrors(result, ' ', context, path);
143
- return result.errors.length;
144
87
  }
145
- function validateItemSection(unions, items, section, context) {
146
- const refName = section.defName;
147
- const discProp = unions.get(refName)?.property ?? null;
148
- let errors = 0;
149
- for (let i = 0; i < items.length; i++) {
150
- const item = items[i];
151
- const path = `${section.key}[${i}]`;
152
- if (item === null || typeof item !== 'object' || Array.isArray(item)) {
153
- errors += validateScalarItem(item, section, context, path);
154
- continue;
155
- }
156
- const label = itemLabel(item, i, discProp);
157
- const resolved = resolveDef(unions, refName, item);
158
- if ('missingType' in resolved) {
159
- recordError(context, `${path}.${resolved.missingType}`, (0, texts_1.ERR_YAML_ITEM_MISSING)(resolved.missingType));
160
- context.output.error(` ${label}: ${(0, texts_1.ERR_YAML_ITEM_MISSING)(resolved.missingType)}`);
161
- errors++;
162
- continue;
163
- }
164
- if ('unknownType' in resolved) {
165
- recordError(context, `${path}.${resolved.prop}`, (0, texts_1.ERR_YAML_ITEM_UNKNOWN)(resolved.prop, resolved.unknownType));
166
- context.output.error(` ${label}: ${(0, texts_1.ERR_YAML_ITEM_UNKNOWN)(resolved.prop, resolved.unknownType)}`);
167
- errors++;
168
- continue;
169
- }
170
- const result = (0, schemaUtils_1.validateAgainstDef)(resolved.defName, item);
171
- if (result.valid) {
172
- context.output.green(` ${label}`);
88
+ function printItems(output, items, findings, checked) {
89
+ items.forEach((item, index) => {
90
+ const own = findings.filter((finding) => finding.at[2] === index);
91
+ const failed = own.some((finding) => finding.severity === 'error');
92
+ if (!isObject(item)) {
93
+ const empty = own.find((finding) => finding.code === 'emptyItem' && finding.at.length === 3);
94
+ if (empty) {
95
+ output.error(` #${index + 1}: ${messageOf(empty)}`);
96
+ return;
97
+ }
98
+ const shape = own.find((finding) => finding.code === 'shape' && finding.at.length === 3);
99
+ if (shape) {
100
+ output.error(` ${String(item)}: ${messageOf(shape)}`);
101
+ return;
102
+ }
103
+ (failed ? output.error : output.green)(` ${String(item)}`);
104
+ printLines(output, own, 3, ' ');
105
+ return;
173
106
  }
174
- else {
175
- errors += result.errors.length;
176
- context.output.error(` ${label}`);
177
- outputErrors(result, ' ', context, path);
107
+ const type = typeFindingAt(own, 4);
108
+ const member = checked.find((node) => node.at[2] === index && node.at.length === 3)?.member;
109
+ const label = itemLabel(item, index, type?.key ?? member?.property ?? null);
110
+ if (type) {
111
+ output.error(` ${label}: ${messageOf(type)}`);
112
+ return;
178
113
  }
179
- }
180
- return errors;
114
+ (failed ? output.error : output.green)(` ${label}`);
115
+ printLines(output, own, 3, ' ');
116
+ });
181
117
  }
182
- function validateObjectSection(unions, label, value, refName, context, path) {
183
- const resolved = resolveDef(unions, refName, value);
184
- if ('missingType' in resolved) {
185
- context.output.error(label);
186
- recordError(context, `${path}.${resolved.missingType}`, (0, texts_1.ERR_YAML_ITEM_MISSING)(resolved.missingType));
187
- context.output.muted(` ${(0, texts_1.ERR_YAML_ITEM_MISSING)(resolved.missingType)}`);
188
- return 1;
118
+ function printSection(output, header, value, findings, checked) {
119
+ const container = findings.find((finding) => finding.code === 'shape' && finding.at.length === 2);
120
+ if (container) {
121
+ output.error(header);
122
+ output.muted(` ${messageOf(container)}`);
123
+ return;
189
124
  }
190
- if ('unknownType' in resolved) {
191
- context.output.error(label);
192
- recordError(context, `${path}.${resolved.prop}`, (0, texts_1.ERR_YAML_ITEM_UNKNOWN)(resolved.prop, resolved.unknownType));
193
- context.output.muted(` ${(0, texts_1.ERR_YAML_ITEM_UNKNOWN)(resolved.prop, resolved.unknownType)}`);
194
- return 1;
125
+ if (Array.isArray(value)) {
126
+ output.blue(header);
127
+ printItems(output, value, findings, checked);
128
+ return;
195
129
  }
196
- const result = (0, schemaUtils_1.validateAgainstDef)(resolved.defName, value);
197
- if (result.valid) {
198
- context.output.green(label);
199
- return 0;
130
+ const type = typeFindingAt(findings, 3);
131
+ if (type) {
132
+ output.error(header);
133
+ output.muted(` ${messageOf(type)}`);
134
+ return;
200
135
  }
201
- context.output.error(label);
202
- outputErrors(result, ' ', context, path);
203
- return result.errors.length;
136
+ const failed = findings.some((finding) => finding.severity === 'error');
137
+ (failed ? output.error : output.green)(header);
138
+ printLines(output, findings, 2, ' ');
204
139
  }
205
- function validateSinglePipeline(parsed, label, unions, context) {
206
- let errors = 0;
207
- const defs = (0, schemaUtils_1.getAllDefs)();
208
- const pipelineDef = defs['PipelineYaml'];
209
- const props = pipelineDef?.properties || {};
210
- const allKnownKeys = new Set([
211
- ...Object.keys(props),
212
- ...SCHEMA_OMITTED_TOP_LEVEL_KEYS,
213
- ]);
214
- const unknownKeys = Object.keys(parsed).filter((k) => !allKnownKeys.has(k));
215
- if (unknownKeys.length > 0) {
216
- for (const key of unknownKeys) {
217
- context.warnings.push({
218
- pipeline: context.pipeline,
219
- path: key,
220
- message: (0, texts_1.TXT_YAML_UNKNOWN_TOP_LEVEL_KEYS)(key),
221
- });
222
- }
223
- context.output.yellow(`${label}${(0, texts_1.TXT_YAML_UNKNOWN_TOP_LEVEL_KEYS)(unknownKeys.join(', '))}`);
224
- }
225
- const { itemSections, objectSections, settingsProps } = classifySections(props);
226
- if (Object.keys(settingsProps).length > 0) {
227
- const settingsData = {};
228
- for (const key of Object.keys(settingsProps)) {
229
- if (key in parsed)
230
- settingsData[key] = parsed[key];
231
- }
232
- const rawSchema = (0, schemaUtils_1.loadRawSchema)();
233
- const result = (0, schemaUtils_1.validateAgainstSchema)({
234
- type: 'object',
235
- properties: settingsProps,
236
- required: (pipelineDef?.required || []).filter((r) => r in settingsProps),
237
- $defs: rawSchema.$defs,
238
- }, settingsData);
239
- if (result.valid) {
240
- context.output.green(`${label}Pipeline settings`);
241
- }
242
- else {
243
- errors += result.errors.length;
244
- context.output.error(`${label}Pipeline settings`);
245
- outputErrors(result, ' ', context);
246
- }
247
- }
248
- const itemByKey = new Map(itemSections.map((s) => [s.key, s]));
249
- const objByKey = new Map(objectSections.map((s) => [s.key, s.defName]));
250
- for (const key of Object.keys(props)) {
251
- if (!(key in parsed))
252
- continue;
253
- const value = parsed[key];
254
- // A valueless section is an absent one: the backend drops the key on save.
255
- if (value === null)
140
+ function printPipeline(output, entry, label, findings, checked, sections) {
141
+ const settings = findings.filter((finding) => !sections.has(finding.at[1]));
142
+ const failed = settings.some((finding) => finding.severity === 'error');
143
+ (failed ? output.error : output.green)(`${label}${texts_1.TXT_YAML_PIPELINE_SETTINGS}`);
144
+ printLines(output, settings, 1, ' ');
145
+ for (const key of Object.keys(entry)) {
146
+ if (!sections.has(key) || entry[key] === null)
256
147
  continue;
257
- const header = `${label}${prettifyKey(key)}`;
258
- if (itemByKey.has(key)) {
259
- if (!Array.isArray(value)) {
260
- errors += reportWrongContainer(header, 'array', value, context, key);
261
- continue;
262
- }
263
- context.output.blue(header);
264
- errors += validateItemSection(unions, value, itemByKey.get(key), context);
265
- }
266
- else if (objByKey.has(key)) {
267
- if (typeof value !== 'object' || Array.isArray(value)) {
268
- errors += reportWrongContainer(header, 'object', value, context, key);
269
- continue;
270
- }
271
- errors += validateObjectSection(unions, header, value, objByKey.get(key), context, key);
272
- }
148
+ printSection(output, `${label}${prettifyKey(key)}`, entry[key], findings.filter((finding) => finding.at[1] === key), checked.filter((node) => node.at[1] === key));
273
149
  }
274
- return errors;
275
150
  }
276
- function validatePipelineFile(file, unions, json) {
277
- const context = {
278
- output: {
279
- normal: output_1.default.progress(json),
280
- error: output_1.default.progress(json, output_1.default.error.bind(output_1.default)),
281
- muted: output_1.default.progress(json, output_1.default.muted.bind(output_1.default)),
282
- green: output_1.default.progress(json, output_1.default.green.bind(output_1.default)),
283
- yellow: output_1.default.progress(json, output_1.default.yellow.bind(output_1.default)),
284
- blue: output_1.default.progress(json, output_1.default.blue.bind(output_1.default)),
285
- },
286
- pipeline: 0,
287
- errors: [],
288
- warnings: [],
289
- };
290
- const invalidDocument = (message) => {
291
- if (json) {
292
- recordError(context, '', message);
293
- output_1.default.json({
294
- valid: false,
295
- pipelines: 0,
296
- errors: context.errors,
297
- warnings: context.warnings,
298
- });
299
- output_1.default.exitCode(1);
300
- }
301
- output_1.default.exitError(message);
151
+ function namesType(property) {
152
+ if (property?.$ref)
153
+ return true;
154
+ const items = property?.type === 'array' ? property.items : undefined;
155
+ return Boolean(items?.$ref || items?.oneOf?.some((branch) => branch?.$ref));
156
+ }
157
+ async function sectionKeys() {
158
+ const raw = (await (0, schemaUtils_1.schemaClient)().raw('pipelines'));
159
+ const properties = raw?.$defs?.PipelineYaml?.properties ?? {};
160
+ return new Set(Object.keys(properties).filter((key) => namesType(properties[key])));
161
+ }
162
+ function printerOf(json) {
163
+ const verbatim = (print) => (text) => print(output_1.default.escapeTermKit(text));
164
+ return {
165
+ normal: verbatim(output_1.default.progress(json)),
166
+ error: verbatim(output_1.default.progress(json, output_1.default.error.bind(output_1.default))),
167
+ muted: verbatim(output_1.default.progress(json, output_1.default.muted.bind(output_1.default))),
168
+ green: verbatim(output_1.default.progress(json, output_1.default.green.bind(output_1.default))),
169
+ yellow: verbatim(output_1.default.progress(json, output_1.default.yellow.bind(output_1.default))),
170
+ blue: verbatim(output_1.default.progress(json, output_1.default.blue.bind(output_1.default))),
302
171
  };
303
- const filePath = (0, path_1.resolve)(file);
304
- if (!(0, fs_1.existsSync)(filePath)) {
305
- invalidDocument((0, texts_1.ERR_YAML_VALIDATE_FILE_NOT_FOUND)(file));
172
+ }
173
+ function invalidDocument(type, json, message) {
174
+ if (json) {
175
+ output_1.default.json(type === 'pipeline'
176
+ ? { valid: false, pipelines: 0, errors: [{ pipeline: 0, path: '', message }], warnings: [] }
177
+ : { valid: false, type, entries: 0, errors: [{ entry: 0, path: '', message }], warnings: [] });
178
+ output_1.default.exitCode(1);
306
179
  }
307
- const YAML = require('yaml');
308
- const content = (0, fs_1.readFileSync)(filePath, 'utf8');
309
- let parsed;
310
- try {
311
- parsed = YAML.parse(content);
180
+ output_1.default.exitError(message);
181
+ }
182
+ function wholeDocumentMessage(type, finding, content, json, guessed) {
183
+ if (finding.code === 'document') {
184
+ const message = finding.expected === 'array'
185
+ ? (0, texts_1.ERR_YAML_DOCUMENT_LIST)(type)
186
+ : finding.got === 'array'
187
+ ? (0, texts_1.ERR_YAML_DOCUMENT_ONE_ENTRY)(type)
188
+ : (0, texts_1.ERR_YAML_DOCUMENT_KEYS_AT_TOP)(type);
189
+ return guessed && !json ? `${message}\n${texts_1.TXT_YAML_DOCUMENT_TYPE_HINT}` : message;
312
190
  }
313
- catch (e) {
314
- invalidDocument((0, texts_1.ERR_YAML_PARSE)(e.message));
191
+ if (finding.code === 'empty')
192
+ return (0, texts_1.ERR_YAML_DOCUMENT_EMPTY)(type);
193
+ const frame = !json && finding.code === 'syntax' && finding.line !== undefined && finding.column !== undefined
194
+ ? codeFrame(content, finding.line, finding.column)
195
+ : '';
196
+ return `${messageOf(finding)}${frame}`;
197
+ }
198
+ async function validated(type, content, json, guessed = false) {
199
+ const { validateYaml } = require('@buddy-works/docs-schema/validate');
200
+ const report = await validateYaml((0, schemaUtils_1.schemaClient)(), documentType_1.YAML_TYPES[type], content);
201
+ const findings = type === 'pipeline' ? applyPolicy(report.findings) : report.findings;
202
+ const whole = findings.find((finding) => finding.at.length === 0);
203
+ if (whole)
204
+ invalidDocument(type, json, wholeDocumentMessage(type, whole, content, json, guessed));
205
+ return { findings, checked: report.checked };
206
+ }
207
+ async function validatePipeline(content, json, guessed) {
208
+ const output = printerOf(json);
209
+ const { findings, checked } = await validated('pipeline', content, json, guessed);
210
+ const YAML = require('yaml');
211
+ const pipelines = YAML.parse(content);
212
+ const sections = await sectionKeys();
213
+ const errors = findings.filter((finding) => finding.severity === 'error').map(issueOf);
214
+ const warnings = findings.filter((finding) => finding.severity === 'warning').map(issueOf);
215
+ pipelines.forEach((pipeline, index) => {
216
+ const own = findings.filter((finding) => finding.at[0] === index);
217
+ if (!isObject(pipeline)) {
218
+ const entry = own.find((finding) => finding.at.length === 1);
219
+ if (entry)
220
+ output.error(` ${(0, texts_1.TXT_YAML_PIPELINE_ENTRY)(index + 1, messageOf(entry))}`);
221
+ return;
222
+ }
223
+ const label = pipelines.length > 1 ? `[${pipeline.pipeline || index + 1}] ` : '';
224
+ printPipeline(output, pipeline, label, own, checked.filter((node) => node.at[0] === index), sections);
225
+ });
226
+ if (json) {
227
+ output_1.default.json({
228
+ valid: errors.length === 0,
229
+ pipelines: pipelines.length,
230
+ errors,
231
+ warnings,
232
+ });
233
+ output_1.default.exitCode(errors.length > 0 ? 1 : 0);
315
234
  }
316
- if (!parsed || typeof parsed !== 'object') {
317
- invalidDocument(texts_1.ERR_YAML_NOT_ARRAY);
235
+ output.normal('');
236
+ if (errors.length > 0) {
237
+ if (guessed)
238
+ output.normal(texts_1.TXT_YAML_DOCUMENT_TYPE_HINT);
239
+ output_1.default.exitError((0, texts_1.ERR_YAML_VALIDATE_FAILED_COUNT)(errors.length));
318
240
  }
319
- if (!Array.isArray(parsed)) {
320
- invalidDocument(texts_1.ERR_YAML_PLAIN_OBJECT);
241
+ else {
242
+ output_1.default.exitSuccess(texts_1.TXT_YAML_VALIDATE_ALL_PASS);
321
243
  }
322
- if (parsed.length === 0) {
323
- invalidDocument(texts_1.ERR_YAML_EMPTY_ARRAY);
244
+ }
245
+ function entriesOf(type, parsed) {
246
+ if (Array.isArray(parsed)) {
247
+ const key = (0, documentType_1.identifierOf)(type);
248
+ return parsed.map((item, index) => {
249
+ const name = isObject(item) ? (key && item[key]) || item.name : undefined;
250
+ return { label: name ? String(name) : `#${index + 1}`, at: [index] };
251
+ });
324
252
  }
325
- let errors = 0;
326
- for (let i = 0; i < parsed.length; i++) {
327
- const pipeline = parsed[i];
328
- context.pipeline = i + 1;
329
- if (!pipeline || typeof pipeline !== 'object' || Array.isArray(pipeline)) {
330
- recordError(context, '', (0, texts_1.ERR_YAML_PIPELINE_NOT_OBJECT)(i + 1));
331
- context.output.error(` ${(0, texts_1.ERR_YAML_PIPELINE_NOT_OBJECT)(i + 1)}`);
332
- errors++;
333
- continue;
334
- }
335
- const pipelineLabel = parsed.length > 1 ? `[${pipeline.pipeline || i + 1}] ` : '';
336
- errors += validateSinglePipeline(pipeline, pipelineLabel, unions, context);
253
+ if (type === 'domain' && isObject(parsed)) {
254
+ return Object.keys(parsed).map((zone) => ({ label: zone, at: [zone] }));
337
255
  }
256
+ const name = isObject(parsed) ? parsed.name : undefined;
257
+ return [{ label: name ? String(name) : '#1', at: [] }];
258
+ }
259
+ async function validateDocument(type, content, json) {
260
+ const output = printerOf(json);
261
+ const { findings } = await validated(type, content, json);
262
+ const parsed = require('yaml').parse(content);
263
+ const entries = entriesOf(type, parsed);
264
+ if (entries.length === 0)
265
+ invalidDocument(type, json, (0, texts_1.ERR_YAML_DOCUMENT_EMPTY)(type));
266
+ const ownerOf = (finding) => Math.max(0, entries.findIndex((entry) => entry.at.every((segment, i) => finding.at[i] === segment)));
267
+ const issueOfFinding = (finding) => {
268
+ const owner = ownerOf(finding);
269
+ return {
270
+ entry: owner + 1,
271
+ path: pathOf(finding.at.slice(entries[owner].at.length)),
272
+ message: messageOf(finding),
273
+ };
274
+ };
275
+ const errors = findings.filter((finding) => finding.severity === 'error').map(issueOfFinding);
276
+ const warnings = findings.filter((finding) => finding.severity === 'warning').map(issueOfFinding);
338
277
  if (json) {
339
- output_1.default.json({
340
- valid: errors === 0,
341
- pipelines: parsed.length,
342
- errors: context.errors,
343
- warnings: context.warnings,
344
- });
345
- output_1.default.exitCode(errors > 0 ? 1 : 0);
278
+ output_1.default.json({ valid: errors.length === 0, type, entries: entries.length, errors, warnings });
279
+ output_1.default.exitCode(errors.length > 0 ? 1 : 0);
346
280
  }
347
- context.output.normal('');
348
- if (errors > 0) {
349
- output_1.default.exitError((0, texts_1.ERR_YAML_VALIDATE_FAILED_COUNT)(errors));
281
+ output.blue((0, texts_1.TXT_YAML_VALIDATED_AS)(type));
282
+ entries.forEach((entry, index) => {
283
+ const own = findings.filter((finding) => ownerOf(finding) === index);
284
+ (own.some((finding) => finding.severity === 'error') ? output.error : output.green)(entry.label);
285
+ printLines(output, own, entry.at.length, ' ');
286
+ });
287
+ output.normal('');
288
+ if (errors.length > 0) {
289
+ output_1.default.exitError((0, texts_1.ERR_YAML_DOCUMENT_FAILED_COUNT)(errors.length));
350
290
  }
351
291
  else {
352
- output_1.default.exitSuccess(texts_1.TXT_YAML_VALIDATE_ALL_PASS);
292
+ output_1.default.exitSuccess(texts_1.TXT_YAML_DOCUMENT_PASSED);
353
293
  }
354
294
  }
355
295
  const commandYamlValidate = (0, utils_1.newCommand)('validate', texts_1.DESC_COMMAND_YAML_VALIDATE, {
@@ -359,8 +299,19 @@ const commandYamlValidate = (0, utils_1.newCommand)('validate', texts_1.DESC_COM
359
299
  });
360
300
  commandYamlValidate.argument('<file>', texts_1.OPTION_YAML_VALIDATE_FILE);
361
301
  commandYamlValidate.option('--format <text|json>', texts_1.OPTION_FORMAT);
302
+ commandYamlValidate.addOption(new commander_1.Option('--type <type>', texts_1.OPTION_YAML_VALIDATE_TYPE).choices(Object.keys(documentType_1.YAML_TYPES)));
362
303
  commandYamlValidate.action(async (file, options) => {
363
- await (0, schemaUtils_1.ensureYamlSchema)();
364
- validatePipelineFile(file, await unionMappings((0, schemaUtils_1.getAllDefs)()), options.format === 'json');
304
+ const json = options.format === 'json';
305
+ const filePath = (0, path_1.resolve)(file);
306
+ const content = (0, fs_1.existsSync)(filePath) ? (0, fs_1.readFileSync)(filePath, 'utf8') : undefined;
307
+ const named = options.type ?? (0, documentType_1.detectYamlType)(content);
308
+ const type = named ?? 'pipeline';
309
+ if (content === undefined)
310
+ invalidDocument(type, json, (0, texts_1.ERR_YAML_VALIDATE_FILE_NOT_FOUND)(file));
311
+ await (0, schemaUtils_1.ensureYamlSchema)(documentType_1.YAML_TYPES[type]);
312
+ if (type === 'pipeline')
313
+ await validatePipeline(content, json, named === undefined);
314
+ else
315
+ await validateDocument(type, content, json);
365
316
  });
366
317
  exports.default = commandYamlValidate;
@@ -7,16 +7,38 @@ const utils_1 = require("../utils");
7
7
  const texts_1 = require("../texts");
8
8
  const pipeline_1 = __importDefault(require("./yaml/pipeline"));
9
9
  const actions_1 = __importDefault(require("./yaml/actions"));
10
+ const customAction_1 = __importDefault(require("./yaml/customAction"));
11
+ const environment_1 = __importDefault(require("./yaml/environment"));
12
+ const targets_1 = __importDefault(require("./yaml/targets"));
13
+ const sandbox_1 = __importDefault(require("./yaml/sandbox"));
14
+ const agent_1 = __importDefault(require("./yaml/agent"));
15
+ const domain_1 = __importDefault(require("./yaml/domain"));
16
+ const distro_1 = __importDefault(require("./yaml/distro"));
10
17
  const validate_1 = __importDefault(require("./yaml/validate"));
11
18
  const commandYaml = (0, utils_1.newCommand)('yaml', texts_1.DESC_COMMAND_YAML, {
12
19
  examples: [
13
20
  ...texts_1.EXAMPLE_YAML_PIPELINE,
14
21
  ...texts_1.EXAMPLE_YAML_ACTIONS_LIST,
15
22
  ...texts_1.EXAMPLE_YAML_ACTIONS_INFO,
23
+ ...texts_1.EXAMPLE_YAML_CUSTOM_ACTION,
24
+ ...texts_1.EXAMPLE_YAML_ENVIRONMENT,
25
+ ...texts_1.EXAMPLE_YAML_TARGETS_LIST,
26
+ ...texts_1.EXAMPLE_YAML_TARGETS_INFO,
27
+ ...texts_1.EXAMPLE_YAML_SANDBOX,
28
+ ...texts_1.EXAMPLE_YAML_AGENT,
29
+ ...texts_1.EXAMPLE_YAML_DOMAIN,
30
+ ...texts_1.EXAMPLE_YAML_DISTRO,
16
31
  ...texts_1.EXAMPLE_YAML_VALIDATE,
17
32
  ],
18
33
  });
19
34
  commandYaml.addCommand(pipeline_1.default);
20
35
  commandYaml.addCommand(actions_1.default);
36
+ commandYaml.addCommand(customAction_1.default);
37
+ commandYaml.addCommand(environment_1.default);
38
+ commandYaml.addCommand(targets_1.default);
39
+ commandYaml.addCommand(sandbox_1.default);
40
+ commandYaml.addCommand(agent_1.default);
41
+ commandYaml.addCommand(domain_1.default);
42
+ commandYaml.addCommand(distro_1.default);
21
43
  commandYaml.addCommand(validate_1.default);
22
44
  exports.default = commandYaml;