我们正在为我们的Angular 2应用程序进行国际化/本地化的设置,并且我希望能够编写一些脚本来执行各种任务,例如生成翻译源文件,使用特定语言的翻译构建和/或提供应用程序。
与其在package.json文件中编写脚本,我希望能够在scripts /目录中捕获单独的脚本文件,而package.json文件中的脚本只需指向这些文件。
例如,我想将
而应该像这样:
而不是这样:
基本上我需要做以下操作:
但是我一直收到这个错误提示:
当我在命令行中运行
与其在package.json文件中编写脚本,我希望能够在scripts /目录中捕获单独的脚本文件,而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 serve"?
- 能够将参数传递给这些脚本
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
时,它非常有效,但是当我尝试从脚本中运行它时,它就不能工作。我猜想我需要设置脚本文件来知道我的.angular-cli.json
文件和package.json
文件在哪里,并对这些文件中的信息进行处理,以使ng serve
从脚本文件中正确运行。但是我可能完全错了,不知道失败的原因。
简而言之:如何成功地从脚本文件中运行带有标志的Angular CLI脚本?如何获取传递的参数?