Add posts to latest story

This commit is contained in:
Bad Manners 2024-08-08 09:49:11 -03:00
parent 7bb8a952ef
commit 6dd8a92318
8 changed files with 51 additions and 31 deletions

View file

@ -51,7 +51,7 @@ const isLibreOfficeRunning = async () =>
async function exportStory(slug: string, options: { outputDir: string }) {
/* Check that LibreOffice is not running */
if (await isLibreOfficeRunning()) {
console.error("LibreOffice cannot be open while this command is running!");
console.error("ERROR: LibreOffice cannot be open while this command is running!");
process.exit(1);
}
/* Check that outputDir is valid */
@ -63,8 +63,8 @@ async function exportStory(slug: string, options: { outputDir: string }) {
files = [];
console.log(`Created directory at ${await mkdir(outputDir, { recursive: true })}`);
}
if (files.length > 1) {
console.error(`Directory ${outputDir} is not empty!`);
if (files.length > 0) {
console.error(`ERROR: Directory ${outputDir} is not empty!`);
process.exit(1);
}
/* Check if Astro development server needs to be spawned */
@ -94,8 +94,8 @@ async function exportStory(slug: string, options: { outputDir: string }) {
throw new Error();
}
} catch {
console.error("Astro dev server didn't respond in time!");
devServerProcess && devServerProcess.kill("SIGINT");
console.error("ERROR: Astro dev server didn't respond in time!");
devServerProcess && devServerProcess.kill();
devServerProcess = null;
process.exit(1);
}
@ -138,8 +138,8 @@ async function exportStory(slug: string, options: { outputDir: string }) {
} finally {
if (devServerProcess) {
console.log("Shutting down the Astro development server...");
if (!devServerProcess.kill("SIGINT")) {
console.error("Unable to shut down Astro dev server!");
if (!devServerProcess.kill("SIGTERM")) {
console.error("WARNING: Unable to shut down Astro dev server!");
}
devServerProcess = null;
}
@ -150,13 +150,11 @@ async function exportStory(slug: string, options: { outputDir: string }) {
await writeFile(pathJoin(outputDir, `${slug}.md`), storyText.replaceAll(/=(?==)/g, "= ").replaceAll("*", "\\*"));
const tempDir = await mkdtemp(pathJoin(tmpdir(), "export-story-"));
await writeFile(pathJoin(tempDir, "temp.txt"), storyText.replaceAll(/\n\n+/g, "\n"));
spawnSync("libreoffice", [
"--convert-to",
"rtf:Rich Text Format",
"--outdir",
tempDir,
pathJoin(tempDir, "temp.txt"),
]);
spawnSync(
"libreoffice",
["--convert-to", "rtf:Rich Text Format", "--outdir", tempDir, pathJoin(tempDir, "temp.txt")],
{ stdio: "ignore" },
);
const rtfText = await readFile(pathJoin(tempDir, "temp.rtf"), "utf-8");
const rtfStyles = getRTFStyles(rtfText);
await writeFile(
@ -164,6 +162,7 @@ async function exportStory(slug: string, options: { outputDir: string }) {
rtfText.replaceAll(rtfStyles["Preformatted Text"], rtfStyles["Normal"]),
);
console.log("Success!");
process.exit(0);
}
await program