| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | import { existsSync, readFileSync } from "fs"; |
| 4 | import { dirname, resolve } from "path"; |
| 5 | import { fileURLToPath } from "url"; |
| 6 | |
| 7 | // Import CLI logger for consistent logging |
| 8 | let logger; |
| 9 | try { |
| 10 | // Try to import the compiled logger |
| 11 | const loggerModule = await import("./dist/util/logger.js"); |
| 12 | logger = loggerModule.logger; |
| 13 | } catch { |
| 14 | // Fallback to console if logger not available (e.g., not built yet) |
| 15 | logger = { |
| 16 | info: console.log, |
| 17 | warn: console.warn, |
| 18 | error: console.error, |
| 19 | }; |
| 20 | } |
| 21 | |
| 22 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 23 | |
| 24 | // Colors for output |
| 25 | const colors = { |
| 26 | green: "\x1b[32m", |
| 27 | red: "\x1b[31m", |
| 28 | yellow: "\x1b[33m", |
| 29 | blue: "\x1b[34m", |
| 30 | reset: "\x1b[0m", |
| 31 | }; |
| 32 | |
| 33 | /** |
| 34 | * Parse the build.mjs file to extract defined aliases |
| 35 | */ |
| 36 | function getDefinedAliases() { |
| 37 | const buildPath = resolve(__dirname, "build.mjs"); |
| 38 | const buildContent = readFileSync(buildPath, "utf8"); |
| 39 | |
| 40 | // Find the alias object in the build file |
| 41 | const aliasMatch = buildContent.match(/alias:\s*{([^}]+)}/s); |
| 42 | if (!aliasMatch) { |
| 43 | logger.warn( |
| 44 | `${colors.yellow}Warning: No aliases found in build.mjs${colors.reset}`, |
| 45 | ); |
| 46 | return new Set(); |
| 47 | } |
| 48 | |
| 49 | // Extract package names from the alias definitions |
| 50 | // Split by lines and filter out comments |
| 51 | const aliasBlock = aliasMatch[1]; |
| 52 | const lines = aliasBlock.split("\n"); |
| 53 | const aliases = new Set(); |
| 54 | |
| 55 | for (const line of lines) { |
| 56 | // Skip commented lines |
| 57 | if (line.trim().startsWith("//")) { |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | // Look for package names in non-commented lines |
| 62 | // Match both quoted and unquoted keys |
| 63 | const quotedMatch = line.match(/"([^"]+)":/); |
| 64 | const unquotedMatch = line.match(/\s+([a-zA-Z@][a-zA-Z0-9@\/_-]*)\s*:/); |
| 65 | |
| 66 | if (quotedMatch) { |
| 67 | aliases.add(quotedMatch[1]); |
| 68 | } else if ( |
| 69 | unquotedMatch && |
| 70 | !unquotedMatch[1].includes("__dirname") && |
| 71 | unquotedMatch[1] !== "js" |
| 72 | ) { |
| 73 | aliases.add(unquotedMatch[1]); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return aliases; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Find all local packages referenced with file: in package.json files |
| 82 | */ |
| 83 | function findLocalPackages(startPath, visited = new Set()) { |
| 84 | const localPackages = new Set(); |
| 85 | |
| 86 | function scanPackageJson(packageJsonPath, depth = 0) { |
| 87 | // Avoid infinite recursion |
| 88 | if (visited.has(packageJsonPath) || depth > 10) { |
| 89 | return; |
| 90 | } |
| 91 | visited.add(packageJsonPath); |
| 92 | |
| 93 | if (!existsSync(packageJsonPath)) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | try { |
| 98 | const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")); |
| 99 | const packageDir = dirname(packageJsonPath); |
| 100 | |
| 101 | // Check both dependencies and devDependencies |
| 102 | const allDeps = { |
| 103 | ...packageJson.dependencies, |
| 104 | ...packageJson.devDependencies, |
| 105 | }; |
| 106 | |
| 107 | for (const [depName, depVersion] of Object.entries(allDeps)) { |
| 108 | if (typeof depVersion === "string" && depVersion.startsWith("file:")) { |
| 109 | // This is a local package |
| 110 | localPackages.add(depName); |
| 111 | |
| 112 | // Recursively check this package's dependencies |
| 113 | const depPath = resolve(packageDir, depVersion.replace("file:", "")); |
| 114 | const depPackageJson = resolve(depPath, "package.json"); |
| 115 | |
| 116 | if (existsSync(depPackageJson)) { |
| 117 | scanPackageJson(depPackageJson, depth + 1); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } catch (error) { |
| 122 | logger.error( |
| 123 | `${colors.red}Error reading ${packageJsonPath}: ${error.message}${colors.reset}`, |
| 124 | ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Start from the CLI's package.json |
| 129 | scanPackageJson(startPath); |
| 130 | |
| 131 | return localPackages; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Main validation function |
| 136 | */ |
| 137 | function validateAliases() { |
| 138 | logger.info( |
| 139 | `${colors.blue}🔍 Validating esbuild aliases...${colors.reset}\n`, |
| 140 | ); |
| 141 | |
| 142 | // Get defined aliases from build.mjs |
| 143 | const definedAliases = getDefinedAliases(); |
| 144 | // console.log(`Found ${definedAliases.size} aliases in build.mjs:`); |
| 145 | // definedAliases.forEach(alias => { |
| 146 | // console.log(` ✓ ${alias}`); |
| 147 | // }); |
| 148 | // console.log(); |
| 149 | |
| 150 | // Find all local packages that need aliases |
| 151 | const cliPackageJson = resolve(__dirname, "package.json"); |
| 152 | const localPackages = findLocalPackages(cliPackageJson); |
| 153 | // console.log(`Found ${localPackages.size} local package dependencies:`); |
| 154 | // localPackages.forEach(pkg => { |
| 155 | // console.log(` • ${pkg}`); |
| 156 | // }); |
| 157 | // console.log(); |
| 158 | |
| 159 | // Check for missing aliases |
| 160 | const missingAliases = []; |
| 161 | for (const pkg of localPackages) { |
| 162 | if (!definedAliases.has(pkg)) { |
| 163 | missingAliases.push(pkg); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Check for unnecessary aliases (defined but not used) |
| 168 | const unnecessaryAliases = []; |
| 169 | for (const alias of definedAliases) { |
| 170 | if (!localPackages.has(alias)) { |
| 171 | unnecessaryAliases.push(alias); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Report results |
| 176 | if (missingAliases.length > 0) { |
| 177 | logger.error( |
| 178 | `${colors.red}❌ Missing aliases (WILL CAUSE RUNTIME ERRORS):${colors.reset}`, |
| 179 | ); |
| 180 | missingAliases.forEach((pkg) => { |
| 181 | logger.error(` ✗ ${pkg}`); |
| 182 | }); |
| 183 | logger.error(""); |
| 184 | logger.error( |
| 185 | `${colors.red}Add these to the alias section in build.mjs:${colors.reset}`, |
| 186 | ); |
| 187 | missingAliases.forEach((pkg) => { |
| 188 | const packageName = pkg.replace("@continuedev/", "").replace(/-/g, "_"); |
| 189 | logger.error( |
| 190 | ` "${pkg}": resolve(__dirname, "../../packages/${pkg.replace("@continuedev/", "")}/dist/index.js"),`, |
| 191 | ); |
| 192 | }); |
| 193 | logger.error(""); |
| 194 | } |
| 195 | |
| 196 | if (unnecessaryAliases.length > 0) { |
| 197 | logger.warn( |
| 198 | `${colors.yellow}⚠️ Potentially unnecessary aliases:${colors.reset}`, |
| 199 | ); |
| 200 | unnecessaryAliases.forEach((alias) => { |
| 201 | logger.warn(` ? ${alias}`); |
| 202 | }); |
| 203 | logger.warn(""); |
| 204 | } |
| 205 | |
| 206 | // Exit with appropriate code |
| 207 | if (missingAliases.length > 0) { |
| 208 | logger.error( |
| 209 | `${colors.red}✗ Validation failed! Missing ${missingAliases.length} required alias(es).${colors.reset}`, |
| 210 | ); |
| 211 | logger.error( |
| 212 | `${colors.red}This would cause runtime errors in the bundled CLI.${colors.reset}`, |
| 213 | ); |
| 214 | process.exit(1); |
| 215 | } else if (unnecessaryAliases.length > 0) { |
| 216 | logger.warn( |
| 217 | `${colors.yellow}✓ Validation passed with warnings.${colors.reset}`, |
| 218 | ); |
| 219 | process.exit(0); |
| 220 | } else { |
| 221 | logger.info( |
| 222 | `${colors.green}✅ All aliases are correctly configured!${colors.reset}`, |
| 223 | ); |
| 224 | process.exit(0); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Run validation |
| 229 | validateAliases(); |