79 lines
2.0 KiB
JavaScript
79 lines
2.0 KiB
JavaScript
import { mkdir, readdir, rename } from 'node:fs/promises';
|
|
import { basename, dirname, join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { spawn } from 'node:child_process';
|
|
|
|
const rootDir = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
const routesDir = join(rootDir, 'src', 'routes');
|
|
const renamedRouteTestFiles = [];
|
|
|
|
async function collectReservedRouteTestFiles(directory) {
|
|
const entries = await readdir(directory, { withFileTypes: true });
|
|
const files = [];
|
|
|
|
for (const entry of entries) {
|
|
const entryPath = join(directory, entry.name);
|
|
|
|
if (entry.isDirectory()) {
|
|
files.push(...(await collectReservedRouteTestFiles(entryPath)));
|
|
continue;
|
|
}
|
|
|
|
if (!entry.name.startsWith('+')) {
|
|
continue;
|
|
}
|
|
|
|
if (!entry.name.includes('.test.') && !entry.name.includes('.spec.')) {
|
|
continue;
|
|
}
|
|
|
|
files.push(entryPath);
|
|
}
|
|
|
|
return files;
|
|
}
|
|
|
|
async function renameReservedRouteTests() {
|
|
const reservedRouteTestFiles = await collectReservedRouteTestFiles(routesDir);
|
|
|
|
for (const sourcePath of reservedRouteTestFiles) {
|
|
const targetPath = join(dirname(sourcePath), basename(sourcePath).slice(1));
|
|
await rename(sourcePath, targetPath);
|
|
renamedRouteTestFiles.push({ sourcePath, targetPath });
|
|
}
|
|
}
|
|
|
|
async function restoreReservedRouteTests() {
|
|
for (const { sourcePath, targetPath } of renamedRouteTestFiles.reverse()) {
|
|
await mkdir(dirname(sourcePath), { recursive: true });
|
|
await rename(targetPath, sourcePath);
|
|
}
|
|
}
|
|
|
|
function runViteBuild() {
|
|
const viteBinPath = join(rootDir, 'node_modules', 'vite', 'bin', 'vite.js');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(process.execPath, [viteBinPath, 'build'], {
|
|
cwd: rootDir,
|
|
stdio: 'inherit'
|
|
});
|
|
|
|
child.once('error', reject);
|
|
child.once('exit', (code) => {
|
|
if (code === 0) {
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
reject(new Error(`vite build exited with code ${code ?? 'unknown'}`));
|
|
});
|
|
});
|
|
}
|
|
|
|
try {
|
|
await renameReservedRouteTests();
|
|
await runViteBuild();
|
|
} finally {
|
|
await restoreReservedRouteTests();
|
|
} |