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