Skip to content

Commit 170bca6

Browse files
committed
Add ArchitectCommand and update build command
1 parent 324e0c5 commit 170bca6

File tree

6 files changed

+173
-66
lines changed

6 files changed

+173
-66
lines changed
+69-52
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,99 @@
1-
import { Command, Option, CommandScope } from '../models/command';
2-
import { BuildOptions } from '../models/build-options';
1+
import { ArchitectCommand } from '../models/architect-command';
2+
import { Option, CommandScope } from '../models/command';
3+
// import { BuildOptions } from '../models/build-options';
34
import { Version } from '../upgrade/version';
4-
import { getAppFromConfig } from '../utilities/app-utils';
5-
import { join } from 'path';
6-
import { RenderUniversalTaskOptions } from '../tasks/render-universal';
5+
// import { getAppFromConfig } from '../utilities/app-utils';
6+
// import { join } from 'path';
7+
// import { RenderUniversalTaskOptions } from '../tasks/render-universal';
8+
// import { run as runArchitect } from '../utilities/architect';
79

8-
const SilentError = require('silent-error');
10+
// const SilentError = require('silent-error');
911

10-
export interface BuildTaskOptions extends BuildOptions {
11-
statsJson?: boolean;
12+
export interface RunOptions {
13+
app?: string;
14+
prod: boolean;
1215
}
1316

14-
export default class BuildCommand extends Command {
17+
export default class BuildCommand extends ArchitectCommand {
1518
public readonly name = 'build';
19+
public readonly target = 'browser';
1620
public readonly description =
1721
'Builds your app and places it into the output path (dist/ by default).';
1822
public static aliases = ['b'];
1923
public scope = CommandScope.inProject;
2024
public arguments: string[];
21-
public options: Option[] = [];
25+
public options: Option[] = [this.prodOption];
2226

23-
public validate(_options: BuildTaskOptions) {
27+
public validate(_options: RunOptions) {
2428
Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
2529
Version.assertTypescriptVersion(this.project.root);
2630
return true;
2731
}
2832

29-
public async run(options: BuildTaskOptions) {
30-
// Add trailing slash if missing to prevent https://github.com/angular/angular-cli/issues/7295
31-
if (options.deployUrl && options.deployUrl.substr(-1) !== '/') {
32-
options.deployUrl += '/';
33+
public async run(options: RunOptions) {
34+
let configuration;
35+
if (options.prod) {
36+
configuration = 'production';
3337
}
38+
const overrides = {...options};
39+
delete overrides.app;
40+
delete overrides.prod;
41+
return this.runArchitect({
42+
app: options.app,
43+
configuration,
44+
overrides
45+
});
46+
// Add trailing slash if missing to prevent https://github.com/angular/angular-cli/issues/7295
47+
// if (options.deployUrl && options.deployUrl.substr(-1) !== '/') {
48+
// options.deployUrl += '/';
49+
// }
3450

35-
const BuildTask = require('../tasks/build').default;
51+
// const BuildTask = require('../tasks/build').default;
3652

37-
const buildTask = new BuildTask({
38-
project: this.project,
39-
ui: this.ui,
40-
});
53+
// const buildTask = new BuildTask({
54+
// project: this.project,
55+
// ui: this.ui,
56+
// });
4157

42-
const clientApp = getAppFromConfig(options.app);
58+
// const clientApp = getAppFromConfig(options.app);
4359

44-
const doAppShell = options.target === 'production' &&
45-
(options.aot === undefined || options.aot === true) &&
46-
!options.skipAppShell;
60+
// const doAppShell = options.target === 'production' &&
61+
// (options.aot === undefined || options.aot === true) &&
62+
// !options.skipAppShell;
4763

48-
let serverApp: any = null;
49-
if (clientApp.appShell && doAppShell) {
50-
serverApp = getAppFromConfig(clientApp.appShell.app);
51-
if (serverApp.platform !== 'server') {
52-
throw new SilentError(`Shell app's platform is not "server"`);
53-
}
54-
}
64+
// let serverApp: any = null;
65+
// if (clientApp.appShell && doAppShell) {
66+
// serverApp = getAppFromConfig(clientApp.appShell.app);
67+
// if (serverApp.platform !== 'server') {
68+
// throw new SilentError(`Shell app's platform is not "server"`);
69+
// }
70+
// }
5571

56-
const buildTaskResult = await buildTask.run(options);
57-
if (!clientApp.appShell || !doAppShell) {
58-
return buildTaskResult;
59-
}
72+
// // const buildTaskResult = await buildTask.run(options);
73+
// const buildTaskResult = await this.runArchitect({project: options.project});
74+
// if (!clientApp.appShell || !doAppShell) {
75+
// return buildTaskResult;
76+
// }
6077

61-
const serverOptions = {
62-
...options,
63-
app: clientApp.appShell.app
64-
};
65-
await buildTask.run(serverOptions);
78+
// const serverOptions = {
79+
// ...options,
80+
// app: clientApp.appShell.app
81+
// };
82+
// await buildTask.run(serverOptions);
6683

67-
const RenderUniversalTask = require('../tasks/render-universal').default;
84+
// const RenderUniversalTask = require('../tasks/render-universal').default;
6885

69-
const renderUniversalTask = new RenderUniversalTask({
70-
project: this.project,
71-
ui: this.ui,
72-
});
73-
const renderUniversalOptions: RenderUniversalTaskOptions = {
74-
inputIndexPath: join(this.project.root, clientApp.outDir, clientApp.index),
75-
route: clientApp.appShell.route,
76-
serverOutDir: join(this.project.root, serverApp.outDir),
77-
outputIndexPath: join(this.project.root, clientApp.outDir, clientApp.index)
78-
};
86+
// const renderUniversalTask = new RenderUniversalTask({
87+
// project: this.project,
88+
// ui: this.ui,
89+
// });
90+
// const renderUniversalOptions: RenderUniversalTaskOptions = {
91+
// inputIndexPath: join(this.project.root, clientApp.outDir, clientApp.index),
92+
// route: clientApp.appShell.route,
93+
// serverOutDir: join(this.project.root, serverApp.outDir),
94+
// outputIndexPath: join(this.project.root, clientApp.outDir, clientApp.index)
95+
// };
7996

80-
return await renderUniversalTask.run(renderUniversalOptions);
97+
// return await renderUniversalTask.run(renderUniversalOptions);
8198
}
8299
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Command, Option } from '../models/command';
2+
import { runTarget } from '../utilities/architect';
3+
4+
export default class RunCommand extends Command {
5+
public readonly name = 'run';
6+
public readonly description = 'Runs an architect configuration.';
7+
public readonly arguments: string[] = ['config'];
8+
public readonly options: Option[] = [];
9+
10+
public async run(options: any) {
11+
const buildEvent = await runTarget(this.project.root, options.config, options)
12+
.toPromise();
13+
if (!buildEvent.success) {
14+
throw new Error('');
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Command, Option } from './command';
2+
import { run, RunOptions } from '../utilities/architect';
3+
4+
export abstract class ArchitectCommand extends Command {
5+
readonly Options: Option[] = [{
6+
name: 'configuration',
7+
description: 'The configuration',
8+
type: String,
9+
aliases: ['c']
10+
}];
11+
12+
readonly arguments = ['project'];
13+
14+
abstract target: string;
15+
16+
protected prodOption: Option = {
17+
name: 'prod',
18+
description: '',
19+
type: Boolean
20+
};
21+
22+
23+
// root: string;
24+
// project: string;
25+
// target: string;
26+
// configuration?: string;
27+
// overrides?: object;
28+
29+
protected async runArchitect(options: RunArchitectOptions): Promise<boolean> {
30+
const runOptions: RunOptions = {
31+
target: this.target,
32+
root: this.project.root,
33+
...options
34+
};
35+
const buildResult = await run(runOptions).toPromise();
36+
37+
return buildResult.success;
38+
}
39+
}
40+
41+
export interface RunArchitectOptions {
42+
app: string;
43+
configuration?: string;
44+
overrides?: object;
45+
}

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

-14
This file was deleted.

packages/@angular/cli/utilities/architect.ts

+33
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,36 @@ export function runTarget(root: string, target: string, options: any): Observabl
3232
);
3333
}
3434

35+
export interface RunOptions {
36+
root: string;
37+
app: string;
38+
target: string;
39+
configuration?: string;
40+
overrides?: object;
41+
}
42+
43+
export function run(options: RunOptions) {
44+
const {root, app, target, configuration, overrides} = options;
45+
46+
const host = new NodeJsSyncHost();
47+
const logger = createConsoleLogger();
48+
const cliConfig = CliConfig.fromProject().config;
49+
const architect = new Architect(normalize(root), host);
50+
51+
const appConfig = getAppFromConfig(app);
52+
const workspaceConfig = createArchitectWorkspace(cliConfig);
53+
const project = getProjectName(appConfig, app);
54+
const convertOverrides: any = convertOptions({ ...overrides });
55+
const context = { logger };
56+
57+
const targetOptions = {
58+
project,
59+
target,
60+
configuration,
61+
overrides: convertOverrides
62+
};
63+
64+
return architect.loadWorkspaceFromJson(workspaceConfig).pipe(
65+
concatMap(() => architect.run(architect.getTarget(targetOptions), context)),
66+
);
67+
}

tests/e2e/tests/basic/build.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {ng} from '../../utils/process';
2+
import {expectFileToMatch} from '../../utils/fs';
3+
4+
export default function() {
5+
return ng('build')
6+
.then(() => expectFileToMatch('dist/index.html', 'main.js'))
7+
.then(() => ng('build', '--prod'))
8+
.then(() => expectFileToMatch('dist/index.html', /main\.[a-zA-Z0-9]{20}\.js/));
9+
}

0 commit comments

Comments
 (0)