Re-organise cli.ts into functions and sub-commands
Add the necessary boilerplate for cli.ts to provide more than one command, with the resource gathering step split out from the build step in order for it to be re-usable.
This commit is contained in:
parent
1efd21a4a0
commit
f708088ef7
2 changed files with 62 additions and 17 deletions
71
cli.ts
71
cli.ts
|
@ -19,22 +19,67 @@ function pageToHtml(page: Page) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const outDir = "build/web";
|
function assembleResources() {
|
||||||
if (!fs.existsSync(outDir)) {
|
const webDir = "public";
|
||||||
fs.mkdirSync(outDir);
|
const resources = new Map<string, string | Page>();
|
||||||
|
for (const entry of fs.readdirSync(webDir, { recursive: true, withFileTypes: true })) {
|
||||||
|
if (!entry.isFile())
|
||||||
|
continue;
|
||||||
|
const parentPath = entry.parentPath.replace(/\\/g, "/");
|
||||||
|
const ref = `${parentPath.slice(webDir.length)}/${entry.name}`;
|
||||||
|
resources.set(ref, `${parentPath}/${entry.name}`);
|
||||||
|
}
|
||||||
|
for (const page of pages) {
|
||||||
|
if (resources.has(page.ref)) {
|
||||||
|
const existing = resources.get(page.ref)!;
|
||||||
|
const other = typeof existing === "string" ? "a static resource" : `"${existing.title}"`;
|
||||||
|
throw new Error(`ref "${page.ref}" is taken up by both "${page.title}" and ${other}`)
|
||||||
|
}
|
||||||
|
resources.set(page.ref, page);
|
||||||
|
}
|
||||||
|
return resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const page of pages) {
|
function build() {
|
||||||
const dirSep = page.ref.indexOf("/", 1);
|
const outDir = "build/web";
|
||||||
if (dirSep !== -1) {
|
const dirsCreated = new Set<string>()
|
||||||
const dir = `${outDir}/${page.ref.slice(0, dirSep)}`;
|
for (const [ref, resource] of assembleResources()) {
|
||||||
if (!fs.existsSync(dir)) {
|
const refDir = `${outDir}${posix.dirname(ref)}`;
|
||||||
fs.mkdirSync(dir);
|
if (!dirsCreated.has(refDir)) {
|
||||||
|
fs.mkdirSync(refDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
console.log(`writing ${outDir}${ref}`);
|
||||||
|
let content;
|
||||||
|
if (typeof resource === "string") {
|
||||||
|
content = fs.readFileSync(resource);
|
||||||
|
} else {
|
||||||
|
content = pageToHtml(resource);
|
||||||
|
}
|
||||||
|
fs.writeFileSync(`${outDir}${ref}`, content);
|
||||||
}
|
}
|
||||||
console.log(`writing ${outDir}${page.ref}`);
|
|
||||||
fs.writeFileSync(`${outDir}${page.ref}`, pageToHtml(page));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`writing ${outDir}/style.css`);
|
function printUsage() {
|
||||||
fs.writeFileSync(`${outDir}/style.css`, fs.readFileSync("public/style.css"));
|
console.log("Usage: cli.js <cmd>");
|
||||||
|
console.log(" build - Copy resources and generated pages to build directory.");
|
||||||
|
}
|
||||||
|
|
||||||
|
function main(runtime: string, script: string, args: string[]) {
|
||||||
|
if (!args.length) {
|
||||||
|
printUsage();
|
||||||
|
process.exitCode = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [command, ...commandArgs] = args;
|
||||||
|
if (command === "build") {
|
||||||
|
build();
|
||||||
|
} else {
|
||||||
|
console.log(`Error: Unkown sub-command ${command}`);
|
||||||
|
printUsage();
|
||||||
|
process.exitCode = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const [runtime, script, ...args] = process.argv;
|
||||||
|
main(runtime, script, args)
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
"main": "build/node/index.js",
|
"main": "build/node/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "tsc",
|
"prepare": "tsc",
|
||||||
"build": "node --enable-source-maps build/node/cli.js",
|
"build": "node --enable-source-maps build/node/cli.js build",
|
||||||
"test": "node --test --enable-source-maps --experimental-test-coverage"
|
"test": "node --test --enable-source-maps --experimental-test-coverage"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue