1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.parseJsonSchemaToOptions = exports.parseJsonSchemaToCommandDescription = exports.parseJsonSchemaToSubCommandDescription = exports.CommandJsonPathException = void 0;
|
---|
11 | const core_1 = require("@angular-devkit/core");
|
---|
12 | const tools_1 = require("@angular-devkit/schematics/tools");
|
---|
13 | const fs_1 = require("fs");
|
---|
14 | const path_1 = require("path");
|
---|
15 | const interface_1 = require("../models/interface");
|
---|
16 | class CommandJsonPathException extends core_1.BaseException {
|
---|
17 | constructor(path, name) {
|
---|
18 | super(`File ${path} was not found while constructing the subcommand ${name}.`);
|
---|
19 | this.path = path;
|
---|
20 | this.name = name;
|
---|
21 | }
|
---|
22 | }
|
---|
23 | exports.CommandJsonPathException = CommandJsonPathException;
|
---|
24 | function _getEnumFromValue(value, enumeration, defaultValue) {
|
---|
25 | if (typeof value !== 'string') {
|
---|
26 | return defaultValue;
|
---|
27 | }
|
---|
28 | if (Object.values(enumeration).includes(value)) {
|
---|
29 | return value;
|
---|
30 | }
|
---|
31 | return defaultValue;
|
---|
32 | }
|
---|
33 | async function parseJsonSchemaToSubCommandDescription(name, jsonPath, registry, schema) {
|
---|
34 | const options = await parseJsonSchemaToOptions(registry, schema);
|
---|
35 | const aliases = [];
|
---|
36 | if (core_1.json.isJsonArray(schema.$aliases)) {
|
---|
37 | schema.$aliases.forEach((value) => {
|
---|
38 | if (typeof value == 'string') {
|
---|
39 | aliases.push(value);
|
---|
40 | }
|
---|
41 | });
|
---|
42 | }
|
---|
43 | if (core_1.json.isJsonArray(schema.aliases)) {
|
---|
44 | schema.aliases.forEach((value) => {
|
---|
45 | if (typeof value == 'string') {
|
---|
46 | aliases.push(value);
|
---|
47 | }
|
---|
48 | });
|
---|
49 | }
|
---|
50 | if (typeof schema.alias == 'string') {
|
---|
51 | aliases.push(schema.alias);
|
---|
52 | }
|
---|
53 | let longDescription = '';
|
---|
54 | if (typeof schema.$longDescription == 'string' && schema.$longDescription) {
|
---|
55 | const ldPath = path_1.resolve(path_1.dirname(jsonPath), schema.$longDescription);
|
---|
56 | try {
|
---|
57 | longDescription = fs_1.readFileSync(ldPath, 'utf-8');
|
---|
58 | }
|
---|
59 | catch (e) {
|
---|
60 | throw new CommandJsonPathException(ldPath, name);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | let usageNotes = '';
|
---|
64 | if (typeof schema.$usageNotes == 'string' && schema.$usageNotes) {
|
---|
65 | const unPath = path_1.resolve(path_1.dirname(jsonPath), schema.$usageNotes);
|
---|
66 | try {
|
---|
67 | usageNotes = fs_1.readFileSync(unPath, 'utf-8');
|
---|
68 | }
|
---|
69 | catch (e) {
|
---|
70 | throw new CommandJsonPathException(unPath, name);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | const description = '' + (schema.description === undefined ? '' : schema.description);
|
---|
74 | return {
|
---|
75 | name,
|
---|
76 | description,
|
---|
77 | ...(longDescription ? { longDescription } : {}),
|
---|
78 | ...(usageNotes ? { usageNotes } : {}),
|
---|
79 | options,
|
---|
80 | aliases,
|
---|
81 | };
|
---|
82 | }
|
---|
83 | exports.parseJsonSchemaToSubCommandDescription = parseJsonSchemaToSubCommandDescription;
|
---|
84 | async function parseJsonSchemaToCommandDescription(name, jsonPath, registry, schema) {
|
---|
85 | const subcommand = await parseJsonSchemaToSubCommandDescription(name, jsonPath, registry, schema);
|
---|
86 | // Before doing any work, let's validate the implementation.
|
---|
87 | if (typeof schema.$impl != 'string') {
|
---|
88 | throw new Error(`Command ${name} has an invalid implementation.`);
|
---|
89 | }
|
---|
90 | const ref = new tools_1.ExportStringRef(schema.$impl, path_1.dirname(jsonPath));
|
---|
91 | const impl = ref.ref;
|
---|
92 | if (impl === undefined || typeof impl !== 'function') {
|
---|
93 | throw new Error(`Command ${name} has an invalid implementation.`);
|
---|
94 | }
|
---|
95 | const scope = _getEnumFromValue(schema.$scope, interface_1.CommandScope, interface_1.CommandScope.Default);
|
---|
96 | const hidden = !!schema.$hidden;
|
---|
97 | return {
|
---|
98 | ...subcommand,
|
---|
99 | scope,
|
---|
100 | hidden,
|
---|
101 | impl,
|
---|
102 | };
|
---|
103 | }
|
---|
104 | exports.parseJsonSchemaToCommandDescription = parseJsonSchemaToCommandDescription;
|
---|
105 | async function parseJsonSchemaToOptions(registry, schema) {
|
---|
106 | const options = [];
|
---|
107 | function visitor(current, pointer, parentSchema) {
|
---|
108 | if (!parentSchema) {
|
---|
109 | // Ignore root.
|
---|
110 | return;
|
---|
111 | }
|
---|
112 | else if (pointer.split(/\/(?:properties|items|definitions)\//g).length > 2) {
|
---|
113 | // Ignore subitems (objects or arrays).
|
---|
114 | return;
|
---|
115 | }
|
---|
116 | else if (core_1.json.isJsonArray(current)) {
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | if (pointer.indexOf('/not/') != -1) {
|
---|
120 | // We don't support anyOf/not.
|
---|
121 | throw new Error('The "not" keyword is not supported in JSON Schema.');
|
---|
122 | }
|
---|
123 | const ptr = core_1.json.schema.parseJsonPointer(pointer);
|
---|
124 | const name = ptr[ptr.length - 1];
|
---|
125 | if (ptr[ptr.length - 2] != 'properties') {
|
---|
126 | // Skip any non-property items.
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | const typeSet = core_1.json.schema.getTypesOfSchema(current);
|
---|
130 | if (typeSet.size == 0) {
|
---|
131 | throw new Error('Cannot find type of schema.');
|
---|
132 | }
|
---|
133 | // We only support number, string or boolean (or array of those), so remove everything else.
|
---|
134 | const types = [...typeSet]
|
---|
135 | .filter((x) => {
|
---|
136 | switch (x) {
|
---|
137 | case 'boolean':
|
---|
138 | case 'number':
|
---|
139 | case 'string':
|
---|
140 | return true;
|
---|
141 | case 'array':
|
---|
142 | // Only include arrays if they're boolean, string or number.
|
---|
143 | if (core_1.json.isJsonObject(current.items) &&
|
---|
144 | typeof current.items.type == 'string' &&
|
---|
145 | ['boolean', 'number', 'string'].includes(current.items.type)) {
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 | return false;
|
---|
149 | default:
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | })
|
---|
153 | .map((x) => _getEnumFromValue(x, interface_1.OptionType, interface_1.OptionType.String));
|
---|
154 | if (types.length == 0) {
|
---|
155 | // This means it's not usable on the command line. e.g. an Object.
|
---|
156 | return;
|
---|
157 | }
|
---|
158 | // Only keep enum values we support (booleans, numbers and strings).
|
---|
159 | const enumValues = ((core_1.json.isJsonArray(current.enum) && current.enum) || []).filter((x) => {
|
---|
160 | switch (typeof x) {
|
---|
161 | case 'boolean':
|
---|
162 | case 'number':
|
---|
163 | case 'string':
|
---|
164 | return true;
|
---|
165 | default:
|
---|
166 | return false;
|
---|
167 | }
|
---|
168 | });
|
---|
169 | let defaultValue = undefined;
|
---|
170 | if (current.default !== undefined) {
|
---|
171 | switch (types[0]) {
|
---|
172 | case 'string':
|
---|
173 | if (typeof current.default == 'string') {
|
---|
174 | defaultValue = current.default;
|
---|
175 | }
|
---|
176 | break;
|
---|
177 | case 'number':
|
---|
178 | if (typeof current.default == 'number') {
|
---|
179 | defaultValue = current.default;
|
---|
180 | }
|
---|
181 | break;
|
---|
182 | case 'boolean':
|
---|
183 | if (typeof current.default == 'boolean') {
|
---|
184 | defaultValue = current.default;
|
---|
185 | }
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | const type = types[0];
|
---|
190 | const $default = current.$default;
|
---|
191 | const $defaultIndex = core_1.json.isJsonObject($default) && $default['$source'] == 'argv' ? $default['index'] : undefined;
|
---|
192 | const positional = typeof $defaultIndex == 'number' ? $defaultIndex : undefined;
|
---|
193 | const required = core_1.json.isJsonArray(current.required)
|
---|
194 | ? current.required.indexOf(name) != -1
|
---|
195 | : false;
|
---|
196 | const aliases = core_1.json.isJsonArray(current.aliases)
|
---|
197 | ? [...current.aliases].map((x) => '' + x)
|
---|
198 | : current.alias
|
---|
199 | ? ['' + current.alias]
|
---|
200 | : [];
|
---|
201 | const format = typeof current.format == 'string' ? current.format : undefined;
|
---|
202 | const visible = current.visible === undefined || current.visible === true;
|
---|
203 | const hidden = !!current.hidden || !visible;
|
---|
204 | const xUserAnalytics = current['x-user-analytics'];
|
---|
205 | const userAnalytics = typeof xUserAnalytics == 'number' ? xUserAnalytics : undefined;
|
---|
206 | // Deprecated is set only if it's true or a string.
|
---|
207 | const xDeprecated = current['x-deprecated'];
|
---|
208 | const deprecated = xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : undefined;
|
---|
209 | const option = {
|
---|
210 | name,
|
---|
211 | description: '' + (current.description === undefined ? '' : current.description),
|
---|
212 | ...(types.length == 1 ? { type } : { type, types }),
|
---|
213 | ...(defaultValue !== undefined ? { default: defaultValue } : {}),
|
---|
214 | ...(enumValues && enumValues.length > 0 ? { enum: enumValues } : {}),
|
---|
215 | required,
|
---|
216 | aliases,
|
---|
217 | ...(format !== undefined ? { format } : {}),
|
---|
218 | hidden,
|
---|
219 | ...(userAnalytics ? { userAnalytics } : {}),
|
---|
220 | ...(deprecated !== undefined ? { deprecated } : {}),
|
---|
221 | ...(positional !== undefined ? { positional } : {}),
|
---|
222 | };
|
---|
223 | options.push(option);
|
---|
224 | }
|
---|
225 | const flattenedSchema = await registry.flatten(schema).toPromise();
|
---|
226 | core_1.json.schema.visitJsonSchema(flattenedSchema, visitor);
|
---|
227 | // Sort by positional.
|
---|
228 | return options.sort((a, b) => {
|
---|
229 | if (a.positional) {
|
---|
230 | if (b.positional) {
|
---|
231 | return a.positional - b.positional;
|
---|
232 | }
|
---|
233 | else {
|
---|
234 | return 1;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | else if (b.positional) {
|
---|
238 | return -1;
|
---|
239 | }
|
---|
240 | else {
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 | });
|
---|
244 | }
|
---|
245 | exports.parseJsonSchemaToOptions = parseJsonSchemaToOptions;
|
---|