Skip to content

npm 参数 #207

@CongratulateWE

Description

@CongratulateWE
"scripts": {
  "view": "echo $npm_config_branch && echo done"
}

npm run view --branch=master

mp-app-home@1.0.1 view
echo $npm_config_branch && echo done
master
done

但是使用 pnpm 的时候,拿不到这个变量。文档上好像没有提到怎么传参,只是说运行 pnpm run --foo=barwebpack --foo=bar效果一样。


我们可以试用 yargs 来获取参数。

#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
// hideBin 相当于 process.argv.slice(2)
const argv = yargs(hideBin(process.argv)).parse()

console.log(argv.branch)

pnpm run prod --branch=master

master

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions