61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
|
|
import fs from 'node:fs/promises';
|
||
|
|
import path from 'node:path';
|
||
|
|
import process from 'node:process';
|
||
|
|
import { fileURLToPath } from 'node:url';
|
||
|
|
|
||
|
|
import { setupElegantRouter } from '../build/plugins/router.ts';
|
||
|
|
|
||
|
|
const __filename = fileURLToPath(import.meta.url);
|
||
|
|
const __dirname = path.dirname(__filename);
|
||
|
|
const rootDir = path.resolve(__dirname, '..');
|
||
|
|
|
||
|
|
const generatedFiles = [
|
||
|
|
path.resolve(rootDir, 'src/router/elegant/imports.ts'),
|
||
|
|
path.resolve(rootDir, 'src/router/elegant/routes.ts'),
|
||
|
|
path.resolve(rootDir, 'src/router/elegant/transform.ts'),
|
||
|
|
path.resolve(rootDir, 'src/typings/elegant-router.d.ts')
|
||
|
|
];
|
||
|
|
|
||
|
|
async function getMtimeMs(filePath) {
|
||
|
|
try {
|
||
|
|
const stat = await fs.stat(filePath);
|
||
|
|
return stat.mtimeMs;
|
||
|
|
} catch {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function getGeneratedFileMtimes() {
|
||
|
|
return Promise.all(generatedFiles.map(getMtimeMs));
|
||
|
|
}
|
||
|
|
|
||
|
|
function isGenerated(before, after) {
|
||
|
|
return after.some((mtimeMs, index) => mtimeMs !== before[index]);
|
||
|
|
}
|
||
|
|
|
||
|
|
async function waitForGeneration(beforeMtimes, timeoutMs = 10000) {
|
||
|
|
const startTime = Date.now();
|
||
|
|
|
||
|
|
while (Date.now() - startTime < timeoutMs) {
|
||
|
|
const afterMtimes = await getGeneratedFileMtimes();
|
||
|
|
|
||
|
|
if (isGenerated(beforeMtimes, afterMtimes)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error('Timed out while waiting for elegant-router generated files.');
|
||
|
|
}
|
||
|
|
|
||
|
|
process.chdir(rootDir);
|
||
|
|
|
||
|
|
const beforeMtimes = await getGeneratedFileMtimes();
|
||
|
|
|
||
|
|
setupElegantRouter();
|
||
|
|
|
||
|
|
await waitForGeneration(beforeMtimes);
|
||
|
|
|
||
|
|
console.log('Generated elegant-router files.');
|