简体   繁体   English

关于package.json中的“scripts”键

[英]about “scripts” key in package.json

Can anyone explain what is the purpose of "scripts" key in package.json file. 任何人都可以解释package.json文件中“脚本”键的用途是什么。

While learning Angular 2 , I came across below script in package.json file, was not sure whats the use of this key. 在学习Angular 2时,我在package.json文件中遇到了下面的脚本,不确定是否使用了这个键。

"scripts": {
"postinstall": "npm run typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"start": "concurrent \"node ./bin/www\" \"npm run tsc:w\"",
"typings" : "typings"
 }

The scripts key provides a convenient place to define project-specific automation scripts right there in your already-mandatory package.json . scripts键提供了一个方便的位置,可以在您已经强制使用的package.json定义项目特定的自动化脚本。 This helps developers just type something simple, like npm run cover or npm run deploy and have a possibly-complex series of steps (with very specific file locations or program arguments) kicked off. 这有助于开发人员只需输入简单的内容,例如npm run covernpm run deploy ,并启动可能复杂的一系列步骤(具有非常特定的文件位置或程序参数)。 This avoids typing long command lines, and it avoids errors. 这可以避免键入长命令行,并避免错误。 For example, one of my projects includes these commands: 例如,我的一个项目包括以下命令:

"scripts": {
    "cover": "cd source/js/jutil && istanbul cover _mocha -- -R spec && open coverage/lcov-report/jutil/index.html",
    "test": "cd source/js/jutil && mocha",
    "deploy": "(git diff --quiet --exit-code --cached || git commit -a -m 'deploy') && git push heroku master && heroku open",
    "start": "gulp start"
}

I can quickly run a test, a coverage test, or deploy to a cloud host with just a few words, and never having to remember the detailed command-line options or file locations. 我可以快速运行测试,覆盖测试,或只需几句话就可以部署到云主机,而且不必记住详细的命令行选项或文件位置。

Beware, however. 但要注意。 There is much controversy in the JavaScript / node.js community about the "best" or "right" place to define such automation. JavaScript / node.js社区中存在很多关于定义这种自动化的“最佳”或“正确”位置的争议。

Many developers prefer to shift automation to separate "task runners" / "build systems" such as gulp , grunt , or even good ole Unix make . 许多开发人员更喜欢将自动化转移到单独的“任务运行者”/“构建系统”,例如gulpgrunt ,甚至是好的Unix make For those projects, the scripts tag will be empty or nearly so. 对于这些项目, scripts标记将为空或几乎为空。 ( npm init by default generates at least a single key, for test .) Instead you need to look to their gulpfile.js , Gruntfile , or Makefile . npm init默认生成至少一个键,用于test 。)而是需要查看他们的gulpfile.jsGruntfileMakefile

Other developers have rejected the idea of becoming build-system mechanics and/or separating out build configuration. 其他开发人员拒绝了成为构建系统机制和/或分离构建配置的想法。 They often prefer to put "a few simple scripts" directly in package.json and call it a day. 他们通常喜欢直接在package.json “一些简单的脚本”并将其称为一天。

In my experience, "a few simple commands" is a great goal but scripting complexity can quickly outstrip it. 根据我的经验,“一些简单的命令”是一个很好的目标,但脚本的复杂性很快就会超过它。 This is especially true in larger projects with many different assets and asset types, for which live-rebuild or live-reload are desired, or that have more than a few deployment options. 在具有许多不同资产和资产类型的大型项目中尤其如此,需要进行实时重建或实时重新加载,或者具有多个部署选项。 I usually end up with gulp doing the heavy-lifting, but perhaps a few scripts that can truly be concisely stated still resident in package.json . 我通常最终会gulp这个繁重的东西,但也许一些真正简洁的脚本仍然存在于package.json "Your mileage may vary." “你的旅费可能会改变。”

请参阅此内容,它将为您提供package.json中更好的创意脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM