1#!/usr/bin/env node
2
3const {spawn, execFileSync} = require('child_process')
4
5const RESOLUTION_REGEX = /(\d+) x (\d+)/
6const DIGIT_FLAG_REGEX = /^--?(\d+)$/
7
8const args = process.argv.slice(2)
9
10// Parse the number of Zed instances to spawn.
11let instanceCount = 1
12const digitMatch = args[0]?.match(DIGIT_FLAG_REGEX)
13if (digitMatch) {
14 instanceCount = parseInt(digitMatch[1])
15 args.shift()
16}
17if (instanceCount > 4) {
18 throw new Error('Cannot spawn more than 4 instances')
19}
20
21// Parse the resolution of the main screen
22const displayInfo = JSON.parse(
23 execFileSync(
24 'system_profiler',
25 ['SPDisplaysDataType', '-json'],
26 {encoding: 'utf8'}
27 )
28)
29const mainDisplayResolution = displayInfo
30 ?.SPDisplaysDataType[0]
31 ?.spdisplays_ndrvs
32 ?.find(entry => entry.spdisplays_main === "spdisplays_yes")
33 ?._spdisplays_resolution
34 ?.match(RESOLUTION_REGEX)
35if (!mainDisplayResolution) {
36 throw new Error('Could not parse screen resolution')
37}
38const screenWidth = parseInt(mainDisplayResolution[1])
39const screenHeight = parseInt(mainDisplayResolution[2])
40
41// Determine the window size for each instance
42let instanceWidth = screenWidth
43let instanceHeight = screenHeight
44if (instanceCount > 1) {
45 instanceWidth = Math.floor(screenWidth / 2)
46 if (instanceCount > 2) {
47 instanceHeight = Math.floor(screenHeight / 2)
48 }
49}
50
51let users = [
52 'nathansobo',
53 'as-cii',
54 'maxbrunsfeld',
55 'iamnbutler'
56]
57
58// If a user is specified, make sure it's first in the list
59const user = process.env.ZED_IMPERSONATE
60if (user) {
61 users = [user].concat(users.filter(u => u !== user))
62}
63
64const positions = [
65 '0,0',
66 `${instanceWidth},0`,
67 `0,${instanceHeight}`,
68 `${instanceWidth},${instanceHeight}`
69]
70
71execFileSync('cargo', ['build'], {stdio: 'inherit'})
72
73setTimeout(() => {
74 for (let i = 0; i < instanceCount; i++) {
75 spawn('target/debug/Zed', i == 0 ? args : [], {
76 stdio: 'inherit',
77 env: {
78 ZED_IMPERSONATE: users[i],
79 ZED_WINDOW_POSITION: positions[i],
80 ZED_STATELESS: '1',
81 ZED_ALWAYS_ACTIVE: '1',
82 ZED_SERVER_URL: 'http://localhost:8080',
83 ZED_ADMIN_API_TOKEN: 'secret',
84 ZED_WINDOW_SIZE: `${instanceWidth},${instanceHeight}`
85 }
86 })
87 }
88}, 0.1)