如何检测脚本是否正在使用npm或yarn运行?

时间:2018-08-09 13:45:43

标签: node.js npm yarnpkg npm-scripts

我需要为npm和yarn编写通用脚本。

npm的package.json:

{
  "scripts": {
    "build:clean": "rim-raf dist",
    "test:clean": "rim-raf coverage",
    "clean": "npm run build:clean; npm run test:clean"
  }
}
纱线的

package.json:

{
  "scripts": {
    "build:clean": "rim-raf dist",
    "test:clean": "rim-raf coverage",
    "clean": "yarn run build:clean; yarn run test:clean"
  }
}

package.json用于npm和yarn:

我通常会使用一些小技巧: $ _

{
  "scripts": {
    "build:clean": "rim-raf dist",
    "test:clean": "rim-raf coverage",
    "clean": "$_ run build:clean; $_ run test:clean"
  }
}

但是有时无法正常工作。

存在某种合法的方法吗?

编辑:

我发现我正在寻找solution

已解决

2 个答案:

答案 0 :(得分:4)

我认为我找到了最佳解决方案。

我在 npm env 中进行了搜索,并将其与 yarn env 进行了比较。 我找到了变量 $ npm_execpath ,其中包含所用软件包管理器的路径。 (对于Windows %npm_execpath%

package.json用于npm和yarn:

{
  "scripts": {
    "build:clean": "rim-raf dist",
    "test:clean": "rim-raf coverage",
    "clean": "$npm_execpath run build:clean; $npm_execpath run test:clean"
  }
}

我针对npm和yarn的最终解决方案package.json可在Linux,Windows,Mac上运行:

如果添加软件包cross-var,则脚本可在Linux,Windows和Mac上运行。

{
  "scripts": {
    "build:clean": "rim-raf dist",
    "test:clean": "rim-raf coverage",
    "clean": "cross-var $npm_execpath run build:clean; cross-var $npm_execpath run test:clean"
  }
}

答案 1 :(得分:0)

您可以尝试以下第三方模块:

https://github.com/BendingBender/yarpm