From f816319e41049c3e71cad862308519aa69131f00 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 12 Jan 2017 17:34:31 -0800 Subject: [PATCH] feature(tsc-wrapped): accept any tsc command line option (#13471) --- tools/@angular/tsc-wrapped/src/main.ts | 26 +++++++++++++-------- tools/@angular/tsc-wrapped/src/tsc.ts | 6 ++--- tools/@angular/tsc-wrapped/test/tsc.spec.ts | 6 +++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/tools/@angular/tsc-wrapped/src/main.ts b/tools/@angular/tsc-wrapped/src/main.ts index 9e2c99eab3..723696ed11 100644 --- a/tools/@angular/tsc-wrapped/src/main.ts +++ b/tools/@angular/tsc-wrapped/src/main.ts @@ -23,7 +23,8 @@ export type CodegenExtension = Promise; export function main( - project: string, cliOptions: CliOptions, codegen?: CodegenExtension): Promise { + project: string, cliOptions: CliOptions, codegen?: CodegenExtension, + options?: ts.CompilerOptions): Promise { try { let projectDir = project; if (fs.lstatSync(project).isFile()) { @@ -34,7 +35,7 @@ export function main( const basePath = path.resolve(process.cwd(), cliOptions.basePath || projectDir); // read the configuration options from wherever you store them - const {parsed, ngOptions} = tsc.readConfiguration(project, basePath); + const {parsed, ngOptions} = tsc.readConfiguration(project, basePath, options); ngOptions.basePath = basePath; const createProgram = (host: ts.CompilerHost, oldProgram?: ts.Program) => ts.createProgram(parsed.fileNames, parsed.options, host, oldProgram); @@ -111,12 +112,17 @@ export function main( // CLI entry point if (require.main === module) { - const args = require('minimist')(process.argv.slice(2)); - const project = args.p || args.project || '.'; - const cliOptions = new CliOptions(args); - main(project, cliOptions).then((exitCode: any) => process.exit(exitCode)).catch((e: any) => { - console.error(e.stack); - console.error('Compilation failed'); - process.exit(1); - }); + const args = process.argv.slice(2); + let {options, fileNames, errors} = (ts as any).parseCommandLine(args); + check(errors); + const project = options.project || '.'; + // TODO(alexeagle): command line should be TSC-compatible, remove "CliOptions" here + const cliOptions = new CliOptions(require('minimist')(args)); + main(project, cliOptions, null, options) + .then((exitCode: any) => process.exit(exitCode)) + .catch((e: any) => { + console.error(e.stack); + console.error('Compilation failed'); + process.exit(1); + }); } diff --git a/tools/@angular/tsc-wrapped/src/tsc.ts b/tools/@angular/tsc-wrapped/src/tsc.ts index a630abb8ec..e2e4e2df83 100644 --- a/tools/@angular/tsc-wrapped/src/tsc.ts +++ b/tools/@angular/tsc-wrapped/src/tsc.ts @@ -18,7 +18,7 @@ import AngularCompilerOptions from './options'; * you should implement a similar interface. */ export interface CompilerInterface { - readConfiguration(project: string, basePath: string): + readConfiguration(project: string, basePath: string, existingOptions?: ts.CompilerOptions): {parsed: ts.ParsedCommandLine, ngOptions: AngularCompilerOptions}; typeCheck(compilerHost: ts.CompilerHost, program: ts.Program): void; emit(program: ts.Program): number; @@ -97,7 +97,7 @@ export class Tsc implements CompilerInterface { constructor(private readFile = ts.sys.readFile, private readDirectory = ts.sys.readDirectory) {} - readConfiguration(project: string, basePath: string) { + readConfiguration(project: string, basePath: string, existingOptions?: ts.CompilerOptions) { this.basePath = basePath; // Allow a directory containing tsconfig.json as the project value @@ -123,7 +123,7 @@ export class Tsc implements CompilerInterface { fileExists: existsSync, readDirectory: this.readDirectory }; - this.parsed = ts.parseJsonConfigFileContent(config, host, basePath); + this.parsed = ts.parseJsonConfigFileContent(config, host, basePath, existingOptions); check(this.parsed.errors); diff --git a/tools/@angular/tsc-wrapped/test/tsc.spec.ts b/tools/@angular/tsc-wrapped/test/tsc.spec.ts index a340bb0277..9eafe33fb7 100644 --- a/tools/@angular/tsc-wrapped/test/tsc.spec.ts +++ b/tools/@angular/tsc-wrapped/test/tsc.spec.ts @@ -25,14 +25,16 @@ describe('options parsing', () => { () => ['tsconfig.json']); it('should combine all options into ngOptions', () => { - const {parsed, ngOptions} = tsc.readConfiguration('projectDir', 'basePath'); + const {parsed, ngOptions} = + tsc.readConfiguration('projectDir', 'basePath', {target: ts.ScriptTarget.ES2015}); expect(ngOptions).toEqual({ genDir: 'basePath', googleClosureOutput: true, module: ts.ModuleKind.CommonJS, outDir: 'basePath/built', - configFilePath: undefined + configFilePath: undefined, + target: ts.ScriptTarget.ES2015 }); }); });