Skip to content

Commit

Permalink
feature(tsc-wrapped): accept any tsc command line option (angular#13471)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored and wKoza committed Feb 12, 2017
1 parent 7eca76c commit 711ff82
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
26 changes: 16 additions & 10 deletions tools/@angular/tsc-wrapped/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export type CodegenExtension =
Promise<void>;

export function main(
project: string, cliOptions: CliOptions, codegen?: CodegenExtension): Promise<any> {
project: string, cliOptions: CliOptions, codegen?: CodegenExtension,
options?: ts.CompilerOptions): Promise<any> {
try {
let projectDir = project;
if (fs.lstatSync(project).isFile()) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
});
}
6 changes: 3 additions & 3 deletions tools/@angular/tsc-wrapped/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions tools/@angular/tsc-wrapped/test/tsc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
});

0 comments on commit 711ff82

Please sign in to comment.