fix: path handling inconsistencies (#2482)
This commit is contained in:
+27
-6
@@ -6,7 +6,9 @@ import { waitForText } from "../../tests/test_utils.tsx";
|
|||||||
import { withChildProcessServer } from "../../tests/test_utils.tsx";
|
import { withChildProcessServer } from "../../tests/test_utils.tsx";
|
||||||
|
|
||||||
async function withTmpDir(fn: (dir: string) => void | Promise<void>) {
|
async function withTmpDir(fn: (dir: string) => void | Promise<void>) {
|
||||||
const dir = await Deno.makeTempDir();
|
const hash = crypto.randomUUID().replaceAll(/-/g, "");
|
||||||
|
const dir = path.join(import.meta.dirname!, "..", "..", `tmp-${hash}`);
|
||||||
|
await Deno.mkdir(dir, { recursive: true });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fn(dir);
|
await fn(dir);
|
||||||
@@ -15,6 +17,25 @@ async function withTmpDir(fn: (dir: string) => void | Promise<void>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Patch project dependencies until there is an easier way
|
||||||
|
// to link JSR dependencies
|
||||||
|
async function patchProject(dir: string): Promise<void> {
|
||||||
|
const jsonPath = path.join(dir, "deno.json");
|
||||||
|
const json = JSON.parse(await Deno.readTextFile(jsonPath));
|
||||||
|
const rootJson = JSON.parse(
|
||||||
|
await Deno.readTextFile(
|
||||||
|
path.join(import.meta.dirname!, "..", "..", "deno.json"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
json.imports = rootJson.imports;
|
||||||
|
json.imports["@fresh/core"] = "../src/mod.ts";
|
||||||
|
json.imports["@fresh/core/dev"] = "../src/dev/mod.ts";
|
||||||
|
json.imports["@fresh/plugin-tailwind"] = "../plugin-tailwindcss/mod.ts";
|
||||||
|
|
||||||
|
await Deno.writeTextFile(jsonPath, JSON.stringify(json, null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
function mockUserInput(steps: Record<string, unknown>) {
|
function mockUserInput(steps: Record<string, unknown>) {
|
||||||
const errorOutput: unknown[][] = [];
|
const errorOutput: unknown[][] = [];
|
||||||
const tty: MockTTY = {
|
const tty: MockTTY = {
|
||||||
@@ -104,8 +125,7 @@ Deno.test("init - with vscode", async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Testing this with JSR isn't as easy anymore as it was before
|
Deno.test("init - can start dev server", async () => {
|
||||||
Deno.test.ignore("init - can start dev server", async () => {
|
|
||||||
await withTmpDir(async (dir) => {
|
await withTmpDir(async (dir) => {
|
||||||
const mock = mockUserInput({
|
const mock = mockUserInput({
|
||||||
[InitStep.ProjectName]: ".",
|
[InitStep.ProjectName]: ".",
|
||||||
@@ -114,6 +134,7 @@ Deno.test.ignore("init - can start dev server", async () => {
|
|||||||
await expectProjectFile(dir, "main.ts");
|
await expectProjectFile(dir, "main.ts");
|
||||||
await expectProjectFile(dir, "dev.ts");
|
await expectProjectFile(dir, "dev.ts");
|
||||||
|
|
||||||
|
await patchProject(dir);
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
dir,
|
dir,
|
||||||
path.join(dir, "dev.ts"),
|
path.join(dir, "dev.ts"),
|
||||||
@@ -128,8 +149,7 @@ Deno.test.ignore("init - can start dev server", async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Testing this with JSR isn't as easy anymore as it was before
|
Deno.test("init - can start build project", async () => {
|
||||||
Deno.test.ignore("init - can start build project", async () => {
|
|
||||||
await withTmpDir(async (dir) => {
|
await withTmpDir(async (dir) => {
|
||||||
const mock = mockUserInput({
|
const mock = mockUserInput({
|
||||||
[InitStep.ProjectName]: ".",
|
[InitStep.ProjectName]: ".",
|
||||||
@@ -138,6 +158,8 @@ Deno.test.ignore("init - can start build project", async () => {
|
|||||||
await expectProjectFile(dir, "main.ts");
|
await expectProjectFile(dir, "main.ts");
|
||||||
await expectProjectFile(dir, "dev.ts");
|
await expectProjectFile(dir, "dev.ts");
|
||||||
|
|
||||||
|
await patchProject(dir);
|
||||||
|
|
||||||
// Build
|
// Build
|
||||||
await new Deno.Command(Deno.execPath(), {
|
await new Deno.Command(Deno.execPath(), {
|
||||||
args: ["run", "-A", path.join(dir, "dev.ts"), "build"],
|
args: ["run", "-A", path.join(dir, "dev.ts"), "build"],
|
||||||
@@ -151,7 +173,6 @@ Deno.test.ignore("init - can start build project", async () => {
|
|||||||
dir,
|
dir,
|
||||||
path.join(dir, "main.ts"),
|
path.join(dir, "main.ts"),
|
||||||
async (address) => {
|
async (address) => {
|
||||||
console.log({ address });
|
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(address);
|
await page.goto(address);
|
||||||
await page.locator("button").click();
|
await page.locator("button").click();
|
||||||
|
|||||||
+6
-2
@@ -39,9 +39,11 @@ export interface ResolvedFreshConfig {
|
|||||||
mode: Mode;
|
mode: Mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseRootPath(root: string): string {
|
export function parseRootPath(root: string, cwd: string): string {
|
||||||
if (root.startsWith("file://")) {
|
if (root.startsWith("file://")) {
|
||||||
root = path.fromFileUrl(root);
|
root = path.fromFileUrl(root);
|
||||||
|
} else if (!path.isAbsolute(root)) {
|
||||||
|
root = path.join(cwd, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ext = path.extname(root);
|
const ext = path.extname(root);
|
||||||
@@ -56,7 +58,9 @@ export function parseRootPath(root: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
|
export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig {
|
||||||
const root = options.root ? parseRootPath(options.root) : Deno.cwd();
|
const root = options.root
|
||||||
|
? parseRootPath(options.root, Deno.cwd())
|
||||||
|
: Deno.cwd();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
root,
|
root,
|
||||||
|
|||||||
+9
-8
@@ -3,12 +3,13 @@ import { parseRootPath } from "./config.ts";
|
|||||||
|
|
||||||
// FIXME: Windows
|
// FIXME: Windows
|
||||||
Deno.test.ignore("parseRootPath", () => {
|
Deno.test.ignore("parseRootPath", () => {
|
||||||
expect(parseRootPath("file:///foo/bar")).toEqual("/foo/bar");
|
const cwd = Deno.cwd();
|
||||||
expect(parseRootPath("file:///foo/bar.ts")).toEqual("/foo");
|
expect(parseRootPath("file:///foo/bar", cwd)).toEqual("/foo/bar");
|
||||||
expect(parseRootPath("/foo/bar")).toEqual("/foo/bar");
|
expect(parseRootPath("file:///foo/bar.ts", cwd)).toEqual("/foo");
|
||||||
expect(parseRootPath("/foo/bar.ts")).toEqual("/foo");
|
expect(parseRootPath("/foo/bar", cwd)).toEqual("/foo/bar");
|
||||||
expect(parseRootPath("/foo/bar.tsx")).toEqual("/foo");
|
expect(parseRootPath("/foo/bar.ts", cwd)).toEqual("/foo");
|
||||||
expect(parseRootPath("/foo/bar.js")).toEqual("/foo");
|
expect(parseRootPath("/foo/bar.tsx", cwd)).toEqual("/foo");
|
||||||
expect(parseRootPath("/foo/bar.jsx")).toEqual("/foo");
|
expect(parseRootPath("/foo/bar.js", cwd)).toEqual("/foo");
|
||||||
expect(parseRootPath("/foo/bar.mjs")).toEqual("/foo");
|
expect(parseRootPath("/foo/bar.jsx", cwd)).toEqual("/foo");
|
||||||
|
expect(parseRootPath("/foo/bar.mjs", cwd)).toEqual("/foo");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { delay } from "../test_utils.ts";
|
|||||||
|
|
||||||
function testTransformer(files: Record<string, string>) {
|
function testTransformer(files: Record<string, string>) {
|
||||||
const mockFs: FsAdapter = {
|
const mockFs: FsAdapter = {
|
||||||
|
cwd: () => "/",
|
||||||
isDirectory: () => Promise.resolve(false),
|
isDirectory: () => Promise.resolve(false),
|
||||||
mkdirp: () => Promise.resolve(),
|
mkdirp: () => Promise.resolve(),
|
||||||
walk: async function* foo() {
|
walk: async function* foo() {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export interface FreshFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface FsAdapter {
|
export interface FsAdapter {
|
||||||
|
cwd(): string;
|
||||||
walk(
|
walk(
|
||||||
root: string | URL,
|
root: string | URL,
|
||||||
options?: WalkOptions,
|
options?: WalkOptions,
|
||||||
@@ -17,6 +18,7 @@ export interface FsAdapter {
|
|||||||
|
|
||||||
export const fsAdapter: FsAdapter = {
|
export const fsAdapter: FsAdapter = {
|
||||||
walk,
|
walk,
|
||||||
|
cwd: Deno.cwd,
|
||||||
async isDirectory(path) {
|
async isDirectory(path) {
|
||||||
try {
|
try {
|
||||||
const stat = await Deno.stat(path);
|
const stat = await Deno.stat(path);
|
||||||
|
|||||||
@@ -65,7 +65,9 @@ export async function fsRoutes<State>(
|
|||||||
const ignore = options.ignoreFilePattern ?? [TEST_FILE_PATTERN];
|
const ignore = options.ignoreFilePattern ?? [TEST_FILE_PATTERN];
|
||||||
const fs = options._fs ?? fsAdapter;
|
const fs = options._fs ?? fsAdapter;
|
||||||
|
|
||||||
const dir = options.dir ? parseRootPath(options.dir) : app.config.root;
|
const dir = options.dir
|
||||||
|
? parseRootPath(options.dir, fs.cwd())
|
||||||
|
: app.config.root;
|
||||||
const islandDir = path.join(dir, "islands");
|
const islandDir = path.join(dir, "islands");
|
||||||
const routesDir = path.join(dir, "routes");
|
const routesDir = path.join(dir, "routes");
|
||||||
|
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ export function serveMiddleware<T>(
|
|||||||
|
|
||||||
export function createFakeFs(files: Record<string, unknown>): FsAdapter {
|
export function createFakeFs(files: Record<string, unknown>): FsAdapter {
|
||||||
return {
|
return {
|
||||||
|
cwd: () => ".",
|
||||||
async *walk(_root) {
|
async *walk(_root) {
|
||||||
// FIXME: ignore
|
// FIXME: ignore
|
||||||
for (const file of Object.keys(files)) {
|
for (const file of Object.keys(files)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user