【每天一个npm包】:which-pm-runs

简介

which-pm-runs: 用于检测正在执行的是哪个包管理器。

它支持识别 npm, pnpm, Yarn, cnpm, 和 bun。同时也支持设置了 npm_config_user_agent 环境变量的其他的包管理器。

安装

bash 复制代码
pnpm add which-pm-runs
# or
npm i which-pm-runs
# or
yarn add which-pm-runs

使用

which-pm-runsonly-allow 中用到了,是同一个作者开发的。

index.js

js 复制代码
const whichPMRuns = require('which-pm-runs')

console.log(whichPMRuns())

package.json

json 复制代码
{
    "scripts": {
        "test": "node index.js"
    }
}

在终端执行以下命令:

bash 复制代码
$ pnpm run test
{ name: 'pnpm', version: '8.6.12' }
bash 复制代码
$ npm run test
{ name: 'npm', version: '9.6.7' }

可以看到,对于不同的包管理器,执行script脚本会得到对应包管理器的名称版本信息。

或许我们会想:这是如何实现的呢?

其实最开始就提到了,就是 npm_config_user_agent 这个环境变量。

关于 npm_config_user_agent

npm_config_user_agent 是一个环境变量,用于存储当前 npm 运行的用户代理信息。这个环境变量通常由 npm 在执行过程中自动生成,用来标识 npm 的版本和所使用的 Node.js 版本等信息。

通常情况下,npm_config_user_agent 的值类似于以下格式:

复制代码
npm/{npm版本} node/{Node.js版本} {操作系统信息}

其中,npm版本 是 npm 的版本号,Node.js版本 是当前 Node.js 的版本号,操作系统信息 包括当前操作系统的名称和版本号等信息。

接下来看看 npm_config_user_agent 具体是什么样的。

index.js

js 复制代码
console.log(process.env.npm_config_user_agent)

package.json

json 复制代码
{
    "scripts: {
        "test": "node index.js"
    }
}

执行以下命令:

bash 复制代码
$ npm run test
npm/9.6.7 node/v18.17.0 win32 x64 workspaces/false
bash 复制代码
$ pnpm run test
pnpm/8.6.12 npm/? node/v18.17.0 win32 x64

有了这些信息就可以正确的获取到对应的包管理的名称版本信息

源码

which-pm-runs/index.js

总共就 18 行代码,原理就是对 npm_config_user_agent 环境变量的处理。

js 复制代码
'use strict'

module.exports = function () {
  if (!process.env.npm_config_user_agent) {
    return undefined
  }
  return pmFromUserAgent(process.env.npm_config_user_agent)
}

function pmFromUserAgent (userAgent) {
  const pmSpec = userAgent.split(' ')[0]
  const separatorPos = pmSpec.lastIndexOf('/')
  const name = pmSpec.substring(0, separatorPos)
  return {
    name: name === 'npminstall' ? 'cnpm' : name,
    version: pmSpec.substring(separatorPos + 1)
  }
}

测试

which-pm-runs/test/index.js

js 复制代码
'use strict'
const test = require('tape')
const execa = require('execa')
const path = require('path')
const os = require('os')

delete process.env.npm_config_user_agent

const fixturesDir = path.join(__dirname, 'fixtures')

test('detects yarn', t => {
  execa('yarn', [], { cwd: path.join(fixturesDir, 'yarn') })
    .then(() => t.end())
    .catch(t.end)
})

test('detects bun', t => {
  if(os.platform() === 'win32') return t.end();
  execa('bun', ['install'], { cwd: path.join(fixturesDir, 'bun') })
    .then(() => t.end())
    .catch(t.end)
})

test('detects npm', t => {
  execa('npm', ['install'], { cwd: path.join(fixturesDir, 'npm') })
    .then(() => t.end())
    .catch(t.end)
})

test('detects pnpm', t => {
  execa('pnpm', ['install'], { cwd: path.join(fixturesDir, 'pnpm') })
    .then(() => t.end())
    .catch(t.end)
})

test('detects cnpm', t => {
  execa('cnpm', ['install'], { cwd: path.join(fixturesDir, 'cnpm') })
    .then(() => t.end())
    .catch(t.end)
})

大概就是到对应的目录分别使用不同的 包管理器 执行 install 命令,然后触发对应的 preinstall 钩子命令。

npm 的测试用例来看看:

which-pm-runs/test/fixtures/npm/package.json

js 复制代码
{
  "scripts": {
    "preinstall": "node index.js"
  }
}

which-pm-runs/test/fixtures/npm/index.js

js 复制代码
'use strict'
const whichPmRuns = require('which-pm-runs')

const pm = whichPmRuns()
if (pm.name !== 'npm' || !pm.version) process.exit(1)

当执行下面的测试用例时,会在 which-pm-runs/test/fixtures/npm 目录下执行 npm install 命令,但是由于在 package.json 中定义了 preinstall 钩子,所以会先执行该命令,即 node index.js

js 复制代码
test('detects npm', t => {
  execa('npm', ['install'], { cwd: path.join(fixturesDir, 'npm') })
    .then(() => t.end())
    .catch(t.end)
})

结语

每天一个 npm 包,每天进步一点点。

相关推荐
DN金猿2 分钟前
使用npm install或cnpm install报错解决
前端·npm·node.js
丘山子3 分钟前
一些鲜为人知的 IP 地址怪异写法
前端·后端·tcp/ip
志存高远6615 分钟前
Kotlin 的 suspend 关键字
前端
www_pp_28 分钟前
# 构建词汇表:自然语言处理中的关键步骤
前端·javascript·自然语言处理·easyui
天天扭码1 小时前
总所周知,JavaScript中有很多函数定义方式,如何“因地制宜”?(ˉ﹃ˉ)
前端·javascript·面试
一个专注写代码的程序媛1 小时前
为什么vue的key值,不用index?
前端·javascript·vue.js
장숙혜1 小时前
ElementUi的Dropdown下拉菜单的详细介绍及使用
前端·javascript·vue.js
火柴盒zhang1 小时前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
某公司摸鱼前端1 小时前
uniapp 仿企微左边公司切换页
前端·uni-app·企业微信
WKK_1 小时前
uniapp自定义封装tabbar
前端·javascript·小程序·uni-app