|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Dependency Checker Script |
| 5 | + * |
| 6 | + * This script scans node_modules for missing dependencies that could cause |
| 7 | + * "Failed to resolve module specifier" errors in production builds. |
| 8 | + * |
| 9 | + * Usage: node check-deps.js |
| 10 | + */ |
| 11 | + |
| 12 | +import fs from 'fs'; |
| 13 | +import path from 'path'; |
| 14 | +import { execSync } from 'child_process'; |
| 15 | +import { fileURLToPath } from 'url'; |
| 16 | + |
| 17 | +const __filename = fileURLToPath(import.meta.url); |
| 18 | +const __dirname = path.dirname(__filename); |
| 19 | + |
| 20 | +console.log('🔍 Scanning for missing dependencies...\n'); |
| 21 | + |
| 22 | +// Read current package.json |
| 23 | +const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); |
| 24 | +const currentDeps = { |
| 25 | + ...packageJson.dependencies || {}, |
| 26 | + ...packageJson.devDependencies || {} |
| 27 | +}; |
| 28 | + |
| 29 | +// Function to extract require/import statements |
| 30 | +function extractDependencies(filePath) { |
| 31 | + try { |
| 32 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 33 | + const requireMatches = content.match(/require\(["']([^"']+)["']\)/g) || []; |
| 34 | + const importMatches = content.match(/import\s+.*?\s+from\s+["']([^"']+)["']/g) || []; |
| 35 | + |
| 36 | + const deps = new Set(); |
| 37 | + |
| 38 | + // Extract require dependencies |
| 39 | + requireMatches.forEach(match => { |
| 40 | + const dep = match.match(/require\(["']([^"']+)["']\)/)[1]; |
| 41 | + if (!dep.startsWith('.') && !dep.startsWith('/')) { |
| 42 | + // Get package name (handle scoped packages) |
| 43 | + const packageName = dep.startsWith('@') |
| 44 | + ? dep.split('/').slice(0, 2).join('/') |
| 45 | + : dep.split('/')[0]; |
| 46 | + deps.add(packageName); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + // Extract import dependencies |
| 51 | + importMatches.forEach(match => { |
| 52 | + const dep = match.match(/from\s+["']([^"']+)["']/)[1]; |
| 53 | + if (!dep.startsWith('.') && !dep.startsWith('/')) { |
| 54 | + const packageName = dep.startsWith('@') |
| 55 | + ? dep.split('/').slice(0, 2).join('/') |
| 56 | + : dep.split('/')[0]; |
| 57 | + deps.add(packageName); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + return Array.from(deps); |
| 62 | + } catch (error) { |
| 63 | + return []; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Function to scan directory recursively |
| 68 | +function scanDirectory(dir, extensions = ['.js', '.ts', '.tsx', '.jsx']) { |
| 69 | + const allDeps = new Set(); |
| 70 | + |
| 71 | + function scanRecursive(currentDir) { |
| 72 | + try { |
| 73 | + const items = fs.readdirSync(currentDir); |
| 74 | + |
| 75 | + for (const item of items) { |
| 76 | + const fullPath = path.join(currentDir, item); |
| 77 | + const stat = fs.statSync(fullPath); |
| 78 | + |
| 79 | + if (stat.isDirectory() && !item.startsWith('.') && item !== 'node_modules') { |
| 80 | + scanRecursive(fullPath); |
| 81 | + } else if (stat.isFile() && extensions.some(ext => item.endsWith(ext))) { |
| 82 | + const deps = extractDependencies(fullPath); |
| 83 | + deps.forEach(dep => allDeps.add(dep)); |
| 84 | + } |
| 85 | + } |
| 86 | + } catch (error) { |
| 87 | + // Skip directories we can't read |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + scanRecursive(dir); |
| 92 | + return Array.from(allDeps); |
| 93 | +} |
| 94 | + |
| 95 | +// Scan specific directories for dependencies |
| 96 | +const directories = [ |
| 97 | + 'node_modules/@lit-protocol', |
| 98 | + 'src' |
| 99 | +]; |
| 100 | + |
| 101 | +const allFoundDeps = new Set(); |
| 102 | + |
| 103 | +directories.forEach(dir => { |
| 104 | + if (fs.existsSync(dir)) { |
| 105 | + console.log(`📁 Scanning ${dir}...`); |
| 106 | + const deps = scanDirectory(dir); |
| 107 | + deps.forEach(dep => allFoundDeps.add(dep)); |
| 108 | + } |
| 109 | +}); |
| 110 | + |
| 111 | +// Filter out built-in Node.js modules and current dependencies |
| 112 | +const builtInModules = new Set([ |
| 113 | + 'fs', 'path', 'crypto', 'stream', 'buffer', 'util', 'events', 'os', 'url', |
| 114 | + 'http', 'https', 'querystring', 'zlib', 'worker_threads', 'fs/promises' |
| 115 | +]); |
| 116 | + |
| 117 | +const missingDeps = Array.from(allFoundDeps).filter(dep => |
| 118 | + !currentDeps[dep] && |
| 119 | + !builtInModules.has(dep) && |
| 120 | + dep !== 'react' && // Common false positives |
| 121 | + dep !== 'node_modules' |
| 122 | +); |
| 123 | + |
| 124 | +console.log('\n📊 Results:'); |
| 125 | +console.log(`Total dependencies found: ${allFoundDeps.size}`); |
| 126 | +console.log(`Missing dependencies: ${missingDeps.length}`); |
| 127 | + |
| 128 | +if (missingDeps.length > 0) { |
| 129 | + console.log('\n❌ Missing Dependencies:'); |
| 130 | + console.log('Add these to your package.json:\n'); |
| 131 | + |
| 132 | + const suggestions = {}; |
| 133 | + |
| 134 | + // Common version suggestions |
| 135 | + const versionMap = { |
| 136 | + 'siwe': '^2.3.2', |
| 137 | + 'siwe-recap': '^0.0.2-alpha.0', |
| 138 | + 'jose': '^4.14.4', |
| 139 | + 'ethers': '5.7.2', |
| 140 | + 'viem': '^2.29.4', |
| 141 | + '@noble/curves': '^1.2.0', |
| 142 | + '@noble/hashes': '^1.3.0', |
| 143 | + 'base64url': '^3.0.1', |
| 144 | + 'cbor-web': '^9.0.2', |
| 145 | + 'elysia': '^1.2.25', |
| 146 | + 'tslib': '^2.3.0', |
| 147 | + 'zod-validation-error': '^3.4.0', |
| 148 | + '@openagenda/verror': '^3.1.4', |
| 149 | + '@simplewebauthn/browser': '^7.2.0' |
| 150 | + }; |
| 151 | + |
| 152 | + missingDeps.forEach(dep => { |
| 153 | + const version = versionMap[dep] || '^latest'; |
| 154 | + suggestions[dep] = version; |
| 155 | + console.log(` "${dep}": "${version}",`); |
| 156 | + }); |
| 157 | + |
| 158 | + console.log('\n💡 Or run this command to install them all:'); |
| 159 | + const installCmd = `pnpm add ${missingDeps.map(dep => `${dep}@${versionMap[dep] || 'latest'}`).join(' ')}`; |
| 160 | + console.log(`\n${installCmd}\n`); |
| 161 | + |
| 162 | +} else { |
| 163 | + console.log('\n✅ All dependencies appear to be present!'); |
| 164 | +} |
| 165 | + |
| 166 | +console.log('\n🔧 Current package.json dependencies:'); |
| 167 | +Object.keys(currentDeps).sort().forEach(dep => { |
| 168 | + console.log(` ${dep}: ${currentDeps[dep]}`); |
| 169 | +}); |
0 commit comments