Skip to content

Commit e3d5ffd

Browse files
crisbetojelbourn
authored andcommitted
build: remove duplicate ngc compilation logic (#9175)
Abstracts away the logic for compiling with ngc that was duplicated between the `build-package.ts` and `compile-entry-point.ts`.
1 parent 900237b commit e3d5ffd

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

tools/package-tools/build-package.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {join, resolve as resolvePath} from 'path';
2-
import {spawn} from 'child_process';
1+
import {join} from 'path';
32
import {red} from 'chalk';
43
import {PackageBundler} from './build-bundles';
54
import {buildConfig} from './build-config';
65
import {getSecondaryEntryPointsForPackage} from './secondary-entry-points';
76
import {compileEntryPoint, renamePrivateReExportsToBeUnique} from './compile-entry-point';
7+
import {ngcCompile} from './ngc-compile';
88

99
const {packagesDir, outputDir} = buildConfig;
1010

@@ -104,17 +104,7 @@ export class BuildPackage {
104104
const entryPointPath = join(this.sourceDir, secondaryEntryPoint);
105105
const entryPointTsconfigPath = join(entryPointPath, tsconfigName);
106106

107-
return new Promise((resolve, reject) => {
108-
const ngcPath = resolvePath('./node_modules/.bin/ngc');
109-
const childProcess = spawn(ngcPath, ['-p', entryPointTsconfigPath], {shell: true});
110-
111-
// Pipe stdout and stderr from the child process.
112-
childProcess.stdout.on('data', (data: any) => console.log(`${data}`));
113-
childProcess.stderr.on('data', (data: any) => console.error(red(`${data}`)));
114-
115-
childProcess.on('exit', (exitCode: number) => exitCode === 0 ? resolve() : reject());
116-
})
117-
.catch(() => {
107+
return ngcCompile(['-p', entryPointTsconfigPath]).catch(() => {
118108
const error = red(`Failed to compile ${secondaryEntryPoint} using ${entryPointTsconfigPath}`);
119109
console.error(error);
120110
});

tools/package-tools/compile-entry-point.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
import {join, resolve as resolvePath} from 'path';
2-
import {spawn} from 'child_process';
1+
import {join} from 'path';
32
import {writeFileSync, readFileSync} from 'fs';
43
import {sync as glob} from 'glob';
54
import {red} from 'chalk';
65
import {BuildPackage} from './build-package';
6+
import {ngcCompile} from './ngc-compile';
77

88
/** Incrementing ID counter. */
99
let nextId = 0;
1010

1111
/** Compiles the TypeScript sources of a primary or secondary entry point. */
1212
export async function compileEntryPoint(buildPackage: BuildPackage, tsconfigName: string,
13-
secondaryEntryPoint = '', es5OutputPath?: string) {
13+
secondaryEntryPoint = '', es5OutputPath?: string) {
1414
const entryPointPath = join(buildPackage.sourceDir, secondaryEntryPoint);
1515
const entryPointTsconfigPath = join(entryPointPath, tsconfigName);
1616
const ngcFlags = ['-p', entryPointTsconfigPath];
1717

1818
if (es5OutputPath) {
19-
ngcFlags.push('--outDir', es5OutputPath);
20-
ngcFlags.push('--target', 'ES5');
19+
ngcFlags.push('--outDir', es5OutputPath, '--target', 'ES5');
2120
}
2221

23-
return new Promise((resolve, reject) => {
24-
const ngcPath = resolvePath('./node_modules/.bin/ngc');
25-
const childProcess = spawn(ngcPath, ngcFlags, {shell: true});
26-
27-
// Pipe stdout and stderr from the child process.
28-
childProcess.stdout.on('data', (data: any) => console.log(`${data}`));
29-
childProcess.stderr.on('data', (data: any) => console.error(red(`${data}`)));
30-
31-
childProcess.on('exit', (exitCode: number) => exitCode === 0 ? resolve() : reject());
32-
})
33-
.catch(() => {
22+
return ngcCompile(ngcFlags).catch(() => {
3423
const error = red(`Failed to compile ${secondaryEntryPoint} using ${entryPointTsconfigPath}`);
3524
console.error(error);
3625
});

tools/package-tools/ngc-compile.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {resolve as resolvePath} from 'path';
2+
import {spawn} from 'child_process';
3+
import {red} from 'chalk';
4+
5+
/**
6+
* Spawns a child process that compiles using ngc.
7+
* @param flags Command-line flags to be passed to ngc.
8+
* @returns Promise that resolves/rejects when the child process exits.
9+
*/
10+
export function ngcCompile(flags: string[]) {
11+
return new Promise((resolve, reject) => {
12+
const ngcPath = resolvePath('./node_modules/.bin/ngc');
13+
const childProcess = spawn(ngcPath, flags, {shell: true});
14+
15+
// Pipe stdout and stderr from the child process.
16+
childProcess.stdout.on('data', (data: string|Buffer) => console.log(`${data}`));
17+
childProcess.stderr.on('data', (data: string|Buffer) => console.error(red(`${data}`)));
18+
childProcess.on('exit', (exitCode: number) => exitCode === 0 ? resolve() : reject());
19+
});
20+
}

0 commit comments

Comments
 (0)