-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathtest_utils.ts
More file actions
153 lines (135 loc) · 3.49 KB
/
Copy pathtest_utils.ts
File metadata and controls
153 lines (135 loc) · 3.49 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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", "--cached-only", "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({
logLevel: "error",
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) {
const prev = Deno.env.get(name);
Deno.env.set(name, value);
return {
[Symbol.dispose]: () => {
if (prev === undefined) {
Deno.env.delete(name);
} else {
Deno.env.set(name, prev);
}
},
};
}
export interface ProdOptions {
cwd: string;
args?: string[];
bin?: string;
env?: Record<string, string>;
}
export async function launchProd(
options: ProdOptions,
fn: (address: string) => void | Promise<void>,
) {
return await withChildProcessServer(
{
cwd: options.cwd,
args: options.args ??
["serve", "-A", "--cached-only", "--port", "0", "_fresh/server.js"],
},
fn,
);
}