|
| 1 | +/** |
| 2 | + * Utilities for detecting install scripts in package.json. |
| 3 | + * |
| 4 | + * Install scripts (preinstall, install, postinstall) run automatically |
| 5 | + * when a package is installed as a dependency - important for security awareness. |
| 6 | + * |
| 7 | + * Also extracts npx package calls from those scripts. |
| 8 | + */ |
| 9 | + |
| 10 | +import type { InstallScriptsInfo } from '#shared/types' |
| 11 | + |
| 12 | +// Scripts that run when installing a package as a dependency |
| 13 | +const INSTALL_SCRIPTS = new Set(['preinstall', 'install', 'postinstall']) |
| 14 | + |
| 15 | +// Pattern to match npx commands with various flags |
| 16 | +// Captures the package name (with optional scope and version) |
| 17 | +const NPX_PATTERN = /\bnpx\s+(?:--?\w+(?:=\S+)?\s+)*(@?[\w.-]+(?:\/[\w.-]+)?(?:@[\w.^~<>=|-]+)?)/g |
| 18 | + |
| 19 | +// Pattern to extract package name and version from captured group |
| 20 | +const PACKAGE_VERSION_PATTERN = /^(@[\w.-]+\/[\w.-]+|[\w.-]+)(?:@(.+))?$/ |
| 21 | + |
| 22 | +/** |
| 23 | + * Extract packages from npx calls in install scripts. |
| 24 | + * Only considers preinstall, install, postinstall - scripts that run for end-users. |
| 25 | + * |
| 26 | + * @param scripts - The scripts object from package.json |
| 27 | + * @returns Record of package name to version (or "latest" if none specified) |
| 28 | + */ |
| 29 | +export function extractNpxDependencies( |
| 30 | + scripts: Record<string, string> | undefined, |
| 31 | +): Record<string, string> { |
| 32 | + if (!scripts) return {} |
| 33 | + |
| 34 | + const npxPackages: Record<string, string> = {} |
| 35 | + |
| 36 | + for (const [scriptName, script] of Object.entries(scripts)) { |
| 37 | + // Only check scripts that run during installation |
| 38 | + if (!INSTALL_SCRIPTS.has(scriptName)) continue |
| 39 | + // Reset regex state |
| 40 | + NPX_PATTERN.lastIndex = 0 |
| 41 | + |
| 42 | + let match: RegExpExecArray | null |
| 43 | + while ((match = NPX_PATTERN.exec(script)) !== null) { |
| 44 | + const captured = match[1] |
| 45 | + if (!captured) continue |
| 46 | + |
| 47 | + // Extract package name and version |
| 48 | + const parsed = PACKAGE_VERSION_PATTERN.exec(captured) |
| 49 | + if (parsed && parsed[1]) { |
| 50 | + const packageName = parsed[1] |
| 51 | + const version = parsed[2] || 'latest' |
| 52 | + |
| 53 | + // Skip common built-in commands that aren't packages |
| 54 | + if (isBuiltinCommand(packageName)) continue |
| 55 | + |
| 56 | + // Only add if not already present (first occurrence wins) |
| 57 | + if (!(packageName in npxPackages)) { |
| 58 | + npxPackages[packageName] = version |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return npxPackages |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Check if a command is a built-in/common command that isn't an npm package |
| 69 | + */ |
| 70 | +function isBuiltinCommand(name: string): boolean { |
| 71 | + const builtins = new Set([ |
| 72 | + // Common shell commands that might be mistakenly captured |
| 73 | + 'env', |
| 74 | + 'node', |
| 75 | + 'npm', |
| 76 | + 'yarn', |
| 77 | + 'pnpm', |
| 78 | + // npx flags that might look like packages |
| 79 | + 'yes', |
| 80 | + 'no', |
| 81 | + 'quiet', |
| 82 | + 'shell', |
| 83 | + ]) |
| 84 | + return builtins.has(name) |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Extract install script information from package.json scripts. |
| 89 | + * Returns info about which install scripts exist and any npx packages they call. |
| 90 | + * |
| 91 | + * @param scripts - The scripts object from package.json |
| 92 | + * @returns Info about install scripts and npx dependencies, or null if no install scripts |
| 93 | + */ |
| 94 | +export function extractInstallScriptsInfo( |
| 95 | + scripts: Record<string, string> | undefined, |
| 96 | +): InstallScriptsInfo | null { |
| 97 | + if (!scripts) return null |
| 98 | + |
| 99 | + const presentScripts: ('preinstall' | 'install' | 'postinstall')[] = [] |
| 100 | + const content: Record<string, string> = {} |
| 101 | + |
| 102 | + for (const scriptName of INSTALL_SCRIPTS) { |
| 103 | + if (scripts[scriptName]) { |
| 104 | + presentScripts.push(scriptName as 'preinstall' | 'install' | 'postinstall') |
| 105 | + content[scriptName] = scripts[scriptName] |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (presentScripts.length === 0) return null |
| 110 | + |
| 111 | + return { |
| 112 | + scripts: presentScripts, |
| 113 | + content, |
| 114 | + npxDependencies: extractNpxDependencies(scripts), |
| 115 | + } |
| 116 | +} |
0 commit comments