Skip to content

Commit

Permalink
build: remove duplicate ngc compilation logic (angular#9175)
Browse files Browse the repository at this point in the history
Abstracts away the logic for compiling with ngc that was duplicated between the `build-package.ts` and `compile-entry-point.ts`.
  • Loading branch information
crisbeto authored and jelbourn committed Jan 2, 2018
1 parent 900237b commit e3d5ffd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
16 changes: 3 additions & 13 deletions tools/package-tools/build-package.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
});
Expand Down
21 changes: 5 additions & 16 deletions tools/package-tools/compile-entry-point.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down
20 changes: 20 additions & 0 deletions tools/package-tools/ngc-compile.ts
Original file line number Diff line number Diff line change
@@ -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());
});
}

0 comments on commit e3d5ffd

Please sign in to comment.