feat: add fresh manifest --watch (#201)
This commit is contained in:
+64
-10
@@ -16,16 +16,19 @@ USAGE:
|
||||
|
||||
OPTIONS:
|
||||
-h, --help Prints help information
|
||||
-w, --watch Watch for changes and regenerate as needed
|
||||
`;
|
||||
|
||||
export interface Args {
|
||||
help: boolean;
|
||||
watch: boolean;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export async function manifestSubcommand(rawArgs: Record<string, any>) {
|
||||
const args: Args = {
|
||||
help: !!rawArgs.help,
|
||||
watch: !!rawArgs.watch,
|
||||
};
|
||||
const directory: string | null = typeof rawArgs._[0] === "string"
|
||||
? rawArgs._[0]
|
||||
@@ -34,14 +37,20 @@ export async function manifestSubcommand(rawArgs: Record<string, any>) {
|
||||
console.log(help);
|
||||
Deno.exit(0);
|
||||
}
|
||||
await manifest(directory);
|
||||
await manifest(directory, args.watch);
|
||||
}
|
||||
|
||||
export async function manifest(directory: string) {
|
||||
directory = resolve(directory);
|
||||
interface Manifest {
|
||||
routes: string[];
|
||||
islands: string[];
|
||||
}
|
||||
|
||||
export async function collect(directory: string): Promise<Manifest> {
|
||||
const routesDir = join(directory, "./routes");
|
||||
const islandsDir = join(directory, "./islands");
|
||||
|
||||
const routes = [];
|
||||
try {
|
||||
const routesDir = join(directory, "./routes");
|
||||
const routesUrl = toFileUrl(routesDir);
|
||||
// TODO(lucacasonato): remove the extranious Deno.readDir when
|
||||
// https://github.com/denoland/deno_std/issues/1310 is fixed.
|
||||
@@ -72,7 +81,6 @@ export async function manifest(directory: string) {
|
||||
|
||||
const islands = [];
|
||||
try {
|
||||
const islandsDir = join(directory, "./islands");
|
||||
const islandsUrl = toFileUrl(islandsDir);
|
||||
for await (const entry of Deno.readDir(islandsDir)) {
|
||||
if (entry.isDirectory) {
|
||||
@@ -97,6 +105,12 @@ export async function manifest(directory: string) {
|
||||
}
|
||||
islands.sort();
|
||||
|
||||
return { routes, islands };
|
||||
}
|
||||
|
||||
export async function generate(directory: string, manifest: Manifest) {
|
||||
const { routes, islands } = manifest;
|
||||
|
||||
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 manifest\`.
|
||||
@@ -130,19 +144,59 @@ const manifest = {
|
||||
export default manifest;
|
||||
`;
|
||||
|
||||
const manifestPath = join(directory, "./fresh.gen.ts");
|
||||
await Deno.writeTextFile(manifestPath, output);
|
||||
const proc = Deno.run({
|
||||
cmd: [Deno.execPath(), "fmt", manifestPath],
|
||||
stdin: "null",
|
||||
stdout: "null",
|
||||
cmd: [Deno.execPath(), "fmt", "-"],
|
||||
stdin: "piped",
|
||||
stdout: "piped",
|
||||
stderr: "null",
|
||||
});
|
||||
const raw = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode(output));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
await raw.pipeTo(proc.stdin.writable);
|
||||
const out = await proc.output();
|
||||
await proc.status();
|
||||
proc.close();
|
||||
|
||||
const manifestStr = new TextDecoder().decode(out);
|
||||
const manifestPath = join(directory, "./fresh.gen.ts");
|
||||
|
||||
await Deno.writeTextFile(manifestPath, manifestStr);
|
||||
console.log(
|
||||
`%cThe manifest has been generated for ${routes.length} routes and ${islands.length} islands.`,
|
||||
"color: blue; font-weight: bold",
|
||||
);
|
||||
}
|
||||
|
||||
export async function manifest(directory: string, watch: boolean) {
|
||||
directory = resolve(directory);
|
||||
|
||||
let currentManifest = await collect(directory);
|
||||
await generate(directory, currentManifest);
|
||||
|
||||
if (!watch) return;
|
||||
|
||||
const watcher = Deno.watchFs(directory, { recursive: true });
|
||||
for await (const _ of watcher) {
|
||||
const newManifest = await collect(directory);
|
||||
// check if the manifests are identical
|
||||
const routesSet = new Set([
|
||||
...currentManifest.routes,
|
||||
...newManifest.routes,
|
||||
]);
|
||||
const islandsSet = new Set([
|
||||
...currentManifest.islands,
|
||||
...newManifest.islands,
|
||||
]);
|
||||
if (
|
||||
routesSet.size !== newManifest.routes.length ||
|
||||
islandsSet.size !== newManifest.islands.length
|
||||
) {
|
||||
currentManifest = newManifest;
|
||||
await generate(directory, currentManifest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user