|
| 1 | +import type { JsrPackageInfo } from '#shared/types/jsr' |
| 2 | + |
| 3 | +/** |
| 4 | + * Composable for generating install commands with support for |
| 5 | + * multiple package managers, @types packages, and JSR. |
| 6 | + */ |
| 7 | +export function useInstallCommand( |
| 8 | + packageName: MaybeRefOrGetter<string | null>, |
| 9 | + requestedVersion: MaybeRefOrGetter<string | null>, |
| 10 | + jsrInfo: MaybeRefOrGetter<JsrPackageInfo | null>, |
| 11 | + typesPackageName: MaybeRefOrGetter<string | null>, |
| 12 | +) { |
| 13 | + const selectedPM = useSelectedPackageManager() |
| 14 | + const { settings } = useSettings() |
| 15 | + |
| 16 | + // Check if we should show @types in install command |
| 17 | + const showTypesInInstall = computed(() => { |
| 18 | + return settings.value.includeTypesInInstall && !!toValue(typesPackageName) |
| 19 | + }) |
| 20 | + |
| 21 | + const installCommandParts = computed(() => { |
| 22 | + const name = toValue(packageName) |
| 23 | + if (!name) return [] |
| 24 | + return getInstallCommandParts({ |
| 25 | + packageName: name, |
| 26 | + packageManager: selectedPM.value, |
| 27 | + version: toValue(requestedVersion), |
| 28 | + jsrInfo: toValue(jsrInfo), |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + const installCommand = computed(() => { |
| 33 | + const name = toValue(packageName) |
| 34 | + if (!name) return '' |
| 35 | + return getInstallCommand({ |
| 36 | + packageName: name, |
| 37 | + packageManager: selectedPM.value, |
| 38 | + version: toValue(requestedVersion), |
| 39 | + jsrInfo: toValue(jsrInfo), |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + // Get the dev dependency flag for the selected package manager |
| 44 | + const devFlag = computed(() => { |
| 45 | + // bun uses lowercase -d, all others use -D |
| 46 | + return selectedPM.value === 'bun' ? '-d' : '-D' |
| 47 | + }) |
| 48 | + |
| 49 | + // @types install command parts (for display) |
| 50 | + const typesInstallCommandParts = computed(() => { |
| 51 | + const types = toValue(typesPackageName) |
| 52 | + if (!types) return [] |
| 53 | + const pm = packageManagers.find(p => p.id === selectedPM.value) |
| 54 | + if (!pm) return [] |
| 55 | + |
| 56 | + const pkgSpec = selectedPM.value === 'deno' ? `npm:${types}` : types |
| 57 | + |
| 58 | + return [pm.label, pm.action, devFlag.value, pkgSpec] |
| 59 | + }) |
| 60 | + |
| 61 | + // Full install command including @types (for copying) |
| 62 | + const fullInstallCommand = computed(() => { |
| 63 | + if (!installCommand.value) return '' |
| 64 | + const types = toValue(typesPackageName) |
| 65 | + if (!showTypesInInstall.value || !types) { |
| 66 | + return installCommand.value |
| 67 | + } |
| 68 | + |
| 69 | + const pm = packageManagers.find(p => p.id === selectedPM.value) |
| 70 | + if (!pm) return installCommand.value |
| 71 | + |
| 72 | + const pkgSpec = selectedPM.value === 'deno' ? `npm:${types}` : types |
| 73 | + |
| 74 | + // Use semicolon to separate commands |
| 75 | + return `${installCommand.value}; ${pm.label} ${pm.action} ${devFlag.value} ${pkgSpec}` |
| 76 | + }) |
| 77 | + |
| 78 | + // Copy state |
| 79 | + const copied = ref(false) |
| 80 | + |
| 81 | + async function copyInstallCommand() { |
| 82 | + if (!fullInstallCommand.value) return |
| 83 | + await navigator.clipboard.writeText(fullInstallCommand.value) |
| 84 | + copied.value = true |
| 85 | + setTimeout(() => (copied.value = false), 2000) |
| 86 | + } |
| 87 | + |
| 88 | + return { |
| 89 | + selectedPM, |
| 90 | + installCommandParts, |
| 91 | + installCommand, |
| 92 | + typesInstallCommandParts, |
| 93 | + fullInstallCommand, |
| 94 | + showTypesInInstall, |
| 95 | + copied, |
| 96 | + copyInstallCommand, |
| 97 | + } |
| 98 | +} |
0 commit comments