Files
portal/scripts/content.mjs
T
2026-09-24 14:37:40 +02:00

51 lines
2.4 KiB
JavaScript

// Point the site at a checkout of the ctao/content repository (default: the
// sibling ../content clone), the same content the live demo builds from.
// src/content/pages and public/uploads become links into that checkout, so
// saves from Sveltia's local-repository mode show up in `npm run dev` directly.
//
// node scripts/content.mjs link if missing, then check the pages (pre-hooks)
// npm run content:link -- [dir] replace existing links or copies
//
// The deploy build copies the content into place instead (deploy/build.sh);
// existing directories are left alone unless --relink is given.
import { existsSync, lstatSync, mkdirSync, readFileSync, rmSync, symlinkSync, unlinkSync } from 'node:fs';
import { dirname, join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = fileURLToPath(new URL('../', import.meta.url));
const examples = JSON.parse(readFileSync(join(root, 'src/data/examples.json'), 'utf8'));
const relink = process.argv.includes('--relink');
const source = resolve(root, process.argv.slice(2).find((arg) => !arg.startsWith('--')) ?? '../content');
const targets = [
{ from: join(source, 'pages'), to: join(root, 'src/content/pages') },
{ from: join(source, 'uploads'), to: join(root, 'public/uploads') },
];
const exists = (path) => {
try { lstatSync(path); return true; } catch { return false; }
};
for (const { from, to } of targets) {
if (exists(to) && !relink) continue;
if (!existsSync(from)) {
throw new Error(`Missing ${from}. Clone the content repository first:\n` +
` git clone https://astro-git.isl-dev.grid.cyfronet.pl/ctao/content.git ${relative(root, source) || '.'}\n` +
'or pass another checkout: npm run content:link -- <path>');
}
if (exists(to)) {
// Remove only the link itself, never the content it points to.
if (lstatSync(to).isSymbolicLink()) unlinkSync(to);
else rmSync(to, { recursive: true }); // an old copied overlay
}
mkdirSync(dirname(to), { recursive: true });
symlinkSync(from, to, 'junction'); // a junction needs no admin rights on Windows
console.log(`Linked ${relative(root, to)} -> ${from}`);
}
const pages = join(root, 'src/content/pages');
for (const slug of ['home', ...examples.map(({ slug }) => slug)]) {
if (!existsSync(join(pages, `${slug}.md`))) {
throw new Error(`Missing ${join(pages, `${slug}.md`)}. Check the content checkout, or run npm run content:link -- <path>.`);
}
}