2016-05-03 20:31:40 -04:00
|
|
|
|
#!/usr/bin/env node
|
2016-06-23 12:47:54 -04: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
|
|
|
|
|
*/
|
|
|
|
|
|
2017-03-27 12:44:35 -04:00
|
|
|
|
// Must be imported first, because Angular decorators throw on load.
|
2016-04-29 13:35:36 -04:00
|
|
|
|
import 'reflect-metadata';
|
|
|
|
|
|
|
|
|
|
import * as ts from 'typescript';
|
2017-08-02 14:20:07 -04:00
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as path from 'path';
|
2017-08-16 18:35:19 -04:00
|
|
|
|
import * as tsickle from 'tsickle';
|
2017-08-09 16:45:45 -04:00
|
|
|
|
import * as api from './transformers/api';
|
|
|
|
|
import * as ngc from './transformers/entry_points';
|
2017-09-15 21:02:22 -04:00
|
|
|
|
import {GENERATED_FILES} from './transformers/util';
|
2017-08-02 14:20:07 -04:00
|
|
|
|
|
2017-09-12 18:53:17 -04:00
|
|
|
|
import {exitCodeFromResult, performCompilation, readConfiguration, formatDiagnostics, Diagnostics, ParsedConfiguration, PerformCompilationResult} from './perform_compile';
|
2017-08-18 17:03:59 -04:00
|
|
|
|
import {performWatchCompilation, createPerformWatchHost} from './perform_watch';
|
2017-01-27 16:19:00 -05:00
|
|
|
|
import {isSyntaxError} from '@angular/compiler';
|
2016-05-01 14:22:39 -04:00
|
|
|
|
|
2017-08-09 16:45:45 -04:00
|
|
|
|
export function main(
|
2017-09-12 18:53:17 -04:00
|
|
|
|
args: string[], consoleError: (s: string) => void = console.error,
|
|
|
|
|
config?: NgcParsedConfiguration): number {
|
|
|
|
|
let {project, rootNames, options, errors: configErrors, watch, emitFlags} =
|
|
|
|
|
config || readNgcCommandLineAndConfiguration(args);
|
2017-08-09 16:45:45 -04:00
|
|
|
|
if (configErrors.length) {
|
|
|
|
|
return reportErrorsAndExit(options, configErrors, consoleError);
|
|
|
|
|
}
|
2017-09-12 18:53:17 -04:00
|
|
|
|
if (watch) {
|
|
|
|
|
const result = watchMode(project, options, consoleError);
|
|
|
|
|
return reportErrorsAndExit({}, result.firstCompileResult, consoleError);
|
|
|
|
|
}
|
|
|
|
|
const {diagnostics: compileDiags} = performCompilation(
|
|
|
|
|
{rootNames, options, emitFlags, emitCallback: createEmitCallback(options)});
|
2017-08-09 16:45:45 -04:00
|
|
|
|
return reportErrorsAndExit(options, compileDiags, consoleError);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 21:02:22 -04:00
|
|
|
|
function createEmitCallback(options: api.CompilerOptions): api.TsEmitCallback|undefined {
|
|
|
|
|
const transformDecorators = options.annotationsAs !== 'decorators';
|
|
|
|
|
const transformTypesToClosure = options.annotateForClosureCompiler;
|
|
|
|
|
if (!transformDecorators && !transformTypesToClosure) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2017-09-08 21:40:32 -04:00
|
|
|
|
const tsickleHost: tsickle.TsickleHost = {
|
2017-09-15 21:02:22 -04:00
|
|
|
|
shouldSkipTsickleProcessing: (fileName) =>
|
|
|
|
|
/\.d\.ts$/.test(fileName) || GENERATED_FILES.test(fileName),
|
2017-09-08 21:40:32 -04:00
|
|
|
|
pathToModuleName: (context, importPath) => '',
|
|
|
|
|
shouldIgnoreWarningsForPath: (filePath) => false,
|
|
|
|
|
fileNameToModuleId: (fileName) => fileName,
|
2017-08-16 18:35:19 -04:00
|
|
|
|
googmodule: false,
|
|
|
|
|
untyped: true,
|
2017-09-28 12:31:28 -04:00
|
|
|
|
convertIndexImportShorthand: false, transformDecorators, transformTypesToClosure,
|
2017-08-16 18:35:19 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ({
|
|
|
|
|
program,
|
|
|
|
|
targetSourceFile,
|
|
|
|
|
writeFile,
|
|
|
|
|
cancellationToken,
|
|
|
|
|
emitOnlyDtsFiles,
|
|
|
|
|
customTransformers = {},
|
|
|
|
|
host,
|
|
|
|
|
options
|
|
|
|
|
}) =>
|
|
|
|
|
tsickle.emitWithTsickle(
|
2017-09-08 21:40:32 -04:00
|
|
|
|
program, tsickleHost, host, options, targetSourceFile, writeFile,
|
2017-08-16 18:35:19 -04:00
|
|
|
|
cancellationToken, emitOnlyDtsFiles, {
|
|
|
|
|
beforeTs: customTransformers.before,
|
|
|
|
|
afterTs: customTransformers.after,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 18:53:17 -04:00
|
|
|
|
export interface NgcParsedConfiguration extends ParsedConfiguration { watch?: boolean; }
|
|
|
|
|
|
|
|
|
|
function readNgcCommandLineAndConfiguration(args: string[]): NgcParsedConfiguration {
|
|
|
|
|
const options: api.CompilerOptions = {};
|
|
|
|
|
const parsedArgs = require('minimist')(args);
|
|
|
|
|
if (parsedArgs.i18nFile) options.i18nInFile = parsedArgs.i18nFile;
|
|
|
|
|
if (parsedArgs.i18nFormat) options.i18nInFormat = parsedArgs.i18nFormat;
|
|
|
|
|
if (parsedArgs.locale) options.i18nInLocale = parsedArgs.locale;
|
|
|
|
|
const mt = parsedArgs.missingTranslation;
|
|
|
|
|
if (mt === 'error' || mt === 'warning' || mt === 'ignore') {
|
|
|
|
|
options.i18nInMissingTranslations = mt;
|
|
|
|
|
}
|
|
|
|
|
const config = readCommandLineAndConfiguration(
|
|
|
|
|
args, options, ['i18nFile', 'i18nFormat', 'locale', 'missingTranslation', 'watch']);
|
|
|
|
|
const watch = parsedArgs.w || parsedArgs.watch;
|
|
|
|
|
return {...config, watch};
|
2017-08-18 17:03:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 18:53:17 -04:00
|
|
|
|
export function readCommandLineAndConfiguration(
|
|
|
|
|
args: string[], existingOptions: api.CompilerOptions = {},
|
|
|
|
|
ngCmdLineOptions: string[] = []): ParsedConfiguration {
|
|
|
|
|
let cmdConfig = ts.parseCommandLine(args);
|
|
|
|
|
const project = cmdConfig.options.project || '.';
|
|
|
|
|
const cmdErrors = cmdConfig.errors.filter(e => {
|
|
|
|
|
if (typeof e.messageText === 'string') {
|
|
|
|
|
const msg = e.messageText;
|
|
|
|
|
return !ngCmdLineOptions.some(o => msg.indexOf(o) >= 0);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
if (cmdErrors.length) {
|
|
|
|
|
return {
|
|
|
|
|
project,
|
|
|
|
|
rootNames: [],
|
|
|
|
|
options: cmdConfig.options,
|
|
|
|
|
errors: cmdErrors,
|
|
|
|
|
emitFlags: api.EmitFlags.Default
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-08-09 16:45:45 -04:00
|
|
|
|
const allDiagnostics: Diagnostics = [];
|
2017-09-12 18:53:17 -04:00
|
|
|
|
const config = readConfiguration(project, cmdConfig.options);
|
|
|
|
|
const options = {...config.options, ...existingOptions};
|
2017-08-31 17:11:29 -04:00
|
|
|
|
if (options.locale) {
|
|
|
|
|
options.i18nInLocale = options.locale;
|
|
|
|
|
}
|
2017-09-12 18:53:17 -04:00
|
|
|
|
return {
|
|
|
|
|
project,
|
|
|
|
|
rootNames: config.rootNames, options,
|
|
|
|
|
errors: config.errors,
|
|
|
|
|
emitFlags: config.emitFlags
|
|
|
|
|
};
|
2017-08-09 16:45:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reportErrorsAndExit(
|
|
|
|
|
options: api.CompilerOptions, allDiagnostics: Diagnostics,
|
|
|
|
|
consoleError: (s: string) => void = console.error): number {
|
|
|
|
|
if (allDiagnostics.length) {
|
|
|
|
|
consoleError(formatDiagnostics(options, allDiagnostics));
|
|
|
|
|
}
|
2017-09-12 18:53:17 -04:00
|
|
|
|
return exitCodeFromResult(allDiagnostics);
|
2017-08-18 17:03:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 18:53:17 -04:00
|
|
|
|
export function watchMode(
|
|
|
|
|
project: string, options: api.CompilerOptions, consoleError: (s: string) => void) {
|
|
|
|
|
return performWatchCompilation(createPerformWatchHost(project, diagnostics => {
|
|
|
|
|
consoleError(formatDiagnostics(options, diagnostics));
|
|
|
|
|
}, options, options => createEmitCallback(options)));
|
2017-08-09 16:45:45 -04:00
|
|
|
|
}
|
2016-05-01 14:22:39 -04:00
|
|
|
|
|
2016-11-30 16:59:53 -05:00
|
|
|
|
// CLI entry point
|
|
|
|
|
if (require.main === module) {
|
2017-08-02 14:20:07 -04:00
|
|
|
|
const args = process.argv.slice(2);
|
2017-09-13 19:55:42 -04:00
|
|
|
|
process.exitCode = main(args);
|
2016-11-30 19:45:40 -05:00
|
|
|
|
}
|