This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020-PRESENT Anthony Fu <https://github.com/antfu>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
# 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`, `bun`, `nub`, `aube`, and `upm`.
|
||||
|
||||
## 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`
|
||||
- `'why'` - show why a package is installed
|
||||
- `'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
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
import { c as AgentCommands, b as AgentCommandValue, R as ResolvedCommand, A as Agent, C as Command, d as ResolveCommandOptions } from './shared/package-manager-detector.vknIX8gX.mjs';
|
||||
|
||||
/**
|
||||
* Split `run` arguments around the script name for package managers that
|
||||
* require `--` to forward extra arguments to the script (npm, pnpm@6).
|
||||
*
|
||||
* The script name is the first positional argument that is neither a flag
|
||||
* nor the value of a preceding value-taking flag (e.g. `-w <workspace>`).
|
||||
* Everything before it (workspace/filter flags) stays in front; everything
|
||||
* after it is the script's own arguments.
|
||||
*
|
||||
* @param args The arguments passed after the `run` command.
|
||||
* @param valueFlags Flags that consume the following argument as their value.
|
||||
* @returns The args split into `before` the script, the `script` itself
|
||||
* (or `undefined` when there is no script name), and the `after` args.
|
||||
*/
|
||||
declare function splitRunArgs(args: string[], valueFlags?: string[]): {
|
||||
before: string[];
|
||||
script: string | undefined;
|
||||
after: string[];
|
||||
};
|
||||
declare const COMMANDS: {
|
||||
npm: AgentCommands;
|
||||
yarn: AgentCommands;
|
||||
'yarn@berry': AgentCommands;
|
||||
pnpm: AgentCommands;
|
||||
'pnpm@6': AgentCommands;
|
||||
'pnpm-rush': AgentCommands;
|
||||
bun: AgentCommands;
|
||||
aube: AgentCommands;
|
||||
deno: AgentCommands;
|
||||
nub: AgentCommands;
|
||||
upm: AgentCommands;
|
||||
};
|
||||
/**
|
||||
* Resolve the command for the agent merging the command arguments with the provided arguments.
|
||||
*
|
||||
* For example, to show how to install `@antfu/ni` globally using `pnpm`:
|
||||
* ```js
|
||||
* import { resolveCommand } from 'package-manager-detector/commands'
|
||||
* const { command, args } = resolveCommand('pnpm', 'global', ['@antfu/ni'])
|
||||
* console.log(`${command} ${args.join(' ')}`) // 'pnpm add -g @antfu/ni'
|
||||
* ```
|
||||
*
|
||||
* @param agent The agent to use.
|
||||
* @param command the command to resolve.
|
||||
* @param args The arguments to pass to the command.
|
||||
* @param options Additional options to resolve the command.
|
||||
* @returns {ResolvedCommand} The resolved command or `null` if the agent command is not found.
|
||||
*/
|
||||
declare function resolveCommand(agent: Agent, command: Command, args: string[], options?: ResolveCommandOptions): ResolvedCommand | null;
|
||||
/**
|
||||
* Construct the command from the agent command merging the command arguments with the provided arguments.
|
||||
* @param value {AgentCommandValue} The agent command to use.
|
||||
* @param args The arguments to pass to the command.
|
||||
* @returns {ResolvedCommand} The resolved command or `null` if the command is `null`.
|
||||
*/
|
||||
declare function constructCommand(value: AgentCommandValue, args: string[]): ResolvedCommand | null;
|
||||
|
||||
export { COMMANDS, constructCommand, resolveCommand, splitRunArgs };
|
||||
+246
@@ -0,0 +1,246 @@
|
||||
function splitRunArgs(args, valueFlags = []) {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i].startsWith("-"))
|
||||
continue;
|
||||
if (i > 0 && valueFlags.includes(args[i - 1]))
|
||||
continue;
|
||||
return { before: args.slice(0, i), script: args[i], after: args.slice(i + 1) };
|
||||
}
|
||||
return { before: args, script: void 0, after: [] };
|
||||
}
|
||||
function dashDashArg(agent, agentCommand, valueFlags = []) {
|
||||
return (args) => {
|
||||
const { before, script, after } = splitRunArgs(args, valueFlags);
|
||||
if (script === void 0)
|
||||
return [agent, agentCommand, ...before];
|
||||
if (after.length > 0)
|
||||
return [agent, agentCommand, ...before, script, "--", ...after];
|
||||
return [agent, agentCommand, ...before, script];
|
||||
};
|
||||
}
|
||||
const npm = {
|
||||
"agent": ["npm", 0],
|
||||
"run": dashDashArg("npm", "run", ["-w", "--workspace"]),
|
||||
"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],
|
||||
"why": ["npm", "why", 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,
|
||||
"why": ["yarn", "why", 0],
|
||||
"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]
|
||||
};
|
||||
function createPnpmCommands(cli) {
|
||||
return {
|
||||
"agent": [cli, 0],
|
||||
"run": [cli, "run", 0],
|
||||
"install": [cli, "i", 0],
|
||||
"frozen": [cli, "i", "--frozen-lockfile", 0],
|
||||
"global": [cli, "add", "-g", 0],
|
||||
"add": [cli, "add", 0],
|
||||
"upgrade": [cli, "update", 0],
|
||||
"upgrade-interactive": [cli, "update", "-i", 0],
|
||||
"dedupe": [cli, "dedupe", 0],
|
||||
"why": [cli, "why", 0],
|
||||
"execute": [cli, "dlx", 0],
|
||||
"execute-local": [cli, "exec", 0],
|
||||
"uninstall": [cli, "remove", 0],
|
||||
"global_uninstall": [cli, "remove", "--global", 0]
|
||||
};
|
||||
}
|
||||
const pnpm = createPnpmCommands("pnpm");
|
||||
const pnpmRush = createPnpmCommands("rush-pnpm");
|
||||
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,
|
||||
"why": ["bun", "why", 0],
|
||||
"execute": ["bun", "x", 0],
|
||||
"execute-local": ["bun", "x", 0],
|
||||
"uninstall": ["bun", "remove", 0],
|
||||
"global_uninstall": ["bun", "remove", "-g", 0]
|
||||
};
|
||||
const aube = {
|
||||
"agent": ["aube", 0],
|
||||
"run": ["aube", "run", 0],
|
||||
"install": ["aube", "install", 0],
|
||||
"frozen": ["aube", "install", "--frozen-lockfile", 0],
|
||||
"global": ["aube", "add", "-g", 0],
|
||||
"add": ["aube", "add", 0],
|
||||
"upgrade": ["aube", "update", 0],
|
||||
"upgrade-interactive": ["aube", "update", "-i", 0],
|
||||
"dedupe": ["aube", "dedupe", 0],
|
||||
"why": ["aube", "why", 0],
|
||||
"execute": ["aube", "dlx", 0],
|
||||
"execute-local": ["aube", "exec", 0],
|
||||
"uninstall": ["aube", "remove", 0],
|
||||
"global_uninstall": ["aube", "remove", "-g", 0]
|
||||
};
|
||||
function denoAdd(args) {
|
||||
if (args.includes("--npm") || args.includes("--jsr"))
|
||||
return ["deno", "add", ...args];
|
||||
return ["deno", "add", ...args.map((arg) => arg.startsWith("-") || arg.includes(":") ? arg : `npm:${arg}`)];
|
||||
}
|
||||
const deno = {
|
||||
"agent": ["deno", 0],
|
||||
"run": ["deno", "task", 0],
|
||||
"install": ["deno", "install", 0],
|
||||
"frozen": ["deno", "install", "--frozen", 0],
|
||||
"global": ["deno", "install", "-g", 0],
|
||||
"add": denoAdd,
|
||||
"upgrade": ["deno", "outdated", "--update", 0],
|
||||
"upgrade-interactive": ["deno", "outdated", "--update", 0],
|
||||
"dedupe": null,
|
||||
"why": ["deno", "why", 0],
|
||||
"execute": ["deno", "x", 0],
|
||||
"execute-local": ["deno", "task", "--eval", 0],
|
||||
"uninstall": ["deno", "remove", 0],
|
||||
"global_uninstall": ["deno", "uninstall", "-g", 0]
|
||||
};
|
||||
const nub = {
|
||||
"agent": ["nub", 0],
|
||||
"run": ["nub", "run", 0],
|
||||
"install": ["nub", "install", 0],
|
||||
"frozen": ["nub", "install", "--frozen-lockfile", 0],
|
||||
"global": ["nub", "add", "-g", 0],
|
||||
"add": ["nub", "add", 0],
|
||||
"upgrade": ["nub", "update", 0],
|
||||
"upgrade-interactive": ["nub", "update", "-i", 0],
|
||||
"dedupe": ["nub", "dedupe", 0],
|
||||
"why": ["nub", "why", 0],
|
||||
"execute": ["nubx", 0],
|
||||
"execute-local": ["nub", "exec", 0],
|
||||
"uninstall": ["nub", "remove", 0],
|
||||
"global_uninstall": ["nub", "remove", "-g", 0]
|
||||
};
|
||||
const upm = {
|
||||
"agent": ["upm", 0],
|
||||
"run": ["upm", "run", 0],
|
||||
"install": ["upm", "install", 0],
|
||||
"frozen": ["upm", "install", "--frozen-lockfile", 0],
|
||||
"global": null,
|
||||
"add": ["upm", "add", 0],
|
||||
"upgrade": null,
|
||||
"upgrade-interactive": null,
|
||||
"dedupe": ["upm", "dedupe", 0],
|
||||
"execute": ["upx", 0],
|
||||
"execute-local": ["upm", "exec", 0],
|
||||
"uninstall": ["upm", "remove", 0],
|
||||
"global_uninstall": null,
|
||||
"why": null
|
||||
};
|
||||
const COMMANDS = {
|
||||
"npm": npm,
|
||||
"yarn": yarn,
|
||||
"yarn@berry": yarnBerry,
|
||||
"pnpm": pnpm,
|
||||
// pnpm v6.x or below
|
||||
"pnpm@6": {
|
||||
...pnpm,
|
||||
run: dashDashArg("pnpm", "run", ["-F", "--filter"])
|
||||
},
|
||||
"pnpm-rush": pnpmRush,
|
||||
"bun": bun,
|
||||
"aube": aube,
|
||||
"deno": deno,
|
||||
"nub": nub,
|
||||
"upm": upm
|
||||
};
|
||||
const WORKSPACE_ROOT_CHECK_COMMANDS = {
|
||||
"yarn": ["add", "uninstall"],
|
||||
"pnpm": ["add"],
|
||||
"pnpm@6": ["add"],
|
||||
"pnpm-rush": ["add"]
|
||||
};
|
||||
const MINIMUM_RELEASE_AGE_FLAGS = {
|
||||
"npm": {
|
||||
commands: ["install", "add", "upgrade", "global", "execute"],
|
||||
flag: (minutes) => `--min-release-age=${minutes / 1440}`
|
||||
},
|
||||
"pnpm": {
|
||||
commands: ["install", "add", "upgrade", "upgrade-interactive", "global", "execute"],
|
||||
flag: (minutes) => `--config.minimum-release-age=${minutes}`
|
||||
},
|
||||
"pnpm-rush": {
|
||||
commands: ["install", "add", "upgrade", "upgrade-interactive", "global", "execute"],
|
||||
flag: (minutes) => `--config.minimum-release-age=${minutes}`
|
||||
},
|
||||
"bun": {
|
||||
commands: ["install", "add", "upgrade", "upgrade-interactive", "global"],
|
||||
flag: (minutes) => `--minimum-release-age=${minutes * 60}`
|
||||
},
|
||||
"deno": {
|
||||
commands: ["install", "add", "global", "execute"],
|
||||
flag: (minutes) => `--minimum-dependency-age=${minutes}`
|
||||
},
|
||||
"nub": {
|
||||
commands: ["install", "add", "upgrade", "upgrade-interactive", "global", "execute"],
|
||||
flag: (minutes) => `--minimum-release-age=${minutes}`
|
||||
},
|
||||
"upm": {
|
||||
commands: ["install", "add", "execute"],
|
||||
flag: (minutes) => `--min-release-age=${minutes / 1440}`
|
||||
}
|
||||
};
|
||||
function resolveCommand(agent, command, args, options = {}) {
|
||||
const value = COMMANDS[agent][command];
|
||||
if (options.ignoreWorkspaceRootCheck && WORKSPACE_ROOT_CHECK_COMMANDS[agent]?.includes(command))
|
||||
args = ["--ignore-workspace-root-check", ...args];
|
||||
const releaseAge = MINIMUM_RELEASE_AGE_FLAGS[agent];
|
||||
if (options.minimumReleaseAge != null && releaseAge?.commands.includes(command))
|
||||
args = [releaseAge.flag(options.minimumReleaseAge), ...args];
|
||||
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, splitRunArgs };
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { A as Agent, a as AgentName } from './shared/package-manager-detector.vknIX8gX.mjs';
|
||||
|
||||
declare const AGENTS: Agent[];
|
||||
declare const LOCKS: Record<string, AgentName>;
|
||||
declare const INSTALL_METADATA: Record<string, AgentName>;
|
||||
declare const INSTALL_PAGE: Record<Agent, string>;
|
||||
|
||||
export { AGENTS, INSTALL_METADATA, INSTALL_PAGE, LOCKS };
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
const AGENTS = [
|
||||
"npm",
|
||||
"yarn",
|
||||
"yarn@berry",
|
||||
"pnpm",
|
||||
"pnpm@6",
|
||||
"pnpm-rush",
|
||||
"bun",
|
||||
"deno",
|
||||
"nub",
|
||||
"aube",
|
||||
"upm"
|
||||
];
|
||||
const LOCKS = {
|
||||
"aube-lock.yaml": "aube",
|
||||
"aube-workspace.yaml": "aube",
|
||||
"bun.lock": "bun",
|
||||
"bun.lockb": "bun",
|
||||
"deno.lock": "deno",
|
||||
"nub.lock": "nub",
|
||||
"upm.lock": "upm",
|
||||
"pnpm-lock.yaml": "pnpm",
|
||||
"pnpm-workspace.yaml": "pnpm",
|
||||
"yarn.lock": "yarn",
|
||||
"package-lock.json": "npm",
|
||||
"npm-shrinkwrap.json": "npm"
|
||||
};
|
||||
const INSTALL_METADATA = {
|
||||
"node_modules/.aube/": "aube",
|
||||
"node_modules/.deno/": "deno",
|
||||
"node_modules/.pnpm/": "pnpm",
|
||||
"node_modules/.upm/": "upm",
|
||||
"node_modules/.yarn-state.yml": "yarn",
|
||||
// yarn v2+ (node-modules)
|
||||
"node_modules/.yarn_integrity": "yarn",
|
||||
// yarn v1
|
||||
"node_modules/.package-lock.json": "npm",
|
||||
".pnp.cjs": "yarn",
|
||||
// yarn v3+ (pnp)
|
||||
".pnp.js": "yarn",
|
||||
// yarn v2 (pnp)
|
||||
"bun.lock": "bun",
|
||||
"bun.lockb": "bun"
|
||||
};
|
||||
const INSTALL_PAGE = {
|
||||
"aube": "https://aube.en.dev/installation",
|
||||
"bun": "https://bun.sh",
|
||||
"deno": "https://deno.com",
|
||||
"pnpm": "https://pnpm.io/installation",
|
||||
"pnpm@6": "https://pnpm.io/6.x/installation",
|
||||
"pnpm-rush": "https://rushjs.io/pages/intro/get_started/",
|
||||
"yarn": "https://classic.yarnpkg.com/en/docs/install",
|
||||
"yarn@berry": "https://yarnpkg.com/getting-started/install",
|
||||
"npm": "https://docs.npmjs.com/cli/configuring-npm/install",
|
||||
"nub": "https://nubjs.com/docs/install",
|
||||
"upm": "https://github.com/unjs/upm#get-started"
|
||||
};
|
||||
|
||||
export { AGENTS, INSTALL_METADATA, INSTALL_PAGE, LOCKS };
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import { e as DetectOptions, f as DetectResult, a as AgentName, A as Agent } from './shared/package-manager-detector.vknIX8gX.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>;
|
||||
/**
|
||||
* Extracts the `major[.minor[.patch]]` part of a version string, e.g. `^9.12.1+sha512.abc` becomes `9.12.1`.
|
||||
* @param version The raw version, as found in `packageManager` or `devEngines.packageManager.version`.
|
||||
* @returns The extracted version, or the input unchanged if it contains no numeric version.
|
||||
*/
|
||||
declare function normalizeAgentVersion(version: string | undefined): string | undefined;
|
||||
/**
|
||||
* Resolves the agent for a package manager name and version, e.g. to pass to `resolveCommand` without running `detect`.
|
||||
*
|
||||
* Yarn above v1 resolves to `yarn@berry` (with version `berry`) and pnpm below v7 to `pnpm@6`.
|
||||
* @param name The package manager name.
|
||||
* @param ver The package manager version, see `normalizeAgentVersion`.
|
||||
* @returns The resolved name, agent and version, or `undefined` if the name is not a known agent.
|
||||
*/
|
||||
declare function resolveAgent(name: AgentName, ver?: string): {
|
||||
name: AgentName;
|
||||
agent: Agent;
|
||||
version: string | undefined;
|
||||
} | undefined;
|
||||
|
||||
export { detect, getUserAgent, normalizeAgentVersion, resolveAgent };
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { INSTALL_METADATA, LOCKS, AGENTS } from './constants.mjs';
|
||||
|
||||
async function pathExists(path2, type) {
|
||||
try {
|
||||
const stat = await fs.stat(path2);
|
||||
return type === "file" ? stat.isFile() : stat.isDirectory();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function getUserAgent() {
|
||||
const userAgent = process.env.npm_config_user_agent;
|
||||
if (!userAgent) {
|
||||
return null;
|
||||
}
|
||||
const name = userAgent.split("/")[0];
|
||||
return AGENTS.includes(name) ? name : null;
|
||||
}
|
||||
function* lookup(cwd = process.cwd()) {
|
||||
let directory = path.resolve(cwd);
|
||||
const { root } = path.parse(directory);
|
||||
while (directory && directory !== root) {
|
||||
yield directory;
|
||||
directory = path.dirname(directory);
|
||||
}
|
||||
}
|
||||
async function parsePackageJson(filepath, options) {
|
||||
if (!filepath || !await pathExists(filepath, "file"))
|
||||
return null;
|
||||
return await handlePackageManager(filepath, options);
|
||||
}
|
||||
async function detect(options = {}) {
|
||||
const {
|
||||
cwd,
|
||||
strategies = ["lockfile", "packageManager-field", "devEngines-field"]
|
||||
} = options;
|
||||
let stopDir;
|
||||
if (typeof options.stopDir === "string") {
|
||||
const resolved = path.resolve(options.stopDir);
|
||||
stopDir = (dir) => dir === resolved;
|
||||
} else {
|
||||
stopDir = options.stopDir;
|
||||
}
|
||||
for (const directory of lookup(cwd)) {
|
||||
for (const strategy of strategies) {
|
||||
switch (strategy) {
|
||||
case "lockfile": {
|
||||
if (await pathExists(path.join(directory, "rush.json"), "file")) {
|
||||
return { name: "pnpm", agent: "pnpm-rush" };
|
||||
}
|
||||
for (const lock of Object.keys(LOCKS)) {
|
||||
if (await pathExists(path.join(directory, lock), "file")) {
|
||||
const name = LOCKS[lock];
|
||||
const result = await parsePackageJson(path.join(directory, "package.json"), options);
|
||||
if (result)
|
||||
return result;
|
||||
else
|
||||
return { name, agent: name };
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "packageManager-field":
|
||||
case "devEngines-field": {
|
||||
const result = await parsePackageJson(path.join(directory, "package.json"), options);
|
||||
if (result)
|
||||
return result;
|
||||
break;
|
||||
}
|
||||
case "install-metadata": {
|
||||
for (const metadata of Object.keys(INSTALL_METADATA)) {
|
||||
const fileOrDir = metadata.endsWith("/") ? "dir" : "file";
|
||||
if (await pathExists(path.join(directory, metadata), fileOrDir)) {
|
||||
const name = INSTALL_METADATA[metadata];
|
||||
const agent = name === "yarn" ? isMetadataYarnClassic(metadata) ? "yarn" : "yarn@berry" : name;
|
||||
return { name, agent };
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (stopDir?.(directory))
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function normalizeAgentVersion(version) {
|
||||
return version?.match(/\d+(\.\d+){0,2}/)?.[0] ?? version;
|
||||
}
|
||||
function getNameAndVer(pkg) {
|
||||
if (typeof pkg.packageManager === "string") {
|
||||
const [name, ver] = pkg.packageManager.replace(/^\^/, "").split("@");
|
||||
return { name, ver: normalizeAgentVersion(ver) };
|
||||
}
|
||||
if (typeof pkg.devEngines?.packageManager?.name === "string") {
|
||||
return {
|
||||
name: pkg.devEngines.packageManager.name,
|
||||
ver: normalizeAgentVersion(pkg.devEngines.packageManager.version)
|
||||
};
|
||||
}
|
||||
return void 0;
|
||||
}
|
||||
async function handlePackageManager(filepath, options) {
|
||||
try {
|
||||
const content = await fs.readFile(filepath, "utf8");
|
||||
const pkg = options.packageJsonParser ? await options.packageJsonParser(content, filepath) : JSON.parse(content);
|
||||
const nameAndVer = getNameAndVer(pkg);
|
||||
if (nameAndVer) {
|
||||
return resolveAgent(nameAndVer.name, nameAndVer.ver) ?? options.onUnknown?.(pkg.packageManager) ?? null;
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function resolveAgent(name, ver) {
|
||||
let agent;
|
||||
let version = ver;
|
||||
if (name === "yarn" && ver && Number.parseInt(ver) > 1) {
|
||||
agent = "yarn@berry";
|
||||
version = "berry";
|
||||
return { name, agent, version };
|
||||
} else if (name === "pnpm" && ver && Number.parseInt(ver) < 7) {
|
||||
agent = "pnpm@6";
|
||||
return { name, agent, version };
|
||||
} else if (AGENTS.includes(name)) {
|
||||
agent = name;
|
||||
return { name, agent, version };
|
||||
}
|
||||
}
|
||||
function isMetadataYarnClassic(metadataPath) {
|
||||
return metadataPath.endsWith(".yarn_integrity");
|
||||
}
|
||||
|
||||
export { detect, getUserAgent, normalizeAgentVersion, resolveAgent };
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export { COMMANDS, constructCommand, resolveCommand } from './commands.mjs';
|
||||
export { AGENTS, INSTALL_METADATA, INSTALL_PAGE, LOCKS } from './constants.mjs';
|
||||
export { detect, getUserAgent } from './detect.mjs';
|
||||
export { A as Agent, b as AgentCommandValue, c as AgentCommands, a as AgentName, C as Command, e as DetectOptions, f as DetectResult, D as DetectStrategy, d as ResolveCommandOptions, R as ResolvedCommand } from './shared/package-manager-detector.vknIX8gX.mjs';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export { COMMANDS, constructCommand, resolveCommand } from './commands.mjs';
|
||||
export { AGENTS, INSTALL_METADATA, INSTALL_PAGE, LOCKS } from './constants.mjs';
|
||||
export { detect, getUserAgent } from './detect.mjs';
|
||||
import 'node:fs/promises';
|
||||
import 'node:path';
|
||||
import 'node:process';
|
||||
Generated
Vendored
+108
@@ -0,0 +1,108 @@
|
||||
type Agent = 'npm' | 'yarn' | 'yarn@berry' | 'pnpm' | 'pnpm@6' | 'pnpm-rush' | 'bun' | 'deno' | 'nub' | 'aube' | 'upm';
|
||||
type AgentName = 'npm' | 'yarn' | 'pnpm' | 'bun' | 'deno' | 'nub' | 'aube' | 'upm';
|
||||
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;
|
||||
'why': 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[];
|
||||
}
|
||||
interface ResolveCommandOptions {
|
||||
/**
|
||||
* Pass `--ignore-workspace-root-check` to pnpm (`add`) and yarn v1 (`add`, `uninstall`).
|
||||
* @default false
|
||||
*/
|
||||
ignoreWorkspaceRootCheck?: boolean;
|
||||
/**
|
||||
* Minimum age in minutes a package version must have before it can be installed,
|
||||
* converted to each agent's own flag and unit.
|
||||
*
|
||||
* Applied to npm, pnpm, bun, deno, nub and upm on commands that resolve versions.
|
||||
* Ignored for yarn, aube and pnpm@6, which have no CLI flag for it.
|
||||
*/
|
||||
minimumReleaseAge?: number;
|
||||
}
|
||||
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`, `deno`, `nub`, `aube`, or `upm`.
|
||||
*/
|
||||
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, ResolveCommandOptions as d, DetectOptions as e, DetectResult as f };
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
{
|
||||
"name": "package-manager-detector",
|
||||
"type": "module",
|
||||
"version": "1.9.0",
|
||||
"description": "Package manager detector",
|
||||
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/antfu-collective/package-manager-detector#readme",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/antfu-collective/package-manager-detector.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/antfu-collective/package-manager-detector/issues"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"exports": {
|
||||
".": "./dist/index.mjs",
|
||||
"./commands": "./dist/commands.mjs",
|
||||
"./detect": "./dist/detect.mjs",
|
||||
"./constants": "./dist/constants.mjs"
|
||||
},
|
||||
"main": "./dist/index.mjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.mts",
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"commands": [
|
||||
"./dist/commands.d.mts"
|
||||
],
|
||||
"detect": [
|
||||
"./dist/detect.d.mts"
|
||||
],
|
||||
"constants": [
|
||||
"./dist/constants.d.mts"
|
||||
]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^9.5.1",
|
||||
"@types/node": "^26.6.4",
|
||||
"bumpp": "^12.3.0",
|
||||
"eslint": "^10.12.0",
|
||||
"tsnapi": "^1.5.0",
|
||||
"typescript": "^6.0.3",
|
||||
"unbuild": "^3.6.1",
|
||||
"vitest": "^5.0.3"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"stub": "unbuild --stub",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"release": "bumpp",
|
||||
"lint": "eslint",
|
||||
"lint:fix": "eslint --fix",
|
||||
"test": "vitest"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user