【每天一个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 包,每天进步一点点。

相关推荐
xw535 分钟前
npm几个实用命令
前端·npm
!win !40 分钟前
npm几个实用命令
前端·npm
代码狂想家44 分钟前
使用openEuler从零构建用户管理系统Web应用平台
前端
dorisrv2 小时前
优雅的React表单状态管理
前端
蓝瑟2 小时前
告别重复造轮子!业务组件多场景复用实战指南
前端·javascript·设计模式
dorisrv3 小时前
高性能的懒加载与无限滚动实现
前端
韭菜炒大葱3 小时前
别等了!用 Vue 3 让 AI 边想边说,字字蹦到你脸上
前端·vue.js·aigc
StarkCoder3 小时前
求求你,别在 Swift 协程开头写 guard let self = self 了!
前端
清妍_3 小时前
一文详解 Taro / 小程序 IntersectionObserver 参数
前端
电商API大数据接口开发Cris3 小时前
构建异步任务队列:高效批量化获取淘宝关键词搜索结果的实践
前端·数据挖掘·api