feat: add fresh manifest --watch (#201)
This commit is contained in:
@@ -22,10 +22,12 @@ const args = parseArgs(Deno.args, {
|
||||
alias: {
|
||||
"help": "h",
|
||||
"version": "V",
|
||||
"watch": "w",
|
||||
},
|
||||
boolean: [
|
||||
"help",
|
||||
"version",
|
||||
"watch",
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
+3
-3
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
+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