79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import { join, resolve, toFileUrl, walk } from "./deps.ts";
|
|
|
|
const help = `fresh routes
|
|
|
|
Regenerate the route mapping for your fresh project.
|
|
|
|
To regenerate the mapping in the current directory:
|
|
fresh routes
|
|
|
|
To regenerate the mapping in the './foobar' subdirectory:
|
|
fresh routes ./foobar
|
|
|
|
USAGE:
|
|
fresh routes [OPTIONS] [DIRECTORY]
|
|
|
|
OPTIONS:
|
|
-h, --help Prints help information
|
|
`;
|
|
|
|
export interface Args {
|
|
help: boolean;
|
|
}
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
export async function routesSubcommand(rawArgs: Record<string, any>) {
|
|
const args: Args = {
|
|
help: !!rawArgs.help,
|
|
};
|
|
const directory: string | null = typeof rawArgs._[0] === "string"
|
|
? rawArgs._[0]
|
|
: Deno.cwd();
|
|
if (args.help) {
|
|
console.log(help);
|
|
Deno.exit(0);
|
|
}
|
|
await routes(directory);
|
|
}
|
|
|
|
export async function routes(directory: string) {
|
|
directory = resolve(directory);
|
|
const files = [];
|
|
const pagesDir = join(directory, "./pages");
|
|
const pagesUrl = toFileUrl(pagesDir);
|
|
const folder = walk(pagesDir, {
|
|
includeDirs: false,
|
|
includeFiles: true,
|
|
exts: ["tsx", "jsx", "ts", "js"],
|
|
});
|
|
for await (const entry of folder) {
|
|
if (entry.isFile) {
|
|
const file = toFileUrl(entry.path).href.substring(pagesUrl.href.length);
|
|
files.push(file);
|
|
}
|
|
}
|
|
|
|
const output = `// DO NOT EDIT. This file is generated by \`fresh\`.
|
|
// This file SHOULD be checked into source version control.
|
|
// To update this file, run \`fresh routes\`.
|
|
|
|
${files.map((file, i) => `import * as $${i} from "./pages${file}";`).join("\n")}
|
|
|
|
const routes = {
|
|
pages: {
|
|
${
|
|
files.map((file, i) => `${JSON.stringify(`./pages${file}`)}: $${i},`)
|
|
.join("\n ")
|
|
}
|
|
},
|
|
baseUrl: import.meta.url,
|
|
};
|
|
|
|
export default routes;
|
|
`;
|
|
|
|
const routeManifestPath = join(directory, "./routes.gen.ts");
|
|
await Deno.writeTextFile(routeManifestPath, output);
|
|
console.log("Route manifest generated and written to", routeManifestPath);
|
|
}
|