From e3d5ffde86f43f4db2e1b78300d660170fe219fd Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 3 Jan 2018 00:24:42 +0200 Subject: [PATCH] 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`. --- tools/package-tools/build-package.ts | 16 +++------------- tools/package-tools/compile-entry-point.ts | 21 +++++---------------- tools/package-tools/ngc-compile.ts | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 tools/package-tools/ngc-compile.ts diff --git a/tools/package-tools/build-package.ts b/tools/package-tools/build-package.ts index 8eaa3a63dc3c..a75402664be9 100644 --- a/tools/package-tools/build-package.ts +++ b/tools/package-tools/build-package.ts @@ -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); }); diff --git a/tools/package-tools/compile-entry-point.ts b/tools/package-tools/compile-entry-point.ts index b62e52e07668..0676eeb1745d 100644 --- a/tools/package-tools/compile-entry-point.ts +++ b/tools/package-tools/compile-entry-point.ts @@ -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); }); diff --git a/tools/package-tools/ngc-compile.ts b/tools/package-tools/ngc-compile.ts new file mode 100644 index 000000000000..77d0ae9f17c4 --- /dev/null +++ b/tools/package-tools/ngc-compile.ts @@ -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()); + }); +}