make cli better

This commit is contained in:
Luca Casonato
2021-05-08 03:49:31 +02:00
parent 9145711e2e
commit 1f756a6d33
6 changed files with 247 additions and 25 deletions
+34
View File
@@ -0,0 +1,34 @@
import { join, toFileUrl, walk } from "./deps.ts";
export async function routesSubcommand() {
const files = [];
const pagesDir = join(Deno.cwd(), "./pages");
const pagesUrl = new URL(pagesDir, "file:///");
const folder = walk(pagesDir, {
includeDirs: false,
includeFiles: true,
exts: ["tsx", "jsx"],
});
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\`.
import { setup } from "${new URL("../../server.ts", import.meta.url)}";
${files.map((file, i) => `import * as $${i} from "./pages${file}";`).join("\n")}
setup([${
files.map((file, i) => `[$${i}, "./pages${file}"]`)
.join(", ")
}], import.meta.url);
`;
await Deno.writeTextFile("./server.ts", output);
}