-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathtest_utils.ts
More file actions
122 lines (106 loc) · 2.89 KB
/
Copy pathtest_utils.ts
File metadata and controls
122 lines (106 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { createBuilder } from "vite";
import * as path from "@std/path";
import { walk } from "@std/fs/walk";
import { withTmpDir } from "../../fresh/src/test_utils.ts";
import { withChildProcessServer } from "../../fresh/tests/test_utils.tsx";
export const DEMO_DIR = path.join(import.meta.dirname!, "..", "demo");
export const FIXTURE_DIR = path.join(import.meta.dirname!, "fixtures");
export async function updateFile(
filePath: string,
fn: (text: string) => string | Promise<string>,
) {
const original = await Deno.readTextFile(filePath);
const result = await fn(original);
await Deno.writeTextFile(filePath, result);
return {
async [Symbol.asyncDispose]() {
await Deno.writeTextFile(filePath, original);
},
};
}
async function copyDir(from: string, to: string) {
const entries = walk(from, {
includeFiles: true,
includeDirs: false,
skip: [/([\\/]+(_fresh|node_modules|vendor)[\\/]+|[\\/]+vite\.config\.ts)/],
});
for await (const entry of entries) {
if (entry.isFile) {
const relative = path.relative(from, entry.path);
const target = path.join(to, relative);
try {
await Deno.mkdir(path.dirname(target), { recursive: true });
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
await Deno.copyFile(entry.path, target);
}
}
}
export async function withDevServer(
fixtureDir: string,
fn: (address: string, dir: string) => void | Promise<void>,
env: Record<string, string> = {},
) {
await using tmp = await withTmpDir({
dir: path.join(import.meta.dirname!, ".."),
prefix: "tmp_vite_",
});
await copyDir(fixtureDir, tmp.dir);
await Deno.writeTextFile(
path.join(tmp.dir, "vite.config.ts"),
`import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [
fresh(),
],
});
`,
);
await withChildProcessServer(
{ cwd: tmp.dir, args: ["run", "-A", "npm:vite", "--port", "0"], env },
async (address) => await fn(address, tmp.dir),
);
}
export async function buildVite(fixtureDir: string) {
const tmp = await withTmpDir({
dir: path.join(import.meta.dirname!, ".."),
prefix: "tmp_vite_",
});
const builder = await createBuilder({
root: fixtureDir,
build: {
emptyOutDir: true,
},
environments: {
ssr: {
build: {
outDir: path.join(tmp.dir, "_fresh", "server"),
},
},
client: {
build: {
outDir: path.join(tmp.dir, "_fresh", "client"),
},
},
},
});
await builder.buildApp();
return {
tmp: tmp.dir,
async [Symbol.asyncDispose]() {
return await tmp[Symbol.asyncDispose]();
},
};
}
export function usingEnv(name: string, value: string) {
Deno.env.set(name, value);
return {
[Symbol.dispose]: () => {
Deno.env.delete(name);
},
};
}