-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathbuild-output-utils.ts
More file actions
53 lines (43 loc) · 1.56 KB
/
build-output-utils.ts
File metadata and controls
53 lines (43 loc) · 1.56 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
import { existsSync } from "node:fs";
import { readFile, readdir } from "node:fs/promises";
import path from "node:path";
import { brotliDecompressSync, gunzipSync } from "node:zlib";
export function getBuildOutputDirs() {
const appRoot = process.cwd();
const sourceRoot = path.join(appRoot, "src");
const serverOutputRoot = path.join(appRoot, ".output/server");
const clientOutputRoot = path.join(appRoot, ".output/public");
if (!existsSync(sourceRoot)) {
throw new Error(`Source dir not found: ${sourceRoot}`);
}
if (!existsSync(serverOutputRoot)) {
throw new Error(
`Server output dir not found: ${serverOutputRoot}. Did you run the build? (pnpm --filter tests run build)`,
);
}
if (!existsSync(clientOutputRoot)) {
throw new Error(
`Client output dir not found: ${clientOutputRoot}. Did you run the build? (pnpm --filter tests run build)`,
);
}
return {
sourceRoot,
serverOutputRoot,
clientOutputRoot,
};
}
export async function getFiles(dir: string, fileRegex: RegExp): Promise<string[]> {
const entries = await readdir(dir, { recursive: true, withFileTypes: true });
return entries
.filter(e => e.isFile() && fileRegex.test(e.name))
.map(e => path.join(e.parentPath, e.name));
}
export async function readFileContent(filePath: string) {
if (filePath.endsWith(".br")) {
return brotliDecompressSync(await readFile(filePath)).toString("utf-8");
}
if (filePath.endsWith(".gz")) {
return gunzipSync(await readFile(filePath)).toString("utf-8");
}
return readFile(filePath, "utf-8");
}