forked from izu/student-web-if-development-kit
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import { existsSync, readdirSync, statSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const root = process.cwd();
|
|
const dist = path.join(root, 'dist');
|
|
const excludedDirs = new Set(['.git', 'dist', 'node_modules']);
|
|
|
|
function collectFiles(dir, predicate = () => true) {
|
|
const entries = readdirSync(dir, { withFileTypes: true });
|
|
const files = [];
|
|
|
|
for (const entry of entries) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
|
|
if (entry.isDirectory()) {
|
|
if (!excludedDirs.has(entry.name)) {
|
|
files.push(...collectFiles(fullPath, predicate));
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (entry.isFile() && predicate(fullPath)) {
|
|
files.push(fullPath);
|
|
}
|
|
}
|
|
|
|
return files;
|
|
}
|
|
|
|
if (!existsSync(dist) || !statSync(dist).isDirectory()) {
|
|
throw new Error('Missing dist directory. Run `npm run build` first.');
|
|
}
|
|
|
|
const missingHtmlFiles = collectFiles(root, (file) => file.endsWith('.html'))
|
|
.map((file) => path.relative(root, file))
|
|
.filter((relativePath) => !existsSync(path.join(dist, relativePath)));
|
|
|
|
const missingAssetFiles = collectFiles(path.join(root, 'assets'))
|
|
.map((file) => path.relative(root, file))
|
|
.filter((relativePath) => !existsSync(path.join(dist, relativePath)));
|
|
|
|
const missingFiles = [...missingHtmlFiles, ...missingAssetFiles];
|
|
|
|
if (missingFiles.length > 0) {
|
|
throw new Error(`Missing built files:\n${missingFiles.join('\n')}`);
|
|
}
|
|
|
|
console.log('All source HTML and static asset files are present in dist.');
|