-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
343 lines (295 loc) · 9.3 KB
/
server.js
File metadata and controls
343 lines (295 loc) · 9.3 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const { spawn } = require("node:child_process");
const ipc = require("@node-ipc/node-ipc").default;
const fs = require("fs");
const chalk = require("chalk");
const os = require("node:os");
const path = require("path");
if (!["darwin", "win32"].includes(process.platform)) {
throw new Error("Unsupported platform: " + process.platform);
}
ipc.config.id = "world";
ipc.config.retry = 1500;
// ipc.config.rawBuffer = true;
ipc.config.encoding = "utf-8";
ipc.config.sync = true;
ipc.config.silent = true;
// ipc.config.logDepth = 0; //default
// ipc.config.logger = () => {};
// log the version from package.json
console.log("testdriver-proxy version", require("./package.json").version);
function markdownToListArray(markdown) {
// Normalize line breaks
const normalizedMarkdown = markdown.replace(/\\n/g, "\n");
// Split into lines, filter out non-list items, and remove the leading number and period
const listItems = normalizedMarkdown
.split("\n")
.map(line => line.trim())
.filter(line => line.length > 0)
.filter((line) => line.match(/^(?:\d+(?:\.|\-)|\-)?\s*(\w.*)$/))
.map((item) => {
item = item.replace(/^(?:\d+(?:\.|\-)|\-)?\s*/, "");
item = item.replace(/\\r/g, "");
return item;
}); // Remove the leading numbers and period
return listItems;
}
ipc.serve(function () {
ipc.server.on("connect", function (socket) {
ipc.server.emit(socket, "status", "connected");
});
ipc.server.on("command", async (data, socket) => {
console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! command");
const { env, cwd, prerun, instructions } = JSON.parse(data.toString());
const version =
env.TESTDRIVERAI_VERSION || process.env.TESTDRIVERAI_VERSION || "latest";
await installTestdriverai(version, socket)
.then(() => {
ipc.server.emit(
socket,
"stdout",
"\nSuccessfully installed testdriverai package\n"
);
})
.catch((err) => {
const errorMessage = `Failed to install testdriverai package: ${err.message}`;
console.error(errorMessage);
ipc.server.emit(socket, "stderr", `\n${errorMessage}\n`);
ipc.server.emit(socket, "close", 1);
});
if (prerun) {
await spawnShell({ cwd, env, prerun }, socket)
.then(() => {
ipc.server.emit(
socket,
"stdout",
"\nSuccessfully ran prerun script\n"
);
})
.catch((err) => {
const errorMessage = `Failed to run prerun script: ${err.message}`;
console.error(errorMessage);
ipc.server.emit(socket, "stderr", `\n${errorMessage}\n`);
ipc.server.emit(socket, "close", 1);
});
}
setTimeout(() => {
// give prerun tiem to resolve, launch an app, etc
// this gives chrome time to launch, so prompts assume prerun has resolved
spawnInterpreter({ cwd, env, instructions }, socket);
}, 5000);
});
});
const installTestdriverai = async function (version, socket) {
return new Promise((resolve, reject) => {
try {
const child = spawn("yarn", ["global", "add", `testdriverai@${version}`], {
env: process.env,
shell: true,
windowsHide: true,
});
child.stdout.setEncoding("utf8");
child.stderr.setEncoding("utf8");
child.stdout.on("data", (data) =>
ipc.server.emit(socket, "stdout", data.toString())
);
child.stderr.on("data", (data) =>
ipc.server.emit(socket, "stderr", data.toString())
);
child.on("error", reject);
child.on("exit", (code) => {
if (code === 0) return resolve();
reject(new Error(`yarn exited with code ${code}`));
});
} catch (err) {
reject(err);
}
});
};
let i = 0;
const spawnInterpreter = function (
{ cwd, env, inspect, instructions },
socket
) {
let child;
let step = 0;
try {
console.log("!!! SPAWNING");
list = markdownToListArray(instructions);
let command = "testdriverai";
let args = [];
if (inspect && process.platform === "win32") {
command = "node";
args = [
"--inspect",
"C:\\Users\\testdriver\\AppData\\Local\\Yarn\\global\\node_modules\\testdriverai\\index.js",
];
}
child = spawn(command, args, {
env: {
TD_SPEAK: false,
TD_ANALYTICS: true,
TD_MINIMIZE: false,
...process.env,
...env,
FORCE_COLOR: true,
},
cwd,
shell: true,
windowsHide: true,
});
child.stdout.setEncoding("utf8");
} catch (e) {
console.log("caught", e);
ipc.server.emit(socket, "stderr", e.toString());
}
child.on("error", function (e) {
console.log("error", e);
ipc.server.emit(socket, "stderr", e.toString());
});
let lineBuffer = "";
child.stdout.on("data", async (data) => {
lineBuffer = data.toString();
let lines = lineBuffer.split("\n");
// always try to summarize when running in ci
lines.push("/summarize");
ipc.server.emit(socket, "stdout", lineBuffer);
if (stripAnsi(lines[lines.length - 1]).indexOf(">") === 0) {
if (!list[i]) {
child.stdin.end();
child.stdout.destroy();
child.stderr.destroy();
child.kill();
} else {
let command = list[i];
child.stdin.write(`${command}\r\n`);
i++;
}
lineBuffer = "";
step += 1;
}
});
child.stderr.on("data", (data) => {
ipc.server.emit(socket, "stderr", data.toString());
});
child.on("close", (code) => {
console.log(code, "close");
if (typeof code !== "number") {
code = 0;
}
ipc.server.emit(socket, "close", code);
});
};
const spawnShell = function ({ cwd, env, prerun }, socket) {
let child;
return new Promise((resolve, reject) => {
try {
const testdriverRepoPath =
env.TESTDRIVERAI_REPO_PATH || process.env.TESTDRIVERAI_REPO_PATH;
if (testdriverRepoPath) cwd = testdriverRepoPath;
// example input 'rm ~/Desktop/WITH-LOVE-FROM-AMERICA.txt \\n npm install dashcam-chrome --save \\n /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --start-maximized --load-extension=./node_modules/dashcam-chrome/build/ 1>/dev/null 2>&1 & \\n exit'
let prerunFilePath = "prerun.sh";
if (process.platform === "win32") {
prerunFilePath = "prerun.ps1";
}
if (testdriverRepoPath) {
prerunFilePath = path.join(
testdriverRepoPath,
"testdriver",
prerunFilePath
);
} else {
prerunFilePath = path.join(os.tmpdir(), prerunFilePath);
}
// Check if the prerun file doesn't exist
// this can happen if the repo supplies this file within `testdriver/prerun`
// mostly for backward compatibility
if (prerun) {
// this should be swapped, prerun should take over
// Write prerun to the prerun file
ipc.server.emit(
socket,
"stdout",
`
${chalk.green("TestDriver: ")}${chalk.yellow("Running Prerun Script")}
\`\`\`
${prerun.replace(/\r\n/g, "\n")}
\`\`\`
From: "${prerunFilePath}"
`
);
if (process.platform === "win32") {
prerun = `$ErrorActionPreference = "Stop"
${prerun}`.replace(/(?<!\r)\n/g, "\r\n");
}
try {
fs.writeFileSync(prerunFilePath, prerun, { flag: "w+" });
} catch (e) {
console.error(e);
}
}
let toRun;
switch (process.platform) {
case "darwin":
toRun = {
command: "source",
args: [prerunFilePath],
};
break;
case "win32":
toRun = {
command: "powershell",
args: [prerunFilePath],
};
break;
}
console.log(
"spawning ",
`${toRun.command} ${toRun.args
.map((arg) => JSON.stringify(arg))
.join(" ")}`
);
if (fs.existsSync(prerunFilePath) && toRun) {
child = spawn(toRun.command, toRun.args, {
env: { ...process.env, ...env },
cwd,
shell: true,
windowsHide: true,
});
} else {
throw new Error(`Prerun file does not exist at ${prerunFilePath}`);
}
} catch (e) {
reject(e);
}
child.stdout.setEncoding("utf8");
child.stderr.setEncoding("utf8");
child.stdout.on("data", async (data) => {
ipc.server.emit(socket, "stdout", data.toString());
});
child.stderr.on("data", (data) => {
ipc.server.emit(socket, "stderr", data.toString());
});
child.on("error", reject);
child.on("exit", (code) => {
if (code === 0) return resolve();
reject(new Error(`Prerun script exited with code ${code}`));
});
});
};
ipc.server.start();
const ansiRegex = (({ onlyFirst = false } = {}) => {
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
].join("|");
return new RegExp(pattern, onlyFirst ? undefined : "g");
})();
function stripAnsi(string) {
if (typeof string !== "string") {
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
}
return string.replace(ansiRegex, "");
}
setInterval(function () {
console.log("server running... " + new Date());
}, 1000 * 20);