chore: improve manifest generator (#125)
Make the manifest generator output nicer: - routes are sorted - manifest is sent through `deno fmt`
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// std
|
||||
export {
|
||||
extname,
|
||||
join,
|
||||
resolve,
|
||||
toFileUrl,
|
||||
|
||||
+28
-17
@@ -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",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user