从脚本文件而不是从CLI运行Angular CLI脚本? 本地化/国际化

我们正在为我们的Angular 2应用程序进行国际化/本地化,我希望能够编写一些脚本来完成各种任务,比如生成翻译源文件,构build和/或为应用程序提供服务来自特定语言的翻译。

我不希望将脚本写在package.json文件中,而是希望能够在脚本/目录中的单独文件中捕获脚本,并且package.json文件中的脚本将指向这些脚本。

例如,我想在package.json使用这样的东西:

 "scripts": { //other irrelevant scripts "generateXLF": "ng-xi18n --i18nFormat=xlf --outFile=./src/locale/baseFile/messages.xlf", //other irrelevant scripts }, 

而是有这样的东西:

 "scripts": { //other irrelevant scripts "generateXLF": "node ./build/scripts/locale/generateXLF.js" //other irrelevant scripts }, 

或者,而不是像这样的东西:

 "scripts": { //other irrelevant scripts "serveLocale": : "./node_modules/.bin/ngc --i18nFile=./src/locale/languages/($someLocaleIPassIn)/messages.($someLocaleIPassIn).xlf --locale=($someLocaleIPassIn) --i18nFormat=xlf", //other irrelevant scripts }, 

而是有这个:

 "scripts": { //other irrelevant scripts "serveLocale": : "node ./build/scripts/locale/serveLocale.js $someLocaleIPassIn" //other irrelevant scripts }, 

基本上我需要做到以下几点:

  • 编写脚本,这些脚本可以封装/执行通常需要使用Angular CLI运行的大量标志的命令。 即我怎么能运行“ng服务”从脚本文件与一堆标志?
  • 能够将parameter passing给这些脚本

目前我是那里的一部分。 例如,在我的generateXLF.js文件中,我目前只是试图运行ng serve ,没有参数或标志,如下所示:

 var ngServe = require('@angular/cli/commands/serve'); new ngServe.default(); 

但我不断收到这个错误:

 C:\Projects\MyProject\StyleGuide\node_modules\@angular\cli\ember-cli\lib\models\command.js:77 this.isWithinProject = this.project.isEmberCLIProject(); ^ TypeError: Cannot read property 'isEmberCLIProject' of undefined at Class.init (C:\Projects\MyProject\StyleGuide\node_modules\@angular\cli\ember-cli\lib\models\command.js:77:40) at Class.superWrapper [as init] (C:\Projects\MyProject\StyleGuide\node_modules\core-object\lib\assign-properties.js:34:20) at Class.CoreObject (C:\Projects\MyProject\StyleGuide\node_modules\core-object\core-object.js:9:15) at Class (C:\Projects\MyProject\StyleGuide\node_modules\core-object\core-object.js:21:5) at Class (C:\Projects\MyProject\StyleGuide\node_modules\core-object\core-object.js:21:5) at C:\Projects\MyProject\StyleGuide\build\scripts\locale\generateXLF.js:32:5 at Object.<anonymous> (C:\Projects\MyProject\StyleGuide\build\scripts\locale\generateXLF.js:37:3) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) Process finished with exit code 1 

ng serve工作正常,当我从CLI运行但不是当我尝试从脚本运行它。 我猜不知道我需要设置脚本文件了解我的.angular-cli.json文件和我的package.json文件的位置,并使用这些文件中的信息来使ng serve正常运行脚本文件。 但是,我可能完全错误,为什么它失败。

TLDR:如何通过脚本文件成功运行带有标志的Angular CLI脚本? 如何挑选我已经通过的参数?

结束了解决它。 这里是我的脚本compileInLocale.js

 #! /usr/bin/env node const angularCli = require('@angular/cli'); const fileSystem = require('fs'); const chalk = require('chalk'); var command = process.argv.slice(2, 3).toString(); var locale = process.argv.slice(3, 4).toString(); console.log(chalk.bgBlueBright.bold('Attempting to execute ' + __filename + ' with command "' + command + '" and locale "' + locale + '".\n')); makeArguments(command, locale); /** * @name makeArguments * @description Create the array of arguments to send to the Angular CLI * @param {String} commandToRun the ng command we want to run, such as 'serve' or 'build' * @param {String} specifiedLocale the locale that the developer specified when they called * the script. For example, the string 'es' from the following command line * entry: 'npm run serveInLocale es' * @returns {void} Nothing. */ function makeArguments(commandToRun, specifiedLocale) { var localeFile = './src/locale/languages/' + specifiedLocale + '/messages.' + specifiedLocale + '.xlf'; if (fileSystem.existsSync(localeFile)) { /* @TODO: add in logic to build the base translation file, then compare the contents of the @TODO <source> tags in the translation file to the <source> tags in the file we are trying @TODO to serve to see if it is up-to-date. */ var argArray = [ commandToRun, '--aot', '--i18nFile=' + localeFile, '--locale=' + specifiedLocale, '--i18nFormat=xlf' ]; console.log(chalk.bgGreen.bold('Preparing to ' + commandToRun + ' file ' + localeFile + '.\n')); serveInSpecifiedLocale(argArray); } else { console.log(chalk.bgRed.bold('Cannot ' + commandToRun + ' file ' + localeFile + '. File does not exist!\n')); } } /** * @name serveInSpecifiedLocale * @description Send an object to the Angular CLI containing our arguments array, and other * properties that Angular CLI needs to execute our command. * @param {Array} argArray the array of arguments we want to give to the CLI * @returns {void} Nothing. */ function serveInSpecifiedLocale(argArray) { /* @TODO: move this to its own file. We will have more scripts in the @TODO future which need to send arguments to the Angular CLI. */ angularCli({ cliArgs: argArray, inputStream: process.stdin, outputStream: process.stdout }); } 

使用如下命令运行它:
npm run buildInLocale es
或这个:
npm run serveInLocale fr

package.json有这样的东西:

 "serveInLocale": "node ./build/scripts/locale/compileInLocale.js serve", "buildInLocale": "node ./build/scripts/locale/compileInLocale.js build" 

根据您是否键入buildInLocaleserveInLocale ,脚本将被作为参数的servebuild命中,随后将locale作为另一个参数,如果我们有该语言环境的文件,则脚本将起作用。

旁注:我最终做了一堆工作,无缘无故的解决这个问题。 这是毫无意义的,因为Angular 2中的国际化工具(i18n属性)在涉及双向绑定时没有办法支持翻译,所以我们最终将使用类似ngx-translate东西。 我仍想发布我的解决scheme,以防其他人需要编写脚本让他们运行通常从CLI运行的脚本。