UNPKG

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