您可以在自定义 npm 命令中使用变量(示例来自 https://www.twilio.com/blog/npm-scripts):
{
"scripts": {
"demo": "echo \"Hello $npm_config_first $npm_config_last\""
}
}
用纱线可以吗?
yarn中没有内置的解决方案,但我找到了一种方法:
{
"scripts": {
"demo0": "echo \"Hello\"",
"demo": "node -e \"const args = process.argv.slice(1).reduce((acc, arg) => { const [key, value] = arg.split('='); acc[key] = value; return acc; }, {}); console.log('Hello', args.first, args.last);\""
}
}
用途:
$ yarn demo0 Kevin Ross // everything after command will be considered as one argument
// interpreted as `echo "Hello" Kevin Ross`
Hello Kevin Ross // <<< output
正确做法:
$ yarn demo first=Kevin last=Ross
Hello Kevin Ross // <<< output