use*_*856 11 node.js package.json
问题:在脚本中:我们要检查环境.变量{dev/test/mock}并根据它运行以下脚本.
如果 $模拟是真实的运行脚本启动模拟别人去达到真正的测试服务器
方案1: 我们添加了在package.json脚本部分中聚合的命令
e.g. : "test": "export NODE_ENV=dev; grunt", [on linux]
which is "test": "(SET NODE_ENV=dev) & (grunt)", [on win32]
方案2: 可能是bat/sh脚本坐在包中,我们从package.json中调出它们
场景3 :(永久解决方案)不确定它是否已经可用
就像是
get arguments from script section: to give flexibility and freedom to end user.
 e.g. : "test": "solution.env NODE_ENV=dev; solution grunt"
我们可以让脚本处理(使用process.platform输入)输出取决于操作系统.
"start-pm2":"if \"%MOCK%\"== \"true \"(npm run mock&pm2 start process.json --env test)else(pm2 start process.json)",[windows]对于linux if .. fi

例如:
// from pacakge.json
"scripts": {
    // ...
    "dist": "run-script-os",
    "dist:win32": "tar -C dist -cvzf %npm_package_name%-%npm_package_version%.tgz .",
    "dist:linux": "tar -C dist -cvzf $npm_package_name-$npm_package_version.tgz ."
},
让我们考虑实现第3个解决方案,例如
"scripts": {
  "command" : "node bin/command.js"
}
const spawn = require("child_process").spawn
const platform = require("os").platform()
const cmd = /^win/.test(platform)
  ? `${process.cwd()}\\bin\\command.bat`
  : `${process.cwd()}/bin/command.sh`
spawn(cmd, [], { stdio: "inherit" }).on("exit", code => process.exit(code))
取决于环境脚本将执行command.bat或command.sh
| 归档时间: | 
 | 
| 查看次数: | 3353 次 | 
| 最近记录: |