Files
root 485589a488 feat(migrate): plans importer
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-04 22:18:44 +10:00

26 lines
1.0 KiB
JavaScript

import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import * as pages from '../../lib/db/repos/pages.js';
import * as map from '../../lib/db/repos/migration_map.js';
const SYS = { kind: 'system', id: null };
const slugify = (s) => s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '').slice(0, 60) || 'page';
function titleOf(body, file) {
const m = body.match(/^#\s+(.+)$/m);
return m ? m[1].trim() : file.replace(/\.md$/, '');
}
export async function importPlans({ dir, spaceId, dryRun = false }) {
let created = 0;
for (const file of readdirSync(dir).filter(f => f.endsWith('.md')).sort()) {
if (await map.seen('plans', file, 'page')) continue;
const body = readFileSync(join(dir, file), 'utf8');
const title = titleOf(body, file);
created++;
if (dryRun) continue;
const page = await pages.create({ space_id: spaceId, slug: slugify(title), title, body_md: body }, SYS);
await map.record('plans', file, 'page', page.id);
}
return { created };
}