npm package.json操作系统特定的脚本 [英] npm package.json OS specific script

查看:290
本文介绍了npm package.json操作系统特定的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个package.json构建脚本,当从Windows,Linux,Mac运行时,该脚本执行一组稍有不同的命令.

I would like to create a package.json build script that executes slightly different set of commands when run from Windows, Linux, Mac.

问题是我找不到将其放入package.json文件的方法,该文件将在每个系统上正常运行.

The problem is that I cannot find a way to put it in package.json file that will run without problems at every system.

这是我想要的一个例子:

Here is an example that I would like to have:

"scripts" : {
    "build.windows" : "echo do windows specific stuff",
    "build.linux" : "echo do linux specific stuff",
    "build.mac" : "echo do mac specific stuff",
    "build" : "??????????????" <- what to put here to execute script designed for OS
                                  on which npm is running
}

推荐答案

您可以通过 node run-script 命令使用脚本. npm run是它的快捷方式.

You can use scripts with node run-script command. npm run is a shortcut of it.

包装json:

"scripts" : {
    "build-windows" : "node build-windows.js",
    "build-linux" : "node build-linux.js",
    "build-mac" : "node build-mac.js",
    "build" : "node build.js"
}

命令行:

npm run build-windows

如果您不喜欢它,则可以在node.js中使用命令.

If you don't like it, you can use commands inside node.js.

包装json:

"scripts" : {
    "build" : "node build.js"
}

Build.js

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }

var os = require('os');
//control OS
//then run command depengin on the OS

if (os.type() === 'Linux') 
   exec("node build-linux.js", puts); 
else if (os.type() === 'Darwin') 
   exec("node build-mac.js", puts); 
else if (os.type() === 'Windows_NT') 
   exec("node build-windows.js", puts);
else
   throw new Error("Unsupported OS found: " + os.type());

这篇关于npm package.json操作系统特定的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆