1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseJsonSchemaToOptions = exports.parseJsonSchemaToCommandDescription = exports.parseJsonSchemaToSubCommandDescription = exports.CommandJsonPathException = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
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 |
|
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 |
|
110 | return;
|
111 | }
|
112 | else if (pointer.split(/\/(?:properties|items|definitions)\//g).length > 2) {
|
113 |
|
114 | return;
|
115 | }
|
116 | else if (core_1.json.isJsonArray(current)) {
|
117 | return;
|
118 | }
|
119 | if (pointer.indexOf('/not/') != -1) {
|
120 |
|
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 |
|
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 |
|
134 | const types = [...typeSet].filter(x => {
|
135 | switch (x) {
|
136 | case 'boolean':
|
137 | case 'number':
|
138 | case 'string':
|
139 | return true;
|
140 | case 'array':
|
141 |
|
142 | if (core_1.json.isJsonObject(current.items)
|
143 | && typeof current.items.type == 'string'
|
144 | && ['boolean', 'number', 'string'].includes(current.items.type)) {
|
145 | return true;
|
146 | }
|
147 | return false;
|
148 | default:
|
149 | return false;
|
150 | }
|
151 | }).map(x => _getEnumFromValue(x, interface_1.OptionType, interface_1.OptionType.String));
|
152 | if (types.length == 0) {
|
153 |
|
154 | return;
|
155 | }
|
156 |
|
157 | const enumValues = (core_1.json.isJsonArray(current.enum) && current.enum || []).filter(x => {
|
158 | switch (typeof x) {
|
159 | case 'boolean':
|
160 | case 'number':
|
161 | case 'string':
|
162 | return true;
|
163 | default:
|
164 | return false;
|
165 | }
|
166 | });
|
167 | let defaultValue = undefined;
|
168 | if (current.default !== undefined) {
|
169 | switch (types[0]) {
|
170 | case 'string':
|
171 | if (typeof current.default == 'string') {
|
172 | defaultValue = current.default;
|
173 | }
|
174 | break;
|
175 | case 'number':
|
176 | if (typeof current.default == 'number') {
|
177 | defaultValue = current.default;
|
178 | }
|
179 | break;
|
180 | case 'boolean':
|
181 | if (typeof current.default == 'boolean') {
|
182 | defaultValue = current.default;
|
183 | }
|
184 | break;
|
185 | }
|
186 | }
|
187 | const type = types[0];
|
188 | const $default = current.$default;
|
189 | const $defaultIndex = (core_1.json.isJsonObject($default) && $default['$source'] == 'argv')
|
190 | ? $default['index'] : undefined;
|
191 | const positional = typeof $defaultIndex == 'number'
|
192 | ? $defaultIndex : undefined;
|
193 | const required = core_1.json.isJsonArray(current.required)
|
194 | ? current.required.indexOf(name) != -1 : false;
|
195 | const aliases = core_1.json.isJsonArray(current.aliases) ? [...current.aliases].map(x => '' + x)
|
196 | : current.alias ? ['' + current.alias] : [];
|
197 | const format = typeof current.format == 'string' ? current.format : undefined;
|
198 | const visible = current.visible === undefined || current.visible === true;
|
199 | const hidden = !!current.hidden || !visible;
|
200 | const xUserAnalytics = current['x-user-analytics'];
|
201 | const userAnalytics = typeof xUserAnalytics == 'number' ? xUserAnalytics : undefined;
|
202 |
|
203 | const xDeprecated = current['x-deprecated'];
|
204 | const deprecated = (xDeprecated === true || typeof xDeprecated === 'string')
|
205 | ? xDeprecated : undefined;
|
206 | const option = {
|
207 | name,
|
208 | description: '' + (current.description === undefined ? '' : current.description),
|
209 | ...types.length == 1 ? { type } : { type, types },
|
210 | ...defaultValue !== undefined ? { default: defaultValue } : {},
|
211 | ...enumValues && enumValues.length > 0 ? { enum: enumValues } : {},
|
212 | required,
|
213 | aliases,
|
214 | ...format !== undefined ? { format } : {},
|
215 | hidden,
|
216 | ...userAnalytics ? { userAnalytics } : {},
|
217 | ...deprecated !== undefined ? { deprecated } : {},
|
218 | ...positional !== undefined ? { positional } : {},
|
219 | };
|
220 | options.push(option);
|
221 | }
|
222 | const flattenedSchema = await registry.flatten(schema).toPromise();
|
223 | core_1.json.schema.visitJsonSchema(flattenedSchema, visitor);
|
224 |
|
225 | return options.sort((a, b) => {
|
226 | if (a.positional) {
|
227 | if (b.positional) {
|
228 | return a.positional - b.positional;
|
229 | }
|
230 | else {
|
231 | return 1;
|
232 | }
|
233 | }
|
234 | else if (b.positional) {
|
235 | return -1;
|
236 | }
|
237 | else {
|
238 | return 0;
|
239 | }
|
240 | });
|
241 | }
|
242 | exports.parseJsonSchemaToOptions = parseJsonSchemaToOptions;
|