diff --git a/cli.ts b/cli.ts index b8fd20fb..d2b35871 100644 --- a/cli.ts +++ b/cli.ts @@ -22,10 +22,12 @@ const args = parseArgs(Deno.args, { alias: { "help": "h", "version": "V", + "watch": "w", }, boolean: [ "help", "version", + "watch", ], }); diff --git a/deno.jsonc b/deno.jsonc index d2149874..107e4b29 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,7 +1,7 @@ { "tasks": { - "test": "deno test -A --no-check=remote --watch", - "fixture": "deno run -A --watch ./tests/fixture/main.ts", - "doc": "deno run -A --watch=www/static,src/,docs/ www/main.ts" + "test": "deno test -A --no-check=remote && deno check www/main.ts && deno check examples/counter/main.ts", + "fixture": "deno run -A --watch ./tests/fixture/main.ts & deno run -A ./cli.ts manifest --watch ./tests/fixture", + "www": "deno run -A --watch=www/static,docs/ ./www/main.ts & deno run -A ./cli.ts manifest --watch ./www" } } diff --git a/examples/counter/deno.json b/examples/counter/deno.json index d25f7104..abc9a70f 100644 --- a/examples/counter/deno.json +++ b/examples/counter/deno.json @@ -1,5 +1,5 @@ { "tasks": { - "start": "deno run -A --watch main.ts" + "start": "deno run -A --watch main.ts & deno run -A ../../cli.ts manifest --watch" } } diff --git a/makefile b/makefile deleted file mode 100644 index ec3c2e03..00000000 --- a/makefile +++ /dev/null @@ -1,13 +0,0 @@ -fmt: - deno fmt - -lint: - deno lint - -test: - deno test -A --no-check=remote - deno cache --no-check=remote www/main.ts - deno cache --no-check=remote examples/counter/main.ts - -dev: - deno run -A --watch=www/static,src/,docs/ --no-check www/main.ts \ No newline at end of file diff --git a/src/cli/manifest.ts b/src/cli/manifest.ts index 374470b0..ed714211 100644 --- a/src/cli/manifest.ts +++ b/src/cli/manifest.ts @@ -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) { 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) { 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 { + 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); + } + } +}