Skip to content
Snippets Groups Projects
Commit a179051b authored by Evy Storozhenko's avatar Evy Storozhenko
Browse files

move imports up and apply changes

parent c8253bc2
No related branches found
No related tags found
No related merge requests found
Pipeline #186021 passed
import fs from "node:fs/promises";
// import { ParseResult, parse, parseFVV } from "./lib/parsing";
import { Protocol, ProtocolData, parseProtocol } from "./lib/parsing/utils"
import {
generateIndexHtml,
generateResolutionsHtml,
generateTranscriptsRssXml,
renderTranscriptPageHtml,
} from "./lib/rendering";
import bootstrap from "bootstrap/dist/css/bootstrap.min.css";
import { Command } from "commander"; import { Command } from "commander";
import * as Path from "path";
async function main() { async function main() {
const program = new Command(); const program = new Command();
...@@ -32,48 +43,58 @@ async function run(inputDir: string, outputDir: string, options: CliOptions) { ...@@ -32,48 +43,58 @@ async function run(inputDir: string, outputDir: string, options: CliOptions) {
throw "Too many files to be read, something is off: " + files.length; throw "Too many files to be read, something is off: " + files.length;
} }
console.info("⚙️ Parsing", files.length, "markdown files..."); console.info("⚙️ Parsing", files.length, "markdown files...");
const parseResults = await Promise.all(
files.map((file) => parseFileWithoutCrashing(file)) const protocols = await async function () {
// parse the protocols
const protocolsWithErrors = await Promise.all(
files.map(file => parseProtocol(file))
); );
const errorResults = parseResults.filter((r) => !r.ok && r.data == undefined);
if (errorResults.length > 0) { // extract errors and exit if any exist
console.error(errorResults.length, "transcripts had errors. Aborting."); const errors = protocolsWithErrors.filter(p => typeof p === "string");
if (errors.length > 0) {
console.error(errors.length, "transcripts had errors. Aborting.");
process.exit(1); process.exit(1);
} }
// (3) Generate transcript pages // there are no errors, so return as Protocol[]
return protocolsWithErrors
.map(p => p as Protocol);
}();
// Generate transcript pages
console.info("✏️ Writing individual HTML files..."); console.info("✏️ Writing individual HTML files...");
await generateAllTranscriptPages( await generateAllTranscriptPages(
outputDir, outputDir,
parseResults.map((res) => res.data), protocols.map(p => ({ data: p.data, type: p.type })),
options options
); );
// (4) Generate index page // Generate index page
console.info("✏️ Writing index file..."); console.info("✏️ Writing index file...");
await generateIndexPage( await generateIndexPage(
outputDir, outputDir,
parseResults.map((res) => res.data), protocols,
options options
); );
// (5) Generate resolution list // Generate resolution list
console.info("✏️ Writing resolutions list file..."); console.info("✏️ Writing resolutions list file...");
await generateResolutionsPage( await generateResolutionsPage(
outputDir, outputDir,
parseResults.map((res) => res.data), protocols.map(p => p.data).filter(data => data.data.resolutions !== undefined),
options options
); );
await fs.writeFile(outputDir + "/bootstrap.min.css", bootstrap, "utf8"); await fs.writeFile(outputDir + "/bootstrap.min.css", bootstrap, "utf8");
// (6) Generate JSON of all data // Generate JSON of all data
console.info("✏️ Writing Metadata as JSON file..."); console.info("✏️ Writing Metadata as JSON file...");
await fs.writeFile( await fs.writeFile(
outputDir + "/index.json", outputDir + "/index.json",
JSON.stringify( JSON.stringify(
parseResults.map((res) => ({ protocols.map(p => ({
...res.data.meta, ...p.data.data,
tags: null, tags: null,
categories: null, categories: null,
contents: "gone_with_this_generator_sorry", contents: "gone_with_this_generator_sorry",
...@@ -81,29 +102,20 @@ async function run(inputDir: string, outputDir: string, options: CliOptions) { ...@@ -81,29 +102,20 @@ async function run(inputDir: string, outputDir: string, options: CliOptions) {
) )
); );
// (7) Create RSS file // Create RSS file
console.info("✏️ Writing RSS feed index.xml..."); console.info("✏️ Writing RSS feed index.xml...");
const rssXml = generateTranscriptsRssXml( const rssXml = generateTranscriptsRssXml(
parseResults.map((res) => res.data), protocols.map(p => p.data),
options.baseUrl ?? process.env.CI_PAGES_URL options.baseUrl ?? process.env.CI_PAGES_URL
); );
await fs.writeFile(outputDir + "/index.xml", rssXml);
await fs.writeFile(outputDir + "/protokolle/index.xml", rssXml);
// (8) Be done await fs.writeFile(Path.join(outputDir, "index.xml"), rssXml);
await fs.writeFile(Path.join(outputDir, "protokolle/index.xml"), rssXml);
// Be done
console.info("✅ All done!"); console.info("✅ All done!");
} }
import fs from "node:fs/promises";
import { parse } from "./lib/parsing";
import {
generateIndexHtml,
generateResolutionsHtml,
generateTranscriptsRssXml,
renderTranscriptPageHtml,
} from "./lib/rendering";
import bootstrap from "bootstrap/dist/css/bootstrap.min.css";
async function getListOfFiles(rootPath: string | undefined): Promise<string[]> { async function getListOfFiles(rootPath: string | undefined): Promise<string[]> {
if (typeof rootPath !== "string") { if (typeof rootPath !== "string") {
throw new TypeError("Missing argument <filesDirectory>"); throw new TypeError("Missing argument <filesDirectory>");
...@@ -114,22 +126,9 @@ async function getListOfFiles(rootPath: string | undefined): Promise<string[]> { ...@@ -114,22 +126,9 @@ async function getListOfFiles(rootPath: string | undefined): Promise<string[]> {
.reverse(); .reverse();
} }
async function parseFileWithoutCrashing(
path: string
): Promise<{ ok: boolean; data: any; error: any }> {
try {
const fileContent = await fs.readFile(path, "utf-8");
const data = parse(fileContent);
return { ok: true, data: data, error: undefined };
} catch (error) {
console.log("Error for file", path, error);
return { ok: false, data: undefined, error: error };
}
}
async function generateAllTranscriptPages( async function generateAllTranscriptPages(
outputDir: string, outputDir: string,
transcripts: { meta: any; html: string; ast: any }[], transcripts: { data: ProtocolData, type: string }[],
options: CliOptions options: CliOptions
) { ) {
// make sure the dir exists and is empty // make sure the dir exists and is empty
...@@ -148,31 +147,56 @@ async function generateAllTranscriptPages( ...@@ -148,31 +147,56 @@ async function generateAllTranscriptPages(
} }
async function generateTranscriptPage( async function generateTranscriptPage(
transcript, transcript: { data: ProtocolData, type: string },
outputDir: string, outputDir: string,
options: CliOptions options: CliOptions
) { ) {
const { meta, html, ast } = transcript; const { data, html, ast } = transcript.data;
const transcriptDir = `protokolle/fsr-sitzung-${meta.number}-${meta.date}`; const type = transcript.type;
const makeDirPromise = fs.mkdir(outputDir + "/" + transcriptDir, {
recursive: true, if (type === "fsr") {
}); const transcriptDir = `protokolle/fsr-sitzung-${data.number}-${data.date}`;
const pageHtml = renderTranscriptPageHtml(html, meta);
const htmlFilePath = outputDir + "/" + transcriptDir + "/index.html"; const dir = Path.join(outputDir, transcriptDir);
await makeDirPromise; await fs.mkdir(dir, { recursive: true, });
await fs.writeFile(htmlFilePath, pageHtml, "utf-8");
await fs.writeFile(Path.join(dir, "index.html"), html, "utf-8");
const pageHtml = renderTranscriptPageHtml(html, data, "fsr");
await fs.writeFile(Path.join(dir, "index.html"), pageHtml, "utf-8");
if (options.outputAst) { if (options.outputAst) {
const astFilePath = outputDir + "/" + transcriptDir + "/ast.json"; await fs.writeFile(
await fs.writeFile(astFilePath, JSON.stringify(ast, null, 2), "utf-8"); Path.join(dir, "ast.json"),
JSON.stringify(ast, null, 2),
"utf-8"
);
}
} else {
const transcriptDir = `protokolle/fvv-sitzung-${data.date}`;
const dir = Path.join(outputDir, transcriptDir);
await fs.mkdir(dir, { recursive: true, });
const pageHtml = renderTranscriptPageHtml(html, data, "fvv");
await fs.writeFile(Path.join(dir, "index.html"), pageHtml, "utf-8");
if (options.outputAst) {
await fs.writeFile(
Path.join(dir, "ast.json"),
JSON.stringify(ast, null, 2),
"utf-8"
);
}
} }
} }
async function generateIndexPage( async function generateIndexPage(
outputDir: string, outputDir: string,
parseResults: { meta: any; html: string; ast: any }[], parseFileResults: Protocol[],
options: CliOptions options: CliOptions
) { ) {
const html = generateIndexHtml(parseResults); const html = generateIndexHtml(parseFileResults);
const htmlFilePath = outputDir + "/index.html"; const htmlFilePath = outputDir + "/index.html";
await fs.writeFile(htmlFilePath, html, "utf8"); await fs.writeFile(htmlFilePath, html, "utf8");
...@@ -180,7 +204,7 @@ async function generateIndexPage( ...@@ -180,7 +204,7 @@ async function generateIndexPage(
async function generateResolutionsPage( async function generateResolutionsPage(
outputDir: string, outputDir: string,
parseResults: { meta: any; html: string; ast: any }[], parseResults: ProtocolData[],
options: CliOptions options: CliOptions
) { ) {
const html = generateResolutionsHtml(parseResults); const html = generateResolutionsHtml(parseResults);
...@@ -189,3 +213,5 @@ async function generateResolutionsPage( ...@@ -189,3 +213,5 @@ async function generateResolutionsPage(
const htmlFilePath = outputDir + "/resolutions/index.html"; const htmlFilePath = outputDir + "/resolutions/index.html";
await fs.writeFile(htmlFilePath, html, "utf8"); await fs.writeFile(htmlFilePath, html, "utf8");
} }
export { Protocol as ParseFileResult };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment