完成世界书、骰子、apiconfig页面处理

This commit is contained in:
2026-04-30 01:35:10 +08:00
parent a3e3711b2b
commit ba9b925c32
4602 changed files with 785225 additions and 23 deletions

View File

@@ -0,0 +1,109 @@
# package-manager-detector
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![JSDocs][jsdocs-src]][jsdocs-href]
[![License][license-src]][license-href]
Package manager detector is based on lock files, the `package.json` `packageManager` field, and installation metadata to detect the package manager used in a project.
It supports `npm`, `yarn`, `pnpm`, `deno`, and `bun`.
## Install
```sh
# pnpm
pnpm add package-manager-detector
# npm
npm i package-manager-detector
# yarn
yarn add package-manager-detector
```
## Usage
To check the file system for which package manager is used:
```js
import { detect } from 'package-manager-detector/detect'
```
or to get the currently running package manager:
```js
import { getUserAgent } from 'package-manager-detector/detect'
```
## Customize Detection Strategy
By default, the `detect` API searches through the current directory for lock files, and if none exists, it looks for the `packageManager` field in `package.json`. If that also doesn't exist, it will check the `devEngines.packageManager` field in `package.json`. If all strategies couldn't detect the package manager, it'll crawl upwards to the parent directory and repeat the detection process until it reaches the root directory.
The strategies can be configured through `detect`'s `strategies` option with the following accepted strategies:
- `'lockfile'`: Look up for lock files.
- `'packageManager-field'`: Look up for the `packageManager` field in package.json.
- `'devEngines-field'`: Look up for the `devEngines.packageManager` field in package.json.
- `'install-metadata'`: Look up for installation metadata added by package managers.
The order of the strategies can also be changed to prioritize one strategy over another. For example, if you prefer to detect the package manager used for installation:
```js
import { detect } from 'package-manager-detector/detect'
const pm = await detect({
strategies: ['install-metadata', 'lockfile', 'packageManager-field', 'devEngines-field']
})
```
## Agents and Commands
This package includes package manager agents and their corresponding commands for:
- `'agent'` - run the package manager with no arguments
- `'install'` - install dependencies
- `'frozen'` - install dependencies using frozen lockfile
- `'add'` - add dependencies
- `'uninstall'` - remove dependencies
- `'global'` - install global packages
- `'global_uninstall'` - remove global packages
- `'upgrade'` - upgrade dependencies
- `'upgrade-interactive'` - upgrade dependencies interactively: not available for `npm`
- `'dedupe'` - deduplicate dependencies with overlapping ranges: not available for `deno` and `bun`
- `'execute'` - download & execute binary scripts
- `'execute-local'` - execute binary scripts (from package locally installed)
- `'run'` - run `package.json` scripts
### Using Agents and Commands
A `resolveCommand` function is provided to resolve the command for a specific agent.
```ts
import { resolveCommand } from 'package-manager-detector/commands'
import { detect } from 'package-manager-detector/detect'
const pm = await detect()
if (!pm)
throw new Error('Could not detect package manager')
const { command, args } = resolveCommand(pm.agent, 'add', ['@antfu/ni']) // { command: 'pnpm', args: ['add', '@antfu/ni'] }
console.log(`Detected the ${pm.agent} package manager. You can run a install with ${command} ${args.join(' ')}`)
```
You can check the source code or the [JSDocs](https://www.jsdocs.io/package/package-manager-detector) for more information.
## License
[MIT](./LICENSE) License © 2020-PRESENT [Anthony Fu](https://github.com/antfu)
<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/package-manager-detector?style=flat&colorA=18181B&colorB=F0DB4F
[npm-version-href]: https://npmjs.com/package/package-manager-detector
[npm-downloads-src]: https://img.shields.io/npm/dm/package-manager-detector?style=flat&colorA=18181B&colorB=F0DB4F
[npm-downloads-href]: https://npmjs.com/package/package-manager-detector
[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=18181B&colorB=F0DB4F
[jsdocs-href]: https://www.jsdocs.io/package/package-manager-detector
[license-src]: https://img.shields.io/github/license/antfu-collective/package-manager-detector.svg?style=flat&colorA=18181B&colorB=F0DB4F
[license-href]: https://github.com/antfu-collective/package-manager-detector/blob/main/LICENSE

View File

@@ -0,0 +1,133 @@
function dashDashArg(agent, agentCommand) {
return (args) => {
if (args.length > 1) {
return [agent, agentCommand, args[0], "--", ...args.slice(1)];
} else {
return [agent, agentCommand, args[0]];
}
};
}
function denoExecute() {
return (args) => {
return ["deno", "run", `npm:${args[0]}`, ...args.slice(1)];
};
}
const npm = {
"agent": ["npm", 0],
"run": dashDashArg("npm", "run"),
"install": ["npm", "i", 0],
"frozen": ["npm", "ci", 0],
"global": ["npm", "i", "-g", 0],
"add": ["npm", "i", 0],
"upgrade": ["npm", "update", 0],
"upgrade-interactive": null,
"dedupe": ["npm", "dedupe", 0],
"execute": ["npx", 0],
"execute-local": ["npx", 0],
"uninstall": ["npm", "uninstall", 0],
"global_uninstall": ["npm", "uninstall", "-g", 0]
};
const yarn = {
"agent": ["yarn", 0],
"run": ["yarn", "run", 0],
"install": ["yarn", "install", 0],
"frozen": ["yarn", "install", "--frozen-lockfile", 0],
"global": ["yarn", "global", "add", 0],
"add": ["yarn", "add", 0],
"upgrade": ["yarn", "upgrade", 0],
"upgrade-interactive": ["yarn", "upgrade-interactive", 0],
"dedupe": null,
"execute": ["npx", 0],
"execute-local": dashDashArg("yarn", "exec"),
"uninstall": ["yarn", "remove", 0],
"global_uninstall": ["yarn", "global", "remove", 0]
};
const yarnBerry = {
...yarn,
"frozen": ["yarn", "install", "--immutable", 0],
"upgrade": ["yarn", "up", 0],
"upgrade-interactive": ["yarn", "up", "-i", 0],
"dedupe": ["yarn", "dedupe", 0],
"execute": ["yarn", "dlx", 0],
"execute-local": ["yarn", "exec", 0],
// Yarn 2+ removed 'global', see https://github.com/yarnpkg/berry/issues/821
"global": ["npm", "i", "-g", 0],
"global_uninstall": ["npm", "uninstall", "-g", 0]
};
const pnpm = {
"agent": ["pnpm", 0],
"run": ["pnpm", "run", 0],
"install": ["pnpm", "i", 0],
"frozen": ["pnpm", "i", "--frozen-lockfile", 0],
"global": ["pnpm", "add", "-g", 0],
"add": ["pnpm", "add", 0],
"upgrade": ["pnpm", "update", 0],
"upgrade-interactive": ["pnpm", "update", "-i", 0],
"dedupe": ["pnpm", "dedupe", 0],
"execute": ["pnpm", "dlx", 0],
"execute-local": ["pnpm", "exec", 0],
"uninstall": ["pnpm", "remove", 0],
"global_uninstall": ["pnpm", "remove", "--global", 0]
};
const bun = {
"agent": ["bun", 0],
"run": ["bun", "run", 0],
"install": ["bun", "install", 0],
"frozen": ["bun", "install", "--frozen-lockfile", 0],
"global": ["bun", "add", "-g", 0],
"add": ["bun", "add", 0],
"upgrade": ["bun", "update", 0],
"upgrade-interactive": ["bun", "update", "-i", 0],
"dedupe": null,
"execute": ["bun", "x", 0],
"execute-local": ["bun", "x", 0],
"uninstall": ["bun", "remove", 0],
"global_uninstall": ["bun", "remove", "-g", 0]
};
const deno = {
"agent": ["deno", 0],
"run": ["deno", "task", 0],
"install": ["deno", "install", 0],
"frozen": ["deno", "install", "--frozen", 0],
"global": ["deno", "install", "-g", 0],
"add": ["deno", "add", 0],
"upgrade": ["deno", "outdated", "--update", 0],
"upgrade-interactive": ["deno", "outdated", "--update", 0],
"dedupe": null,
"execute": denoExecute(),
"execute-local": ["deno", "task", "--eval", 0],
"uninstall": ["deno", "remove", 0],
"global_uninstall": ["deno", "uninstall", "-g", 0]
};
const COMMANDS = {
"npm": npm,
"yarn": yarn,
"yarn@berry": yarnBerry,
"pnpm": pnpm,
// pnpm v6.x or below
"pnpm@6": {
...pnpm,
run: dashDashArg("pnpm", "run")
},
"bun": bun,
"deno": deno
};
function resolveCommand(agent, command, args) {
const value = COMMANDS[agent][command];
return constructCommand(value, args);
}
function constructCommand(value, args) {
if (value == null)
return null;
const list = typeof value === "function" ? value(args) : value.flatMap((v) => {
if (typeof v === "number")
return args;
return [v];
});
return {
command: list[0],
args: list.slice(1)
};
}
export { COMMANDS, constructCommand, resolveCommand };

View File

@@ -0,0 +1,16 @@
import { d as DetectOptions, e as DetectResult, a as AgentName } from './shared/package-manager-detector.DksAilYA.mjs';
/**
* Detects the package manager used in the running process.
*
* This method will check for `process.env.npm_config_user_agent`.
*/
declare function getUserAgent(): AgentName | null;
/**
* Detects the package manager used in the project.
* @param options {DetectOptions} The options to use when detecting the package manager.
* @returns {Promise<DetectResult | null>} The detected package manager or `null` if not found.
*/
declare function detect(options?: DetectOptions): Promise<DetectResult | null>;
export { detect, getUserAgent };

View File

@@ -0,0 +1,92 @@
type Agent = 'npm' | 'yarn' | 'yarn@berry' | 'pnpm' | 'pnpm@6' | 'bun' | 'deno';
type AgentName = 'npm' | 'yarn' | 'pnpm' | 'bun' | 'deno';
type AgentCommandValue = (string | number)[] | ((args: string[]) => string[]) | null;
interface AgentCommands {
'agent': AgentCommandValue;
'run': AgentCommandValue;
'install': AgentCommandValue;
'frozen': AgentCommandValue;
'global': AgentCommandValue;
'add': AgentCommandValue;
'upgrade': AgentCommandValue;
'upgrade-interactive': AgentCommandValue;
'dedupe': AgentCommandValue;
'execute': AgentCommandValue;
'execute-local': AgentCommandValue;
'uninstall': AgentCommandValue;
'global_uninstall': AgentCommandValue;
}
type Command = keyof AgentCommands;
interface ResolvedCommand {
/**
* CLI command.
*/
command: string;
/**
* Arguments for the CLI command, merged with user arguments.
*/
args: string[];
}
type DetectStrategy = 'lockfile' | 'packageManager-field' | 'devEngines-field' | 'install-metadata';
interface DetectOptions {
/**
* Current working directory to start looking up for package manager.
* @default `process.cwd()`
*/
cwd?: string;
/**
* The strategies to use for detecting the package manager. The strategies
* are executed in the order it's specified for every directory that it iterates
* upwards from the `cwd`.
*
* - `'lockfile'`: Look up for lock files.
* - `'packageManager-field'`: Look up for the `packageManager` field in package.json.
* - `'devEngines-field'`: Look up for the `devEngines.packageManager` field in package.json.
* - `'install-metadata'`: Look up for installation metadata added by package managers.
*
* @default ['lockfile', 'packageManager-field', 'devEngines-field']
*/
strategies?: DetectStrategy[];
/**
* Callback when unknown package manager from package.json.
* @param packageManager - The `packageManager` value from package.json file.
*/
onUnknown?: (packageManager: string) => DetectResult | null | undefined;
/**
* The path to stop traversing up the directory.
*/
stopDir?: string | ((currentDir: string) => boolean);
/**
* Optional custom parser for `package.json` content.
*
* If provided, it will be used instead of `JSON.parse` when reading `package.json` files.
* This can be used to support JSONC, YAML, or other formats.
*
* @param content - The content of the file.
* @param filepath - The absolute path to the file.
* @returns The parsed object or a Promise resolving to it.
* @default JSON.parse
*/
packageJsonParser?: (content: string, filepath: string) => any | Promise<any>;
}
interface DetectResult {
/**
* Agent name without the specifier.
*
* Can be `npm`, `yarn`, `pnpm`, `bun`, or `deno`.
*/
name: AgentName;
/**
* Agent specifier to resolve the command.
*
* May contain '@' to differentiate the version (e.g. 'yarn@berry').
* Use `name` for the agent name without the specifier.
*/
agent: Agent;
/**
* Specific version of the agent, read from `packageManager` field in package.json.
*/
version?: string;
}
export type { Agent as A, Command as C, DetectStrategy as D, ResolvedCommand as R, AgentName as a, AgentCommandValue as b, AgentCommands as c, DetectOptions as d, DetectResult as e };