feat: add fresh manifest --watch (#201)
This commit is contained in:
@@ -22,10 +22,12 @@ const args = parseArgs(Deno.args, {
|
|||||||
alias: {
|
alias: {
|
||||||
"help": "h",
|
"help": "h",
|
||||||
"version": "V",
|
"version": "V",
|
||||||
|
"watch": "w",
|
||||||
},
|
},
|
||||||
boolean: [
|
boolean: [
|
||||||
"help",
|
"help",
|
||||||
"version",
|
"version",
|
||||||
|
"watch",
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"test": "deno test -A --no-check=remote --watch",
|
"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",
|
"fixture": "deno run -A --watch ./tests/fixture/main.ts & deno run -A ./cli.ts manifest --watch ./tests/fixture",
|
||||||
"doc": "deno run -A --watch=www/static,src/,docs/ www/main.ts"
|
"www": "deno run -A --watch=www/static,docs/ ./www/main.ts & deno run -A ./cli.ts manifest --watch ./www"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"tasks": {
|
"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:
|
OPTIONS:
|
||||||
-h, --help Prints help information
|
-h, --help Prints help information
|
||||||
|
-w, --watch Watch for changes and regenerate as needed
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export interface Args {
|
export interface Args {
|
||||||
help: boolean;
|
help: boolean;
|
||||||
|
watch: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
export async function manifestSubcommand(rawArgs: Record<string, any>) {
|
export async function manifestSubcommand(rawArgs: Record<string, any>) {
|
||||||
const args: Args = {
|
const args: Args = {
|
||||||
help: !!rawArgs.help,
|
help: !!rawArgs.help,
|
||||||
|
watch: !!rawArgs.watch,
|
||||||
};
|
};
|
||||||
const directory: string | null = typeof rawArgs._[0] === "string"
|
const directory: string | null = typeof rawArgs._[0] === "string"
|
||||||
? rawArgs._[0]
|
? rawArgs._[0]
|
||||||
@@ -34,14 +37,20 @@ export async function manifestSubcommand(rawArgs: Record<string, any>) {
|
|||||||
console.log(help);
|
console.log(help);
|
||||||
Deno.exit(0);
|
Deno.exit(0);
|
||||||
}
|
}
|
||||||
await manifest(directory);
|
await manifest(directory, args.watch);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function manifest(directory: string) {
|
interface Manifest {
|
||||||
directory = resolve(directory);
|
routes: string[];
|
||||||
|
islands: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function collect(directory: string): Promise<Manifest> {
|
||||||
|
const routesDir = join(directory, "./routes");
|
||||||
|
const islandsDir = join(directory, "./islands");
|
||||||
|
|
||||||
const routes = [];
|
const routes = [];
|
||||||
try {
|
try {
|
||||||
const routesDir = join(directory, "./routes");
|
|
||||||
const routesUrl = toFileUrl(routesDir);
|
const routesUrl = toFileUrl(routesDir);
|
||||||
// TODO(lucacasonato): remove the extranious Deno.readDir when
|
// TODO(lucacasonato): remove the extranious Deno.readDir when
|
||||||
// https://github.com/denoland/deno_std/issues/1310 is fixed.
|
// https://github.com/denoland/deno_std/issues/1310 is fixed.
|
||||||
@@ -72,7 +81,6 @@ export async function manifest(directory: string) {
|
|||||||
|
|
||||||
const islands = [];
|
const islands = [];
|
||||||
try {
|
try {
|
||||||
const islandsDir = join(directory, "./islands");
|
|
||||||
const islandsUrl = toFileUrl(islandsDir);
|
const islandsUrl = toFileUrl(islandsDir);
|
||||||
for await (const entry of Deno.readDir(islandsDir)) {
|
for await (const entry of Deno.readDir(islandsDir)) {
|
||||||
if (entry.isDirectory) {
|
if (entry.isDirectory) {
|
||||||
@@ -97,6 +105,12 @@ export async function manifest(directory: string) {
|
|||||||
}
|
}
|
||||||
islands.sort();
|
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\`.
|
const output = `// DO NOT EDIT. This file is generated by \`fresh\`.
|
||||||
// This file SHOULD be checked into source version control.
|
// This file SHOULD be checked into source version control.
|
||||||
// To update this file, run \`fresh manifest\`.
|
// To update this file, run \`fresh manifest\`.
|
||||||
@@ -130,19 +144,59 @@ const manifest = {
|
|||||||
export default manifest;
|
export default manifest;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const manifestPath = join(directory, "./fresh.gen.ts");
|
|
||||||
await Deno.writeTextFile(manifestPath, output);
|
|
||||||
const proc = Deno.run({
|
const proc = Deno.run({
|
||||||
cmd: [Deno.execPath(), "fmt", manifestPath],
|
cmd: [Deno.execPath(), "fmt", "-"],
|
||||||
stdin: "null",
|
stdin: "piped",
|
||||||
stdout: "null",
|
stdout: "piped",
|
||||||
stderr: "null",
|
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();
|
await proc.status();
|
||||||
proc.close();
|
proc.close();
|
||||||
|
|
||||||
|
const manifestStr = new TextDecoder().decode(out);
|
||||||
|
const manifestPath = join(directory, "./fresh.gen.ts");
|
||||||
|
|
||||||
|
await Deno.writeTextFile(manifestPath, manifestStr);
|
||||||
console.log(
|
console.log(
|
||||||
`%cThe manifest has been generated for ${routes.length} routes and ${islands.length} islands.`,
|
`%cThe manifest has been generated for ${routes.length} routes and ${islands.length} islands.`,
|
||||||
"color: blue; font-weight: bold",
|
"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