|
| 1 | +import chalk from 'chalk'; |
| 2 | +import { Command, CommandScope, Option } from '../models/command'; |
| 3 | +import { parseOptions } from '../models/command-runner'; |
| 4 | +import { CliConfig } from '../models/config'; |
| 5 | +import { SchematicAvailableOptions } from '../tasks/schematic-get-options'; |
| 6 | + |
| 7 | +const SilentError = require('silent-error'); |
| 8 | + |
| 9 | + |
| 10 | +export default class AddCommand extends Command { |
| 11 | + readonly name = 'add'; |
| 12 | + readonly description = 'Add support for a library to your project.'; |
| 13 | + scope = CommandScope.inProject; |
| 14 | + arguments = ['collection']; |
| 15 | + options: Option[] = []; |
| 16 | + |
| 17 | + private async _parseSchematicOptions(collectionName: string): Promise<any> { |
| 18 | + const SchematicGetOptionsTask = require('../tasks/schematic-get-options').default; |
| 19 | + |
| 20 | + const getOptionsTask = new SchematicGetOptionsTask({ |
| 21 | + ui: this.ui, |
| 22 | + project: this.project |
| 23 | + }); |
| 24 | + |
| 25 | + const availableOptions: SchematicAvailableOptions[] = await getOptionsTask.run({ |
| 26 | + schematicName: 'ng-add', |
| 27 | + collectionName, |
| 28 | + }); |
| 29 | + |
| 30 | + const options = this.options.concat(availableOptions || []); |
| 31 | + |
| 32 | + return parseOptions(this._rawArgs, options, []); |
| 33 | + } |
| 34 | + |
| 35 | + validate(options: any) { |
| 36 | + const collectionName = options.collection; |
| 37 | + |
| 38 | + if (!collectionName) { |
| 39 | + throw new SilentError( |
| 40 | + `The "ng ${this.name}" command requires a name argument to be specified eg. ` |
| 41 | + + `${chalk.yellow('ng add [name] ')}. For more details, use "ng help".` |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + async run(commandOptions: any) { |
| 49 | + const collectionName = commandOptions.collection; |
| 50 | + |
| 51 | + if (!collectionName) { |
| 52 | + throw new SilentError( |
| 53 | + `The "ng ${this.name}" command requires a name argument to be specified eg. ` |
| 54 | + + `${chalk.yellow('ng add [name] ')}. For more details, use "ng help".` |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + const packageManager = CliConfig.fromGlobal().get('packageManager'); |
| 59 | + |
| 60 | + const NpmInstall = require('../tasks/npm-install').default; |
| 61 | + const SchematicRunTask = require('../tasks/schematic-run').default; |
| 62 | + |
| 63 | + const packageName = collectionName.startsWith('@') |
| 64 | + ? collectionName.split('/', 2).join('/') |
| 65 | + : collectionName.split('/', 1)[0]; |
| 66 | + |
| 67 | + // We don't actually add the package to package.json, that would be the work of the package |
| 68 | + // itself. |
| 69 | + let npmInstall = new NpmInstall({ |
| 70 | + ui: this.ui, |
| 71 | + project: this.project, |
| 72 | + packageManager, |
| 73 | + packageName, |
| 74 | + save: false, |
| 75 | + }); |
| 76 | + |
| 77 | + const schematicRunTask = new SchematicRunTask({ |
| 78 | + ui: this.ui, |
| 79 | + project: this.project |
| 80 | + }); |
| 81 | + |
| 82 | + await npmInstall.run(); |
| 83 | + |
| 84 | + // Reparse the options with the new schematic accessible. |
| 85 | + commandOptions = await this._parseSchematicOptions(collectionName); |
| 86 | + |
| 87 | + const runOptions = { |
| 88 | + taskOptions: commandOptions, |
| 89 | + workingDir: this.project.root, |
| 90 | + collectionName, |
| 91 | + schematicName: 'ng-add', |
| 92 | + allowPrivate: true, |
| 93 | + }; |
| 94 | + |
| 95 | + await schematicRunTask.run(runOptions); |
| 96 | + } |
| 97 | +} |
0 commit comments