在Windows上的package.json中外部定位的'npm run'脚本? [英] Externally-located 'npm run' scripts in package.json on Windows?
问题描述
如我们所知,您可以使用 npm run
通过向<$添加脚本
哈希来运行任意命令c $ c> package.json :
As we know, you can run arbitrary commands using npm run
by adding a scripts
hash to your package.json
:
"scripts": {
"build-js": "browserify browser/main.js | uglifyjs -mc > static/bundle.js"
}
然后用 npm运行build-js
运行。
你也可以将这些命令移到单独的脚本中,例如bash脚本,如下所示:
You can also move these commands out into separate scripts, such as bash scripts, as such:
"scripts": {
"build-js": "bin/build.sh"
}
这显然没有'由于Windows无法运行bash脚本,因此本机可以在Windows上运行。你可以安装bash端口等,但是我希望能够使用某种原生Windows构造来做同样的事情。
This obviously doesn't natively work on Windows, due to Windows not being capable of running bash scripts. You can install bash ports and such, but I'd love to be able to use some sort of native Windows construct for doing the same thing.
我试过了一些其他方法,包括使用 child_process.exec
从标准节点脚本文件中运行任意命令:
I've tried some other approaches, including using child_process.exec
to run arbitrary commands from within a standard node script file:
"scripts": {
"build-js": "node bin/build.js"
}
但是我注意到 child_process
对相对较大/密集的操作进行扼流,使得使用起来难以置信。
But I've noticed child_process
chokes on relatively large/intensive operations, making it implausible to use.
是否有特定于Windows的(甚至更好的,跨平台)方式来移动这些 package.json
npm运行
将脚本分成不同的文件?最好不需要bash?
Is there a Windows-specific (or even better, cross-platform) way to move these package.json
npm run
scripts out into separate files? Preferably one that doesn't require bash?
推荐答案
来自关于使用NPM作为构建工具的有用文章,为什么不简单地使用一个JavaScript文件?
From a helpful article on using NPM as a build tool, why not simply use a JavaScript file?
以下是文章中给出的示例(为清晰起见略作修改):
Here's the example given in the article (slightly modified for clarity):
// scripts/favicon.js
var favicons = require('favicons');
var path = require('path');
favicons({
source: path.resolve('../assets/images/logo.png'),
dest: path.resolve('../dist/'),
});
// package.json
"scripts": {
"build-favicon": "node scripts/favicon.js",
},
"devDependencies": {
"favicons": "latest",
}
使用命令在终端中运行npm run build-favicon
这篇关于在Windows上的package.json中外部定位的'npm run'脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!