61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
/**
|
|
* Copies story assets (images, etc.) from content/stories/<slug>/
|
|
* into static/stories/<slug>/ so they're available as static files
|
|
* during the build.
|
|
*
|
|
* Run before `vite build`: node scripts/copy-assets.js
|
|
*/
|
|
|
|
import { readdirSync, cpSync, existsSync, mkdirSync, statSync } from 'node:fs';
|
|
import { join, resolve } from 'node:path';
|
|
|
|
const CONTENT_DIR = resolve('public/stories');
|
|
const STATIC_DIR = resolve('static/stories');
|
|
|
|
if (!existsSync(CONTENT_DIR)) {
|
|
console.log('No content/stories directory found, skipping asset copy.');
|
|
process.exit(0);
|
|
}
|
|
|
|
if (!existsSync(STATIC_DIR)) {
|
|
mkdirSync(STATIC_DIR, { recursive: true });
|
|
}
|
|
|
|
const slugs = readdirSync(CONTENT_DIR, { withFileTypes: true })
|
|
.filter((d) => d.isDirectory())
|
|
.map((d) => d.name);
|
|
|
|
let copied = 0;
|
|
|
|
for (const slug of slugs) {
|
|
const storyDir = join(CONTENT_DIR, slug);
|
|
const destDir = join(STATIC_DIR, slug);
|
|
|
|
const entries = readdirSync(storyDir, { withFileTypes: true });
|
|
|
|
for (const entry of entries) {
|
|
// Skip the .aml file itself
|
|
if (entry.name.endsWith('.aml')) continue;
|
|
|
|
const srcPath = join(storyDir, entry.name);
|
|
const destPath = join(destDir, entry.name);
|
|
|
|
if (!existsSync(destDir)) {
|
|
mkdirSync(destDir, { recursive: true });
|
|
}
|
|
|
|
if (entry.isDirectory()) {
|
|
cpSync(srcPath, destPath, { recursive: true });
|
|
copied++;
|
|
} else if (entry.isFile()) {
|
|
const srcStat = statSync(srcPath);
|
|
// Only copy if dest doesn't exist or is older
|
|
if (!existsSync(destPath) || statSync(destPath).mtimeMs < srcStat.mtimeMs) {
|
|
cpSync(srcPath, destPath);
|
|
copied++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`Copied ${copied} asset(s) from ${slugs.length} story folder(s).`);
|