-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular/cli): add the add command
- Loading branch information
Showing
15 changed files
with
266 additions
and
115 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import chalk from 'chalk'; | ||
import { Command, CommandScope, Option } from '../models/command'; | ||
import { parseOptions } from '../models/command-runner'; | ||
import { CliConfig } from '../models/config'; | ||
import { SchematicAvailableOptions } from '../tasks/schematic-get-options'; | ||
|
||
const SilentError = require('silent-error'); | ||
|
||
|
||
export default class AddCommand extends Command { | ||
readonly name = 'add'; | ||
readonly description = 'Add support for a library to your project.'; | ||
scope = CommandScope.inProject; | ||
arguments = ['collection']; | ||
options: Option[] = []; | ||
|
||
private async _parseSchematicOptions(collectionName: string): Promise<any> { | ||
const SchematicGetOptionsTask = require('../tasks/schematic-get-options').default; | ||
|
||
const getOptionsTask = new SchematicGetOptionsTask({ | ||
ui: this.ui, | ||
project: this.project | ||
}); | ||
|
||
const availableOptions: SchematicAvailableOptions[] = await getOptionsTask.run({ | ||
schematicName: 'ng-add', | ||
collectionName, | ||
}); | ||
|
||
const options = this.options.concat(availableOptions || []); | ||
|
||
return parseOptions(this._rawArgs, options, []); | ||
} | ||
|
||
validate(options: any) { | ||
const collectionName = options.collection; | ||
|
||
if (!collectionName) { | ||
throw new SilentError( | ||
`The "ng ${this.name}" command requires a name argument to be specified eg. ` | ||
+ `${chalk.yellow('ng add [name] ')}. For more details, use "ng help".` | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
async run(commandOptions: any) { | ||
const collectionName = commandOptions.collection; | ||
|
||
if (!collectionName) { | ||
throw new SilentError( | ||
`The "ng ${this.name}" command requires a name argument to be specified eg. ` | ||
+ `${chalk.yellow('ng add [name] ')}. For more details, use "ng help".` | ||
); | ||
} | ||
|
||
const packageManager = CliConfig.fromGlobal().get('packageManager'); | ||
|
||
const NpmInstall = require('../tasks/npm-install').default; | ||
const SchematicRunTask = require('../tasks/schematic-run').default; | ||
|
||
const packageName = collectionName.startsWith('@') | ||
? collectionName.split('/', 2).join('/') | ||
: collectionName.split('/', 1)[0]; | ||
|
||
// We don't actually add the package to package.json, that would be the work of the package | ||
// itself. | ||
let npmInstall = new NpmInstall({ | ||
ui: this.ui, | ||
project: this.project, | ||
packageManager, | ||
packageName, | ||
save: false, | ||
}); | ||
|
||
const schematicRunTask = new SchematicRunTask({ | ||
ui: this.ui, | ||
project: this.project | ||
}); | ||
|
||
await npmInstall.run(); | ||
|
||
// Reparse the options with the new schematic accessible. | ||
commandOptions = await this._parseSchematicOptions(collectionName); | ||
|
||
const runOptions = { | ||
taskOptions: commandOptions, | ||
workingDir: this.project.root, | ||
collectionName, | ||
schematicName: 'ng-add', | ||
allowPrivate: true, | ||
}; | ||
|
||
await schematicRunTask.run(runOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ModuleNotFoundException, resolve } from '@angular-devkit/core/node'; | ||
|
||
const Task = require('../ember-cli/lib/models/task'); | ||
import chalk from 'chalk'; | ||
import { spawn } from 'child_process'; | ||
|
||
|
||
export default Task.extend({ | ||
run: async function () { | ||
const ui = this.ui; | ||
let packageManager = this.packageManager; | ||
if (packageManager === 'default') { | ||
packageManager = 'npm'; | ||
} | ||
|
||
ui.writeLine(chalk.green(`Installing packages for tooling via ${packageManager}.`)); | ||
|
||
const installArgs = ['install']; | ||
if (packageManager === 'npm') { | ||
installArgs.push('--quiet'); | ||
} | ||
if (this.packageName) { | ||
try { | ||
// Verify if we need to install the package (it might already be there). | ||
// If it's available and we shouldn't save, simply return. Nothing to be done. | ||
resolve(this.packageName, { checkLocal: true, basedir: this.project.root }); | ||
|
||
if (!this.save) { | ||
return; | ||
} | ||
} catch (e) { | ||
if (!(e instanceof ModuleNotFoundException)) { | ||
throw e; | ||
} | ||
} | ||
installArgs.push(this.packageName); | ||
} | ||
|
||
if (!this.save) { | ||
installArgs.push('--no-save'); | ||
} | ||
const installOptions = { | ||
stdio: 'inherit', | ||
shell: true | ||
}; | ||
|
||
await new Promise((resolve, reject) => { | ||
spawn(packageManager, installArgs, installOptions) | ||
.on('close', (code: number) => { | ||
if (code === 0) { | ||
ui.writeLine(chalk.green(`Installed packages for tooling via ${packageManager}.`)); | ||
resolve(); | ||
} else { | ||
const message = 'Package install failed, see above.'; | ||
ui.writeLine(chalk.red(message)); | ||
reject(message); | ||
} | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "@angular-devkit-tests/ng-add-simple", | ||
"schematics": { | ||
"ng-add": { | ||
"factory": "./index.js", | ||
"description": "Create an empty application" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.default = () => tree => tree.create('/ng-add-test', 'hello world'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "@angular-devkit-tests/ng-add-simple", | ||
"version": "1.0.0", | ||
"schematics": "./collection.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"schematics": { | ||
"ng-add": { | ||
"factory": "./index.js", | ||
"description": "Add empty file to your application." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.default = (options) => tree => tree.create(options.name || 'empty-file', ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "empty-app", | ||
"schematics": "./collection.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { assetDir } from '../../../utils/assets'; | ||
import { expectFileToExist, symlinkFile } from '../../../utils/fs'; | ||
import { ng } from '../../../utils/process'; | ||
|
||
|
||
export default async function () { | ||
await ng('add', '@angular-devkit-tests/ng-add-simple'); | ||
await expectFileToExist('ng-add-test'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { assetDir } from '../../../utils/assets'; | ||
import { expectFileToExist, symlinkFile } from '../../../utils/fs'; | ||
import { ng } from '../../../utils/process'; | ||
import { expectToFail } from '../../../utils/utils'; | ||
|
||
|
||
export default async function () { | ||
await symlinkFile(assetDir('add-collection'), `./node_modules/add-collection`, 'dir'); | ||
|
||
await ng('add', 'add-collection'); | ||
await expectFileToExist('empty-file'); | ||
|
||
await ng('add', 'add-collection', '--name=blah'); | ||
await expectFileToExist('blah'); | ||
|
||
// TODO: reenable this check when schematics fail the CLI command. | ||
await expectToFail(() => ng('add', 'add-collection')); // File already exists. | ||
} |