2023-10-09 22:40:36 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2023-12-16 00:27:58 +03:00
|
|
|
const { spawn, execFileSync } = require("child_process");
|
2023-10-09 22:40:36 +03:00
|
|
|
|
2023-12-16 00:27:58 +03:00
|
|
|
const RESOLUTION_REGEX = /(\d+) x (\d+)/;
|
|
|
|
const DIGIT_FLAG_REGEX = /^--?(\d+)$/;
|
|
|
|
const ZED_2_MODE = "--zed2";
|
2023-10-09 22:40:36 +03:00
|
|
|
|
2023-12-16 00:27:58 +03:00
|
|
|
const args = process.argv.slice(2);
|
2023-10-09 22:40:36 +03:00
|
|
|
|
|
|
|
// Parse the number of Zed instances to spawn.
|
2023-12-16 00:27:58 +03:00
|
|
|
let instanceCount = 1;
|
|
|
|
const digitMatch = args[0]?.match(DIGIT_FLAG_REGEX);
|
2023-10-09 22:40:36 +03:00
|
|
|
if (digitMatch) {
|
2023-12-16 00:27:58 +03:00
|
|
|
instanceCount = parseInt(digitMatch[1]);
|
|
|
|
args.shift();
|
2023-10-09 22:40:36 +03:00
|
|
|
}
|
2023-12-16 00:27:58 +03:00
|
|
|
const isZed2 = args.some((arg) => arg === ZED_2_MODE);
|
2023-10-09 22:40:36 +03:00
|
|
|
if (instanceCount > 4) {
|
2023-12-16 00:27:58 +03:00
|
|
|
throw new Error("Cannot spawn more than 4 instances");
|
2023-10-09 22:40:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the resolution of the main screen
|
|
|
|
const displayInfo = JSON.parse(
|
2023-12-16 00:27:58 +03:00
|
|
|
execFileSync("system_profiler", ["SPDisplaysDataType", "-json"], {
|
|
|
|
encoding: "utf8",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
const mainDisplayResolution =
|
|
|
|
displayInfo?.SPDisplaysDataType[0]?.spdisplays_ndrvs
|
|
|
|
?.find((entry) => entry.spdisplays_main === "spdisplays_yes")
|
|
|
|
?._spdisplays_resolution?.match(RESOLUTION_REGEX);
|
2023-10-09 22:40:36 +03:00
|
|
|
if (!mainDisplayResolution) {
|
2023-12-16 00:27:58 +03:00
|
|
|
throw new Error("Could not parse screen resolution");
|
2023-10-09 22:40:36 +03:00
|
|
|
}
|
2023-12-16 00:27:58 +03:00
|
|
|
const screenWidth = parseInt(mainDisplayResolution[1]);
|
|
|
|
const screenHeight = parseInt(mainDisplayResolution[2]);
|
2023-10-09 22:40:36 +03:00
|
|
|
|
|
|
|
// Determine the window size for each instance
|
2023-12-16 00:27:58 +03:00
|
|
|
let instanceWidth = screenWidth;
|
|
|
|
let instanceHeight = screenHeight;
|
2023-10-09 22:40:36 +03:00
|
|
|
if (instanceCount > 1) {
|
2023-12-16 00:27:58 +03:00
|
|
|
instanceWidth = Math.floor(screenWidth / 2);
|
2023-10-09 22:40:36 +03:00
|
|
|
if (instanceCount > 2) {
|
2023-12-16 00:27:58 +03:00
|
|
|
instanceHeight = Math.floor(screenHeight / 2);
|
2023-10-09 22:40:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 00:27:58 +03:00
|
|
|
let users = ["nathansobo", "as-cii", "maxbrunsfeld", "iamnbutler"];
|
2023-10-09 22:40:36 +03:00
|
|
|
|
2023-12-16 00:27:58 +03:00
|
|
|
const RUST_LOG = process.env.RUST_LOG || "info";
|
2023-10-12 03:39:41 +03:00
|
|
|
|
2023-10-09 22:40:36 +03:00
|
|
|
// If a user is specified, make sure it's first in the list
|
2023-12-16 00:27:58 +03:00
|
|
|
const user = process.env.ZED_IMPERSONATE;
|
2023-10-09 22:40:36 +03:00
|
|
|
if (user) {
|
2023-12-16 00:27:58 +03:00
|
|
|
users = [user].concat(users.filter((u) => u !== user));
|
2023-10-09 22:40:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const positions = [
|
2023-12-16 00:27:58 +03:00
|
|
|
"0,0",
|
2023-10-09 22:40:36 +03:00
|
|
|
`${instanceWidth},0`,
|
|
|
|
`0,${instanceHeight}`,
|
2023-12-16 00:27:58 +03:00
|
|
|
`${instanceWidth},${instanceHeight}`,
|
|
|
|
];
|
2023-10-09 22:40:36 +03:00
|
|
|
|
2023-12-16 00:27:58 +03:00
|
|
|
const buildArgs = isZed2 ? ["build", "-p", "zed2"] : ["build"];
|
|
|
|
const zedBinary = isZed2 ? "target/debug/Zed2" : "target/debug/Zed";
|
|
|
|
execFileSync("cargo", buildArgs, { stdio: "inherit" });
|
2023-10-09 22:40:36 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
for (let i = 0; i < instanceCount; i++) {
|
2023-11-23 16:19:06 +03:00
|
|
|
spawn(zedBinary, i == 0 ? args : [], {
|
2023-12-16 00:27:58 +03:00
|
|
|
stdio: "inherit",
|
2023-10-09 22:40:36 +03:00
|
|
|
env: {
|
|
|
|
ZED_IMPERSONATE: users[i],
|
|
|
|
ZED_WINDOW_POSITION: positions[i],
|
2023-12-16 00:27:58 +03:00
|
|
|
ZED_STATELESS: "1",
|
|
|
|
ZED_ALWAYS_ACTIVE: "1",
|
|
|
|
ZED_SERVER_URL: "http://localhost:8080",
|
|
|
|
ZED_ADMIN_API_TOKEN: "secret",
|
2023-10-12 03:39:41 +03:00
|
|
|
ZED_WINDOW_SIZE: `${instanceWidth},${instanceHeight}`,
|
2023-10-22 17:03:13 +03:00
|
|
|
PATH: process.env.PATH,
|
2023-10-12 03:39:41 +03:00
|
|
|
RUST_LOG,
|
2023-12-16 00:27:58 +03:00
|
|
|
},
|
|
|
|
});
|
2023-10-09 22:40:36 +03:00
|
|
|
}
|
2023-12-16 00:27:58 +03:00
|
|
|
}, 0.1);
|