From e57e9c48f11fd6a2e038ba7cb50b4299bbad4da0 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Tue, 8 Mar 2022 00:24:10 +0100 Subject: [PATCH] chore: improve manifest generator (#125) Make the manifest generator output nicer: - routes are sorted - manifest is sent through `deno fmt` --- examples/counter/fresh.gen.ts | 2 +- src/cli/deps.ts | 1 + src/cli/manifest.ts | 45 ++++++++++++++-------- tests/fixture/fresh.gen.ts | 66 ++++++++++++++++---------------- tests/fixture_error/fresh.gen.ts | 2 +- www/fresh.gen.ts | 10 ++--- 6 files changed, 69 insertions(+), 57 deletions(-) diff --git a/examples/counter/fresh.gen.ts b/examples/counter/fresh.gen.ts index cac718f5..cb4c5d8b 100644 --- a/examples/counter/fresh.gen.ts +++ b/examples/counter/fresh.gen.ts @@ -1,6 +1,6 @@ // 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`. +// To update this file, run `fresh manifest`. import * as $0 from "./routes/index.tsx"; import * as $$0 from "./islands/Counter.tsx"; diff --git a/src/cli/deps.ts b/src/cli/deps.ts index 1dcead72..5691ce44 100644 --- a/src/cli/deps.ts +++ b/src/cli/deps.ts @@ -1,5 +1,6 @@ // std export { + extname, join, resolve, toFileUrl, diff --git a/src/cli/manifest.ts b/src/cli/manifest.ts index 23d5ffea..374470b0 100644 --- a/src/cli/manifest.ts +++ b/src/cli/manifest.ts @@ -1,4 +1,5 @@ -import { join, resolve, toFileUrl, walk } from "./deps.ts"; +import { extname, join, resolve, toFileUrl, walk } from "./deps.ts"; +import { error } from "./error.ts"; const help = `fresh manifest @@ -67,26 +68,23 @@ export async function manifest(directory: string) { throw err; } } + routes.sort(); const islands = []; try { const islandsDir = join(directory, "./islands"); const islandsUrl = toFileUrl(islandsDir); - // TODO(lucacasonato): remove the extranious Deno.readDir when - // https://github.com/denoland/deno_std/issues/1310 is fixed. - for await (const _ of Deno.readDir(islandsDir)) { - // do nothing - } - const islandsFolder = walk(islandsDir, { - includeDirs: false, - includeFiles: true, - exts: ["tsx", "jsx", "ts", "js"], - }); - for await (const entry of islandsFolder) { - if (entry.isFile) { - const file = toFileUrl(entry.path).href.substring( - islandsUrl.href.length, + for await (const entry of Deno.readDir(islandsDir)) { + if (entry.isDirectory) { + error( + `Found subdirectory '${entry.name}' in islands/. The islands/ folder must not contain any subdirectories.`, ); + } + if (entry.isFile) { + const ext = extname(entry.name); + if (![".tsx", ".jsx", ".ts", ".js"].includes(ext)) continue; + const path = join(islandsDir, entry.name); + const file = toFileUrl(path).href.substring(islandsUrl.href.length); islands.push(file); } } @@ -97,10 +95,11 @@ export async function manifest(directory: string) { throw err; } } + islands.sort(); 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\`. +// To update this file, run \`fresh manifest\`. ${ routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join( @@ -133,5 +132,17 @@ export default manifest; const manifestPath = join(directory, "./fresh.gen.ts"); await Deno.writeTextFile(manifestPath, output); - console.log("Manifest generated and written to", manifestPath); + const proc = Deno.run({ + cmd: [Deno.execPath(), "fmt", manifestPath], + stdin: "null", + stdout: "null", + stderr: "null", + }); + await proc.status(); + proc.close(); + + console.log( + `%cThe manifest has been generated for ${routes.length} routes and ${islands.length} islands.`, + "color: blue; font-weight: bold", + ); } diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 79364b54..14117582 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -1,43 +1,43 @@ // 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`. +// To update this file, run `fresh manifest`. -import * as $0 from "./routes/index.tsx"; -import * as $1 from "./routes/params.tsx"; -import * as $2 from "./routes/static.tsx"; -import * as $3 from "./routes/intercept_args.tsx"; -import * as $4 from "./routes/_render.ts"; -import * as $5 from "./routes/props/[id].tsx"; -import * as $6 from "./routes/failure.ts"; -import * as $7 from "./routes/_middleware.ts"; -import * as $8 from "./routes/books/[id].tsx"; -import * as $9 from "./routes/wildcard.tsx"; -import * as $10 from "./routes/api/get_only.ts"; -import * as $11 from "./routes/_500.tsx"; -import * as $12 from "./routes/intercept.tsx"; -import * as $13 from "./routes/_404.tsx"; -import * as $14 from "./routes/[name].tsx"; -import * as $15 from "./routes/_app.tsx"; +import * as $0 from "./routes/[name].tsx"; +import * as $1 from "./routes/_404.tsx"; +import * as $2 from "./routes/_500.tsx"; +import * as $3 from "./routes/_app.tsx"; +import * as $4 from "./routes/_middleware.ts"; +import * as $5 from "./routes/_render.ts"; +import * as $6 from "./routes/api/get_only.ts"; +import * as $7 from "./routes/books/[id].tsx"; +import * as $8 from "./routes/failure.ts"; +import * as $9 from "./routes/index.tsx"; +import * as $10 from "./routes/intercept.tsx"; +import * as $11 from "./routes/intercept_args.tsx"; +import * as $12 from "./routes/params.tsx"; +import * as $13 from "./routes/props/[id].tsx"; +import * as $14 from "./routes/static.tsx"; +import * as $15 from "./routes/wildcard.tsx"; import * as $$0 from "./islands/Test.tsx"; const manifest = { routes: { - "./routes/index.tsx": $0, - "./routes/params.tsx": $1, - "./routes/static.tsx": $2, - "./routes/intercept_args.tsx": $3, - "./routes/_render.ts": $4, - "./routes/props/[id].tsx": $5, - "./routes/failure.ts": $6, - "./routes/_middleware.ts": $7, - "./routes/books/[id].tsx": $8, - "./routes/wildcard.tsx": $9, - "./routes/api/get_only.ts": $10, - "./routes/_500.tsx": $11, - "./routes/intercept.tsx": $12, - "./routes/_404.tsx": $13, - "./routes/[name].tsx": $14, - "./routes/_app.tsx": $15, + "./routes/[name].tsx": $0, + "./routes/_404.tsx": $1, + "./routes/_500.tsx": $2, + "./routes/_app.tsx": $3, + "./routes/_middleware.ts": $4, + "./routes/_render.ts": $5, + "./routes/api/get_only.ts": $6, + "./routes/books/[id].tsx": $7, + "./routes/failure.ts": $8, + "./routes/index.tsx": $9, + "./routes/intercept.tsx": $10, + "./routes/intercept_args.tsx": $11, + "./routes/params.tsx": $12, + "./routes/props/[id].tsx": $13, + "./routes/static.tsx": $14, + "./routes/wildcard.tsx": $15, }, islands: { "./islands/Test.tsx": $$0, diff --git a/tests/fixture_error/fresh.gen.ts b/tests/fixture_error/fresh.gen.ts index 3651310e..602e65c5 100644 --- a/tests/fixture_error/fresh.gen.ts +++ b/tests/fixture_error/fresh.gen.ts @@ -1,6 +1,6 @@ // 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`. +// To update this file, run `fresh manifest`. import * as $0 from "./routes/index.tsx"; diff --git a/www/fresh.gen.ts b/www/fresh.gen.ts index cc82899f..0876344f 100644 --- a/www/fresh.gen.ts +++ b/www/fresh.gen.ts @@ -1,18 +1,18 @@ // 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`. +// To update this file, run `fresh manifest`. import * as $0 from "./routes/_render.ts"; -import * as $1 from "./routes/gfm.css.ts"; -import * as $2 from "./routes/docs/[...slug].tsx"; +import * as $1 from "./routes/docs/[...slug].tsx"; +import * as $2 from "./routes/gfm.css.ts"; import * as $3 from "./routes/index.tsx"; import * as $$0 from "./islands/Counter.tsx"; const manifest = { routes: { "./routes/_render.ts": $0, - "./routes/gfm.css.ts": $1, - "./routes/docs/[...slug].tsx": $2, + "./routes/docs/[...slug].tsx": $1, + "./routes/gfm.css.ts": $2, "./routes/index.tsx": $3, }, islands: {