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`.
This commit is contained in:
Kristiyan Kostadinov
2018-01-03 00:24:42 +02:00
committed by Jeremy Elbourn
parent 900237b431
commit e3d5ffde86
3 changed files with 28 additions and 29 deletions
+3 -13
View File
@@ -1,10 +1,10 @@
import {join, resolve as resolvePath} from 'path';
import {spawn} from 'child_process';
import {join} from 'path';
import {red} from 'chalk';
import {PackageBundler} from './build-bundles';
import {buildConfig} from './build-config';
import {getSecondaryEntryPointsForPackage} from './secondary-entry-points';
import {compileEntryPoint, renamePrivateReExportsToBeUnique} from './compile-entry-point';
import {ngcCompile} from './ngc-compile';
const {packagesDir, outputDir} = buildConfig;
@@ -104,17 +104,7 @@ export class BuildPackage {
const entryPointPath = join(this.sourceDir, secondaryEntryPoint);
const entryPointTsconfigPath = join(entryPointPath, tsconfigName);
return new Promise((resolve, reject) => {
const ngcPath = resolvePath('./node_modules/.bin/ngc');
const childProcess = spawn(ngcPath, ['-p', entryPointTsconfigPath], {shell: true});
// Pipe stdout and stderr from the child process.
childProcess.stdout.on('data', (data: any) => console.log(`${data}`));
childProcess.stderr.on('data', (data: any) => console.error(red(`${data}`)));
childProcess.on('exit', (exitCode: number) => exitCode === 0 ? resolve() : reject());
})
.catch(() => {
return ngcCompile(['-p', entryPointTsconfigPath]).catch(() => {
const error = red(`Failed to compile ${secondaryEntryPoint} using ${entryPointTsconfigPath}`);
console.error(error);
});
+5 -16
View File
@@ -1,36 +1,25 @@
import {join, resolve as resolvePath} from 'path';
import {spawn} from 'child_process';
import {join} from 'path';
import {writeFileSync, readFileSync} from 'fs';
import {sync as glob} from 'glob';
import {red} from 'chalk';
import {BuildPackage} from './build-package';
import {ngcCompile} from './ngc-compile';
/** Incrementing ID counter. */
let nextId = 0;
/** Compiles the TypeScript sources of a primary or secondary entry point. */
export async function compileEntryPoint(buildPackage: BuildPackage, tsconfigName: string,
secondaryEntryPoint = '', es5OutputPath?: string) {
secondaryEntryPoint = '', es5OutputPath?: string) {
const entryPointPath = join(buildPackage.sourceDir, secondaryEntryPoint);
const entryPointTsconfigPath = join(entryPointPath, tsconfigName);
const ngcFlags = ['-p', entryPointTsconfigPath];
if (es5OutputPath) {
ngcFlags.push('--outDir', es5OutputPath);
ngcFlags.push('--target', 'ES5');
ngcFlags.push('--outDir', es5OutputPath, '--target', 'ES5');
}
return new Promise((resolve, reject) => {
const ngcPath = resolvePath('./node_modules/.bin/ngc');
const childProcess = spawn(ngcPath, ngcFlags, {shell: true});
// Pipe stdout and stderr from the child process.
childProcess.stdout.on('data', (data: any) => console.log(`${data}`));
childProcess.stderr.on('data', (data: any) => console.error(red(`${data}`)));
childProcess.on('exit', (exitCode: number) => exitCode === 0 ? resolve() : reject());
})
.catch(() => {
return ngcCompile(ngcFlags).catch(() => {
const error = red(`Failed to compile ${secondaryEntryPoint} using ${entryPointTsconfigPath}`);
console.error(error);
});
+20
View File
@@ -0,0 +1,20 @@
import {resolve as resolvePath} from 'path';
import {spawn} from 'child_process';
import {red} from 'chalk';
/**
* Spawns a child process that compiles using ngc.
* @param flags Command-line flags to be passed to ngc.
* @returns Promise that resolves/rejects when the child process exits.
*/
export function ngcCompile(flags: string[]) {
return new Promise((resolve, reject) => {
const ngcPath = resolvePath('./node_modules/.bin/ngc');
const childProcess = spawn(ngcPath, flags, {shell: true});
// Pipe stdout and stderr from the child process.
childProcess.stdout.on('data', (data: string|Buffer) => console.log(`${data}`));
childProcess.stderr.on('data', (data: string|Buffer) => console.error(red(`${data}`)));
childProcess.on('exit', (exitCode: number) => exitCode === 0 ? resolve() : reject());
});
}