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

相关推荐
昔人'15 分钟前
css使用 :where() 来简化大型 CSS 选择器列表
前端·css
昔人'18 分钟前
css `dorp-shadow`
前端·css
流***陌26 分钟前
扭蛋机 Roll 福利房小程序前端功能设计:融合趣味互动与福利适配
前端·小程序
烛阴1 小时前
用 Python 揭秘 IP 地址背后的地理位置和信息
前端·python
前端开发爱好者1 小时前
尤雨溪官宣:"新玩具" 比 Prettier 快 45 倍!
前端·javascript·vue.js
why技术1 小时前
从18w到1600w播放量,我的一点思考。
java·前端·后端
欧阳呀1 小时前
Vue+element ui导入组件封装——超级优雅版
前端·javascript·vue.js·elementui
清风徐来QCQ1 小时前
css总结
前端
天***88962 小时前
js封装一个双精度算法实现
开发语言·前端·javascript
Algebraaaaa3 小时前
什么是前端、后端与全栈开发,Qt属于什么?
开发语言·前端·qt