|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Patches Vite config to add the correct base path for deployment. |
| 5 | + */ |
| 6 | + |
| 7 | +import { readFileSync, writeFileSync, existsSync } from 'fs'; |
| 8 | +import { resolve } from 'path'; |
| 9 | + |
| 10 | +const landingName = process.argv[2]; |
| 11 | + |
| 12 | +if (!landingName) { |
| 13 | + console.error('Error: Landing name is required'); |
| 14 | + process.exit(1); |
| 15 | +} |
| 16 | + |
| 17 | +const configFiles = ['vite.config.ts', 'vite.config.js', 'vite.config.mjs']; |
| 18 | +let viteConfigPath = null; |
| 19 | + |
| 20 | +for (const configFile of configFiles) { |
| 21 | + if (existsSync(configFile)) { |
| 22 | + viteConfigPath = resolve(configFile); |
| 23 | + break; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +if (!viteConfigPath) { |
| 28 | + console.error('Error: No vite config file found (checked: vite.config.ts, vite.config.js, vite.config.mjs)'); |
| 29 | + process.exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +console.log(`Found Vite config: ${viteConfigPath}`); |
| 33 | + |
| 34 | +let content = readFileSync(viteConfigPath, 'utf-8'); |
| 35 | + |
| 36 | +const basePath = `/lp/${landingName}/`; |
| 37 | + |
| 38 | +if (content.includes('base:')) { |
| 39 | + console.log('Base path already exists, replacing...'); |
| 40 | + content = content.replace( |
| 41 | + /base\s*:\s*['"`][^'"`]*['"`]/g, |
| 42 | + `base: '${basePath}'` |
| 43 | + ); |
| 44 | +} else { |
| 45 | + console.log('Adding base path to config...'); |
| 46 | + content = content.replace( |
| 47 | + /defineConfig\s*\(\s*\{/, |
| 48 | + `defineConfig({\n base: '${basePath}',` |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +writeFileSync(viteConfigPath, content, 'utf-8'); |
| 53 | + |
| 54 | +console.log(`Successfully patched ${viteConfigPath} with base: '${basePath}'`); |
| 55 | + |
0 commit comments