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

相关推荐
正小安19 分钟前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光2 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   2 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   2 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
Fan_web2 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常2 小时前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式
莹雨潇潇3 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
Jiaberrr3 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui
Tiffany_Ho4 小时前
【TypeScript】知识点梳理(三)
前端·typescript