2016-06-01 14:58:11 -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-06-01 14:58:11 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract i18n messages from source code
|
|
|
|
*/
|
|
|
|
// Must be imported first, because angular2 decorators throws on load.
|
|
|
|
import 'reflect-metadata';
|
|
|
|
|
2016-07-21 13:56:58 -07:00
|
|
|
import * as compiler from '@angular/compiler';
|
2016-10-21 15:17:57 -07:00
|
|
|
import * as tsc from '@angular/tsc-wrapped';
|
2016-07-21 13:56:58 -07:00
|
|
|
import * as path from 'path';
|
2016-06-01 14:58:11 -07:00
|
|
|
import * as ts from 'typescript';
|
2016-10-21 15:17:57 -07:00
|
|
|
|
|
|
|
import {Extractor} from './extractor';
|
2016-06-01 14:58:11 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
function extract(
|
2016-08-12 14:45:36 -07:00
|
|
|
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.I18nExtractionCliOptions,
|
|
|
|
program: ts.Program, host: ts.CompilerHost) {
|
2016-11-18 08:40:41 -08:00
|
|
|
const extractor = Extractor.create(ngOptions, cliOptions.i18nFormat, program, host);
|
2016-10-21 15:17:57 -07:00
|
|
|
|
2016-08-30 18:07:40 -07:00
|
|
|
const bundlePromise: Promise<compiler.MessageBundle> = extractor.extract();
|
2016-07-21 13:56:58 -07:00
|
|
|
|
|
|
|
return (bundlePromise).then(messageBundle => {
|
2016-08-12 22:13:54 -07:00
|
|
|
let ext: string;
|
2016-08-30 18:07:40 -07:00
|
|
|
let serializer: compiler.Serializer;
|
2016-08-12 22:13:54 -07:00
|
|
|
const format = (cliOptions.i18nFormat || 'xlf').toLowerCase();
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case 'xmb':
|
|
|
|
ext = 'xmb';
|
2016-08-30 18:07:40 -07:00
|
|
|
serializer = new compiler.Xmb();
|
2016-08-12 22:13:54 -07:00
|
|
|
break;
|
|
|
|
case 'xliff':
|
|
|
|
case 'xlf':
|
|
|
|
default:
|
|
|
|
ext = 'xlf';
|
2016-11-02 17:40:15 -07:00
|
|
|
serializer = new compiler.Xliff();
|
2016-08-12 22:13:54 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dstPath = path.join(ngOptions.genDir, `messages.${ext}`);
|
2016-07-21 13:56:58 -07:00
|
|
|
host.writeFile(dstPath, messageBundle.write(serializer), false);
|
|
|
|
});
|
2016-06-01 14:58:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Entry point
|
|
|
|
if (require.main === module) {
|
|
|
|
const args = require('minimist')(process.argv.slice(2));
|
2016-08-12 14:45:36 -07:00
|
|
|
const project = args.p || args.project || '.';
|
|
|
|
const cliOptions = new tsc.I18nExtractionCliOptions(args);
|
|
|
|
tsc.main(project, cliOptions, extract)
|
|
|
|
.then((exitCode: any) => process.exit(exitCode))
|
|
|
|
.catch((e: any) => {
|
2016-06-08 16:38:52 -07:00
|
|
|
console.error(e.stack);
|
2016-07-21 13:56:58 -07:00
|
|
|
console.error('Extraction failed');
|
2016-06-08 16:38:52 -07:00
|
|
|
process.exit(1);
|
|
|
|
});
|
2016-08-12 14:45:36 -07:00
|
|
|
}
|