-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathinstall-command.ts
More file actions
66 lines (55 loc) · 2 KB
/
install-command.ts
File metadata and controls
66 lines (55 loc) · 2 KB
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
import type { JsrPackageInfo } from '#shared/types/jsr'
export const packageManagers = [
{ id: 'npm', label: 'npm', action: 'install' },
{ id: 'pnpm', label: 'pnpm', action: 'add' },
{ id: 'yarn', label: 'yarn', action: 'add' },
{ id: 'bun', label: 'bun', action: 'add' },
{ id: 'deno', label: 'deno', action: 'add' },
{ id: 'jsr', label: 'jsr', action: 'add' },
] as const
export type PackageManagerId = (typeof packageManagers)[number]['id']
export interface InstallCommandOptions {
packageName: string
packageManager: PackageManagerId
version?: string | null
jsrInfo?: JsrPackageInfo | null
}
/**
* Get the package specifier for a given package manager.
* Handles npm: prefix for deno and jsr (when not native).
*/
export function getPackageSpecifier(options: InstallCommandOptions): string {
const { packageName, packageManager, jsrInfo } = options
if (packageManager === 'deno') {
// deno add npm:package
return `npm:${packageName}`
}
if (packageManager === 'jsr') {
if (jsrInfo?.exists && jsrInfo.scope && jsrInfo.name) {
// Native JSR package: @scope/name
return `@${jsrInfo.scope}/${jsrInfo.name}`
}
// npm compatibility: npm:package
return `npm:${packageName}`
}
// Standard package managers (npm, pnpm, yarn, bun)
return packageName
}
/**
* Generate the full install command for a package.
*/
export function getInstallCommand(options: InstallCommandOptions): string {
return getInstallCommandParts(options).join(' ')
}
/**
* Generate install command as an array of parts.
* First element is the command (e.g., "npm"), rest are arguments.
* Useful for rendering with different styling for command vs args.
*/
export function getInstallCommandParts(options: InstallCommandOptions): string[] {
const pm = packageManagers.find(p => p.id === options.packageManager)
if (!pm) return []
const spec = getPackageSpecifier(options)
const version = options.version ? `@${options.version}` : ''
return [pm.label, pm.action, `${spec}${version}`]
}