Move catalog indexes to Markdown front matter
This commit is contained in:
+8
-23
@@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { lstat, readdir, readFile } from "node:fs/promises";
|
||||
import { lstat, readdir } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import process from "node:process";
|
||||
import { parseDocument } from "yaml";
|
||||
import { readCatalogIndex } from "./catalogIndex.js";
|
||||
|
||||
type CatalogType = "category" | "program";
|
||||
|
||||
@@ -43,7 +43,7 @@ function usage(): void {
|
||||
console.log(`Usage: pnpm tree [options] [path]
|
||||
|
||||
With no path, the script prints the complete tree from catalog. A path may point
|
||||
to a category directory, a program directory, or its index.yaml file.
|
||||
to a category directory, a program directory, or its index.md file.
|
||||
|
||||
--versions show versions under each program
|
||||
--paths show relative directory paths
|
||||
@@ -98,30 +98,15 @@ async function resolveDirectory(target: string): Promise<string> {
|
||||
if (targetStat.isDirectory()) {
|
||||
return absoluteTarget;
|
||||
}
|
||||
if (targetStat.isFile() && path.basename(absoluteTarget) === "index.yaml") {
|
||||
if (targetStat.isFile() && path.basename(absoluteTarget) === "index.md") {
|
||||
return path.dirname(absoluteTarget);
|
||||
}
|
||||
throw new Error(`Expected a catalog directory or index.yaml file: ${target}`);
|
||||
throw new Error(`Expected a catalog directory or index.md file: ${target}`);
|
||||
}
|
||||
|
||||
async function readCatalogNode(directory: string): Promise<CatalogNode> {
|
||||
const indexPath = path.join(directory, "index.yaml");
|
||||
let source: string;
|
||||
|
||||
try {
|
||||
source = await readFile(indexPath, "utf8");
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(`${indexPath}: failed to read index.yaml: ${reason}`);
|
||||
}
|
||||
|
||||
const document = parseDocument(source, { prettyErrors: true });
|
||||
if (document.errors.length > 0) {
|
||||
const errors = document.errors.map((error) => error.message).join("; ");
|
||||
throw new Error(`${indexPath}: ${errors}`);
|
||||
}
|
||||
|
||||
const data = document.toJS() as CatalogIndex;
|
||||
const indexPath = path.join(directory, "index.md");
|
||||
const { metadata: data } = await readCatalogIndex<CatalogIndex>(indexPath);
|
||||
if (data.type !== "category" && data.type !== "program") {
|
||||
throw new Error(`${indexPath}: unknown type`);
|
||||
}
|
||||
@@ -161,7 +146,7 @@ async function readCatalogNode(directory: string): Promise<CatalogNode> {
|
||||
|
||||
const childDirectory = path.join(directory, entry.name);
|
||||
try {
|
||||
const childIndexStat = await lstat(path.join(childDirectory, "index.yaml"));
|
||||
const childIndexStat = await lstat(path.join(childDirectory, "index.md"));
|
||||
if (childIndexStat.isFile()) {
|
||||
children.push(await readCatalogNode(childDirectory));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user