refactor(compiler-cli): cleanup API for transformer based ngc
This is in preparation for watch mode.
This commit is contained in:
@ -14,15 +14,87 @@ import * as ts from 'typescript';
|
||||
import * as tsc from '@angular/tsc-wrapped';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as ngc from './ngc';
|
||||
import * as api from './transformers/api';
|
||||
import * as ngc from './transformers/entry_points';
|
||||
import {performCompilation, readConfiguration, formatDiagnostics, Diagnostics, ParsedConfiguration} from './perform-compile';
|
||||
|
||||
import {isSyntaxError} from '@angular/compiler';
|
||||
|
||||
import {readConfiguration} from './perform-compile';
|
||||
|
||||
import {CodeGenerator} from './codegen';
|
||||
|
||||
function codegen(
|
||||
export function main(
|
||||
args: string[], consoleError: (s: string) => void = console.error): Promise<number> {
|
||||
const parsedArgs = require('minimist')(args);
|
||||
const {rootNames, options, errors: configErrors} = readCommandLineAndConfiguration(parsedArgs);
|
||||
if (configErrors.length) {
|
||||
return Promise.resolve(reportErrorsAndExit(options, configErrors, consoleError));
|
||||
}
|
||||
if (options.disableTransformerPipeline) {
|
||||
return disabledTransformerPipelineNgcMain(parsedArgs, consoleError);
|
||||
}
|
||||
const {diagnostics: compileDiags} = performCompilation(rootNames, options);
|
||||
return Promise.resolve(reportErrorsAndExit(options, compileDiags, consoleError));
|
||||
}
|
||||
|
||||
export function mainSync(
|
||||
args: string[], consoleError: (s: string) => void = console.error): number {
|
||||
const parsedArgs = require('minimist')(args);
|
||||
const {rootNames, options, errors: configErrors} = readCommandLineAndConfiguration(parsedArgs);
|
||||
if (configErrors.length) {
|
||||
return reportErrorsAndExit(options, configErrors, consoleError);
|
||||
}
|
||||
const {diagnostics: compileDiags} = performCompilation(rootNames, options);
|
||||
return reportErrorsAndExit(options, compileDiags, consoleError);
|
||||
}
|
||||
|
||||
function readCommandLineAndConfiguration(args: any): ParsedConfiguration {
|
||||
const project = args.p || args.project || '.';
|
||||
const allDiagnostics: Diagnostics = [];
|
||||
const config = readConfiguration(project);
|
||||
const options = mergeCommandLineParams(args, config.options);
|
||||
return {rootNames: config.rootNames, options, errors: config.errors};
|
||||
}
|
||||
|
||||
function reportErrorsAndExit(
|
||||
options: api.CompilerOptions, allDiagnostics: Diagnostics,
|
||||
consoleError: (s: string) => void = console.error): number {
|
||||
const exitCode = allDiagnostics.some(d => d.category === ts.DiagnosticCategory.Error) ? 1 : 0;
|
||||
if (allDiagnostics.length) {
|
||||
consoleError(formatDiagnostics(options, allDiagnostics));
|
||||
}
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
function mergeCommandLineParams(
|
||||
cliArgs: {[k: string]: string}, options: api.CompilerOptions): api.CompilerOptions {
|
||||
// TODO: also merge in tsc command line parameters by calling
|
||||
// ts.readCommandLine.
|
||||
if (cliArgs.i18nFile) options.i18nInFile = cliArgs.i18nFile;
|
||||
if (cliArgs.i18nFormat) options.i18nInFormat = cliArgs.i18nFormat;
|
||||
if (cliArgs.locale) options.i18nInLocale = cliArgs.locale;
|
||||
const mt = cliArgs.missingTranslation;
|
||||
if (mt === 'error' || mt === 'warning' || mt === 'ignore') {
|
||||
options.i18nInMissingTranslations = mt;
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
function disabledTransformerPipelineNgcMain(
|
||||
args: any, consoleError: (s: string) => void = console.error): Promise<number> {
|
||||
const cliOptions = new tsc.NgcCliOptions(args);
|
||||
const project = args.p || args.project || '.';
|
||||
return tsc.main(project, cliOptions, disabledTransformerPipelineCodegen)
|
||||
.then(() => 0)
|
||||
.catch(e => {
|
||||
if (e instanceof tsc.UserError || isSyntaxError(e)) {
|
||||
consoleError(e.message);
|
||||
} else {
|
||||
consoleError(e.stack);
|
||||
}
|
||||
return Promise.resolve(1);
|
||||
});
|
||||
}
|
||||
|
||||
function disabledTransformerPipelineCodegen(
|
||||
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.NgcCliOptions, program: ts.Program,
|
||||
host: ts.CompilerHost) {
|
||||
if (ngOptions.enableSummariesForJit === undefined) {
|
||||
@ -32,38 +104,8 @@ function codegen(
|
||||
return CodeGenerator.create(ngOptions, cliOptions, program, host).codegen();
|
||||
}
|
||||
|
||||
export function main(
|
||||
args: any, consoleError: (s: string) => void = console.error): Promise<number> {
|
||||
const project = args.p || args.project || '.';
|
||||
const cliOptions = new tsc.NgcCliOptions(args);
|
||||
|
||||
return tsc.main(project, cliOptions, codegen).then(() => 0).catch(e => {
|
||||
if (e instanceof tsc.UserError || isSyntaxError(e)) {
|
||||
consoleError(e.message);
|
||||
return Promise.resolve(1);
|
||||
} else {
|
||||
consoleError(e.stack);
|
||||
consoleError('Compilation failed');
|
||||
return Promise.resolve(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// CLI entry point
|
||||
if (require.main === module) {
|
||||
const args = process.argv.slice(2);
|
||||
const parsedArgs = require('minimist')(args);
|
||||
const project = parsedArgs.p || parsedArgs.project || '.';
|
||||
|
||||
const projectDir = fs.lstatSync(project).isFile() ? path.dirname(project) : project;
|
||||
|
||||
// file names in tsconfig are resolved relative to this absolute path
|
||||
const basePath = path.resolve(process.cwd(), projectDir);
|
||||
const {ngOptions} = readConfiguration(project, basePath);
|
||||
|
||||
if (ngOptions.disableTransformerPipeline) {
|
||||
main(parsedArgs).then((exitCode: number) => process.exit(exitCode));
|
||||
} else {
|
||||
process.exit(ngc.main(args, s => console.error(s)));
|
||||
}
|
||||
main(args).then((exitCode: number) => process.exitCode = exitCode);
|
||||
}
|
||||
|
Reference in New Issue
Block a user