Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: remove duplicate ngc compilation logic #9175

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
});
}