-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcommands.ts
165 lines (152 loc) · 5.01 KB
/
commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type { Agent, AgentCommands, AgentCommandValue, Command, ResolvedCommand } from './types'
function npmRun(agent: string) {
return (args: string[]) => {
if (args.length > 1) {
return [agent, 'run', args[0], '--', ...args.slice(1)]
}
else {
return [agent, 'run', args[0]]
}
}
}
function denoExecute() {
return (args: string[]) => {
return ['deno', 'run', `npm:${args[0]}`, ...args.slice(1)]
}
}
const npm: AgentCommands = {
'agent': ['npm', 0],
'run': npmRun('npm'),
'install': ['npm', 'i', 0],
'frozen': ['npm', 'ci', 0],
'global': ['npm', 'i', '-g', 0],
'add': ['npm', 'i', 0],
'upgrade': ['npm', 'update', 0],
'upgrade-interactive': null,
'execute': ['npx', 0],
'execute-local': ['npx', 0],
'uninstall': ['npm', 'uninstall', 0],
'global_uninstall': ['npm', 'uninstall', '-g', 0],
}
/** yarn 1 */
const yarn: AgentCommands = {
'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],
'execute': ['npx', 0],
'execute-local': ['yarn', 'exec', 0],
'uninstall': ['yarn', 'remove', 0],
'global_uninstall': ['yarn', 'global', 'remove', 0],
}
/** yarn 2+ */
const yarnBerry: AgentCommands = {
...yarn,
'frozen': ['yarn', 'install', '--immutable', 0],
'upgrade': ['yarn', 'up', 0],
'upgrade-interactive': ['yarn', 'up', '-i', 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: AgentCommands = {
'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],
'execute': ['pnpm', 'dlx', 0],
'execute-local': ['pnpm', 'exec', 0],
'uninstall': ['pnpm', 'remove', 0],
'global_uninstall': ['pnpm', 'remove', '--global', 0],
}
const bun: AgentCommands = {
'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', 0],
'execute': ['bun', 'x', 0],
'execute-local': ['bun', 'x', 0],
'uninstall': ['bun', 'remove', 0],
'global_uninstall': ['bun', 'remove', '-g', 0],
}
const deno: AgentCommands = {
'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],
'execute': denoExecute(),
'execute-local': ['deno', 'task', '--eval', 0],
'uninstall': ['deno', 'remove', 0],
'global_uninstall': ['deno', 'uninstall', '-g', 0],
}
export const COMMANDS = {
'npm': npm,
'yarn': yarn,
'yarn@berry': yarnBerry,
'pnpm': pnpm,
// pnpm v6.x or below
'pnpm@6': <AgentCommands>{
...pnpm,
run: npmRun('pnpm'),
},
'bun': bun,
'deno': deno,
} satisfies Record<Agent, 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.
* @returns {ResolvedCommand} The resolved command or `null` if the agent command is not found.
*/
export function resolveCommand(agent: Agent, command: Command, args: string[]): ResolvedCommand | null {
const value = COMMANDS[agent][command] as AgentCommandValue
return constructCommand(value, args)
}
/**
* 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`.
*/
export function constructCommand(value: AgentCommandValue, args: string[]): ResolvedCommand | null {
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),
}
}