-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ts
More file actions
56 lines (45 loc) · 1.7 KB
/
install.ts
File metadata and controls
56 lines (45 loc) · 1.7 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
import { nodeFileTrace } from "@vercel/nft";
import fs from "fs/promises";
import path from "path";
// Get command-line arguments
let [rootDir, serverFile, targetDir] = process.argv.slice(2);
if (targetDir === undefined) {
targetDir = serverFile;
serverFile = rootDir;
rootDir = "";
}
if (!serverFile || !targetDir) {
console.error("Usage: node --import=@swc-node/register/esm-register install.ts <server-file> <target-directory>");
process.exit(1);
}
if (rootDir) {
if (!path.isAbsolute(targetDir)) {
targetDir = path.relative(path.resolve(rootDir), path.resolve(targetDir));
}
process.chdir(rootDir);
}
try {
// Trace dependencies of the provided file
const { fileList } = await nodeFileTrace([serverFile],
{ conditions: ['node', 'production'] });
// Resolve absolute paths to avoid nested copying
const targetDirAbsolute = path.resolve(targetDir);
// Copy each file to the target directory with its nested structure
for (const filePath of fileList) {
const targetPath = path.join(targetDir, filePath);
// Get the absolute path of the source file
const sourcePathAbsolute = path.resolve(filePath);
// Check if the file is already in the target directory
if (sourcePathAbsolute.startsWith(targetDirAbsolute)) {
continue;
}
// Create necessary directories
await fs.mkdir(path.dirname(targetPath), { recursive: true });
// Copy the file
await fs.copyFile(filePath, targetPath);
console.log(`Copied ${ path.join(rootDir, filePath) } to ${ targetPath }`);
}
console.log("All files copied successfully.");
} catch (err) {
console.error("Error:", err);
}