refactor(compiler-cli): allow custom error checking function in ngc (#18355)

PR Close #18355
This commit is contained in:
Abhimanyu Deora 2017-07-26 11:12:54 -07:00 committed by Miško Hevery
parent cae3e6dca0
commit d20ac14fe2
1 changed files with 18 additions and 12 deletions

View File

@ -78,14 +78,15 @@ function syntheticError(message: string): ts.Diagnostic {
}
export function readConfiguration(
project: string, basePath: string, existingOptions?: ts.CompilerOptions) {
project: string, basePath: string, checkFunc: (cwd: string, ...args: any[]) => void = check,
existingOptions?: ts.CompilerOptions) {
// Allow a directory containing tsconfig.json as the project value
// Note, TS@next returns an empty array, while earlier versions throw
const projectFile =
fs.lstatSync(project).isDirectory() ? path.join(project, 'tsconfig.json') : project;
let {config, error} = ts.readConfigFile(projectFile, ts.sys.readFile);
if (error) check(basePath, [error]);
if (error) checkFunc(basePath, [error]);
const parseConfigHost = {
useCaseSensitiveFileNames: true,
fileExists: fs.existsSync,
@ -94,7 +95,7 @@ export function readConfiguration(
};
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, basePath, existingOptions);
check(basePath, parsed.errors);
checkFunc(basePath, parsed.errors);
// Default codegen goes to the current directory
// Parsed options are already converted to absolute paths
@ -124,7 +125,8 @@ function getProjectDirectory(project: string): string {
export function performCompilation(
basePath: string, files: string[], options: ts.CompilerOptions, ngOptions: any,
consoleError: (s: string) => void = console.error, tsCompilerHost?: ts.CompilerHost) {
consoleError: (s: string) => void = console.error,
checkFunc: (cwd: string, ...args: any[]) => void = check, tsCompilerHost?: ts.CompilerHost) {
try {
ngOptions.basePath = basePath;
ngOptions.genDir = basePath;
@ -144,7 +146,7 @@ export function performCompilation(
if (ngOptions.flatModuleOutFile && !ngOptions.skipMetadataEmit) {
const {host: bundleHost, indexName, errors} =
createBundleIndexHost(ngOptions, rootFileNames, host);
if (errors) check(basePath, errors);
if (errors) checkFunc(basePath, errors);
if (indexName) addGeneratedFileName(indexName);
host = bundleHost;
}
@ -156,16 +158,17 @@ export function performCompilation(
ng.createProgram({rootNames: rootFileNames, host: ngHost, options: ngHostOptions});
// Check parameter diagnostics
check(basePath, ngProgram.getTsOptionDiagnostics(), ngProgram.getNgOptionDiagnostics());
checkFunc(basePath, ngProgram.getTsOptionDiagnostics(), ngProgram.getNgOptionDiagnostics());
// Check syntactic diagnostics
check(basePath, ngProgram.getTsSyntacticDiagnostics());
checkFunc(basePath, ngProgram.getTsSyntacticDiagnostics());
// Check TypeScript semantic and Angular structure diagnostics
check(basePath, ngProgram.getTsSemanticDiagnostics(), ngProgram.getNgStructuralDiagnostics());
checkFunc(
basePath, ngProgram.getTsSemanticDiagnostics(), ngProgram.getNgStructuralDiagnostics());
// Check Angular semantic diagnostics
check(basePath, ngProgram.getNgSemanticDiagnostics());
checkFunc(basePath, ngProgram.getNgSemanticDiagnostics());
ngProgram.emit({
emitFlags: api.EmitFlags.Default |
@ -183,7 +186,9 @@ export function performCompilation(
}
export function main(args: string[], consoleError: (s: string) => void = console.error): number {
export function main(
args: string[], consoleError: (s: string) => void = console.error,
checkFunc: (cwd: string, ...args: any[]) => void = check): number {
try {
const parsedArgs = require('minimist')(args);
const project = parsedArgs.p || parsedArgs.project || '.';
@ -192,8 +197,9 @@ export function main(args: string[], consoleError: (s: string) => void = console
// file names in tsconfig are resolved relative to this absolute path
const basePath = path.resolve(process.cwd(), projectDir);
const {parsed, ngOptions} = readConfiguration(project, basePath);
return performCompilation(basePath, parsed.fileNames, parsed.options, ngOptions, consoleError);
const {parsed, ngOptions} = readConfiguration(project, basePath, checkFunc);
return performCompilation(
basePath, parsed.fileNames, parsed.options, ngOptions, consoleError, checkFunc);
} catch (e) {
consoleError(e.stack);
consoleError('Compilation failed');