[6a3a178] | 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 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 10 | if (k2 === undefined) k2 = k;
|
---|
| 11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 12 | }) : (function(o, m, k, k2) {
|
---|
| 13 | if (k2 === undefined) k2 = k;
|
---|
| 14 | o[k2] = m[k];
|
---|
| 15 | }));
|
---|
| 16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 18 | }) : function(o, v) {
|
---|
| 19 | o["default"] = v;
|
---|
| 20 | });
|
---|
| 21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 22 | if (mod && mod.__esModule) return mod;
|
---|
| 23 | var result = {};
|
---|
| 24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 25 | __setModuleDefault(result, mod);
|
---|
| 26 | return result;
|
---|
| 27 | };
|
---|
| 28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 29 | exports.runCommand = void 0;
|
---|
| 30 | const core_1 = require("@angular-devkit/core");
|
---|
| 31 | const fs_1 = require("fs");
|
---|
| 32 | const path_1 = require("path");
|
---|
| 33 | const json_file_1 = require("../utilities/json-file");
|
---|
| 34 | const json_schema_1 = require("../utilities/json-schema");
|
---|
| 35 | const analytics_1 = require("./analytics");
|
---|
| 36 | const command_1 = require("./command");
|
---|
| 37 | const parser = __importStar(require("./parser"));
|
---|
| 38 | // NOTE: Update commands.json if changing this. It's still deep imported in one CI validation
|
---|
| 39 | const standardCommands = {
|
---|
| 40 | 'add': '../commands/add.json',
|
---|
| 41 | 'analytics': '../commands/analytics.json',
|
---|
| 42 | 'build': '../commands/build.json',
|
---|
| 43 | 'deploy': '../commands/deploy.json',
|
---|
| 44 | 'config': '../commands/config.json',
|
---|
| 45 | 'doc': '../commands/doc.json',
|
---|
| 46 | 'e2e': '../commands/e2e.json',
|
---|
| 47 | 'extract-i18n': '../commands/extract-i18n.json',
|
---|
| 48 | 'make-this-awesome': '../commands/easter-egg.json',
|
---|
| 49 | 'generate': '../commands/generate.json',
|
---|
| 50 | 'help': '../commands/help.json',
|
---|
| 51 | 'lint': '../commands/lint.json',
|
---|
| 52 | 'new': '../commands/new.json',
|
---|
| 53 | 'run': '../commands/run.json',
|
---|
| 54 | 'serve': '../commands/serve.json',
|
---|
| 55 | 'test': '../commands/test.json',
|
---|
| 56 | 'update': '../commands/update.json',
|
---|
| 57 | 'version': '../commands/version.json',
|
---|
| 58 | };
|
---|
| 59 | /**
|
---|
| 60 | * Create the analytics instance.
|
---|
| 61 | * @private
|
---|
| 62 | */
|
---|
| 63 | async function _createAnalytics(workspace, skipPrompt = false) {
|
---|
| 64 | let config = await analytics_1.getGlobalAnalytics();
|
---|
| 65 | // If in workspace and global analytics is enabled, defer to workspace level
|
---|
| 66 | if (workspace && config) {
|
---|
| 67 | const skipAnalytics = skipPrompt ||
|
---|
| 68 | (process.env['NG_CLI_ANALYTICS'] &&
|
---|
| 69 | (process.env['NG_CLI_ANALYTICS'].toLowerCase() === 'false' ||
|
---|
| 70 | process.env['NG_CLI_ANALYTICS'] === '0'));
|
---|
| 71 | // TODO: This should honor the `no-interactive` option.
|
---|
| 72 | // It is currently not an `ng` option but rather only an option for specific commands.
|
---|
| 73 | // The concept of `ng`-wide options are needed to cleanly handle this.
|
---|
| 74 | if (!skipAnalytics && !(await analytics_1.hasWorkspaceAnalyticsConfiguration())) {
|
---|
| 75 | await analytics_1.promptProjectAnalytics();
|
---|
| 76 | }
|
---|
| 77 | config = await analytics_1.getWorkspaceAnalytics();
|
---|
| 78 | }
|
---|
| 79 | const maybeSharedAnalytics = await analytics_1.getSharedAnalytics();
|
---|
| 80 | if (config && maybeSharedAnalytics) {
|
---|
| 81 | return new core_1.analytics.MultiAnalytics([config, maybeSharedAnalytics]);
|
---|
| 82 | }
|
---|
| 83 | else if (config) {
|
---|
| 84 | return config;
|
---|
| 85 | }
|
---|
| 86 | else if (maybeSharedAnalytics) {
|
---|
| 87 | return maybeSharedAnalytics;
|
---|
| 88 | }
|
---|
| 89 | else {
|
---|
| 90 | return new core_1.analytics.NoopAnalytics();
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | async function loadCommandDescription(name, path, registry) {
|
---|
| 94 | const schemaPath = path_1.resolve(__dirname, path);
|
---|
| 95 | const schema = json_file_1.readAndParseJson(schemaPath);
|
---|
| 96 | if (!core_1.isJsonObject(schema)) {
|
---|
| 97 | throw new Error('Invalid command JSON loaded from ' + JSON.stringify(schemaPath));
|
---|
| 98 | }
|
---|
| 99 | return json_schema_1.parseJsonSchemaToCommandDescription(name, schemaPath, registry, schema);
|
---|
| 100 | }
|
---|
| 101 | /**
|
---|
| 102 | * Run a command.
|
---|
| 103 | * @param args Raw unparsed arguments.
|
---|
| 104 | * @param logger The logger to use.
|
---|
| 105 | * @param workspace Workspace information.
|
---|
| 106 | * @param commands The map of supported commands.
|
---|
| 107 | * @param options Additional options.
|
---|
| 108 | */
|
---|
| 109 | async function runCommand(args, logger, workspace, commands = standardCommands, options = {
|
---|
| 110 | currentDirectory: process.cwd(),
|
---|
| 111 | }) {
|
---|
| 112 | var _a;
|
---|
| 113 | // This registry is exclusively used for flattening schemas, and not for validating.
|
---|
| 114 | const registry = new core_1.schema.CoreSchemaRegistry([]);
|
---|
| 115 | registry.registerUriHandler((uri) => {
|
---|
| 116 | if (uri.startsWith('ng-cli://')) {
|
---|
| 117 | const content = fs_1.readFileSync(path_1.join(__dirname, '..', uri.substr('ng-cli://'.length)), 'utf-8');
|
---|
| 118 | return Promise.resolve(JSON.parse(content));
|
---|
| 119 | }
|
---|
| 120 | else {
|
---|
| 121 | return null;
|
---|
| 122 | }
|
---|
| 123 | });
|
---|
| 124 | let commandName = undefined;
|
---|
| 125 | for (let i = 0; i < args.length; i++) {
|
---|
| 126 | const arg = args[i];
|
---|
| 127 | if (!arg.startsWith('-')) {
|
---|
| 128 | commandName = arg;
|
---|
| 129 | args.splice(i, 1);
|
---|
| 130 | break;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | let description = null;
|
---|
| 134 | // if no commands were found, use `help`.
|
---|
| 135 | if (!commandName) {
|
---|
| 136 | if (args.length === 1 && args[0] === '--version') {
|
---|
| 137 | commandName = 'version';
|
---|
| 138 | }
|
---|
| 139 | else {
|
---|
| 140 | commandName = 'help';
|
---|
| 141 | }
|
---|
| 142 | if (!(commandName in commands)) {
|
---|
| 143 | logger.error(core_1.tags.stripIndent `
|
---|
| 144 | The "${commandName}" command seems to be disabled.
|
---|
| 145 | This is an issue with the CLI itself. If you see this comment, please report it and
|
---|
| 146 | provide your repository.
|
---|
| 147 | `);
|
---|
| 148 | return 1;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | if (commandName in commands) {
|
---|
| 152 | description = await loadCommandDescription(commandName, commands[commandName], registry);
|
---|
| 153 | }
|
---|
| 154 | else {
|
---|
| 155 | const commandNames = Object.keys(commands);
|
---|
| 156 | // Optimize loading for common aliases
|
---|
| 157 | if (commandName.length === 1) {
|
---|
| 158 | commandNames.sort((a, b) => {
|
---|
| 159 | const aMatch = a[0] === commandName;
|
---|
| 160 | const bMatch = b[0] === commandName;
|
---|
| 161 | if (aMatch && !bMatch) {
|
---|
| 162 | return -1;
|
---|
| 163 | }
|
---|
| 164 | else if (!aMatch && bMatch) {
|
---|
| 165 | return 1;
|
---|
| 166 | }
|
---|
| 167 | else {
|
---|
| 168 | return 0;
|
---|
| 169 | }
|
---|
| 170 | });
|
---|
| 171 | }
|
---|
| 172 | for (const name of commandNames) {
|
---|
| 173 | const aliasDesc = await loadCommandDescription(name, commands[name], registry);
|
---|
| 174 | const aliases = aliasDesc.aliases;
|
---|
| 175 | if (aliases && aliases.some((alias) => alias === commandName)) {
|
---|
| 176 | commandName = name;
|
---|
| 177 | description = aliasDesc;
|
---|
| 178 | break;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | if (!description) {
|
---|
| 183 | const commandsDistance = {};
|
---|
| 184 | const name = commandName;
|
---|
| 185 | const allCommands = Object.keys(commands).sort((a, b) => {
|
---|
| 186 | if (!(a in commandsDistance)) {
|
---|
| 187 | commandsDistance[a] = core_1.strings.levenshtein(a, name);
|
---|
| 188 | }
|
---|
| 189 | if (!(b in commandsDistance)) {
|
---|
| 190 | commandsDistance[b] = core_1.strings.levenshtein(b, name);
|
---|
| 191 | }
|
---|
| 192 | return commandsDistance[a] - commandsDistance[b];
|
---|
| 193 | });
|
---|
| 194 | logger.error(core_1.tags.stripIndent `
|
---|
| 195 | The specified command ("${commandName}") is invalid. For a list of available options,
|
---|
| 196 | run "ng help".
|
---|
| 197 |
|
---|
| 198 | Did you mean "${allCommands[0]}"?
|
---|
| 199 | `);
|
---|
| 200 | return 1;
|
---|
| 201 | }
|
---|
| 202 | try {
|
---|
| 203 | const parsedOptions = parser.parseArguments(args, description.options, logger);
|
---|
| 204 | command_1.Command.setCommandMap(async () => {
|
---|
| 205 | const map = {};
|
---|
| 206 | for (const [name, path] of Object.entries(commands)) {
|
---|
| 207 | map[name] = await loadCommandDescription(name, path, registry);
|
---|
| 208 | }
|
---|
| 209 | return map;
|
---|
| 210 | });
|
---|
| 211 | const analytics = options.analytics || (await _createAnalytics(!!workspace, description.name === 'update'));
|
---|
| 212 | const context = {
|
---|
| 213 | workspace,
|
---|
| 214 | analytics,
|
---|
| 215 | currentDirectory: options.currentDirectory,
|
---|
| 216 | root: (_a = workspace === null || workspace === void 0 ? void 0 : workspace.basePath) !== null && _a !== void 0 ? _a : options.currentDirectory,
|
---|
| 217 | };
|
---|
| 218 | const command = new description.impl(context, description, logger);
|
---|
| 219 | // Flush on an interval (if the event loop is waiting).
|
---|
| 220 | let analyticsFlushPromise = Promise.resolve();
|
---|
| 221 | const analyticsFlushInterval = setInterval(() => {
|
---|
| 222 | analyticsFlushPromise = analyticsFlushPromise.then(() => analytics.flush());
|
---|
| 223 | }, 1000);
|
---|
| 224 | const result = await command.validateAndRun(parsedOptions);
|
---|
| 225 | // Flush one last time.
|
---|
| 226 | clearInterval(analyticsFlushInterval);
|
---|
| 227 | await analyticsFlushPromise.then(() => analytics.flush());
|
---|
| 228 | return result;
|
---|
| 229 | }
|
---|
| 230 | catch (e) {
|
---|
| 231 | if (e instanceof parser.ParseArgumentException) {
|
---|
| 232 | logger.fatal('Cannot parse arguments. See below for the reasons.');
|
---|
| 233 | logger.fatal(' ' + e.comments.join('\n '));
|
---|
| 234 | return 1;
|
---|
| 235 | }
|
---|
| 236 | else {
|
---|
| 237 | throw e;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | exports.runCommand = runCommand;
|
---|