-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-utils.ts
60 lines (54 loc) · 1.49 KB
/
test-utils.ts
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
import { copyFile, mkdir, readdir } from "node:fs/promises";
import { join } from "node:path";
import type { AstroIntegration } from "astro";
import { dev as createAstroServer } from "astro";
import { type Plugin, createServer } from "vite";
/**
* Start the development server of Vite
* @param root file system path to the root of the Vite project
* @param plugin Vite plugin
*/
export async function startVite(root: string, plugin: Plugin) {
const server = await createServer({
configFile: false,
root,
plugins: [plugin],
});
await server.listen();
return {
server,
url: server.resolvedUrls?.local[0] ?? "http://localhost:5173",
};
}
/**
* Start the development server of Astro
* @param root file system path to the root of the Astro project
* @param plugin Astro integration plugin
*/
export async function startAstro(root: string, plugin: AstroIntegration) {
const server = await createAstroServer({
configFile: false,
root,
integrations: [plugin],
});
return {
server,
url: `http://localhost:${server.address.port}`,
};
}
/**
* Recursively copy a directory
*/
export async function copyDirectory(src: string, dest: string) {
await mkdir(dest, { recursive: true });
const entries = await readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = join(src, entry.name);
const destPath = join(dest, entry.name);
if (entry.isDirectory()) {
await copyDirectory(srcPath, destPath);
} else {
await copyFile(srcPath, destPath);
}
}
}