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
|
import * as fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { nodeFileTrace } from '@vercel/nft';
export async function copyDependenciesToFunction(
root: URL,
functionFolder: URL,
serverEntry: string
) {
const entryPath = fileURLToPath(new URL(`./${serverEntry}`, functionFolder));
const result = await nodeFileTrace([entryPath], {
base: fileURLToPath(root),
});
for (const file of result.fileList) {
if (file.startsWith('.vercel/')) continue;
const origin = new URL(file, root);
const dest = new URL(file, functionFolder);
const meta = await fs.stat(origin);
const isSymlink = (await fs.lstat(origin)).isSymbolicLink();
// Create directories recursively
if (meta.isDirectory() && !isSymlink) {
await fs.mkdir(new URL('..', dest), { recursive: true });
} else {
await fs.mkdir(new URL('.', dest), { recursive: true });
}
if (isSymlink) {
const link = await fs.readlink(origin);
await fs.symlink(link, dest, meta.isDirectory() ? 'dir' : 'file');
} else {
await fs.copyFile(origin, dest);
}
}
}
|