Skip to content

Commit

Permalink
refactor(commands): turn .run commands into tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed Feb 8, 2017
1 parent c3a5255 commit f245244
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 223 deletions.
17 changes: 0 additions & 17 deletions packages/@angular/cli/commands/build.run.ts

This file was deleted.

19 changes: 16 additions & 3 deletions packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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,
Expand All @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/@angular/cli/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
99 changes: 0 additions & 99 deletions packages/@angular/cli/commands/init.run.ts

This file was deleted.

11 changes: 10 additions & 1 deletion packages/@angular/cli/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
67 changes: 0 additions & 67 deletions packages/@angular/cli/commands/serve.run.ts

This file was deleted.

79 changes: 69 additions & 10 deletions packages/@angular/cli/commands/serve.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -32,16 +38,16 @@ 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,
default: defaultHost,
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,
Expand All @@ -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,
Expand All @@ -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 changes: 17 additions & 0 deletions packages/@angular/cli/models/build-options.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit f245244

Please sign in to comment.