Skip to content

Commit b8328dc

Browse files
authored
refactor(commands): turn .run commands into tasks (#4251)
1 parent c3a5255 commit b8328dc

File tree

11 files changed

+224
-223
lines changed

11 files changed

+224
-223
lines changed

packages/@angular/cli/commands/build.run.ts

-17
This file was deleted.

packages/@angular/cli/commands/build.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { BuildOptions } from '../models/webpack-config';
1+
import { BuildOptions } from '../models/build-options';
2+
import { Version } from '../upgrade/version';
23

34
const Command = require('../ember-cli/lib/models/command');
45

@@ -22,7 +23,7 @@ export const BaseBuildCommandOptions: any = [
2223
{ name: 'i18n-file', type: String },
2324
{ name: 'i18n-format', type: String },
2425
{ name: 'locale', type: String },
25-
{ name: 'extract-css', type: Boolean, aliases: ['ec']},
26+
{ name: 'extract-css', type: Boolean, aliases: ['ec'] },
2627
{
2728
name: 'output-hashing',
2829
type: String,
@@ -46,7 +47,19 @@ const BuildCommand = Command.extend({
4647
]),
4748

4849
run: function (commandOptions: BuildTaskOptions) {
49-
return require('./build.run').default.call(this, commandOptions);
50+
const project = this.project;
51+
52+
// Check angular version.
53+
Version.assertAngularVersionIs2_3_1OrHigher(project.root);
54+
55+
const BuildTask = require('../tasks/build').default;
56+
57+
const buildTask = new BuildTask({
58+
cliProject: project,
59+
ui: this.ui,
60+
});
61+
62+
return buildTask.run(commandOptions);
5063
}
5164
});
5265

packages/@angular/cli/commands/help.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const HelpCommand = Command.extend({
2222
run: function (commandOptions: any, rawArgs: any) {
2323
let commandFiles = fs.readdirSync(__dirname)
2424
// Remove files that are not JavaScript or Typescript
25-
.filter(file => file.match(/\.(j|t)s$/) && !file.match(/\.d.ts$/) && !file.match(/\.run.ts$/))
25+
.filter(file => file.match(/\.(j|t)s$/) && !file.match(/\.d.ts$/))
2626
.map(file => path.parse(file).name)
2727
.map(file => file.toLowerCase());
2828

packages/@angular/cli/commands/init.run.ts

-99
This file was deleted.

packages/@angular/cli/commands/init.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ const InitCommand: any = Command.extend({
2626
anonymousOptions: ['<glob-pattern>'],
2727

2828
run: function (commandOptions: any, rawArgs: string[]) {
29-
return require('./init.run').default.call(this, commandOptions, rawArgs);
29+
const InitTask = require('../tasks/init').default;
30+
31+
const initTask = new InitTask({
32+
cliProject: this.project,
33+
project: this.project,
34+
tasks: this.tasks,
35+
ui: this.ui,
36+
});
37+
38+
return initTask.run(commandOptions, rawArgs);
3039
}
3140
});
3241

packages/@angular/cli/commands/serve.run.ts

-67
This file was deleted.

packages/@angular/cli/commands/serve.ts

+69-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { BuildOptions } from '../models/webpack-config';
1+
import * as denodeify from 'denodeify';
2+
import { BuildOptions } from '../models/build-options';
23
import { BaseBuildCommandOptions } from './build';
34
import { CliConfig } from '../models/config';
5+
import { Version } from '../upgrade/version';
6+
import { ServeTaskOptions } from './serve';
7+
8+
const SilentError = require('silent-error');
49
const PortFinder = require('portfinder');
510
const Command = require('../ember-cli/lib/models/command');
6-
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
11+
const getPort = <any>denodeify(PortFinder.getPort);
712

813
PortFinder.basePort = 49152;
914

15+
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
1016
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
1117
const defaultHost = config.get('defaults.serve.host');
1218

@@ -32,16 +38,16 @@ const ServeCommand = Command.extend({
3238
aliases: ['server', 's'],
3339

3440
availableOptions: BaseBuildCommandOptions.concat([
35-
{ name: 'port', type: Number, default: defaultPort, aliases: ['p'] },
41+
{ name: 'port', type: Number, default: defaultPort, aliases: ['p'] },
3642
{
3743
name: 'host',
3844
type: String,
3945
default: defaultHost,
4046
aliases: ['H'],
4147
description: `Listens only on ${defaultHost} by default`
4248
},
43-
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
44-
{ name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] },
49+
{ name: 'proxy-config', type: 'Path', aliases: ['pc'] },
50+
{ name: 'live-reload', type: Boolean, default: true, aliases: ['lr'] },
4551
{
4652
name: 'live-reload-host',
4753
type: String,
@@ -66,9 +72,9 @@ const ServeCommand = Command.extend({
6672
default: true,
6773
description: 'Whether to live reload CSS (default true)'
6874
},
69-
{ name: 'ssl', type: Boolean, default: false },
70-
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
71-
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
75+
{ name: 'ssl', type: Boolean, default: false },
76+
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
77+
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
7278
{
7379
name: 'open',
7480
type: Boolean,
@@ -84,9 +90,62 @@ const ServeCommand = Command.extend({
8490
}
8591
]),
8692

87-
run: function(commandOptions: ServeTaskOptions) {
88-
return require('./serve.run').default.call(this, commandOptions);
93+
run: function (commandOptions: ServeTaskOptions) {
94+
const ServeTask = require('../tasks/serve').default;
95+
96+
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
97+
commandOptions.liveReloadHost = commandOptions.liveReloadHost || commandOptions.host;
98+
99+
return checkExpressPort(commandOptions)
100+
.then(() => autoFindLiveReloadPort(commandOptions))
101+
.then((opts: ServeTaskOptions) => {
102+
const serve = new ServeTask({
103+
ui: this.ui,
104+
project: this.project,
105+
});
106+
107+
return serve.run(opts);
108+
});
89109
}
90110
});
91111

112+
function checkExpressPort(commandOptions: ServeTaskOptions) {
113+
return getPort({ port: commandOptions.port, host: commandOptions.host })
114+
.then((foundPort: number) => {
115+
116+
if (commandOptions.port !== foundPort && commandOptions.port !== 0) {
117+
throw new SilentError(
118+
`Port ${commandOptions.port} is already in use. Use '--port' to specify a different port.`
119+
);
120+
}
121+
122+
// otherwise, our found port is good
123+
commandOptions.port = foundPort;
124+
return commandOptions;
125+
126+
});
127+
}
128+
129+
function autoFindLiveReloadPort(commandOptions: ServeTaskOptions) {
130+
return getPort({ port: commandOptions.liveReloadPort, host: commandOptions.liveReloadHost })
131+
.then((foundPort: number) => {
132+
133+
// if live reload port matches express port, try one higher
134+
if (foundPort === commandOptions.port) {
135+
commandOptions.liveReloadPort = foundPort + 1;
136+
return autoFindLiveReloadPort(commandOptions);
137+
}
138+
139+
// port was already open
140+
if (foundPort === commandOptions.liveReloadPort) {
141+
return commandOptions;
142+
}
143+
144+
// use found port as live reload port
145+
commandOptions.liveReloadPort = foundPort;
146+
return commandOptions;
147+
148+
});
149+
}
150+
92151
export default ServeCommand;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface BuildOptions {
2+
target?: string;
3+
environment?: string;
4+
outputPath?: string;
5+
aot?: boolean;
6+
sourcemap?: boolean;
7+
vendorChunk?: boolean;
8+
baseHref?: string;
9+
deployUrl?: string;
10+
verbose?: boolean;
11+
progress?: boolean;
12+
i18nFile?: string;
13+
i18nFormat?: string;
14+
locale?: string;
15+
extractCss?: boolean;
16+
outputHashing?: string;
17+
}

0 commit comments

Comments
 (0)