55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
|
||
|
|
import { createHash } from "node:crypto";
|
||
|
|
import { mkdir, readFile, writeFile, copyFile } from "node:fs/promises";
|
||
|
|
import path from "node:path";
|
||
|
|
import { fileURLToPath } from "node:url";
|
||
|
|
|
||
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
||
|
|
const reconstructionRoot = path.resolve(here, "..");
|
||
|
|
const configPath = path.join(reconstructionRoot, "selected-sources.json");
|
||
|
|
const outputRoot = path.join(reconstructionRoot, "sources");
|
||
|
|
const manifestPath = path.join(reconstructionRoot, "selected-sources-manifest.json");
|
||
|
|
|
||
|
|
const config = JSON.parse(await readFile(configPath, "utf8"));
|
||
|
|
const manifest = [];
|
||
|
|
|
||
|
|
for (const source of config.sources) {
|
||
|
|
const bytes = await readFile(source.path);
|
||
|
|
const text = bytes.toString("utf8");
|
||
|
|
const title = text.match(/^#\s+(.+)$/m)?.[1]?.trim() ?? path.basename(source.path);
|
||
|
|
const targetDir = path.join(outputRoot, source.group);
|
||
|
|
const targetPath = path.join(targetDir, `${source.id}.source.txt`);
|
||
|
|
await mkdir(targetDir, { recursive: true });
|
||
|
|
await copyFile(source.path, targetPath);
|
||
|
|
manifest.push({
|
||
|
|
id: source.id,
|
||
|
|
title,
|
||
|
|
group: source.group,
|
||
|
|
status: source.status,
|
||
|
|
privacy: source.privacy,
|
||
|
|
role: source.role,
|
||
|
|
notion_url: `https://app.notion.com/p/${source.id}`,
|
||
|
|
source_path: source.path,
|
||
|
|
imported_path: path.relative(reconstructionRoot, targetPath),
|
||
|
|
bytes: bytes.length,
|
||
|
|
sha256: createHash("sha256").update(bytes).digest("hex")
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
await writeFile(
|
||
|
|
manifestPath,
|
||
|
|
`${JSON.stringify({
|
||
|
|
schema: "guanghu.selected-notion-source-manifest/v0.1",
|
||
|
|
generated_at: new Date().toISOString(),
|
||
|
|
source_count: manifest.length,
|
||
|
|
copy_mode: "VERBATIM_BYTES_AS_SOURCE_TEXT",
|
||
|
|
sources: manifest
|
||
|
|
}, null, 2)}\n`
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log(JSON.stringify({
|
||
|
|
source_count: manifest.length,
|
||
|
|
manifest: manifestPath,
|
||
|
|
output_root: outputRoot
|
||
|
|
}, null, 2));
|