/**
* @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
*/
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import {mainXi18n} from '../src/extract_i18n';
import {isInBazel, makeTempDir, setup} from './test_support';
function getNgRootDir() {
const moduleFilename = module.filename.replace(/\\/g, '/');
const distIndex = moduleFilename.indexOf('/dist/all');
return moduleFilename.substr(0, distIndex);
}
const EXPECTED_XMB = `
]>
${welcomeMessage}
`); write('src/comp1.ts', ` import {Component} from '@angular/core'; @Component({ selector: 'basic', templateUrl: './basic.html', }) export class BasicCmp1 {}`); write('src/comp2.ts', ` import {Component} from '@angular/core'; @Component({ selector: 'basic2', template: \`${welcomeMessage}
\`, }) export class BasicCmp2 {} @Component({ selector: 'basic4', template: \`${welcomeMessage}
\`, }) export class BasicCmp4 {}`); write('src/comp3.ts', ` import {Component} from '@angular/core'; @Component({ selector: 'basic3', templateUrl: './basic.html', }) export class BasicCmp3 {}`); write('src/module.ts', ` import {NgModule} from '@angular/core'; import {BasicCmp1} from './comp1'; import {BasicCmp2, BasicCmp4} from './comp2'; import {BasicCmp3} from './comp3'; @NgModule({ declarations: [BasicCmp1, BasicCmp2, BasicCmp3, BasicCmp4] }) export class I18nModule {} `); } it('should extract xmb', () => { writeConfig(); writeSources(); const exitCode = mainXi18n(['-p', basePath, '--i18nFormat=xmb', '--outFile=custom_file.xmb'], errorSpy); expect(errorSpy).not.toHaveBeenCalled(); expect(exitCode).toBe(0); const xmbOutput = path.join(outDir, 'custom_file.xmb'); expect(fs.existsSync(xmbOutput)).toBeTruthy(); const xmb = fs.readFileSync(xmbOutput, {encoding: 'utf-8'}); expect(xmb).toEqual(EXPECTED_XMB); }); it('should extract xlf', () => { writeConfig(); writeSources(); const exitCode = mainXi18n(['-p', basePath, '--i18nFormat=xlf', '--locale=fr'], errorSpy); expect(errorSpy).not.toHaveBeenCalled(); expect(exitCode).toBe(0); const xlfOutput = path.join(outDir, 'messages.xlf'); expect(fs.existsSync(xlfOutput)).toBeTruthy(); const xlf = fs.readFileSync(xlfOutput, {encoding: 'utf-8'}); expect(xlf).toEqual(EXPECTED_XLIFF); }); it('should extract xlf2', () => { writeConfig(); writeSources(); const exitCode = mainXi18n(['-p', basePath, '--i18nFormat=xlf2', '--outFile=messages.xliff2.xlf'], errorSpy); expect(errorSpy).not.toHaveBeenCalled(); expect(exitCode).toBe(0); const xlfOutput = path.join(outDir, 'messages.xliff2.xlf'); expect(fs.existsSync(xlfOutput)).toBeTruthy(); const xlf = fs.readFileSync(xlfOutput, {encoding: 'utf-8'}); expect(xlf).toEqual(EXPECTED_XLIFF2); }); it('should not emit js', () => { writeConfig(); writeSources(); const exitCode = mainXi18n(['-p', basePath, '--i18nFormat=xlf2', '--outFile=messages.xliff2.xlf'], errorSpy); expect(errorSpy).not.toHaveBeenCalled(); expect(exitCode).toBe(0); const moduleOutput = path.join(outDir, 'src', 'module.js'); expect(fs.existsSync(moduleOutput)).toBeFalsy(); }); });