如何检测脚本是使用 npm 还是 yarn 运行? [英] How to detect that the script is running with npm or yarn?

查看:149
本文介绍了如何检测脚本是使用 npm 还是 yarn 运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to write universal scripts for npm and yarn.

{
  "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:

我通常使用小技巧:***$_***

package.json for npm and yarn:

I usually use little hack: ***$_***

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

但有时它无法正常工作.

是否存在一些合法的方法来做到这一点?

But sometime it does not work correctly.

Exists some legit way to do this?

推荐答案

我想我找到了最佳解决方案.

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

I think I found the optimal solution.

I searched in npm env and compared it with yarn env. I found the variable $npm_execpath that contains the path to used package manager. (For Windows %npm_execpath%)

{
  "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 上运行.>

My final solution package.json for npm and yarn works on Linux, Windows, Mac:

If add package cross-var, scripts works on Linux, Windows and 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"
  }
}

这篇关于如何检测脚本是使用 npm 还是 yarn 运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆