如何在package.json(nodeJs)中创建跨平台脚本(单行多个命令) [英] how to create cross platform scripts (multiple command for single line) in package.json (nodeJs)
问题描述
问题: 在脚本中: 我们要检查环境.变量{dev/test/mock}并根据它运行以下脚本.
issue: in script: we want to check env. variable {dev/test/mock} and do following script run based on it.
如果 $ mock是真实的,则运行脚本开始模拟 else 继续到达真实的测试服务器
if $mock is true the run script start-mock else go on reach real test server
方案1: 我们在package.json脚本部分添加了汇总的命令
scenario 1: we added commands aggregated in package.json script section
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中调用了它们
scenario 2: could be bat/sh script sitting in package and we called them out from package.json
方案3:(永久解决方案) 不确定它是否已经可用
scenario 3: (permanent solution) not sure if its already available out there
类似
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输入)的脚本取决于操作系统.
where we can have script to process (input with process.platform) out put depends on OS.
"start-pm2":如果\"%MOCK%\"== \" true \(npm运行模拟& pm2 start process.json --env测试),否则(pm2 start process.json)" ,[windows] for linux if .. fi
"start-pm2": "if \"%MOCK%\" == \"true\" ( npm run mock & pm2 start process.json --env test ) else ( pm2 start process.json )", [windows] for linux if.. fi
推荐答案
使用: run-脚本操作系统
例如:
// 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 ."
},
这篇关于如何在package.json(nodeJs)中创建跨平台脚本(单行多个命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!