Restructured toolchain to its own workspace
This commit is contained in:
102
packages/@zblog/toolchain/src/zblog-toolchain.ts
Normal file
102
packages/@zblog/toolchain/src/zblog-toolchain.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import * as fs from "fs";
|
||||
import { toHtml } from "./markdown.js";
|
||||
|
||||
export type Paths = {
|
||||
outputDirectory: string;
|
||||
notes: {
|
||||
rootPath: string;
|
||||
assetsDirectoryName: string;
|
||||
noteFileName: string;
|
||||
};
|
||||
templates: {
|
||||
rootPath: string;
|
||||
noteTemplateName: string;
|
||||
indexTemplateName: string;
|
||||
notFoundName: string;
|
||||
};
|
||||
};
|
||||
|
||||
export class ToolchainBuilder {
|
||||
private paths: Paths;
|
||||
constructor(paths: Paths) {
|
||||
this.paths = paths;
|
||||
}
|
||||
|
||||
build() {
|
||||
if (fs.readdirSync(".").includes(this.paths.outputDirectory)) {
|
||||
fs.rmSync(this.paths.outputDirectory, { recursive: true, force: true });
|
||||
console.log("Starting rebuild");
|
||||
} else {
|
||||
console.log("Starting build");
|
||||
}
|
||||
fs.mkdirSync(this.paths.outputDirectory);
|
||||
console.log("Indexing content..");
|
||||
const directoryNames = fs.readdirSync(this.paths.notes.rootPath);
|
||||
const manifest = directoryNames.map((noteDirectory) => ({
|
||||
directoryName: noteDirectory,
|
||||
name: noteDirectory,
|
||||
markdown: fs.readFileSync(
|
||||
`${this.paths.notes.rootPath}/${noteDirectory}/${this.paths.notes.noteFileName}`,
|
||||
{ encoding: "utf-8" }
|
||||
),
|
||||
assetDirectoryPath: `${this.paths.notes.rootPath}/${noteDirectory}/${this.paths.notes.assetsDirectoryName}`,
|
||||
publicAssetDirectoryPath: `/${noteDirectory}_`,
|
||||
}));
|
||||
console.log("Parsing content..");
|
||||
manifest.forEach((m) => {
|
||||
const notePath = `${this.paths.templates.rootPath}/${this.paths.templates.noteTemplateName}`;
|
||||
let htmlTemplate = fs.readFileSync(notePath, {
|
||||
encoding: "utf-8",
|
||||
});
|
||||
htmlTemplate = htmlTemplate.replace(
|
||||
"{{markdown}}",
|
||||
toHtml(m.markdown, m.publicAssetDirectoryPath)
|
||||
);
|
||||
fs.writeFileSync(
|
||||
`${this.paths.outputDirectory}/${m.directoryName}.html`,
|
||||
htmlTemplate,
|
||||
{
|
||||
encoding: "utf-8",
|
||||
flag: "ax",
|
||||
}
|
||||
);
|
||||
if (!fs.existsSync(m.assetDirectoryPath)) return;
|
||||
const assetsList = fs.readdirSync(m.assetDirectoryPath);
|
||||
assetsList.forEach((assetName) =>
|
||||
fs.cpSync(
|
||||
`${m.assetDirectoryPath}/${assetName}`,
|
||||
`${this.paths.outputDirectory}/${m.name}_${assetName}`
|
||||
)
|
||||
);
|
||||
});
|
||||
console.log("Moving static templates..");
|
||||
[this.paths.templates.notFoundName].forEach((filename) => {
|
||||
fs.copyFileSync(
|
||||
`${this.paths.templates.rootPath}/${filename}`,
|
||||
`${this.paths.outputDirectory}/${filename}`
|
||||
);
|
||||
});
|
||||
console.log("Building startpage..");
|
||||
let htmlTemplate = fs.readFileSync(
|
||||
`${this.paths.templates.rootPath}/${this.paths.templates.indexTemplateName}`,
|
||||
{
|
||||
encoding: "utf-8",
|
||||
}
|
||||
);
|
||||
const links = manifest
|
||||
.map((m) => `<a href='/${m.directoryName}.html'>${m.name}</a>`)
|
||||
.reverse();
|
||||
const unorderedListItems = links.map((l) => `<li>${l}</li>`).join("\r\n");
|
||||
const html = `
|
||||
<ul>
|
||||
${unorderedListItems}
|
||||
<ul>
|
||||
`;
|
||||
htmlTemplate = htmlTemplate.replace("{{content}}", html);
|
||||
fs.writeFileSync(`${this.paths.outputDirectory}/index.html`, htmlTemplate, {
|
||||
encoding: "utf-8",
|
||||
flag: "ax",
|
||||
});
|
||||
console.log("Done");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user