-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathstaticAssets.ts
32 lines (25 loc) · 969 Bytes
/
staticAssets.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
import fs from 'fs';
import path from 'path';
export const STATIC_DIR = path.join(__dirname, '../tmp/static');
export default async function setupStaticAssets(): Promise<void> {
if (fs.existsSync(STATIC_DIR)) {
await fs.promises.rm(STATIC_DIR, { recursive: true });
}
await fs.promises.mkdir(STATIC_DIR, { recursive: true });
}
export function addStaticAsset(localOutPath: string, fileName: string, cb: () => string): void {
const newPath = path.join(STATIC_DIR, fileName);
// Only copy files once
if (!fs.existsSync(newPath)) {
fs.writeFileSync(newPath, cb(), 'utf-8');
}
symlinkAsset(newPath, path.join(localOutPath, fileName));
}
export function symlinkAsset(originalPath: string, targetPath: string): void {
try {
fs.linkSync(originalPath, targetPath);
} catch {
// ignore errors here, probably means the file already exists
// Since we always build into a new directory for each test, we can safely ignore this
}
}