102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
#!/usr/bin/env tsx
|
|
/**
|
|
* Vite-Nginx Configuration Sync Validator
|
|
*
|
|
* Ensures that when a Vite config uses a custom cacheDir,
|
|
* the corresponding nginx config has a location block for it.
|
|
*
|
|
* Usage: npx tsx verify-vite-nginx-sync.ts
|
|
*/
|
|
|
|
import { readFileSync, existsSync } from 'node:fs';
|
|
import { join, basename, dirname } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { glob } from 'glob';
|
|
|
|
interface ValidationError {
|
|
domain: string;
|
|
viteConfig: string;
|
|
nginxConfig: string;
|
|
cacheDir: string;
|
|
message: string;
|
|
}
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const DOMAINS_PATH = join(__dirname, '../../../deployments/@domains');
|
|
|
|
async function main(): Promise<void> {
|
|
const errors: ValidationError[] = [];
|
|
|
|
// Find all vite.config.ts files in domain deployments
|
|
const viteConfigs = await glob(`${DOMAINS_PATH}/*/root/vite.config.ts`);
|
|
|
|
for (const viteConfigPath of viteConfigs) {
|
|
// viteConfigPath: .../deployments/@domains/<domain>/root/vite.config.ts
|
|
// domainDir should be: .../deployments/@domains/<domain>
|
|
const rootDir = dirname(viteConfigPath); // .../root
|
|
const domainDir = dirname(rootDir); // .../<domain>
|
|
const domainId = basename(domainDir);
|
|
const nginxConfigPath = join(domainDir, 'nginx/local.conf');
|
|
|
|
// Read vite config
|
|
const viteContent = readFileSync(viteConfigPath, 'utf-8');
|
|
|
|
// Check for custom cacheDir
|
|
const cacheDirMatch = viteContent.match(/cacheDir:\s*['"]([^'"]+)['"]/);
|
|
if (!cacheDirMatch) {
|
|
continue; // Using default cache location, no special nginx config needed
|
|
}
|
|
|
|
const cacheDir = cacheDirMatch[1];
|
|
|
|
// Check if nginx config exists
|
|
if (!existsSync(nginxConfigPath)) {
|
|
errors.push({
|
|
domain: domainId,
|
|
viteConfig: viteConfigPath,
|
|
nginxConfig: nginxConfigPath,
|
|
cacheDir,
|
|
message: `nginx config not found`,
|
|
});
|
|
continue;
|
|
}
|
|
|
|
// Read nginx config
|
|
const nginxContent = readFileSync(nginxConfigPath, 'utf-8');
|
|
|
|
// Check for corresponding location block
|
|
const locationPattern = new RegExp(`location\\s+/${cacheDir.replace(/^\./, '\\.')}/`);
|
|
if (!locationPattern.test(nginxContent)) {
|
|
errors.push({
|
|
domain: domainId,
|
|
viteConfig: viteConfigPath,
|
|
nginxConfig: nginxConfigPath,
|
|
cacheDir,
|
|
message: `nginx missing location block for /${cacheDir}/`,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Report results
|
|
if (errors.length === 0) {
|
|
console.log('✓ All Vite cacheDir configs have matching nginx location blocks');
|
|
process.exit(0);
|
|
}
|
|
|
|
console.error('✗ Vite-Nginx configuration mismatch detected:\n');
|
|
for (const error of errors) {
|
|
console.error(` ${error.domain}:`);
|
|
console.error(` Vite cacheDir: ${error.cacheDir}`);
|
|
console.error(` Issue: ${error.message}`);
|
|
console.error(` Fix: Add "location /${error.cacheDir}/ { ... }" to nginx config`);
|
|
console.error('');
|
|
}
|
|
|
|
console.error('Run ./run domains:build to regenerate nginx configs after fixing templates.ts');
|
|
process.exit(1);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error('Validation failed:', err);
|
|
process.exit(1);
|
|
});
|