Elffuss BraidHub

kikocis/continue public

main · 3.1 KiBRawPermalink
1/**
2 * @file Copy lancedb to the current directory. It is also intended to run as a child process.
3 */
4
5const { fork } = require("child_process");
6const fs = require("fs");
7const path = require("path");
8
9const ncp = require("ncp").ncp;
10
11const { execCmdSync } = require("../../../scripts/util");
12
13async function installNodeModuleInTempDirAndCopyToCurrent(packageName, toCopy) {
14 console.log(`Copying ${packageName} to ${toCopy}`);
15 // This is a way to install only one package without npm trying to install all the dependencies
16 // Create a temporary directory for installing the package
17 const adjustedName = packageName.replace(/@/g, "").replace("/", "-");
18 const currentDir = process.cwd();
19 const tempDir = path.join(
20 currentDir,
21 "tmp",
22 `continue-node_modules-${adjustedName}`,
23 );
24
25 // // Remove the dir we will be copying to
26 // rimrafSync(`node_modules/${toCopy}`);
27
28 // // Ensure the temporary directory exists
29 fs.mkdirSync(tempDir, { recursive: true });
30
31 try {
32 // Move to the temporary directory
33 process.chdir(tempDir);
34
35 // Initialize a new package.json and install the package
36 execCmdSync(`npm init -y && npm i -f ${packageName} --no-save`);
37
38 console.log(
39 `Contents of: ${packageName}`,
40 fs.readdirSync(path.join(tempDir, "node_modules", toCopy)),
41 );
42
43 // Without this it seems the file isn't completely written to disk
44 await new Promise((resolve) => setTimeout(resolve, 2000));
45
46 // Copy the installed package back to the current directory
47 await new Promise((resolve, reject) => {
48 ncp(
49 path.join(tempDir, "node_modules", toCopy),
50 path.join(currentDir, "node_modules", toCopy),
51 { dereference: true },
52 (error) => {
53 if (error) {
54 console.error(
55 `[error] Error copying ${packageName} package`,
56 error,
57 );
58 reject(error);
59 } else {
60 resolve();
61 }
62 },
63 );
64 });
65 } finally {
66 // Clean up the temporary directory
67 // rimrafSync(tempDir);
68
69 // Return to the original directory
70 process.chdir(currentDir);
71 }
72}
73
74process.on("message", (msg) => {
75 installNodeModuleInTempDirAndCopyToCurrent(
76 msg.payload.packageName,
77 msg.payload.toCopy,
78 )
79 .then(() => process.send({ done: true }))
80 .catch((error) => {
81 console.error(error); // show the error in the parent process
82 process.send({ error: true });
83 });
84});
85
86/**
87 * invoke a child process to install a node module into temporary directory and copy it over into node modules
88 * @param {string} packageName the module to install and copy
89 * @param {string} toCopy directory to copy into inside node modules
90 */
91async function installAndCopyNodeModules(packageName, toCopy) {
92 const child = fork(__filename, { stdio: "inherit", cwd: process.cwd() });
93 child.send({
94 payload: {
95 packageName,
96 toCopy,
97 },
98 });
99
100 return new Promise((resolve, reject) => {
101 child.on("message", (msg) => {
102 if (msg.error) {
103 reject();
104 } else {
105 resolve();
106 }
107 });
108 });
109}
110
111module.exports = {
112 installAndCopyNodeModules,
113};