bdy 1.26.4-dev → 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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.26.4-dev",
4
+ "version": "1.26.5-beta",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "homepage": "https://buddy.works/docs/cli",
@@ -54,7 +54,7 @@
54
54
  ],
55
55
  "dependencies": {
56
56
  "@buddy-works/ci-info": "1.1.7",
57
- "@buddy-works/docs-schema": "0.1.3",
57
+ "@buddy-works/docs-schema": "0.1.4",
58
58
  "@fastify/accept-negotiator": "2.0.1",
59
59
  "@fastify/cors": "11.2.0",
60
60
  "@inquirer/prompts": "8.5.2",
@@ -97,7 +97,7 @@
97
97
  "uuid": "14.0.0",
98
98
  "which": "4.0.0",
99
99
  "ws": "8.21.0",
100
- "yaml": "2.8.3",
100
+ "yaml": "2.9.1",
101
101
  "zod": "4.6.7"
102
102
  },
103
103
  "devDependencies": {
@@ -56,6 +56,7 @@ commandYamlActionsInfo.action(async (type, options) => {
56
56
  common: (0, render_1.propertiesJson)(common),
57
57
  },
58
58
  types: (0, render_1.typesJson)(action.types),
59
+ outputs: action.outputs,
59
60
  examples: action.examples,
60
61
  });
61
62
  output_1.default.exitNormal();
@@ -66,19 +67,23 @@ commandYamlActionsInfo.action(async (type, options) => {
66
67
  output_1.default.gray(action.description);
67
68
  output_1.default.normal('');
68
69
  if (specific.length > 0) {
69
- output_1.default.blue('Type-specific fields');
70
+ output_1.default.blue(texts_1.TXT_YAML_TYPE_SPECIFIC_FIELDS);
70
71
  (0, render_1.outputProperties)(specific);
71
72
  output_1.default.normal('');
72
73
  }
73
74
  if (common.length > 0) {
74
- output_1.default.blue('Common fields');
75
+ output_1.default.blue(texts_1.TXT_YAML_COMMON_FIELDS);
75
76
  (0, render_1.outputProperties)(common);
76
77
  }
77
78
  if (Object.keys(action.types).length > 0) {
78
79
  output_1.default.normal('');
79
- output_1.default.blue('Types');
80
+ output_1.default.blue(texts_1.TXT_YAML_TYPES);
80
81
  (0, render_1.outputTypes)(action.types);
81
82
  }
83
+ if (action.outputs.variables.length > 0 || action.outputs.files.length > 0) {
84
+ output_1.default.normal('');
85
+ (0, render_1.outputOutputs)(action.outputs);
86
+ }
82
87
  if (action.examples.length > 0) {
83
88
  output_1.default.normal('');
84
89
  (0, render_1.outputExamples)(action.examples);
@@ -9,12 +9,11 @@ const texts_1 = require("../../../texts");
9
9
  const schema_1 = require("./schema");
10
10
  const schemaUtils_1 = require("../schemaUtils");
11
11
  const detect_1 = require("./detect");
12
+ const LIST_WRAP = { max: 80 };
12
13
  function actionTable(actions) {
13
14
  const data = [['TYPE', 'NAME', 'DESCRIPTION']];
14
- for (const action of actions) {
15
- const desc = action.description.length > 80 ? `${action.description.substring(0, 77)}...` : action.description;
16
- data.push([action.type, action.name, desc]);
17
- }
15
+ for (const action of actions)
16
+ data.push([action.type, action.name, action.description]);
18
17
  return data;
19
18
  }
20
19
  function actionJson(actions, detectedTypes) {
@@ -51,8 +50,8 @@ commandYamlActionsList.action(async (options) => {
51
50
  if (detectedTypes.size > 0) {
52
51
  const detected = actions.filter((a) => detectedTypes.has(a.type));
53
52
  output_1.default.normal('');
54
- output_1.default.blue('Suggested based on your repo');
55
- output_1.default.table(actionTable(detected));
53
+ output_1.default.blue(texts_1.TXT_YAML_ACTIONS_SUGGESTED_FROM_REPO);
54
+ output_1.default.wrappedTable(actionTable(detected), LIST_WRAP);
56
55
  }
57
56
  output_1.default.muted(texts_1.TXT_ACTION_USE_LIST);
58
57
  output_1.default.exitNormal();
@@ -62,7 +61,7 @@ commandYamlActionsList.action(async (options) => {
62
61
  output_1.default.json(actionJson(results, detectedTypes));
63
62
  }
64
63
  else {
65
- output_1.default.table(actionTable(results));
64
+ output_1.default.wrappedTable(actionTable(results), LIST_WRAP);
66
65
  }
67
66
  output_1.default.exitNormal();
68
67
  return;
@@ -75,12 +74,12 @@ commandYamlActionsList.action(async (options) => {
75
74
  }
76
75
  if (detectedTypes.size > 0) {
77
76
  const detected = actions.filter((a) => detectedTypes.has(a.type)).sort(byTypeName);
78
- output_1.default.blue('Suggested');
79
- output_1.default.table(actionTable(detected));
77
+ output_1.default.blue(texts_1.TXT_YAML_ACTIONS_SUGGESTED);
78
+ output_1.default.wrappedTable(actionTable(detected), LIST_WRAP);
80
79
  output_1.default.normal('');
81
80
  }
82
- output_1.default.blue(`All actions (${actions.length})`);
83
- output_1.default.table(actionTable([...actions].sort(byTypeName)));
81
+ output_1.default.blue((0, texts_1.TXT_YAML_ACTIONS_ALL)(actions.length));
82
+ output_1.default.wrappedTable(actionTable([...actions].sort(byTypeName)), LIST_WRAP);
84
83
  output_1.default.exitNormal();
85
84
  });
86
85
  exports.default = commandYamlActionsList;
@@ -16,6 +16,7 @@ const toActionDef = (member) => ({
16
16
  examples: member.examples,
17
17
  tags: member.tags,
18
18
  types: member.document?.types ?? {},
19
+ outputs: member.outputs,
19
20
  });
20
21
  let memCached = null;
21
22
  async function loadSchema() {
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const texts_1 = require("../../texts");
4
+ const schemaUtils_1 = require("./schemaUtils");
5
+ const rooted_1 = require("./rooted");
6
+ exports.default = (0, rooted_1.rootedYamlCommand)({
7
+ name: 'agent',
8
+ description: texts_1.DESC_COMMAND_YAML_AGENT,
9
+ examples: texts_1.EXAMPLE_YAML_AGENT,
10
+ document: 'agents',
11
+ scope: schemaUtils_1.agentsScope,
12
+ title: texts_1.TXT_YAML_AGENT_PROPERTIES,
13
+ });
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const texts_1 = require("../../texts");
4
+ const schemaUtils_1 = require("./schemaUtils");
5
+ const rooted_1 = require("./rooted");
6
+ exports.default = (0, rooted_1.rootedYamlCommand)({
7
+ name: 'custom-action',
8
+ description: texts_1.DESC_COMMAND_YAML_CUSTOM_ACTION,
9
+ examples: texts_1.EXAMPLE_YAML_CUSTOM_ACTION,
10
+ document: 'customActions',
11
+ scope: schemaUtils_1.customActionsScope,
12
+ title: texts_1.TXT_YAML_CUSTOM_ACTION_PROPERTIES,
13
+ });
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const texts_1 = require("../../texts");
4
+ const schemaUtils_1 = require("./schemaUtils");
5
+ const rooted_1 = require("./rooted");
6
+ exports.default = (0, rooted_1.rootedYamlCommand)({
7
+ name: 'distro',
8
+ description: texts_1.DESC_COMMAND_YAML_DISTRO,
9
+ examples: texts_1.EXAMPLE_YAML_DISTRO,
10
+ document: 'distributions',
11
+ scope: schemaUtils_1.distributionsScope,
12
+ title: texts_1.TXT_YAML_DISTRO_PROPERTIES,
13
+ });
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.YAML_TYPES = void 0;
4
+ exports.identifierOf = identifierOf;
5
+ exports.detectYamlType = detectYamlType;
6
+ exports.YAML_TYPES = {
7
+ pipeline: 'pipelines',
8
+ 'custom-action': 'customActions',
9
+ environment: 'environments',
10
+ target: 'targets',
11
+ sandbox: 'sandboxes',
12
+ agent: 'agents',
13
+ domain: 'domains',
14
+ distro: 'distributions',
15
+ };
16
+ // `agent` before `target`: an agent entry has a `target` switch of its own.
17
+ const BY_IDENTIFIER = [
18
+ ['pipeline', 'pipeline'],
19
+ ['environment', 'environment'],
20
+ ['sandbox', 'sandbox'],
21
+ ['distribution', 'distro'],
22
+ ['agent', 'agent'],
23
+ ['target', 'target'],
24
+ ];
25
+ function identifierOf(type) {
26
+ return BY_IDENTIFIER.find(([, owner]) => owner === type)?.[0];
27
+ }
28
+ function isObject(value) {
29
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
30
+ }
31
+ function typeOfEntry(entry) {
32
+ const found = BY_IDENTIFIER.find(([key]) => Object.hasOwn(entry, key));
33
+ if (!found)
34
+ return undefined;
35
+ const isSwitch = typeof entry.target === 'boolean' || entry.target === 'on' || entry.target === 'off';
36
+ return found[1] === 'target' && !Object.hasOwn(entry, 'type') && isSwitch ? 'agent' : found[1];
37
+ }
38
+ function typeOfContent(content) {
39
+ let parsed;
40
+ try {
41
+ parsed = require('yaml').parse(content);
42
+ }
43
+ catch {
44
+ return undefined;
45
+ }
46
+ if (Array.isArray(parsed)) {
47
+ for (const item of parsed) {
48
+ const type = isObject(item) ? typeOfEntry(item) : undefined;
49
+ if (type)
50
+ return type;
51
+ }
52
+ return undefined;
53
+ }
54
+ if (!isObject(parsed))
55
+ return undefined;
56
+ const zones = Object.values(parsed);
57
+ if (zones.length > 0 && zones.every((zone) => isObject(zone) && Object.hasOwn(zone, 'records'))) {
58
+ return 'domain';
59
+ }
60
+ return typeOfEntry(parsed);
61
+ }
62
+ function detectYamlType(content) {
63
+ return content === undefined ? undefined : typeOfContent(content);
64
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const texts_1 = require("../../texts");
4
+ const schemaUtils_1 = require("./schemaUtils");
5
+ const rooted_1 = require("./rooted");
6
+ exports.default = (0, rooted_1.rootedYamlCommand)({
7
+ name: 'domain',
8
+ description: texts_1.DESC_COMMAND_YAML_DOMAIN,
9
+ examples: texts_1.EXAMPLE_YAML_DOMAIN,
10
+ document: 'domains',
11
+ scope: schemaUtils_1.domainsScope,
12
+ title: texts_1.TXT_YAML_DOMAIN_PROPERTIES,
13
+ });
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const texts_1 = require("../../texts");
4
+ const schemaUtils_1 = require("./schemaUtils");
5
+ const rooted_1 = require("./rooted");
6
+ exports.default = (0, rooted_1.rootedYamlCommand)({
7
+ name: 'environment',
8
+ description: texts_1.DESC_COMMAND_YAML_ENVIRONMENT,
9
+ examples: texts_1.EXAMPLE_YAML_ENVIRONMENT,
10
+ document: 'environments',
11
+ scope: schemaUtils_1.environmentsScope,
12
+ title: texts_1.TXT_YAML_ENVIRONMENT_PROPERTIES,
13
+ typesHint: texts_1.TXT_YAML_ENVIRONMENT_TARGET_FIELDS,
14
+ });
@@ -8,27 +8,74 @@ const output_1 = __importDefault(require("../../output"));
8
8
  const texts_1 = require("../../texts");
9
9
  const schemaUtils_1 = require("./schemaUtils");
10
10
  const render_1 = require("./render");
11
+ const ACTION_UNION = 'Action';
11
12
  const commandYamlPipeline = (0, utils_1.newCommand)('pipeline', texts_1.DESC_COMMAND_YAML_PIPELINE, {
12
13
  requiredScopes: [],
13
14
  examples: texts_1.EXAMPLE_YAML_PIPELINE,
14
15
  });
15
16
  commandYamlPipeline.option('--format <text|json>', texts_1.OPTION_FORMAT);
17
+ commandYamlPipeline.option('--event <type>', texts_1.OPTION_YAML_PIPELINE_EVENT);
18
+ async function eventTypes() {
19
+ const properties = await (0, schemaUtils_1.pipelinesScope)().properties('PipelineEventYaml');
20
+ const type = properties.find((property) => property.name === 'type');
21
+ return (type?.enum ?? []).map(String);
22
+ }
23
+ async function outputEvent(type, format) {
24
+ const known = await eventTypes();
25
+ if (!known.includes(type)) {
26
+ output_1.default.exitError((0, texts_1.ERR_YAML_PIPELINE_EVENT_UNKNOWN)(type, known.join(', ')));
27
+ }
28
+ const variables = await (0, schemaUtils_1.pipelinesScope)().eventVariables(type);
29
+ if (format === 'json') {
30
+ output_1.default.json({ event: type, variables });
31
+ }
32
+ else if (variables.length === 0) {
33
+ output_1.default.normal((0, texts_1.TXT_YAML_PIPELINE_EVENT_NONE)(type));
34
+ }
35
+ else {
36
+ output_1.default.blue((0, texts_1.TXT_YAML_EVENT_VARIABLES_OF)(type));
37
+ (0, render_1.outputVariables)(variables);
38
+ }
39
+ output_1.default.exitNormal();
40
+ }
16
41
  commandYamlPipeline.action(async (options) => {
17
42
  await (0, schemaUtils_1.ensureYamlSchema)();
43
+ if (options.event !== undefined) {
44
+ await outputEvent(options.event.toUpperCase(), options.format);
45
+ return;
46
+ }
18
47
  const scope = (0, schemaUtils_1.pipelinesScope)();
19
48
  const properties = await scope.properties('PipelineYaml');
20
- const examples = ((await scope.root())?.examples ?? []);
49
+ const root = await scope.root();
50
+ const examples = (root?.examples ?? []);
51
+ const types = Object.fromEntries(Object.entries(root?.types ?? {}).filter(([name]) => name !== ACTION_UNION));
52
+ const eventVariables = await scope.eventVariables();
21
53
  if (options.format === 'json') {
22
54
  output_1.default.json({
23
55
  properties: (0, render_1.propertiesJson)(properties),
24
56
  required: properties.filter((p) => p.required).map((p) => p.name),
57
+ types: (0, render_1.typesJson)(types),
58
+ eventVariables,
25
59
  examples,
26
60
  });
27
61
  }
28
62
  else {
29
- output_1.default.blue('Pipeline properties');
63
+ output_1.default.blue(texts_1.TXT_YAML_PIPELINE_PROPERTIES);
30
64
  output_1.default.normal('');
31
65
  (0, render_1.outputProperties)(properties);
66
+ if (Object.keys(types).length > 0) {
67
+ output_1.default.normal('');
68
+ output_1.default.blue(texts_1.TXT_YAML_TYPES);
69
+ output_1.default.muted(texts_1.TXT_YAML_PIPELINE_ACTION_FIELDS);
70
+ (0, render_1.outputTypes)(types);
71
+ }
72
+ const events = Object.keys(eventVariables);
73
+ if (events.length > 0) {
74
+ output_1.default.normal('');
75
+ output_1.default.blue(texts_1.TXT_YAML_EVENT_VARIABLES);
76
+ output_1.default.normal((0, texts_1.TXT_YAML_PIPELINE_EVENTS_WITH_VARIABLES)(events.join(', ')));
77
+ output_1.default.muted(texts_1.TXT_YAML_PIPELINE_EVENT_HINT);
78
+ }
32
79
  if (examples.length > 0) {
33
80
  output_1.default.normal('');
34
81
  (0, render_1.outputExamples)(examples);
@@ -7,14 +7,18 @@ exports.propertiesJson = propertiesJson;
7
7
  exports.outputProperties = outputProperties;
8
8
  exports.typesJson = typesJson;
9
9
  exports.outputTypes = outputTypes;
10
+ exports.outputVariables = outputVariables;
11
+ exports.outputOutputs = outputOutputs;
10
12
  exports.outputExamples = outputExamples;
11
13
  const output_1 = __importDefault(require("../../output"));
14
+ const texts_1 = require("../../texts");
12
15
  const shownProperty = (prop) => prop;
13
16
  const shownField = (field) => ({
14
17
  ...field,
15
18
  minimum: field.constraints?.minimum,
16
19
  maximum: field.constraints?.maximum,
17
20
  });
21
+ const nameOf = (one) => (one.mapKey ? `<${one.name}>` : one.name);
18
22
  const typeOf = (one) => (one.const !== undefined ? `"${one.const}"` : one.type);
19
23
  function describe(one) {
20
24
  const parts = [];
@@ -42,20 +46,16 @@ function row(one) {
42
46
  out.minimum = one.minimum;
43
47
  if (one.maximum !== undefined)
44
48
  out.maximum = one.maximum;
49
+ if (one.mapKey)
50
+ out.mapKey = true;
45
51
  return out;
46
52
  }
47
53
  function table(rows) {
48
54
  const data = [['FIELD', 'TYPE', 'REQUIRED', 'DESCRIPTION']];
49
55
  for (const one of rows) {
50
- const desc = describe(one);
51
- data.push([
52
- one.name,
53
- typeOf(one),
54
- one.required ? 'yes' : '',
55
- desc.length > 100 ? `${desc.substring(0, 97)}...` : desc,
56
- ]);
56
+ data.push([nameOf(one), typeOf(one), one.required ? 'yes' : '', describe(one)]);
57
57
  }
58
- output_1.default.table(data);
58
+ output_1.default.wrappedTable(data, { wrapColumn: 3, fixedWidths: { 1: 30 } });
59
59
  }
60
60
  function sorted(properties) {
61
61
  return properties.map(shownProperty).toSorted((a, b) => {
@@ -100,6 +100,25 @@ function outputTypes(types) {
100
100
  table(namedFields(type));
101
101
  }
102
102
  }
103
+ const EXAMPLE_WIDTH = 30;
104
+ function outputVariables(variables) {
105
+ output_1.default.wrappedTable([
106
+ ['VARIABLE', 'DESCRIPTION', 'EXAMPLE'],
107
+ ...variables.map((one) => [one.name, one.description, one.example ?? '']),
108
+ ], { wrapColumn: 1, fixedWidths: { 2: EXAMPLE_WIDTH } });
109
+ }
110
+ function outputOutputs(outputs) {
111
+ if (outputs.variables.length > 0) {
112
+ output_1.default.blue(texts_1.TXT_YAML_EXPORTED_VARIABLES);
113
+ outputVariables(outputs.variables);
114
+ }
115
+ if (outputs.files.length > 0) {
116
+ if (outputs.variables.length > 0)
117
+ output_1.default.normal('');
118
+ output_1.default.blue(texts_1.TXT_YAML_OUTPUT_FILES);
119
+ output_1.default.wrappedTable([['PATH', 'DESCRIPTION'], ...outputs.files.map((one) => [one.path, one.description])]);
120
+ }
121
+ }
103
122
  function formatExampleYaml(value) {
104
123
  try {
105
124
  const YAML = require('yaml');
@@ -114,10 +133,10 @@ function formatExampleYaml(value) {
114
133
  return value.trimEnd();
115
134
  }
116
135
  function outputExamples(examples) {
117
- output_1.default.blue('Examples');
136
+ output_1.default.blue(texts_1.TXT_YAML_EXAMPLES);
118
137
  for (const ex of examples) {
119
138
  output_1.default.normal('');
120
- output_1.default.yellow(`# ${ex.title ?? 'Example'}`);
121
- output_1.default.gray(formatExampleYaml(ex.value));
139
+ output_1.default.yellow(output_1.default.escapeTermKit((0, texts_1.TXT_YAML_EXAMPLE_TITLE)(ex.title)));
140
+ output_1.default.gray(output_1.default.escapeTermKit(formatExampleYaml(ex.value)));
122
141
  }
123
142
  }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.rootedYamlCommand = rootedYamlCommand;
7
+ const utils_1 = require("../../utils");
8
+ const output_1 = __importDefault(require("../../output"));
9
+ const texts_1 = require("../../texts");
10
+ const schemaUtils_1 = require("./schemaUtils");
11
+ const render_1 = require("./render");
12
+ function rootedYamlCommand({ name, description, examples, document, scope, title, typesHint, }) {
13
+ const command = (0, utils_1.newCommand)(name, description, { requiredScopes: [], examples });
14
+ command.option('--format <text|json>', texts_1.OPTION_FORMAT);
15
+ command.action(async (options) => {
16
+ await (0, schemaUtils_1.ensureYamlSchema)(document);
17
+ const root = await scope().root();
18
+ const properties = root ? await scope().properties(root.name) : [];
19
+ const types = root?.types ?? {};
20
+ const rootExamples = (root?.examples ?? []);
21
+ if (options.format === 'json') {
22
+ output_1.default.json({
23
+ properties: (0, render_1.propertiesJson)(properties),
24
+ required: properties.filter((p) => p.required).map((p) => p.name),
25
+ types: (0, render_1.typesJson)(types),
26
+ examples: rootExamples,
27
+ });
28
+ output_1.default.exitNormal();
29
+ return;
30
+ }
31
+ output_1.default.blue(title);
32
+ output_1.default.normal('');
33
+ (0, render_1.outputProperties)(properties);
34
+ if (Object.keys(types).length > 0) {
35
+ output_1.default.normal('');
36
+ output_1.default.blue(texts_1.TXT_YAML_TYPES);
37
+ if (typesHint)
38
+ output_1.default.muted(typesHint);
39
+ (0, render_1.outputTypes)(types);
40
+ }
41
+ if (rootExamples.length > 0) {
42
+ output_1.default.normal('');
43
+ (0, render_1.outputExamples)(rootExamples);
44
+ }
45
+ output_1.default.exitNormal();
46
+ });
47
+ return command;
48
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const texts_1 = require("../../texts");
4
+ const schemaUtils_1 = require("./schemaUtils");
5
+ const rooted_1 = require("./rooted");
6
+ exports.default = (0, rooted_1.rootedYamlCommand)({
7
+ name: 'sandbox',
8
+ description: texts_1.DESC_COMMAND_YAML_SANDBOX,
9
+ examples: texts_1.EXAMPLE_YAML_SANDBOX,
10
+ document: 'sandboxes',
11
+ scope: schemaUtils_1.sandboxesScope,
12
+ title: texts_1.TXT_YAML_SANDBOX_PROPERTIES,
13
+ });
@@ -3,124 +3,69 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.pipelinesScope = exports.actionsScope = void 0;
6
+ exports.customActionsScope = exports.agentsScope = exports.distributionsScope = exports.environmentsScope = exports.domainsScope = exports.targetsScope = exports.sandboxesScope = exports.pipelinesScope = exports.actionsScope = void 0;
7
7
  exports.ensureYamlSchema = ensureYamlSchema;
8
- exports.loadRawSchema = loadRawSchema;
9
- exports.getDefValidator = getDefValidator;
10
- exports.validateAgainstDef = validateAgainstDef;
11
- exports.validateAgainstSchema = validateAgainstSchema;
12
- exports.getAllDefs = getAllDefs;
8
+ exports.schemaClient = schemaClient;
13
9
  const docs_schema_1 = require("@buddy-works/docs-schema");
14
10
  const utils_1 = require("../../utils");
15
11
  const input_1 = __importDefault(require("../../input"));
16
12
  const diskCache_1 = require("../../diskCache");
17
13
  const texts_1 = require("../../texts");
18
- function schemaUrl() {
14
+ const FILES = {
15
+ pipelines: 'yaml-schema.json',
16
+ sandboxes: 'sandboxes-schema.json',
17
+ targets: 'targets-schema.json',
18
+ domains: 'domains-schema.json',
19
+ environments: 'environments-schema.json',
20
+ distributions: 'distributions-schema.json',
21
+ agents: 'agents-schema.json',
22
+ customActions: 'custom-actions-schema.json',
23
+ };
24
+ function schemaUrl(document) {
19
25
  const env = (0, utils_1.getApiEnv)(input_1.default.restApiTokenClient(true).baseUrl);
20
- return `https://es.buddy.works/yaml-schema/${env}/yaml-schema.json`;
26
+ return `https://es.buddy.works/yaml-schema/${env}/${FILES[document]}`;
21
27
  }
22
- async function ensureYamlSchema() {
23
- const url = schemaUrl();
28
+ const ensured = new Set();
29
+ let client = null;
30
+ async function ensureYamlSchema(document = 'pipelines') {
31
+ const url = schemaUrl(document);
24
32
  const paths = (0, diskCache_1.cachePaths)('yaml-schema', url);
25
- if ((0, diskCache_1.isHardFresh)((0, diskCache_1.readMeta)(paths.meta)))
26
- return;
27
- if ((await (0, diskCache_1.refresh)(url, paths)) === 'none') {
33
+ if (!(0, diskCache_1.isHardFresh)((0, diskCache_1.readMeta)(paths.meta)) && (await (0, diskCache_1.refresh)(url, paths)) === 'none') {
28
34
  throw new Error((0, texts_1.ERR_YAML_SCHEMA_FETCH_FAILED)(url));
29
35
  }
36
+ if (!ensured.has(document)) {
37
+ ensured.add(document);
38
+ client = null;
39
+ }
30
40
  }
31
- function readCachedSchema() {
32
- const path = (0, diskCache_1.cachePaths)('yaml-schema', schemaUrl()).raw;
41
+ function readCachedSchema(document) {
42
+ const path = (0, diskCache_1.cachePaths)('yaml-schema', schemaUrl(document)).raw;
33
43
  const raw = (0, diskCache_1.readCached)(path);
34
44
  if (!raw)
35
45
  throw new Error((0, texts_1.ERR_YAML_SCHEMA_READ_FAILED)(path));
36
46
  return JSON.parse(raw);
37
47
  }
38
- let client = null;
39
- function schemas() {
40
- if (!client)
41
- client = docs_schema_1.BuddySchema.from({ pipelines: loadRawSchema() });
48
+ function schemaClient() {
49
+ if (!client) {
50
+ client = docs_schema_1.BuddySchema.from(Object.fromEntries([...ensured].map((document) => [document, readCachedSchema(document)])));
51
+ }
42
52
  return client;
43
53
  }
44
- const actionsScope = () => schemas().actions;
54
+ const actionsScope = () => schemaClient().actions;
45
55
  exports.actionsScope = actionsScope;
46
- const pipelinesScope = () => schemas().pipelines;
56
+ const pipelinesScope = () => schemaClient().pipelines;
47
57
  exports.pipelinesScope = pipelinesScope;
48
- let cachedRaw = null;
49
- let ajvInstance = null;
50
- const validatorCache = new Map();
51
- function loadRawSchema() {
52
- if (cachedRaw)
53
- return cachedRaw;
54
- cachedRaw = readCachedSchema();
55
- return cachedRaw;
56
- }
57
- // Read off the schema, not listed: AJV strict mode rejects any x- keyword it was
58
- // not told about, and the backend keeps adding them.
59
- function vendorKeywords(schema) {
60
- const found = new Set();
61
- const walk = (node) => {
62
- if (!node || typeof node !== 'object')
63
- return;
64
- if (Array.isArray(node)) {
65
- node.forEach(walk);
66
- return;
67
- }
68
- for (const [key, value] of Object.entries(node)) {
69
- if (key.startsWith('x-'))
70
- found.add(key);
71
- walk(value);
72
- }
73
- };
74
- walk(schema);
75
- return [...found];
76
- }
77
- function getAjv() {
78
- if (ajvInstance)
79
- return ajvInstance;
80
- const Ajv = require('ajv/dist/2020').default;
81
- const addFormats = require('ajv-formats').default;
82
- const schema = loadRawSchema();
83
- const ajv = new Ajv({ allErrors: true, coerceTypes: true });
84
- addFormats(ajv);
85
- for (const kw of vendorKeywords(schema))
86
- ajv.addKeyword(kw);
87
- ajv.addKeyword('discriminator');
88
- ajv.addSchema(schema);
89
- ajvInstance = ajv;
90
- return ajv;
91
- }
92
- function getDefValidator(defName) {
93
- if (validatorCache.has(defName))
94
- return validatorCache.get(defName);
95
- const ajv = getAjv();
96
- const raw = loadRawSchema();
97
- const validate = ajv.compile({ $ref: `${raw.$id}#/$defs/${defName}` });
98
- validatorCache.set(defName, validate);
99
- return validate;
100
- }
101
- function formatErrors(errors) {
102
- if (!errors)
103
- return [];
104
- return errors.map((err) => {
105
- let errPath = err.instancePath.replace(/^\//, '').replace(/\//g, '.');
106
- if (err.keyword === 'required' && err.params?.missingProperty) {
107
- errPath = errPath ? `${errPath}.${err.params.missingProperty}` : err.params.missingProperty;
108
- }
109
- return { path: errPath, message: err.message || 'unknown error' };
110
- });
111
- }
112
- function validateAgainstDef(defName, data) {
113
- const validate = getDefValidator(defName);
114
- const valid = validate(data);
115
- return { valid: !!valid, errors: formatErrors(validate.errors) };
116
- }
117
- function validateAgainstSchema(schema, data) {
118
- const ajv = getAjv();
119
- const validate = ajv.compile(schema);
120
- const valid = validate(data);
121
- return { valid: !!valid, errors: formatErrors(validate.errors) };
122
- }
123
- function getAllDefs() {
124
- const raw = loadRawSchema();
125
- return raw.$defs || {};
126
- }
58
+ const sandboxesScope = () => schemaClient().sandboxes;
59
+ exports.sandboxesScope = sandboxesScope;
60
+ const targetsScope = () => schemaClient().targets;
61
+ exports.targetsScope = targetsScope;
62
+ const domainsScope = () => schemaClient().domains;
63
+ exports.domainsScope = domainsScope;
64
+ const environmentsScope = () => schemaClient().environments;
65
+ exports.environmentsScope = environmentsScope;
66
+ const distributionsScope = () => schemaClient().distributions;
67
+ exports.distributionsScope = distributionsScope;
68
+ const agentsScope = () => schemaClient().agents;
69
+ exports.agentsScope = agentsScope;
70
+ const customActionsScope = () => schemaClient().customActions;
71
+ exports.customActionsScope = customActionsScope;