feat: prevent duplicate routes when generating manifest (#1410)

This commit is contained in:
Reed von Redwitz
2023-07-11 14:39:48 +02:00
committed by GitHub
parent d4c2258eb6
commit 9177d9afc3
9 changed files with 146 additions and 21 deletions
+11
View File
@@ -39,6 +39,7 @@ async function collectDir(dir: string): Promise<string[]> {
}
const paths = [];
const fileNames = new Set<string>();
const routesFolder = walk(dir, {
includeDirs: false,
includeFiles: true,
@@ -46,6 +47,16 @@ async function collectDir(dir: string): Promise<string[]> {
});
for await (const entry of routesFolder) {
const fileNameWithoutExt = relative(dir, entry.path).split(".").slice(0, -1)
.join(".");
if (fileNames.has(fileNameWithoutExt)) {
throw new Error(
`Route conflict detected. Multiple files have the same name: ${dir}${fileNameWithoutExt}`,
);
}
fileNames.add(fileNameWithoutExt);
paths.push(relative(dir, entry.path));
}
+20
View File
@@ -0,0 +1,20 @@
{
"lock": false,
"tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts",
"update": "deno run -A -r https://fresh.deno.dev/update ."
},
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3",
"$std/": "https://deno.land/std@0.190.0/"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts");
+17
View File
@@ -0,0 +1,17 @@
// DO NOT EDIT. This file is generated by fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/simple_name_conflict/users.ts";
import * as $1 from "./routes/simple_name_conflict/users.tsx";
const manifest = {
routes: {
"./routes/simple_name_conflict/users.ts": $0,
"./routes/simple_name_conflict/users.tsx": $1,
},
islands: {},
baseUrl: import.meta.url,
};
export default manifest;
+12
View File
@@ -0,0 +1,12 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import "$std/dotenv/load.ts";
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
await start(manifest);
@@ -0,0 +1,7 @@
import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
GET(_req, _ctx) {
return new Response("hello");
},
};
@@ -0,0 +1,3 @@
export default function Home() {
return <div>Hello</div>;
}
+18
View File
@@ -0,0 +1,18 @@
import { startFreshServerExpectErrors } from "./test_utils.ts";
import { dirname, join } from "$std/path/mod.ts";
import { assertStringIncludes } from "./deps.ts";
const dir = dirname(import.meta.url);
Deno.test({
name: "route-conflicts",
async fn() {
const errorMessage = await startFreshServerExpectErrors({
args: ["run", "-A", join(dir, "./fixture_route_analysis/dev.ts")],
});
assertStringIncludes(
errorMessage,
"Error: Route conflict detected. Multiple files have the same name",
);
},
});
+53 -21
View File
@@ -1,28 +1,8 @@
import { delay, Page, puppeteer, TextLineStream } from "./deps.ts";
export async function startFreshServer(options: Deno.CommandOptions) {
const serverProcess = new Deno.Command(Deno.execPath(), {
...options,
stdin: "null",
stdout: "piped",
stderr: "inherit",
}).spawn();
const { serverProcess, lines, address } = await spawnServer(options);
const decoder = new TextDecoderStream();
const lines: ReadableStream<string> = serverProcess.stdout
.pipeThrough(decoder)
.pipeThrough(new TextLineStream(), {
preventCancel: true,
});
let address = "";
for await (const line of lines) {
const match = line.match(/https?:\/\/localhost:\d+/g);
if (match) {
address = match[0];
break;
}
}
if (!address) {
throw new Error("Server didn't start up");
}
@@ -57,3 +37,55 @@ export async function withPageName(
await serverProcess.status;
}
}
export async function startFreshServerExpectErrors(
options: Deno.CommandOptions,
) {
const { serverProcess, address } = await spawnServer(options, true);
if (address) {
throw Error("Server started correctly");
}
const errorDecoder = new TextDecoderStream();
const errorLines: ReadableStream<string> = serverProcess.stderr
.pipeThrough(errorDecoder)
.pipeThrough(new TextLineStream(), {
preventCancel: true,
});
let output = "";
for await (const line of errorLines) {
output += line + "\n";
}
return output;
}
async function spawnServer(
options: Deno.CommandOptions,
expectErrors = false,
) {
const serverProcess = new Deno.Command(Deno.execPath(), {
...options,
stdin: "null",
stdout: "piped",
stderr: expectErrors ? "piped" : "inherit",
}).spawn();
const decoder = new TextDecoderStream();
const lines: ReadableStream<string> = serverProcess.stdout
.pipeThrough(decoder)
.pipeThrough(new TextLineStream(), {
preventCancel: true,
});
let address = "";
for await (const line of lines) {
const match = line.match(/https?:\/\/localhost:\d+/g);
if (match) {
address = match[0];
break;
}
}
return { serverProcess, lines, address };
}