node.js 使用 npm 运行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34937724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running bash scripts with npm
提问by Mark McKelvy
I want to try using npm to run my various build tasks for a web application. I know I can do this by adding a scriptsfield to my package.jsonlike so:
我想尝试使用 npm 为 Web 应用程序运行各种构建任务。我知道我可以通过向scripts我添加一个字段来做到这package.json一点:
"scripts": {
"build": "some build command"
},
This gets unwieldy when you have more complex commands with a bunch of options. Is it possible to move these commands to a bash script or something along those lines? Something like:
当您有更复杂的命令和一堆选项时,这会变得很笨拙。是否可以将这些命令移动到 bash 脚本或类似的东西?就像是:
"scripts": {
"build": "build.sh"
},
where npm run buildwould execute the commands in the build.shfile?
在哪里npm run build执行build.sh文件中的命令?
Reading through thispost it seems like it is, but I'm not clear on exactly where I'm supposed to drop my build.shfile or if I'm missing something.
通读这篇文章似乎是这样,但我不清楚我应该把我的build.sh文件放在哪里,或者我是否遗漏了什么。
回答by eblahm
Its totally possible...
它完全有可能...
"scripts": {
"build": "./build.sh"
},
also, make sure you put a hash bang at the top of your bash file #!/usr/bin/env bash
另外,请确保在 bash 文件的顶部放置一个哈希爆炸 #!/usr/bin/env bash
also make sure you have permissions to execute the file
还要确保您有权执行该文件
chmod +x ./build.sh
Finally, the command to run build in npm would be
最后,在 npm 中运行构建的命令是
npm run build
回答by CNSKnight
Even Simpler:
更简单:
I routinely do this for one-offs and PoC's not involving a VCS
我经常这样做是一次性的,PoC 不涉及 VCS
包.json{
"scripts": {
"ship": "rsync -avz deployable/* <some-server>:/var/www/some-site/sub-dir/"
},
}
...

