54 lines
4.0 KiB
JavaScript
54 lines
4.0 KiB
JavaScript
import { readdir, readFile, writeFile } from 'node:fs/promises';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
const root = new URL('../', import.meta.url);
|
|
const readJSON = async path => JSON.parse(await readFile(new URL(path, root), 'utf8'));
|
|
const pairs = ['kind', 'title', 'name', 'intro', 'context', 'implementation', 'decisions'];
|
|
export function validateProjects(projects, processes) {
|
|
const ids = new Set();
|
|
const codes = new Set(processes.map(p => p.code));
|
|
const require = (ok, message) => { if (!ok) throw new Error(message); };
|
|
const pair = value => Array.isArray(value) && value.length === 2 && value.every(v => typeof v === 'string' && v.trim());
|
|
const path = value => typeof value === 'string' && value.length > 0 && !value.startsWith('/') && !value.split('/').some(p => p === '..' || p === '.');
|
|
const mapping = value => Array.isArray(value) && value.every(c => codes.has(c));
|
|
for (const p of projects) {
|
|
require(/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(p.id) && !ids.has(p.id), `Invalid or duplicate project ID: ${p.id}`);
|
|
ids.add(p.id);
|
|
for (const key of pairs) require(pair(p[key]), `${p.id}.${key}: provide Dutch and English text`);
|
|
for (const key of ['number', 'challenge']) require(typeof p[key] === 'string' && p[key].trim(), `${p.id}.${key}: required text`);
|
|
for (const key of ['stack', 'nodes']) require(Array.isArray(p[key]) && p[key].length > 0 && p[key].every(v => typeof v === 'string' && v.trim()), `${p.id}.${key}: provide text entries`);
|
|
require(p.nodes.length === 3, `${p.id}.nodes: provide three diagram labels`);
|
|
require(path(p.path), `${p.id}.path: provide a repository-relative path`);
|
|
require(mapping(p.codes), `${p.id}.codes: unknown process code`);
|
|
require(Array.isArray(p.evidence), `${p.id}.evidence: expected an array`);
|
|
for (const e of p.evidence) {
|
|
require(pair(e.label) && path(e.path) && typeof e.type === 'string' && e.type.trim() && mapping(e.codes) && (e.directory === undefined || typeof e.directory === 'boolean'), `${p.id}: invalid evidence item`);
|
|
}
|
|
}
|
|
}
|
|
export async function compileContent() {
|
|
const settings = await readJSON('content/settings.json');
|
|
const files = (await readdir(new URL('content/projects/', root))).filter(f => f.endsWith('.json')).sort();
|
|
const projects = await Promise.all(files.map(f => readJSON(`content/projects/${f}`)));
|
|
projects.sort((a, b) => a.number.localeCompare(b.number, undefined, { numeric: true }));
|
|
validateProjects(projects, settings.processes);
|
|
return '// Generated from content/ by npm run content. Edit the JSON source files.\n' +
|
|
Object.entries({ ...settings, projects }).map(([key, value]) => `export const ${key} = ${JSON.stringify(value, null, 2)};`).join('\n') +
|
|
'\nexport const source = (path, directory = false) => `${profile.school}/${directory ? "tree" : "blob"}/${revision}/${path.split("/").map(encodeURIComponent).join("/")}`;\n';
|
|
}
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
const [command, id] = process.argv.slice(2);
|
|
if (command === 'add') {
|
|
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(id || '')) throw new Error('Usage: npm run project:add -- my-project');
|
|
const files = (await readdir(new URL('content/projects/', root))).filter(f => f.endsWith('.json'));
|
|
const existing = await Promise.all(files.map(f => readJSON(`content/projects/${f}`)));
|
|
const p = { id, number: String(Math.max(0, ...existing.map(p => Number(p.number) || 0)) + 1).padStart(2, '0'), challenge: '—' };
|
|
for (const key of pairs) p[key] = [key === 'name' ? id : 'Nog aanvullen', key === 'name' ? id : 'To be completed'];
|
|
Object.assign(p, { stack: ['To be completed'], codes: [], path: 'projects', nodes: ['Input', 'Application', 'Output'], evidence: [] });
|
|
await writeFile(new URL(`content/projects/${id}.json`, root), JSON.stringify(p, null, 2) + '\n', { flag: 'wx' });
|
|
console.log(`Created content/projects/${id}.json — replace the placeholder text before publishing.`);
|
|
}
|
|
await writeFile(new URL('content.js', root), await compileContent());
|
|
console.log('Validated and generated content.js');
|
|
}
|