forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.ts
More file actions
110 lines (88 loc) · 3.29 KB
/
add.ts
File metadata and controls
110 lines (88 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// tslint:disable:no-global-tslint-disable no-any file-header
import { tags, terminal } from '@angular-devkit/core';
import { NodePackageDoesNotSupportSchematics } from '@angular-devkit/schematics/tools';
import { CommandScope, Option } from '../models/command';
import { parseOptions } from '../models/command-runner';
import { SchematicCommand } from '../models/schematic-command';
import { NpmInstall } from '../tasks/npm-install';
import { getPackageManager } from '../utilities/config';
export default class AddCommand extends SchematicCommand {
readonly name = 'add';
readonly description = 'Add support for a library to your project.';
readonly allowPrivateSchematics = true;
scope = CommandScope.inProject;
arguments = ['collection'];
options: Option[] = [];
private async _parseSchematicOptions(collectionName: string): Promise<any> {
const schematicOptions = await this.getOptions({
schematicName: 'ng-add',
collectionName,
});
const options = this.options.concat(schematicOptions.options);
const args = schematicOptions.arguments.map(arg => arg.name);
return parseOptions(this._rawArgs, options, args, this.argStrategy);
}
validate(options: any) {
const collectionName = options._[0];
if (!collectionName) {
this.logger.fatal(
`The "ng ${this.name}" command requires a name argument to be specified eg. `
+ `${terminal.yellow('ng add [name] ')}. For more details, use "ng help".`,
);
return false;
}
return true;
}
async run(options: any) {
const firstArg = options._[0];
if (!firstArg) {
this.logger.fatal(
`The "ng ${this.name}" command requires a name argument to be specified eg. `
+ `${terminal.yellow('ng add [name] ')}. For more details, use "ng help".`,
);
return 1;
}
const packageManager = getPackageManager();
const npmInstall: NpmInstall = require('../tasks/npm-install').default;
const packageName = firstArg.startsWith('@')
? firstArg.split('/', 2).join('/')
: firstArg.split('/', 1)[0];
// Remove the tag/version from the package name.
const collectionName = (
packageName.startsWith('@')
? packageName.split('@', 2).join('@')
: packageName.split('@', 1).join('@')
) + firstArg.slice(packageName.length);
// We don't actually add the package to package.json, that would be the work of the package
// itself.
await npmInstall(
packageName,
this.logger,
packageManager,
this.project.root,
);
// Reparse the options with the new schematic accessible.
options = await this._parseSchematicOptions(collectionName);
const runOptions = {
schematicOptions: options,
workingDir: this.project.root,
collectionName,
schematicName: 'ng-add',
allowPrivate: true,
dryRun: false,
force: false,
};
try {
return await this.runSchematic(runOptions);
} catch (e) {
if (e instanceof NodePackageDoesNotSupportSchematics) {
this.logger.error(tags.oneLine`
The package that you are trying to add does not support schematics. You can try using
a different version of the package or contact the package author to add ng-add support.
`);
return 1;
}
throw e;
}
}
}