-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.mjs
More file actions
279 lines (256 loc) · 7.67 KB
/
Copy pathutil.mjs
File metadata and controls
279 lines (256 loc) · 7.67 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import path from "path";
// import fs from "fs";
import child_process from "child_process";
import { mkdirp } from "mkdirp";
import { rimraf } from "rimraf";
// const uniquify = (array) => {
// return [...new Set(array)];
// };
export const runCharacter = async (characterJsonPath, { env = {} } = {}) => {
const mastraPath = import.meta.resolve("mastra").replace("file://", "");
const cp = child_process.spawn(process.execPath, [mastraPath, "dev"], {
env: {
...env,
_CHARACTER_JSON_PATH: characterJsonPath,
},
});
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
return new Promise((resolve, reject) => {
cp.on("close", (code) => {
if (code !== 0) {
reject(new Error(`Process exited with code ${code}`));
} else {
resolve();
}
});
cp.on("error", (error) => {
reject(error);
});
});
};
export const getPluginType = (plugin) => {
if (/^https?:\/\//.test(plugin)) {
return "http";
} else if (plugin.startsWith("composio:")) {
return "composio";
} else {
return "npm";
}
};
export const sortPlugins = (plugins) => {
const http = [];
const npm = [];
const composio = [];
for (const plugin of plugins) {
const pluginType = getPluginType(plugin);
if (pluginType === "http") {
http.push(plugin);
} else if (pluginType === "npm") {
npm.push(plugin);
} else if (pluginType === "composio") {
composio.push(plugin);
}
}
return {
http,
npm,
composio,
};
};
export const getNpmPackageType = (packageSpecifier) => {
if (packageSpecifier.startsWith("github:")) {
return "github";
} else {
return "npm";
}
};
export const sortNpmPackages = (packageSpecifiers) => {
const github = [];
const npm = [];
for (const packageSpecifier of packageSpecifiers) {
const packageType = getNpmPackageType(packageSpecifier);
if (packageType === "github") {
github.push(packageSpecifier);
} else {
npm.push(packageSpecifier);
}
}
return { github, npm };
};
const installNpmGithubPackages = async (packageSpecifiers) => {
console.log(`Installing github packages: ${packageSpecifiers.join(", ")}`);
// wipe packages directory
const packagesDir = path.resolve(process.cwd(), "packages");
await rimraf(packagesDir);
// Ensure packages directory exists
try {
await mkdirp(packagesDir);
} catch (error) {
console.error(`Error creating packages directory: ${error.stack}`);
return Promise.reject(error);
}
// Transform package specifiers to GitHub URLs
const repoNames = packageSpecifiers.map((specifier) => {
specifier = specifier.replace("github:", "");
specifier = `https://github.com/${specifier}`;
return specifier;
});
// git clone all packages
await Promise.all(
repoNames.map(async (repoUrl) => {
return new Promise((resolveClone, rejectClone) => {
console.log(`Cloning ${repoUrl}...`);
const cp = child_process.spawn(
"git",
["clone", "--depth", "1", repoUrl],
{
stdio: "inherit",
cwd: packagesDir,
}
);
cp.on("error", (error) => {
console.error(
`Error executing git clone for ${repoUrl}: ${error.stack}`
);
rejectClone(error);
});
cp.on("close", (code) => {
if (code !== 0) {
console.error(`git clone for ${repoUrl} exited with code ${code}`);
rejectClone(new Error(`git clone exited with code ${code}`));
} else {
console.log(`Successfully cloned ${repoUrl}`);
resolveClone();
}
});
});
})
);
// build and install all plugins into local packages
await Promise.all(
packageSpecifiers.map(async (packageSpecifier) => {
const packageBasename = path.basename(
packageSpecifier.replace("github:", "")
);
const packagePath = path.resolve(packagesDir, packageBasename);
// pnpm install in local directory
await new Promise((resolveInstall, rejectInstall) => {
const cp = child_process.spawn("pnpm", ["install"], {
stdio: "inherit",
cwd: packagePath,
env: { ...process.env },
});
cp.on("error", (error) => {
console.error(
`Error executing pnpm install for ${packageBasename}: ${error.stack}`
);
rejectInstall(error);
});
cp.on("close", (code) => {
if (code !== 0) {
rejectInstall(new Error(`pnpm install exited with code ${code}`));
} else {
resolveInstall();
}
});
});
// pnpm build
// note: this is advisory and allowed to fail
await new Promise((resolveSpawn, rejectSpawn) => {
const cp = child_process.spawn("pnpm", ["build"], {
stdio: "inherit",
cwd: packagePath,
env: { ...process.env },
});
cp.on("error", (error) => {
console.error(
`Error executing pnpm build for ${packageBasename}: ${error.stack}`
);
resolveSpawn({
success: false,
package: packageBasename,
error: error.stack,
});
});
cp.on("close", (code) => {
if (code !== 0) {
console.error(
`pnpm build for ${packageBasename} exited with code ${code}`
);
resolveSpawn({
success: false,
package: packageBasename,
error: `exited with code ${code}`,
});
} else {
console.log(`Build completed successfully for ${packageBasename}`);
resolveSpawn({ success: true, package: packageBasename });
}
});
});
})
);
// install all plugins into app
// pnpm install to app
await new Promise((resolveInstall, rejectInstall) => {
const packageRefs = packageSpecifiers.map((packageSpecifier) => {
const packageBasename = path.basename(
packageSpecifier.replace("github:", "")
);
const packagePath = path.resolve(packagesDir, packageBasename);
return `file:${packagePath}`;
});
console.log(`Installing ${JSON.stringify(packageRefs)} to app`);
const cp = child_process.spawn(
"pnpm",
["install", ...packageRefs],
{
stdio: "inherit",
cwd: process.cwd(),
env: { ...process.env },
}
);
cp.on("error", (error) => {
console.error(
`Error executing pnpm install for ${packageBasename}: ${error.stack}`
);
rejectInstall(error);
});
cp.on("close", (code) => {
if (code !== 0) {
rejectInstall(new Error(`pnpm install exited with code ${code}`));
} else {
resolveInstall();
}
});
});
};
const installNpmBasicPackages = async (packageSpecifiers) => {
// packageSpecifiers = uniquify(packageSpecifiers);
console.log(`Installing npm basic packages: ${packageSpecifiers.join(", ")}`);
const cp = child_process.spawn("pnpm", ["install", ...packageSpecifiers], {
stdio: "inherit",
cwd: process.cwd(),
});
return new Promise((resolve, reject) => {
cp.on("error", (error) => {
reject(error);
});
cp.on("close", (code) => {
if (code !== 0) {
reject(new Error(`pnpm install exited with code ${code}`));
} else {
resolve();
}
});
});
};
export const installNpmPackages = async (packageSpecifiers) => {
console.log(`Installing packages: ${packageSpecifiers.join(", ")}`);
const { github, npm } = sortNpmPackages(packageSpecifiers);
await Promise.all([
installNpmGithubPackages(github),
installNpmBasicPackages(npm),
]);
};