feature(tsc-wrapped): accept any tsc command line option (#13471)

This commit is contained in:
Alex Eagle 2017-01-12 17:34:31 -08:00 committed by Miško Hevery
parent 5047d9780d
commit f816319e41
3 changed files with 23 additions and 15 deletions

View File

@ -23,7 +23,8 @@ export type CodegenExtension =
Promise<void>; Promise<void>;
export function main( export function main(
project: string, cliOptions: CliOptions, codegen?: CodegenExtension): Promise<any> { project: string, cliOptions: CliOptions, codegen?: CodegenExtension,
options?: ts.CompilerOptions): Promise<any> {
try { try {
let projectDir = project; let projectDir = project;
if (fs.lstatSync(project).isFile()) { if (fs.lstatSync(project).isFile()) {
@ -34,7 +35,7 @@ export function main(
const basePath = path.resolve(process.cwd(), cliOptions.basePath || projectDir); const basePath = path.resolve(process.cwd(), cliOptions.basePath || projectDir);
// read the configuration options from wherever you store them // 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; ngOptions.basePath = basePath;
const createProgram = (host: ts.CompilerHost, oldProgram?: ts.Program) => const createProgram = (host: ts.CompilerHost, oldProgram?: ts.Program) =>
ts.createProgram(parsed.fileNames, parsed.options, host, oldProgram); ts.createProgram(parsed.fileNames, parsed.options, host, oldProgram);
@ -111,12 +112,17 @@ export function main(
// CLI entry point // CLI entry point
if (require.main === module) { if (require.main === module) {
const args = require('minimist')(process.argv.slice(2)); const args = process.argv.slice(2);
const project = args.p || args.project || '.'; let {options, fileNames, errors} = (ts as any).parseCommandLine(args);
const cliOptions = new CliOptions(args); check(errors);
main(project, cliOptions).then((exitCode: any) => process.exit(exitCode)).catch((e: any) => { const project = options.project || '.';
console.error(e.stack); // TODO(alexeagle): command line should be TSC-compatible, remove "CliOptions" here
console.error('Compilation failed'); const cliOptions = new CliOptions(require('minimist')(args));
process.exit(1); 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);
});
} }

View File

@ -18,7 +18,7 @@ import AngularCompilerOptions from './options';
* you should implement a similar interface. * you should implement a similar interface.
*/ */
export interface CompilerInterface { export interface CompilerInterface {
readConfiguration(project: string, basePath: string): readConfiguration(project: string, basePath: string, existingOptions?: ts.CompilerOptions):
{parsed: ts.ParsedCommandLine, ngOptions: AngularCompilerOptions}; {parsed: ts.ParsedCommandLine, ngOptions: AngularCompilerOptions};
typeCheck(compilerHost: ts.CompilerHost, program: ts.Program): void; typeCheck(compilerHost: ts.CompilerHost, program: ts.Program): void;
emit(program: ts.Program): number; 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) {} 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; this.basePath = basePath;
// Allow a directory containing tsconfig.json as the project value // Allow a directory containing tsconfig.json as the project value
@ -123,7 +123,7 @@ export class Tsc implements CompilerInterface {
fileExists: existsSync, fileExists: existsSync,
readDirectory: this.readDirectory readDirectory: this.readDirectory
}; };
this.parsed = ts.parseJsonConfigFileContent(config, host, basePath); this.parsed = ts.parseJsonConfigFileContent(config, host, basePath, existingOptions);
check(this.parsed.errors); check(this.parsed.errors);

View File

@ -25,14 +25,16 @@ describe('options parsing', () => {
() => ['tsconfig.json']); () => ['tsconfig.json']);
it('should combine all options into ngOptions', () => { 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({ expect(ngOptions).toEqual({
genDir: 'basePath', genDir: 'basePath',
googleClosureOutput: true, googleClosureOutput: true,
module: ts.ModuleKind.CommonJS, module: ts.ModuleKind.CommonJS,
outDir: 'basePath/built', outDir: 'basePath/built',
configFilePath: undefined configFilePath: undefined,
target: ts.ScriptTarget.ES2015
}); });
}); });
}); });