mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-18 03:23:57 +08:00
refactor(commands): turn .run commands into tasks (#4251)
This commit is contained in:
parent
c3a5255f1b
commit
b8328dca55
@ -1,17 +0,0 @@
|
||||
import { Version } from '../upgrade/version';
|
||||
import Build from '../tasks/build';
|
||||
import { BuildTaskOptions } from './build';
|
||||
|
||||
export default function buildRun(commandOptions: BuildTaskOptions) {
|
||||
const project = this.project;
|
||||
|
||||
// Check angular version.
|
||||
Version.assertAngularVersionIs2_3_1OrHigher(project.root);
|
||||
|
||||
const buildTask = new Build({
|
||||
cliProject: project,
|
||||
ui: this.ui,
|
||||
});
|
||||
|
||||
return buildTask.run(commandOptions);
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import { BuildOptions } from '../models/webpack-config';
|
||||
import { BuildOptions } from '../models/build-options';
|
||||
import { Version } from '../upgrade/version';
|
||||
|
||||
const Command = require('../ember-cli/lib/models/command');
|
||||
|
||||
@ -22,7 +23,7 @@ export const BaseBuildCommandOptions: any = [
|
||||
{ name: 'i18n-file', type: String },
|
||||
{ name: 'i18n-format', type: String },
|
||||
{ name: 'locale', type: String },
|
||||
{ name: 'extract-css', type: Boolean, aliases: ['ec']},
|
||||
{ name: 'extract-css', type: Boolean, aliases: ['ec'] },
|
||||
{
|
||||
name: 'output-hashing',
|
||||
type: String,
|
||||
@ -46,7 +47,19 @@ const BuildCommand = Command.extend({
|
||||
]),
|
||||
|
||||
run: function (commandOptions: BuildTaskOptions) {
|
||||
return require('./build.run').default.call(this, commandOptions);
|
||||
const project = this.project;
|
||||
|
||||
// Check angular version.
|
||||
Version.assertAngularVersionIs2_3_1OrHigher(project.root);
|
||||
|
||||
const BuildTask = require('../tasks/build').default;
|
||||
|
||||
const buildTask = new BuildTask({
|
||||
cliProject: project,
|
||||
ui: this.ui,
|
||||
});
|
||||
|
||||
return buildTask.run(commandOptions);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -22,7 +22,7 @@ const HelpCommand = Command.extend({
|
||||
run: function (commandOptions: any, rawArgs: any) {
|
||||
let commandFiles = fs.readdirSync(__dirname)
|
||||
// Remove files that are not JavaScript or Typescript
|
||||
.filter(file => file.match(/\.(j|t)s$/) && !file.match(/\.d.ts$/) && !file.match(/\.run.ts$/))
|
||||
.filter(file => file.match(/\.(j|t)s$/) && !file.match(/\.d.ts$/))
|
||||
.map(file => path.parse(file).name)
|
||||
.map(file => file.toLowerCase());
|
||||
|
||||
|
@ -1,99 +0,0 @@
|
||||
import * as chalk from 'chalk';
|
||||
import LinkCli from '../tasks/link-cli';
|
||||
import NpmInstall from '../tasks/npm-install';
|
||||
import { validateProjectName } from '../utilities/validate-project-name';
|
||||
|
||||
const Promise = require('../ember-cli/lib/ext/promise');
|
||||
const SilentError = require('silent-error');
|
||||
const normalizeBlueprint = require('../ember-cli/lib/utilities/normalize-blueprint-option');
|
||||
const GitInit = require('../tasks/git-init');
|
||||
|
||||
|
||||
export default function initRun(commandOptions: any, rawArgs: string[]) {
|
||||
if (commandOptions.dryRun) {
|
||||
commandOptions.skipNpm = true;
|
||||
}
|
||||
|
||||
const installBlueprint = new this.tasks.InstallBlueprint({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
|
||||
// needs an explicit check in case it's just 'undefined'
|
||||
// due to passing of options from 'new' and 'addon'
|
||||
let gitInit: any;
|
||||
if (commandOptions.skipGit === false) {
|
||||
gitInit = new GitInit({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
}
|
||||
|
||||
let npmInstall: any;
|
||||
if (!commandOptions.skipNpm) {
|
||||
npmInstall = new NpmInstall({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
}
|
||||
|
||||
let linkCli: any;
|
||||
if (commandOptions.linkCli) {
|
||||
linkCli = new LinkCli({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
}
|
||||
|
||||
const project = this.project;
|
||||
const packageName = commandOptions.name !== '.' && commandOptions.name || project.name();
|
||||
|
||||
if (!packageName) {
|
||||
const message = 'The `ng ' + this.name + '` command requires a ' +
|
||||
'package.json in current folder with name attribute or a specified name via arguments. ' +
|
||||
'For more details, use `ng help`.';
|
||||
|
||||
return Promise.reject(new SilentError(message));
|
||||
}
|
||||
|
||||
const blueprintOpts = {
|
||||
dryRun: commandOptions.dryRun,
|
||||
blueprint: 'ng2',
|
||||
rawName: packageName,
|
||||
targetFiles: rawArgs || '',
|
||||
rawArgs: rawArgs.toString(),
|
||||
sourceDir: commandOptions.sourceDir,
|
||||
style: commandOptions.style,
|
||||
prefix: commandOptions.prefix,
|
||||
routing: commandOptions.routing,
|
||||
inlineStyle: commandOptions.inlineStyle,
|
||||
inlineTemplate: commandOptions.inlineTemplate,
|
||||
ignoredUpdateFiles: ['favicon.ico'],
|
||||
skipGit: commandOptions.skipGit,
|
||||
skipTests: commandOptions.skipTests
|
||||
};
|
||||
|
||||
validateProjectName(packageName);
|
||||
|
||||
blueprintOpts.blueprint = normalizeBlueprint(blueprintOpts.blueprint);
|
||||
|
||||
return installBlueprint.run(blueprintOpts)
|
||||
.then(function () {
|
||||
if (commandOptions.skipGit === false) {
|
||||
return gitInit.run(commandOptions, rawArgs);
|
||||
}
|
||||
})
|
||||
.then(function () {
|
||||
if (!commandOptions.skipNpm) {
|
||||
return npmInstall.run();
|
||||
}
|
||||
})
|
||||
.then(function () {
|
||||
if (commandOptions.linkCli) {
|
||||
return linkCli.run();
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
this.ui.writeLine(chalk.green(`Project '${packageName}' successfully created.`));
|
||||
});
|
||||
}
|
@ -26,7 +26,16 @@ const InitCommand: any = Command.extend({
|
||||
anonymousOptions: ['<glob-pattern>'],
|
||||
|
||||
run: function (commandOptions: any, rawArgs: string[]) {
|
||||
return require('./init.run').default.call(this, commandOptions, rawArgs);
|
||||
const InitTask = require('../tasks/init').default;
|
||||
|
||||
const initTask = new InitTask({
|
||||
cliProject: this.project,
|
||||
project: this.project,
|
||||
tasks: this.tasks,
|
||||
ui: this.ui,
|
||||
});
|
||||
|
||||
return initTask.run(commandOptions, rawArgs);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -1,67 +0,0 @@
|
||||
import * as denodeify from 'denodeify';
|
||||
const assign = require('lodash/assign');
|
||||
const SilentError = require('silent-error');
|
||||
const PortFinder = require('portfinder');
|
||||
import ServeTask from '../tasks/serve';
|
||||
import { Version } from '../upgrade/version';
|
||||
import { ServeTaskOptions } from './serve';
|
||||
|
||||
PortFinder.basePort = 49152;
|
||||
|
||||
const getPort = <any>denodeify(PortFinder.getPort);
|
||||
|
||||
export default function serveRun(commandOptions: ServeTaskOptions) {
|
||||
// Check angular version.
|
||||
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
|
||||
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
|
||||
|
||||
return checkExpressPort(commandOptions)
|
||||
.then(() => autoFindLiveReloadPort(commandOptions))
|
||||
.then((opts: ServeTaskOptions) => {
|
||||
const serve = new ServeTask({
|
||||
ui: this.ui,
|
||||
project: this.project,
|
||||
});
|
||||
|
||||
return serve.run(opts);
|
||||
});
|
||||
}
|
||||
|
||||
function checkExpressPort(commandOptions: ServeTaskOptions) {
|
||||
return getPort({ port: commandOptions.port, host: commandOptions.host })
|
||||
.then((foundPort: number) => {
|
||||
|
||||
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
|
||||
throw new SilentError(
|
||||
`Port ${commandOptions.port} is already in use. Use '--port' to specify a different port.`
|
||||
);
|
||||
}
|
||||
|
||||
// otherwise, our found port is good
|
||||
commandOptions.port = foundPort;
|
||||
return commandOptions;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function autoFindLiveReloadPort(commandOptions: ServeTaskOptions) {
|
||||
return getPort({ port: commandOptions.liveReloadPort, host: commandOptions.liveReloadHost })
|
||||
.then((foundPort: number) => {
|
||||
|
||||
// if live reload port matches express port, try one higher
|
||||
if (foundPort === commandOptions.port) {
|
||||
commandOptions.liveReloadPort = foundPort + 1;
|
||||
return autoFindLiveReloadPort(commandOptions);
|
||||
}
|
||||
|
||||
// port was already open
|
||||
if (foundPort === commandOptions.liveReloadPort) {
|
||||
return commandOptions;
|
||||
}
|
||||
|
||||
// use found port as live reload port
|
||||
commandOptions.liveReloadPort = foundPort;
|
||||
return commandOptions;
|
||||
|
||||
});
|
||||
}
|
@ -1,12 +1,18 @@
|
||||
import { BuildOptions } from '../models/webpack-config';
|
||||
import * as denodeify from 'denodeify';
|
||||
import { BuildOptions } from '../models/build-options';
|
||||
import { BaseBuildCommandOptions } from './build';
|
||||
import { CliConfig } from '../models/config';
|
||||
import { Version } from '../upgrade/version';
|
||||
import { ServeTaskOptions } from './serve';
|
||||
|
||||
const SilentError = require('silent-error');
|
||||
const PortFinder = require('portfinder');
|
||||
const Command = require('../ember-cli/lib/models/command');
|
||||
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
|
||||
const getPort = <any>denodeify(PortFinder.getPort);
|
||||
|
||||
PortFinder.basePort = 49152;
|
||||
|
||||
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
|
||||
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
|
||||
const defaultHost = config.get('defaults.serve.host');
|
||||
|
||||
@ -32,7 +38,7 @@ const ServeCommand = Command.extend({
|
||||
aliases: ['server', 's'],
|
||||
|
||||
availableOptions: BaseBuildCommandOptions.concat([
|
||||
{ name: 'port', type: Number, default: defaultPort, aliases: ['p'] },
|
||||
{ name: 'port', type: Number, default: defaultPort, aliases: ['p'] },
|
||||
{
|
||||
name: 'host',
|
||||
type: String,
|
||||
@ -40,8 +46,8 @@ const ServeCommand = Command.extend({
|
||||
aliases: ['H'],
|
||||
description: `Listens only on ${defaultHost} by default`
|
||||
},
|
||||
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
|
||||
{ name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] },
|
||||
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
|
||||
{ name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] },
|
||||
{
|
||||
name: 'live-reload-host',
|
||||
type: String,
|
||||
@ -66,9 +72,9 @@ const ServeCommand = Command.extend({
|
||||
default: true,
|
||||
description: 'Whether to live reload CSS (default true)'
|
||||
},
|
||||
{ name: 'ssl', type: Boolean, default: false },
|
||||
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
|
||||
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
|
||||
{ name: 'ssl', type: Boolean, default: false },
|
||||
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
|
||||
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
|
||||
{
|
||||
name: 'open',
|
||||
type: Boolean,
|
||||
@ -84,9 +90,62 @@ const ServeCommand = Command.extend({
|
||||
}
|
||||
]),
|
||||
|
||||
run: function(commandOptions: ServeTaskOptions) {
|
||||
return require('./serve.run').default.call(this, commandOptions);
|
||||
run: function (commandOptions: ServeTaskOptions) {
|
||||
const ServeTask = require('../tasks/serve').default;
|
||||
|
||||
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
|
||||
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
|
||||
|
||||
return checkExpressPort(commandOptions)
|
||||
.then(() => autoFindLiveReloadPort(commandOptions))
|
||||
.then((opts: ServeTaskOptions) => {
|
||||
const serve = new ServeTask({
|
||||
ui: this.ui,
|
||||
project: this.project,
|
||||
});
|
||||
|
||||
return serve.run(opts);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function checkExpressPort(commandOptions: ServeTaskOptions) {
|
||||
return getPort({ port: commandOptions.port, host: commandOptions.host })
|
||||
.then((foundPort: number) => {
|
||||
|
||||
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
|
||||
throw new SilentError(
|
||||
`Port ${commandOptions.port} is already in use. Use '--port' to specify a different port.`
|
||||
);
|
||||
}
|
||||
|
||||
// otherwise, our found port is good
|
||||
commandOptions.port = foundPort;
|
||||
return commandOptions;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function autoFindLiveReloadPort(commandOptions: ServeTaskOptions) {
|
||||
return getPort({ port: commandOptions.liveReloadPort, host: commandOptions.liveReloadHost })
|
||||
.then((foundPort: number) => {
|
||||
|
||||
// if live reload port matches express port, try one higher
|
||||
if (foundPort === commandOptions.port) {
|
||||
commandOptions.liveReloadPort = foundPort + 1;
|
||||
return autoFindLiveReloadPort(commandOptions);
|
||||
}
|
||||
|
||||
// port was already open
|
||||
if (foundPort === commandOptions.liveReloadPort) {
|
||||
return commandOptions;
|
||||
}
|
||||
|
||||
// use found port as live reload port
|
||||
commandOptions.liveReloadPort = foundPort;
|
||||
return commandOptions;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
export default ServeCommand;
|
||||
|
17
packages/@angular/cli/models/build-options.ts
Normal file
17
packages/@angular/cli/models/build-options.ts
Normal file
@ -0,0 +1,17 @@
|
||||
export interface BuildOptions {
|
||||
target?: string;
|
||||
environment?: string;
|
||||
outputPath?: string;
|
||||
aot?: boolean;
|
||||
sourcemap?: boolean;
|
||||
vendorChunk?: boolean;
|
||||
baseHref?: string;
|
||||
deployUrl?: string;
|
||||
verbose?: boolean;
|
||||
progress?: boolean;
|
||||
i18nFile?: string;
|
||||
i18nFormat?: string;
|
||||
locale?: string;
|
||||
extractCss?: boolean;
|
||||
outputHashing?: string;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
const webpackMerge = require('webpack-merge');
|
||||
import { CliConfig } from './config';
|
||||
import { BuildOptions } from './build-options';
|
||||
import {
|
||||
getCommonConfig,
|
||||
getDevConfig,
|
||||
@ -11,24 +12,6 @@ import {
|
||||
|
||||
const path = require('path');
|
||||
|
||||
export interface BuildOptions {
|
||||
target?: string;
|
||||
environment?: string;
|
||||
outputPath?: string;
|
||||
aot?: boolean;
|
||||
sourcemap?: boolean;
|
||||
vendorChunk?: boolean;
|
||||
baseHref?: string;
|
||||
deployUrl?: string;
|
||||
verbose?: boolean;
|
||||
progress?: boolean;
|
||||
i18nFile?: string;
|
||||
i18nFormat?: string;
|
||||
locale?: string;
|
||||
extractCss?: boolean;
|
||||
outputHashing?: string;
|
||||
}
|
||||
|
||||
export interface WebpackConfigOptions {
|
||||
projectRoot: string;
|
||||
buildOptions: BuildOptions;
|
||||
|
103
packages/@angular/cli/tasks/init.ts
Normal file
103
packages/@angular/cli/tasks/init.ts
Normal file
@ -0,0 +1,103 @@
|
||||
import * as chalk from 'chalk';
|
||||
import LinkCli from '../tasks/link-cli';
|
||||
import NpmInstall from '../tasks/npm-install';
|
||||
import { validateProjectName } from '../utilities/validate-project-name';
|
||||
|
||||
const Task = require('../ember-cli/lib/models/task');
|
||||
const Promise = require('../ember-cli/lib/ext/promise');
|
||||
const SilentError = require('silent-error');
|
||||
const normalizeBlueprint = require('../ember-cli/lib/utilities/normalize-blueprint-option');
|
||||
const GitInit = require('../tasks/git-init');
|
||||
|
||||
|
||||
export default Task.extend({
|
||||
run: function (commandOptions: any, rawArgs: string[]) {
|
||||
if (commandOptions.dryRun) {
|
||||
commandOptions.skipNpm = true;
|
||||
}
|
||||
|
||||
const installBlueprint = new this.tasks.InstallBlueprint({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
|
||||
// needs an explicit check in case it's just 'undefined'
|
||||
// due to passing of options from 'new' and 'addon'
|
||||
let gitInit: any;
|
||||
if (commandOptions.skipGit === false) {
|
||||
gitInit = new GitInit({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
}
|
||||
|
||||
let npmInstall: any;
|
||||
if (!commandOptions.skipNpm) {
|
||||
npmInstall = new NpmInstall({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
}
|
||||
|
||||
let linkCli: any;
|
||||
if (commandOptions.linkCli) {
|
||||
linkCli = new LinkCli({
|
||||
ui: this.ui,
|
||||
project: this.project
|
||||
});
|
||||
}
|
||||
|
||||
const project = this.project;
|
||||
const packageName = commandOptions.name !== '.' && commandOptions.name || project.name();
|
||||
|
||||
if (!packageName) {
|
||||
const message = 'The `ng ' + this.name + '` command requires a ' +
|
||||
'package.json in current folder with name attribute or a specified name via arguments. ' +
|
||||
'For more details, use `ng help`.';
|
||||
|
||||
return Promise.reject(new SilentError(message));
|
||||
}
|
||||
|
||||
const blueprintOpts = {
|
||||
dryRun: commandOptions.dryRun,
|
||||
blueprint: 'ng2',
|
||||
rawName: packageName,
|
||||
targetFiles: rawArgs || '',
|
||||
rawArgs: rawArgs.toString(),
|
||||
sourceDir: commandOptions.sourceDir,
|
||||
style: commandOptions.style,
|
||||
prefix: commandOptions.prefix,
|
||||
routing: commandOptions.routing,
|
||||
inlineStyle: commandOptions.inlineStyle,
|
||||
inlineTemplate: commandOptions.inlineTemplate,
|
||||
ignoredUpdateFiles: ['favicon.ico'],
|
||||
skipGit: commandOptions.skipGit,
|
||||
skipTests: commandOptions.skipTests
|
||||
};
|
||||
|
||||
validateProjectName(packageName);
|
||||
|
||||
blueprintOpts.blueprint = normalizeBlueprint(blueprintOpts.blueprint);
|
||||
|
||||
return installBlueprint.run(blueprintOpts)
|
||||
.then(function () {
|
||||
if (commandOptions.skipGit === false) {
|
||||
return gitInit.run(commandOptions, rawArgs);
|
||||
}
|
||||
})
|
||||
.then(function () {
|
||||
if (!commandOptions.skipNpm) {
|
||||
return npmInstall.run();
|
||||
}
|
||||
})
|
||||
.then(function () {
|
||||
if (commandOptions.linkCli) {
|
||||
return linkCli.run();
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
this.ui.writeLine(chalk.green(`Project '${packageName}' successfully created.`));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -2,21 +2,21 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as chalk from 'chalk';
|
||||
import * as rimraf from 'rimraf';
|
||||
const SilentError = require('silent-error');
|
||||
const Task = require('../ember-cli/lib/models/task');
|
||||
import * as webpack from 'webpack';
|
||||
const WebpackDevServer = require('webpack-dev-server');
|
||||
import * as url from 'url';
|
||||
import { oneLine, stripIndents } from 'common-tags';
|
||||
import { getWebpackStatsConfig } from '../models/webpack-configs/utils';
|
||||
import { NgCliWebpackConfig } from '../models/webpack-config';
|
||||
import { ServeTaskOptions } from '../commands/serve';
|
||||
import { CliConfig } from '../models/config';
|
||||
import { oneLine } from 'common-tags';
|
||||
import * as url from 'url';
|
||||
import {stripIndents} from 'common-tags';
|
||||
|
||||
const WebpackDevServer = require('webpack-dev-server');
|
||||
const Task = require('../ember-cli/lib/models/task');
|
||||
const SilentError = require('silent-error');
|
||||
const opn = require('opn');
|
||||
|
||||
export default Task.extend({
|
||||
run: function(serveTaskOptions: ServeTaskOptions) {
|
||||
run: function (serveTaskOptions: ServeTaskOptions) {
|
||||
const ui = this.ui;
|
||||
|
||||
let webpackCompiler: any;
|
||||
|
Loading…
x
Reference in New Issue
Block a user