This repository was archived by the owner on Dec 4, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
158 lines (144 loc) · 5.42 KB
/
Copy pathvite.config.ts
File metadata and controls
158 lines (144 loc) · 5.42 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
import { defineConfig, Plugin } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { fileURLToPath } from 'url';
import { exec } from 'child_process';
import { promisify } from 'util';
import fs from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const execAsync = promisify(exec);
function desktopWindowsBridgePlugin(): Plugin {
return {
name: 'desktop-windows-bridge',
configureServer(server) {
server.middlewares.use('/api/windows', async (req, res) => {
if (req.method !== 'GET') {
res.statusCode = 405;
res.end();
return;
}
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
if (process.platform !== 'win32') {
res.end(
JSON.stringify([
{ id: 101, processName: 'slack', title: 'Slack', x: 0, y: 0, width: 1920, height: 1080 },
{ id: 102, processName: 'chrome', title: 'Google Chrome', x: 1920, y: 0, width: 1920, height: 1080 },
])
);
return;
}
try {
const psPath = path.resolve(__dirname, './scripts/get-desktop-windows.ps1');
if (fs.existsSync(psPath)) {
const { stdout } = await execAsync(
`powershell -NoProfile -ExecutionPolicy Bypass -File "${psPath}"`,
{ timeout: 3500 }
);
if (stdout && stdout.trim()) {
const parsed = JSON.parse(stdout.trim());
const array = Array.isArray(parsed) ? parsed : [parsed];
const windows = array
.map((item: any) => ({
id: Number(item.Id || item.Pid || 0),
processName: String(item.ProcessName || item.Process || ''),
title: String(item.Title || item.MainWindowTitle || ''),
x: typeof item.X === 'number' ? item.X : undefined,
y: typeof item.Y === 'number' ? item.Y : undefined,
width: typeof item.Width === 'number' ? item.Width : undefined,
height: typeof item.Height === 'number' ? item.Height : undefined,
}))
.filter(
(w) =>
w.title &&
w.title.length > 0 &&
!w.title.toLowerCase().includes('eyefocus') &&
!w.title.toLowerCase().includes('gaze-controlled') &&
!w.processName.toLowerCase().includes('antigravity')
);
if (windows.length > 0) {
windows.sort((a, b) => (a.x ?? 0) - (b.x ?? 0));
res.end(JSON.stringify(windows));
return;
}
}
}
} catch (err) {
console.warn('[Vite Bridge] Error querying open windows:', err);
}
res.end(
JSON.stringify([
{ id: 101, processName: 'slack', title: 'Slack', x: 0, y: 0, width: 1920, height: 1080 },
{ id: 102, processName: 'chrome', title: 'Google Chrome', x: 1920, y: 0, width: 1920, height: 1080 },
])
);
});
server.middlewares.use('/api/focus', async (req, res) => {
if (req.method === 'OPTIONS') {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.statusCode = 204;
res.end();
return;
}
if (req.method !== 'POST') {
res.statusCode = 405;
res.end();
return;
}
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
let body = '';
req.on('data', (chunk) => (body += chunk));
req.on('end', async () => {
try {
const data = JSON.parse(body);
const psPath = path.resolve(__dirname, './scripts/focus-window.ps1');
if (fs.existsSync(psPath) && process.platform === 'win32') {
const titleParam = data.title ? `-Title "${data.title.replace(/[`"$]/g, '`$&')}"` : '';
const procParam = data.processName ? `-ProcessName "${data.processName.replace(/[`"$]/g, '`$&')}"` : '';
const idParam = data.id ? `-Id ${data.id}` : '';
await execAsync(
`powershell -NoProfile -ExecutionPolicy Bypass -File "${psPath}" ${titleParam} ${procParam} ${idParam}`,
{ timeout: 3000 }
);
}
res.end(JSON.stringify({ success: true, message: `Focused ${data.processName || data.title}` }));
} catch (e: any) {
res.statusCode = 500;
res.end(JSON.stringify({ success: false, error: e?.message }));
}
});
});
},
};
}
// https://vitejs.dev/config/
export default defineConfig({
base: './',
plugins: [react(), desktopWindowsBridgePlugin()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
server: {
port: 5173,
strictPort: true,
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
build: {
outDir: 'dist',
emptyOutDir: true,
},
// @ts-ignore vitest config
test: {
globals: true,
environment: 'node',
},
});