winterspec
Version:
Write Winter-CG compatible routes with filesystem routing and tons of features
54 lines (51 loc) • 2.07 kB
JavaScript
import { Command, Option } from "clipanion";
import path from "node:path";
import { join } from "node:path/posix";
import fs from "fs/promises";
import { createRoutePathMapFromDirectory } from "../../routes/create-route-map-from-directory.js";
export class BundleRoutesCommand extends Command {
constructor() {
super(...arguments);
this.routesDirectory = Option.String("-i,--routes-directory", {
required: true,
description: "Directory containing the route files",
});
this.output = Option.String("-o,--output", {
required: false,
description: "Output file path for the bundled routes (usually dist/static-routes.js)",
});
}
async execute() {
if (!this.output) {
this.output = "dist/static-routes.js";
}
const absoluteRoutesDir = path.resolve(process.cwd(), this.routesDirectory);
const absoluteOutputFile = path.resolve(process.cwd(), this.output);
const relativeOutputDir = path.dirname(this.output);
// Generate the route map
const routeMap = await createRoutePathMapFromDirectory(absoluteRoutesDir);
// Generate the output file content
const outputContent = `
// import { WinterSpecRouteMap } from "@winterspec/types"
const routeMap = {
${Object.entries(routeMap)
.map(([route, { relativePath }]) => `"${route}": (await import('./${join(relativeOutputDir, this.routesDirectory, relativePath)}')).default`)
.join(",\n ")}
}
export default routeMap
`;
// Write the output file
await fs.writeFile(absoluteOutputFile, outputContent, "utf-8");
this.context.stdout.write(`Routes bundled successfully to ${this.output}\n`);
}
}
BundleRoutesCommand.paths = [["bundle-routes"]];
BundleRoutesCommand.usage = Command.Usage({
description: "Bundle routes into a single file",
examples: [
[
"Bundle routes",
"winterspec bundle-routes --routes-directory ./api --output bundle.js",
],
],
});