/** * @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 {makeTempDir} from '@angular/tsc-wrapped/test/test_support'; import * as fs from 'fs'; import * as path from 'path'; import * as ts from 'typescript'; import {main} from '../src/extract_i18n'; function getNgRootDir() { const moduleFilename = module.filename.replace(/\\/g, '/'); const distIndex = moduleFilename.indexOf('/dist/all'); return moduleFilename.substr(0, distIndex); } const EXPECTED_XMB = ` ]> src/module.ts:1translate me src/module.ts:2Welcome `; const EXPECTED_XLIFF = ` translate me src/module.ts 1 desc meaning Welcome src/module.ts 2 `; const EXPECTED_XLIFF2 = ` desc meaning src/module.ts:1 translate me src/module.ts:2 Welcome `; describe('extract_i18n command line', () => { let basePath: string; let outDir: string; let write: (fileName: string, content: string) => void; let errorSpy: jasmine.Spy&((s: string) => void); function writeConfig(tsconfig: string = '{"extends": "./tsconfig-base.json"}') { write('tsconfig.json', tsconfig); } beforeEach(() => { errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error); basePath = makeTempDir(); write = (fileName: string, content: string) => { const dir = path.dirname(fileName); if (dir != '.') { const newDir = path.join(basePath, dir); if (!fs.existsSync(newDir)) fs.mkdirSync(newDir); } fs.writeFileSync(path.join(basePath, fileName), content, {encoding: 'utf-8'}); }; write('tsconfig-base.json', `{ "compilerOptions": { "experimentalDecorators": true, "skipLibCheck": true, "noImplicitAny": true, "types": [], "outDir": "built", "rootDir": ".", "baseUrl": ".", "declaration": true, "target": "es5", "module": "es2015", "moduleResolution": "node", "lib": ["es6", "dom"], "typeRoots": ["node_modules/@types"] } }`); outDir = path.resolve(basePath, 'built'); const ngRootDir = getNgRootDir(); const nodeModulesPath = path.resolve(basePath, 'node_modules'); fs.mkdirSync(nodeModulesPath); fs.symlinkSync( path.resolve(ngRootDir, 'dist', 'all', '@angular'), path.resolve(nodeModulesPath, '@angular')); fs.symlinkSync( path.resolve(ngRootDir, 'node_modules', 'rxjs'), path.resolve(nodeModulesPath, 'rxjs')); }); function writeSources() { write('src/basic.html', [ `
`, `

Welcome

`, ].join('\n')); write('src/module.ts', ` import {Component, NgModule} from '@angular/core'; @Component({ selector: 'basic', templateUrl: './basic.html', }) export class BasicCmp {} @NgModule({ declarations: [BasicCmp] }) export class I18nModule {} `); } it('should extract xmb', () => { writeConfig(); writeSources(); const exitCode = main(['-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 = main(['-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 = main(['-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 = main(['-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(); }); });