issue: in script: we want to check env. variable {dev/test/mock} and do following script run based on it.
问题:在脚本中:我们要检查环境。变量{dev / test / mock}并根据它运行以下脚本。
if $mock is true the run script start-mock else go on reach real test server
如果$ mock为true,则运行脚本start-mock继续到达真正的测试服务器
scenario 1: we added commands aggregated in package.json script section
场景1:我们添加了在package.json脚本部分聚合的命令
e.g. : "test": "export NODE_ENV=dev; grunt", [on linux]
which is "test": "(SET NODE_ENV=dev) & (grunt)", [on win32]
scenario 2: could be bat/sh script sitting in package and we called them out from package.json
方案2:可能是bat / sh脚本坐在包中,我们从package.json中调出它们
scenario 3: (permanent solution) not sure if its already available out there
场景3 :(永久解决方案)不确定它是否已经可用
something like
get arguments from script section: to give flexibility and freedom to end user.
e.g. : "test": "solution.env NODE_ENV=dev; solution grunt"
where we can have script to process (input with process.platform) out put depends on OS.
我们可以让脚本处理(使用process.platform输入)输出取决于操作系统。
"start-pm2": "if \"%MOCK%\" == \"true\" ( npm run mock & pm2 start process.json --env test ) else ( 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]对于linux如果.. fi
3 个解决方案
#1
5
Lets consider implementation of 3-th solution like e.g.
让我们考虑实现第3个解决方案,例如
package.json
"scripts": {
"command" : "node bin/command.js"
}
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))
depends on environments script will execute command.bat
or command.sh
取决于环境脚本将执行command.bat或command.sh
#2
4
Use: run-script-os
For example:
// 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
2
You will need to implement solution 3.
您需要实施解决方案3。
You can use cross-env package that does it for you.
您可以使用为您执行此操作的cross-env包。
#1
5
Lets consider implementation of 3-th solution like e.g.
让我们考虑实现第3个解决方案,例如
package.json
"scripts": {
"command" : "node bin/command.js"
}
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))
depends on environments script will execute command.bat
or command.sh
取决于环境脚本将执行command.bat或command.sh
#2
4
Use: run-script-os
For example:
// 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
2
You will need to implement solution 3.
您需要实施解决方案3。
You can use cross-env package that does it for you.
您可以使用为您执行此操作的cross-env包。