UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const core_1 = require("@angular-devkit/core");
12const tools_1 = require("@angular-devkit/schematics/tools");
13const command_1 = require("../models/command");
14const command_runner_1 = require("../models/command-runner");
15const config_1 = require("../utilities/config");
16const schematic_command_1 = require("../models/schematic-command");
17const SilentError = require('silent-error');
18class AddCommand extends schematic_command_1.SchematicCommand {
19 constructor() {
20 super(...arguments);
21 this.name = 'add';
22 this.description = 'Add support for a library to your project.';
23 this.allowPrivateSchematics = true;
24 this.scope = command_1.CommandScope.inProject;
25 this.arguments = ['collection'];
26 this.options = [];
27 }
28 _parseSchematicOptions(collectionName) {
29 return __awaiter(this, void 0, void 0, function* () {
30 const schematicOptions = yield this.getOptions({
31 schematicName: 'ng-add',
32 collectionName,
33 });
34 const options = this.options.concat(schematicOptions.options);
35 const args = schematicOptions.arguments.map(arg => arg.name);
36 return command_runner_1.parseOptions(this._rawArgs, options, args, this.argStrategy);
37 });
38 }
39 validate(options) {
40 const collectionName = options._[0];
41 if (!collectionName) {
42 throw new SilentError(`The "ng ${this.name}" command requires a name argument to be specified eg. `
43 + `${core_1.terminal.yellow('ng add [name] ')}. For more details, use "ng help".`);
44 }
45 return true;
46 }
47 run(options) {
48 return __awaiter(this, void 0, void 0, function* () {
49 const firstArg = options._[0];
50 if (!firstArg) {
51 throw new SilentError(`The "ng ${this.name}" command requires a name argument to be specified eg. `
52 + `${core_1.terminal.yellow('ng add [name] ')}. For more details, use "ng help".`);
53 }
54 const packageManager = config_1.getPackageManager();
55 const npmInstall = require('../tasks/npm-install').default;
56 const packageName = firstArg.startsWith('@')
57 ? firstArg.split('/', 2).join('/')
58 : firstArg.split('/', 1)[0];
59 // Remove the tag/version from the package name.
60 const collectionName = (packageName.startsWith('@')
61 ? packageName.split('@', 2).join('@')
62 : packageName.split('@', 1).join('@')) + firstArg.slice(packageName.length);
63 // We don't actually add the package to package.json, that would be the work of the package
64 // itself.
65 yield npmInstall(packageName, this.logger, packageManager, this.project.root);
66 // Reparse the options with the new schematic accessible.
67 options = yield this._parseSchematicOptions(collectionName);
68 const runOptions = {
69 schematicOptions: options,
70 workingDir: this.project.root,
71 collectionName,
72 schematicName: 'ng-add',
73 allowPrivate: true,
74 dryRun: false,
75 force: false,
76 };
77 try {
78 return yield this.runSchematic(runOptions);
79 }
80 catch (e) {
81 if (e instanceof tools_1.NodePackageDoesNotSupportSchematics) {
82 throw new SilentError(core_1.tags.oneLine `
83 The package that you are trying to add does not support schematics. You can try using
84 a different version of the package or contact the package author to add ng-add support.
85 `);
86 }
87 throw e;
88 }
89 });
90 }
91}
92exports.default = AddCommand;
93//# sourceMappingURL=/Users/hansl/Sources/hansl/angular-cli/commands/add.js.map
\No newline at end of file