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

相关推荐
abigale032 小时前
webpack+vite前端构建工具 -11实战中的配置技巧
前端·webpack·node.js
专注API从业者2 小时前
构建淘宝评论监控系统:API 接口开发与实时数据采集教程
大数据·前端·数据库·oracle
Joker`s smile2 小时前
Chrome安装老版本、不同版本,自制便携版本用于前端调试
前端·chrome
weixin_416639972 小时前
爬虫工程师Chrome开发者工具简单介绍
前端·chrome·爬虫
我是如子啊2 小时前
【解决“此扩展可能损坏”】Edge浏览器(chrome系列通杀))扩展损坏?一招保留数据快速修复
前端·chrome·edge
灵性花火2 小时前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
孤水寒月6 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀6 小时前
html初学者第一天
前端·html
耶啵奶膘9 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
视频砖家9 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能