yarn 控制台脚本中是否有类似的 npm 变量(`npm_config_`)?

问题描述 投票:0回答:1

您可以在自定义 npm 命令中使用变量(示例来自 https://www.twilio.com/blog/npm-scripts):

{
  "scripts": {
    "demo": "echo \"Hello $npm_config_first $npm_config_last\""
  }
}

用纱线可以吗?

javascript npm terminal yarnpkg npm-scripts
1个回答
0
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.