|
| 1 | +import type { JsrPackageInfo } from '#shared/types/jsr' |
| 2 | +import { getPackageSpecifier, packageManagers } from './install-command' |
| 3 | +import type { PackageManagerId } from './install-command' |
| 4 | + |
| 5 | +/** |
| 6 | + * Metadata needed to determine if a package is binary-only. |
| 7 | + */ |
| 8 | +export interface PackageMetadata { |
| 9 | + name: string |
| 10 | + bin?: string | Record<string, string> |
| 11 | + main?: string |
| 12 | + module?: unknown |
| 13 | + exports?: unknown |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Determine if a package is "binary-only" (executable without library entry points). |
| 18 | + * Binary-only packages should show execute commands without install commands. |
| 19 | + * |
| 20 | + * A package is binary-only if: |
| 21 | + * - Name starts with "create-" (e.g., create-vite) |
| 22 | + * - Scoped name contains "/create-" (e.g., @vue/create-app) |
| 23 | + * - Has bin field but no main, module, or exports fields |
| 24 | + */ |
| 25 | +export function isBinaryOnlyPackage(pkg: PackageMetadata): boolean { |
| 26 | + const baseName = pkg.name.startsWith('@') ? pkg.name.split('/')[1] : pkg.name |
| 27 | + |
| 28 | + // Check create-* patterns |
| 29 | + if (baseName?.startsWith('create-') || pkg.name.includes('/create-')) { |
| 30 | + return true |
| 31 | + } |
| 32 | + |
| 33 | + // Has bin but no entry points |
| 34 | + const hasBin = |
| 35 | + pkg.bin !== undefined && (typeof pkg.bin === 'string' || Object.keys(pkg.bin).length > 0) |
| 36 | + const hasEntryPoint = !!pkg.main || !!pkg.module || !!pkg.exports |
| 37 | + |
| 38 | + return hasBin && !hasEntryPoint |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Check if a package uses the create-* naming convention. |
| 43 | + */ |
| 44 | +export function isCreatePackage(packageName: string): boolean { |
| 45 | + const baseName = packageName.startsWith('@') ? packageName.split('/')[1] : packageName |
| 46 | + return baseName?.startsWith('create-') || packageName.includes('/create-') || false |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Information about executable commands provided by a package. |
| 51 | + */ |
| 52 | +export interface ExecutableInfo { |
| 53 | + /** Primary command name (typically the package name or first bin key) */ |
| 54 | + primaryCommand: string |
| 55 | + /** All available command names */ |
| 56 | + commands: string[] |
| 57 | + /** Whether this package has any executables */ |
| 58 | + hasExecutable: boolean |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Extract executable command information from a package's bin field. |
| 63 | + * Handles both string format ("bin": "./cli.js") and object format ("bin": { "cmd": "./cli.js" }). |
| 64 | + */ |
| 65 | +export function getExecutableInfo( |
| 66 | + packageName: string, |
| 67 | + bin: string | Record<string, string> | undefined, |
| 68 | +): ExecutableInfo { |
| 69 | + if (!bin) { |
| 70 | + return { primaryCommand: '', commands: [], hasExecutable: false } |
| 71 | + } |
| 72 | + |
| 73 | + // String format: package name becomes the command |
| 74 | + if (typeof bin === 'string') { |
| 75 | + return { |
| 76 | + primaryCommand: packageName, |
| 77 | + commands: [packageName], |
| 78 | + hasExecutable: true, |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Object format: keys are command names |
| 83 | + const commands = Object.keys(bin) |
| 84 | + const firstCommand = commands[0] |
| 85 | + if (!firstCommand) { |
| 86 | + return { primaryCommand: '', commands: [], hasExecutable: false } |
| 87 | + } |
| 88 | + |
| 89 | + // Prefer command matching package name if it exists, otherwise use first |
| 90 | + const baseName = packageName.startsWith('@') ? packageName.split('/')[1] : packageName |
| 91 | + const primaryCommand = baseName && commands.includes(baseName) ? baseName : firstCommand |
| 92 | + |
| 93 | + return { |
| 94 | + primaryCommand, |
| 95 | + commands, |
| 96 | + hasExecutable: true, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export interface RunCommandOptions { |
| 101 | + packageName: string |
| 102 | + packageManager: PackageManagerId |
| 103 | + version?: string | null |
| 104 | + jsrInfo?: JsrPackageInfo | null |
| 105 | + /** Specific command to run (for packages with multiple bin entries) */ |
| 106 | + command?: string |
| 107 | + /** Whether this is a binary-only package (affects which execute command to use) */ |
| 108 | + isBinaryOnly?: boolean |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Generate run command as an array of parts. |
| 113 | + * First element is the package manager label (e.g., "pnpm"), rest are arguments. |
| 114 | + * For example: ["pnpm", "exec", "eslint"] or ["pnpm", "dlx", "create-vite"] |
| 115 | + */ |
| 116 | +export function getRunCommandParts(options: RunCommandOptions): string[] { |
| 117 | + const pm = packageManagers.find(p => p.id === options.packageManager) |
| 118 | + if (!pm) return [] |
| 119 | + |
| 120 | + const spec = getPackageSpecifier(options) |
| 121 | + |
| 122 | + // Choose execute command based on package type |
| 123 | + const executeCmd = options.isBinaryOnly ? pm.executeRemote : pm.executeLocal |
| 124 | + const executeParts = executeCmd.split(' ') |
| 125 | + |
| 126 | + // For deno, always use the package specifier |
| 127 | + if (options.packageManager === 'deno') { |
| 128 | + return [...executeParts, spec] |
| 129 | + } |
| 130 | + |
| 131 | + // For local execute with specific command name different from package name |
| 132 | + // e.g., `pnpm exec tsc` for typescript package |
| 133 | + if (options.command && options.command !== options.packageName) { |
| 134 | + const baseName = options.packageName.startsWith('@') |
| 135 | + ? options.packageName.split('/')[1] |
| 136 | + : options.packageName |
| 137 | + // If command matches base package name, use the package spec |
| 138 | + if (options.command === baseName) { |
| 139 | + return [...executeParts, spec] |
| 140 | + } |
| 141 | + // Otherwise use the command name directly |
| 142 | + return [...executeParts, options.command] |
| 143 | + } |
| 144 | + |
| 145 | + return [...executeParts, spec] |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Generate the full run command for a package. |
| 150 | + */ |
| 151 | +export function getRunCommand(options: RunCommandOptions): string { |
| 152 | + return getRunCommandParts(options).join(' ') |
| 153 | +} |
0 commit comments