Cleanup after cloned content method
This commit is contained in:
+43
-27
@@ -1,34 +1,50 @@
|
||||
// Copy the four editable files into Astro's ignored content directory.
|
||||
// --ensure seeds only a fresh checkout; an incomplete existing set fails loudly.
|
||||
import { cpSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
|
||||
import { resolve, join } from 'node:path';
|
||||
// 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 ensure = process.argv.includes('--ensure');
|
||||
const source = resolve(process.argv[2] && !ensure ? process.argv[2] : join(root, 'sample-content'));
|
||||
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');
|
||||
const uploads = join(root, 'public/uploads');
|
||||
|
||||
function validate(directory) {
|
||||
for (const { slug } of examples) {
|
||||
if (!existsSync(join(directory, `${slug}.md`))) {
|
||||
throw new Error(`Missing ${join(directory, `${slug}.md`)}. Run npm run content:setup or npm run content:sync -- <content-clone>.`);
|
||||
}
|
||||
for (const { slug } of examples) {
|
||||
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>.`);
|
||||
}
|
||||
}
|
||||
|
||||
if (ensure && existsSync(pages)) {
|
||||
validate(pages);
|
||||
} else {
|
||||
validate(join(source, 'pages'));
|
||||
if (!existsSync(join(source, 'uploads'))) throw new Error(`Missing uploads directory in ${source}`);
|
||||
mkdirSync(pages, { recursive: true });
|
||||
mkdirSync(uploads, { recursive: true });
|
||||
for (const { slug } of examples) {
|
||||
cpSync(join(source, 'pages', `${slug}.md`), join(pages, `${slug}.md`));
|
||||
}
|
||||
cpSync(join(source, 'uploads'), uploads, { recursive: true });
|
||||
console.log(`Example content copied from ${source}`);
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Local dev: put the content repo's files where the build expects them.
|
||||
# Usage: ./scripts/link-content.sh [path-to-content-clone] (default ../ctao-content)
|
||||
# Cross-platform implementation is shared with npm run content:sync.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
node scripts/content.mjs "${1:-../ctao-content}"
|
||||
@@ -1,19 +0,0 @@
|
||||
// Prepare a disposable content repository for Sveltia's local folder picker.
|
||||
import { cpSync, existsSync, mkdirSync } from 'node:fs';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const root = fileURLToPath(new URL('../', import.meta.url));
|
||||
const local = join(root, '.tmp/content');
|
||||
if (!existsSync(local)) {
|
||||
mkdirSync(local, { recursive: true });
|
||||
cpSync(join(root, 'sample-content/pages'), join(local, 'pages'), { recursive: true });
|
||||
cpSync(join(root, 'sample-content/uploads'), join(local, 'uploads'), { recursive: true });
|
||||
}
|
||||
if (!existsSync(join(local, '.git'))) {
|
||||
execFileSync('git', ['init', '--quiet', '--initial-branch=main', local], { stdio: 'inherit' });
|
||||
}
|
||||
execFileSync(process.execPath, [join(root, 'scripts/content.mjs'), local], { stdio: 'inherit' });
|
||||
console.log(`In Sveltia, choose Work with Local Repository and select: ${local}`);
|
||||
console.log('After saving, run: npm run content:sync -- .tmp/content');
|
||||
Reference in New Issue
Block a user