2016-05-03 17:31:40 -07:00
|
|
|
#!/usr/bin/env node
|
2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-04-29 10:35:36 -07:00
|
|
|
|
|
|
|
// Must be imported first, because angular2 decorators throws on load.
|
|
|
|
import 'reflect-metadata';
|
|
|
|
|
|
|
|
import * as ts from 'typescript';
|
2016-05-27 16:22:16 -07:00
|
|
|
import * as tsc from '@angular/tsc-wrapped';
|
2017-01-27 13:19:00 -08:00
|
|
|
import {isSyntaxError} from '@angular/compiler';
|
2016-05-01 11:22:39 -07:00
|
|
|
|
2016-05-24 10:53:48 -07:00
|
|
|
import {CodeGenerator} from './codegen';
|
2016-05-01 11:22:39 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
function codegen(
|
2016-08-12 14:45:36 -07:00
|
|
|
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.NgcCliOptions, program: ts.Program,
|
|
|
|
host: ts.CompilerHost) {
|
2016-11-15 13:57:25 -08:00
|
|
|
return CodeGenerator.create(ngOptions, cliOptions, program, host).codegen();
|
2016-04-29 10:35:36 -07:00
|
|
|
}
|
|
|
|
|
2016-11-30 16:45:40 -08:00
|
|
|
export function main(
|
|
|
|
args: any, consoleError: (s: string) => void = console.error): Promise<number> {
|
2016-08-12 14:45:36 -07:00
|
|
|
const project = args.p || args.project || '.';
|
|
|
|
const cliOptions = new tsc.NgcCliOptions(args);
|
2016-11-30 13:59:53 -08:00
|
|
|
|
|
|
|
return tsc.main(project, cliOptions, codegen).then(() => 0).catch(e => {
|
2017-01-27 13:19:00 -08:00
|
|
|
if (e instanceof tsc.UserError || isSyntaxError(e)) {
|
2016-11-30 13:59:53 -08:00
|
|
|
consoleError(e.message);
|
2016-11-30 16:37:28 -08:00
|
|
|
return Promise.resolve(1);
|
2016-11-30 13:59:53 -08:00
|
|
|
} else {
|
|
|
|
consoleError(e.stack);
|
|
|
|
consoleError('Compilation failed');
|
|
|
|
return Promise.resolve(1);
|
|
|
|
}
|
2016-08-12 14:45:36 -07:00
|
|
|
});
|
2016-04-29 10:35:36 -07:00
|
|
|
}
|
2016-11-30 13:59:53 -08:00
|
|
|
|
|
|
|
// CLI entry point
|
|
|
|
if (require.main === module) {
|
|
|
|
const args = require('minimist')(process.argv.slice(2));
|
2016-11-30 16:37:28 -08:00
|
|
|
main(args).then((exitCode: number) => process.exit(exitCode));
|
2016-11-30 16:45:40 -08:00
|
|
|
}
|