forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-command.ts
More file actions
96 lines (84 loc) · 2.65 KB
/
install-command.ts
File metadata and controls
96 lines (84 loc) · 2.65 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
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
import type { JsrPackageInfo } from '#shared/types/jsr'
// @unocss-include
export const packageManagers = [
{
id: 'npm',
label: 'npm',
action: 'install',
execute: 'npx',
icon: 'i-simple-icons:npm',
},
{
id: 'pnpm',
label: 'pnpm',
action: 'add',
execute: 'pnpm dlx',
icon: 'i-simple-icons:pnpm',
},
{
id: 'yarn',
label: 'yarn',
action: 'add',
execute: 'yarn dlx',
icon: 'i-simple-icons:yarn',
},
{ id: 'bun', label: 'bun', action: 'add', execute: 'bunx', icon: 'i-simple-icons:bun' },
{
id: 'deno',
label: 'deno',
action: 'add',
execute: 'deno run',
icon: 'i-simple-icons:deno',
},
{ id: 'vlt', label: 'vlt', action: 'install', execute: 'vlt x', icon: 'i-custom-vlt' },
] 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 jsr: prefix for deno (when available on JSR).
*/
export function getPackageSpecifier(options: InstallCommandOptions): string {
const { packageName, packageManager, jsrInfo } = options
if (packageManager === 'deno') {
if (jsrInfo?.exists && jsrInfo.scope && jsrInfo.name) {
// Native JSR package: jsr:@scope/name
return `jsr:@${jsrInfo.scope}/${jsrInfo.name}`
}
// npm compatibility: npm:package
return `npm:${packageName}`
}
// Standard package managers (npm, pnpm, yarn, bun, vlt)
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}`]
}
export function getExecuteCommand(options: InstallCommandOptions): string {
return getExecuteCommandParts(options).join(' ')
}
export function getExecuteCommandParts(options: InstallCommandOptions): string[] {
const pm = packageManagers.find(p => p.id === options.packageManager)
if (!pm) return []
return [pm.execute, getPackageSpecifier(options)]
}