67 lines
1.9 KiB
TypeScript
Raw Normal View History

2016-06-01 14:58:11 -07:00
#!/usr/bin/env node
/**
* @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';
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';
import {Extractor} from './extractor';
2016-06-01 14:58:11 -07:00
function extract(
ngOptions: tsc.AngularCompilerOptions, cliOptions: tsc.I18nExtractionCliOptions,
program: ts.Program, host: ts.CompilerHost) {
const extractor = Extractor.create(ngOptions, cliOptions.i18nFormat, program, host);
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;
let serializer: compiler.Serializer;
2016-08-12 22:13:54 -07:00
const format = (cliOptions.i18nFormat || 'xlf').toLowerCase();
switch (format) {
case 'xmb':
ext = 'xmb';
serializer = new compiler.Xmb();
2016-08-12 22:13:54 -07:00
break;
case 'xliff':
case 'xlf':
default:
ext = 'xlf';
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));
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) => {
console.error(e.stack);
2016-07-21 13:56:58 -07:00
console.error('Extraction failed');
process.exit(1);
});
}