-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.js
More file actions
272 lines (232 loc) · 8.44 KB
/
main.js
File metadata and controls
272 lines (232 loc) · 8.44 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const { spawn } = require('child_process');
const path = require('path');
const readline = require('readline');
const fs = require('fs');
const fsp = require('fs/promises');
const os = require('os');
const { identifier: APP_IDENTIFIER } = require('./package.json');
let mainWindow;
let processInstanceId = 0;
// Map of instanceId -> { process, terminated }
const spawnedProcesses = new Map();
async function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
},
icon: path.join(__dirname, '..', 'src-tauri', 'icons', 'icon.png')
});
// Load the test page from the http-server
mainWindow.loadURL('http://localhost:8081/test/index.html');
// Open DevTools for debugging
mainWindow.webContents.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null;
});
}
// IPC handlers
// Spawn a child process and forward stdio to the calling renderer.
// Returns an instanceId so the renderer can target the correct process.
ipcMain.handle('spawn-process', async (event, command, args) => {
const instanceId = ++processInstanceId;
const sender = event.sender;
console.log(`Spawning: ${command} ${args.join(' ')} (instance ${instanceId})`);
const childProcess = spawn(command, args, {
stdio: ['pipe', 'pipe', 'pipe']
});
const instance = { process: childProcess, terminated: false };
spawnedProcesses.set(instanceId, instance);
const rl = readline.createInterface({
input: childProcess.stdout,
crlfDelay: Infinity
});
rl.on('line', (line) => {
if (!sender.isDestroyed()) {
sender.send('process-stdout', instanceId, line);
}
});
childProcess.stderr.on('data', (data) => {
if (!sender.isDestroyed()) {
sender.send('process-stderr', instanceId, data.toString());
}
});
childProcess.on('close', (code, signal) => {
instance.terminated = true;
console.log(`Process (instance ${instanceId}) exited with code ${code} and signal ${signal}`);
if (!sender.isDestroyed()) {
sender.send('process-close', instanceId, { code, signal });
}
});
childProcess.on('error', (err) => {
console.error(`Failed to start process (instance ${instanceId}):`, err);
});
return instanceId;
});
// Write data to a specific spawned process stdin
ipcMain.handle('write-to-process', (event, instanceId, data) => {
const instance = spawnedProcesses.get(instanceId);
if (instance && !instance.terminated) {
instance.process.stdin.write(data);
}
});
ipcMain.handle('quit-app', (event, exitCode) => {
console.log('Quit requested with exit code:', exitCode);
gracefulShutdown(exitCode);
});
ipcMain.on('console-log', (event, message) => {
console.log('Renderer:', message);
});
// CLI args (mirrors Tauri's cli.getMatches for --quit-when-done / -q)
ipcMain.handle('get-cli-args', () => {
return process.argv;
});
// App path (repo root when running from source)
ipcMain.handle('get-app-path', () => {
return app.getAppPath();
});
// Directory APIs
ipcMain.handle('get-documents-dir', () => {
// Match Tauri's documentDir which ends with a trailing slash
return path.join(os.homedir(), 'Documents') + path.sep;
});
ipcMain.handle('get-home-dir', () => {
// Match Tauri's homeDir which ends with a trailing slash
const home = os.homedir();
return home.endsWith(path.sep) ? home : home + path.sep;
});
ipcMain.handle('get-temp-dir', () => {
return os.tmpdir();
});
ipcMain.handle('get-app-data-dir', () => {
// Match Tauri's appLocalDataDir which uses the bundle identifier "fs.phcode"
// Linux: ~/.local/share/fs.phcode/
// macOS: ~/Library/Application Support/fs.phcode/
// Windows: %LOCALAPPDATA%/fs.phcode/
const home = os.homedir();
let appDataDir;
switch (process.platform) {
case 'darwin':
appDataDir = path.join(home, 'Library', 'Application Support', APP_IDENTIFIER);
break;
case 'win32':
appDataDir = path.join(process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), APP_IDENTIFIER);
break;
default:
appDataDir = path.join(process.env.XDG_DATA_HOME || path.join(home, '.local', 'share'), APP_IDENTIFIER);
}
return appDataDir + path.sep;
});
// Get Windows drive letters (returns null on non-Windows platforms)
ipcMain.handle('get-windows-drives', async () => {
if (process.platform !== 'win32') {
return null;
}
// On Windows, check which drive letters exist by testing A-Z
const drives = [];
for (let i = 65; i <= 90; i++) { // A-Z
const letter = String.fromCharCode(i);
const drivePath = `${letter}:\\`;
try {
await fsp.access(drivePath);
drives.push(letter);
} catch {
// Drive doesn't exist
}
}
return drives.length > 0 ? drives : null;
});
// Dialogs
ipcMain.handle('show-open-dialog', async (event, options) => {
const result = await dialog.showOpenDialog(mainWindow, options);
return result.filePaths;
});
ipcMain.handle('show-save-dialog', async (event, options) => {
const result = await dialog.showSaveDialog(mainWindow, options);
return result.filePath;
});
// Electron IPC only preserves Error.message when errors cross the IPC boundary (see
// https://github.com/electron/electron/issues/24427). To preserve error.code for FS
// operations, we catch errors and return them as plain objects {error: {code, message}}.
// The preload layer unwraps these back into proper Error objects.
function fsResult(promise) {
return promise.catch(err => {
return { __fsError: true, code: err.code, message: err.message };
});
}
// FS operations
ipcMain.handle('fs-readdir', async (event, dirPath) => {
return fsResult(
fsp.readdir(dirPath, { withFileTypes: true })
.then(entries => entries.map(e => ({ name: e.name, isDirectory: e.isDirectory() })))
);
});
ipcMain.handle('fs-stat', async (event, filePath) => {
return fsResult(
fsp.stat(filePath).then(stats => ({
isFile: stats.isFile(),
isDirectory: stats.isDirectory(),
isSymbolicLink: stats.isSymbolicLink(),
size: stats.size,
mode: stats.mode,
ctimeMs: stats.ctimeMs,
atimeMs: stats.atimeMs,
mtimeMs: stats.mtimeMs,
nlink: stats.nlink,
dev: stats.dev
}))
);
});
ipcMain.handle('fs-mkdir', (event, dirPath, options) => fsResult(fsp.mkdir(dirPath, options)));
ipcMain.handle('fs-unlink', (event, filePath) => fsResult(fsp.unlink(filePath)));
ipcMain.handle('fs-rmdir', (event, dirPath, options) => fsResult(fsp.rm(dirPath, options)));
ipcMain.handle('fs-rename', (event, oldPath, newPath) => fsResult(fsp.rename(oldPath, newPath)));
ipcMain.handle('fs-read-file', (event, filePath) => fsResult(fsp.readFile(filePath)));
ipcMain.handle('fs-write-file', (event, filePath, data) => fsResult(fsp.writeFile(filePath, Buffer.from(data))));
function waitForTrue(fn, timeout) {
return new Promise((resolve) => {
const startTime = Date.now();
function check() {
if (fn()) {
resolve(true);
} else if (Date.now() - startTime > timeout) {
resolve(false);
} else {
setTimeout(check, 50);
}
}
check();
});
}
async function gracefulShutdown(exitCode = 0) {
console.log('Initiating graceful shutdown...');
for (const [, instance] of spawnedProcesses) {
if (!instance.terminated) {
try {
instance.process.kill();
} catch (e) {
// Process may already be terminated
}
await waitForTrue(() => instance.terminated, 1000);
}
}
app.exit(exitCode);
}
app.whenReady().then(async () => {
await createWindow();
});
app.on('window-all-closed', () => {
gracefulShutdown(0);
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Handle process termination signals
process.on('SIGINT', () => gracefulShutdown(0));
process.on('SIGTERM', () => gracefulShutdown(0));