|
1 | 1 | /* |
2 | | - * Copyright 2024 Adobe. All rights reserved. |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
3 | 3 | * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
4 | 4 | * you may not use this file except in compliance with the License. You may obtain a copy |
5 | 5 | * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | 10 | * governing permissions and limitations under the License. |
11 | 11 | */ |
12 | 12 | import { fileURLToPath } from 'url'; |
| 13 | +import { execSync, spawnSync } from 'child_process'; |
13 | 14 | import path from 'path'; |
14 | 15 | import fse from 'fs-extra'; |
15 | 16 | import * as esbuild from 'esbuild'; |
@@ -233,9 +234,107 @@ export default class EdgeESBuildBundler extends BaseBundler { |
233 | 234 | ].join('\n'), { name: 'wrangler.toml' }); |
234 | 235 | } |
235 | 236 |
|
| 237 | + /** |
| 238 | + * Checks if a command is available in the system PATH |
| 239 | + * @param {string} command - The command to check |
| 240 | + * @returns {boolean} - True if the command is available |
| 241 | + */ |
236 | 242 | // eslint-disable-next-line class-methods-use-this |
237 | | - validateBundle() { |
238 | | - // TODO: validate edge bundle |
239 | | - // Could potentially use wrangler/viceroy for validation |
| 243 | + isCommandAvailable(command) { |
| 244 | + try { |
| 245 | + execSync(`which ${command}`, { stdio: 'ignore' }); |
| 246 | + return true; |
| 247 | + } catch { |
| 248 | + return false; |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Validates the edge bundle using wrangler (Cloudflare) or viceroy (Fastly) if available. |
| 254 | + * This helps catch runtime issues before deployment. |
| 255 | + */ |
| 256 | + async validateBundle() { |
| 257 | + const { cfg } = this; |
| 258 | + const bundlePath = cfg.edgeBundle; |
| 259 | + const bundleDir = path.dirname(path.resolve(cfg.cwd, bundlePath)); |
| 260 | + |
| 261 | + // Try wrangler validation first (Cloudflare) |
| 262 | + const hasWrangler = this.isCommandAvailable('wrangler'); |
| 263 | + if (hasWrangler) { |
| 264 | + cfg.log.info('--: validating edge bundle with wrangler...'); |
| 265 | + try { |
| 266 | + // Create a minimal wrangler.toml for validation |
| 267 | + const wranglerToml = path.join(bundleDir, 'wrangler.toml'); |
| 268 | + const wranglerConfig = [ |
| 269 | + 'name = "validation-test"', |
| 270 | + `main = "${path.basename(bundlePath)}"`, |
| 271 | + 'compatibility_date = "2024-01-01"', |
| 272 | + 'no_bundle = true', |
| 273 | + ].join('\n'); |
| 274 | + await fse.writeFile(wranglerToml, wranglerConfig); |
| 275 | + |
| 276 | + // Run wrangler deploy --dry-run to validate without deploying |
| 277 | + const result = spawnSync('wrangler', ['deploy', '--dry-run'], { |
| 278 | + cwd: bundleDir, |
| 279 | + stdio: 'pipe', |
| 280 | + timeout: 30000, |
| 281 | + }); |
| 282 | + |
| 283 | + // Clean up temporary wrangler.toml |
| 284 | + await fse.remove(wranglerToml); |
| 285 | + |
| 286 | + if (result.status === 0) { |
| 287 | + cfg.log.info(chalk`{green ok:} wrangler validation passed`); |
| 288 | + } else { |
| 289 | + const stderr = result.stderr?.toString() || ''; |
| 290 | + cfg.log.warn(chalk`{yellow warn:} wrangler validation issues: ${stderr}`); |
| 291 | + } |
| 292 | + } catch (err) { |
| 293 | + cfg.log.warn(chalk`{yellow warn:} wrangler validation failed: ${err.message}`); |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + // Try Fastly validation (via fastly CLI which uses viceroy) |
| 298 | + const hasFastly = this.isCommandAvailable('fastly'); |
| 299 | + if (hasFastly) { |
| 300 | + cfg.log.info('--: validating edge bundle with fastly (viceroy)...'); |
| 301 | + try { |
| 302 | + // Create a minimal fastly.toml for validation |
| 303 | + const fastlyToml = path.join(bundleDir, 'fastly.toml'); |
| 304 | + const fastlyConfig = [ |
| 305 | + 'manifest_version = 2', |
| 306 | + 'name = "validation-test"', |
| 307 | + 'language = "javascript"', |
| 308 | + '[scripts]', |
| 309 | + 'build = ""', |
| 310 | + ].join('\n'); |
| 311 | + await fse.writeFile(fastlyToml, fastlyConfig); |
| 312 | + |
| 313 | + // Run fastly compute serve with --skip-build to validate |
| 314 | + // Use a short timeout and immediately kill to just check if bundle loads |
| 315 | + const result = spawnSync('fastly', ['compute', 'serve', '--skip-build', '--file', path.basename(bundlePath)], { |
| 316 | + cwd: bundleDir, |
| 317 | + stdio: 'pipe', |
| 318 | + timeout: 5000, |
| 319 | + }); |
| 320 | + |
| 321 | + // Clean up temporary fastly.toml |
| 322 | + await fse.remove(fastlyToml); |
| 323 | + |
| 324 | + // Check if it started successfully (will timeout, but no errors means valid) |
| 325 | + const stderr = result.stderr?.toString() || ''; |
| 326 | + if (!stderr.includes('error') && !stderr.includes('Error')) { |
| 327 | + cfg.log.info(chalk`{green ok:} fastly validation passed`); |
| 328 | + } else { |
| 329 | + cfg.log.warn(chalk`{yellow warn:} fastly validation issues: ${stderr}`); |
| 330 | + } |
| 331 | + } catch (err) { |
| 332 | + cfg.log.warn(chalk`{yellow warn:} fastly validation failed: ${err.message}`); |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | + if (!hasWrangler && !hasFastly) { |
| 337 | + cfg.log.info('--: skipping bundle validation (neither wrangler nor fastly CLI installed)'); |
| 338 | + } |
240 | 339 | } |
241 | 340 | } |
0 commit comments