-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathuseInstallCommand.ts
More file actions
98 lines (83 loc) · 2.89 KB
/
useInstallCommand.ts
File metadata and controls
98 lines (83 loc) · 2.89 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
97
98
import type { JsrPackageInfo } from '#shared/types/jsr'
/**
* Composable for generating install commands with support for
* multiple package managers, @types packages, and JSR.
*/
export function useInstallCommand(
packageName: MaybeRefOrGetter<string | null>,
requestedVersion: MaybeRefOrGetter<string | null>,
jsrInfo: MaybeRefOrGetter<JsrPackageInfo | null>,
typesPackageName: MaybeRefOrGetter<string | null>,
) {
const selectedPM = useSelectedPackageManager()
const { settings } = useSettings()
// Check if we should show @types in install command
const showTypesInInstall = computed(() => {
return settings.value.includeTypesInInstall && !!toValue(typesPackageName)
})
const installCommandParts = computed(() => {
const name = toValue(packageName)
if (!name) return []
return getInstallCommandParts({
packageName: name,
packageManager: selectedPM.value,
version: toValue(requestedVersion),
jsrInfo: toValue(jsrInfo),
})
})
const installCommand = computed(() => {
const name = toValue(packageName)
if (!name) return ''
return getInstallCommand({
packageName: name,
packageManager: selectedPM.value,
version: toValue(requestedVersion),
jsrInfo: toValue(jsrInfo),
})
})
// Get the dev dependency flag for the selected package manager
const devFlag = computed(() => {
// bun uses lowercase -d, all others use -D
return selectedPM.value === 'bun' ? '-d' : '-D'
})
// @types install command parts (for display)
const typesInstallCommandParts = computed(() => {
const types = toValue(typesPackageName)
if (!types) return []
const pm = packageManagers.find(p => p.id === selectedPM.value)
if (!pm) return []
const pkgSpec = selectedPM.value === 'deno' ? `npm:${types}` : types
return [pm.label, pm.action, devFlag.value, pkgSpec]
})
// Full install command including @types (for copying)
const fullInstallCommand = computed(() => {
if (!installCommand.value) return ''
const types = toValue(typesPackageName)
if (!showTypesInInstall.value || !types) {
return installCommand.value
}
const pm = packageManagers.find(p => p.id === selectedPM.value)
if (!pm) return installCommand.value
const pkgSpec = selectedPM.value === 'deno' ? `npm:${types}` : types
// Use semicolon to separate commands
return `${installCommand.value}; ${pm.label} ${pm.action} ${devFlag.value} ${pkgSpec}`
})
// Copy state
const copied = ref(false)
async function copyInstallCommand() {
if (!fullInstallCommand.value) return
await navigator.clipboard.writeText(fullInstallCommand.value)
copied.value = true
setTimeout(() => (copied.value = false), 2000)
}
return {
selectedPM,
installCommandParts,
installCommand,
typesInstallCommandParts,
fullInstallCommand,
showTypesInInstall,
copied,
copyInstallCommand,
}
}