我们可以在 npm 脚本中实现占位符并使用 npm 命令替换它吗?

Can we implement a placeholder in npm scripts and replace it using the npm command?

我有一个 npm 脚本

"scripts": {
"test:chrome": "set DEV_MODE=staging& npx testcafe \"chrome --start-fullscreen\" automation_suite/tests"
}

我想将 DEV_MODE=staging 替换为 DEV_MODE=dev 而不是编写多个脚本。

类似于

npm run test:chrome --dev 应将 staging 替换为 dev 并执行脚本

您可以编写一些带有默认值的命令,如果用户需要传递另一个参数,则可以覆盖。样品:

"scripts": {
    "test:chrome": "export DEV_MODE=${DEV_MODE:-staging} && echo $DEV_MODE"
  },

运行:

$npm run test:chrome
#Output: staging

$DEV_MODE=dev npm run test:chrome
#Output: dev

这至少会简化您的写作方式。