-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
222 lines (198 loc) · 6.1 KB
/
Copy pathmain.js
File metadata and controls
222 lines (198 loc) · 6.1 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const git = require('./git');
// --- Config file management (lazy init - must not call app.getPath before ready) ---
let configDir = null;
let configPath = null;
function getConfigDir() {
if (!configDir) {
configDir = app.getPath('userData');
}
return configDir;
}
function getConfigPath() {
if (!configPath) {
configPath = path.join(getConfigDir(), 'config.json');
}
return configPath;
}
function getDefaultConfig() {
return { registry: [], workspaces: [] };
}
function loadConfig() {
const cfgPath = getConfigPath();
const cfgDir = getConfigDir();
try {
if (!fs.existsSync(cfgPath)) {
fs.mkdirSync(cfgDir, { recursive: true });
fs.writeFileSync(cfgPath, JSON.stringify(getDefaultConfig(), null, 2));
}
const raw = fs.readFileSync(cfgPath, 'utf-8');
const config = JSON.parse(raw);
if (!Array.isArray(config.registry)) config.registry = [];
if (!Array.isArray(config.workspaces)) config.workspaces = [];
return config;
} catch (e) {
try {
if (fs.existsSync(cfgPath)) {
fs.copyFileSync(cfgPath, cfgPath + '.backup');
}
} catch (_) {}
const def = getDefaultConfig();
fs.writeFileSync(cfgPath, JSON.stringify(def, null, 2));
return def;
}
}
function saveConfig(config) {
fs.writeFileSync(getConfigPath(), JSON.stringify(config, null, 2));
}
// --- Window ---
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 900,
height: 620,
minWidth: 700,
minHeight: 500,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
icon: undefined,
title: 'Git Workspace Manager',
});
mainWindow.setMenuBarVisibility(false);
mainWindow.loadFile('index.html');
// Forward git command logs to renderer
git.setLogger((entry) => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('git-log', entry);
}
});
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => app.quit());
// --- IPC Handlers ---
ipcMain.handle('get-config', () => loadConfig());
ipcMain.handle('save-config', (_e, config) => {
saveConfig(config);
return true;
});
// Add a single repo to registry by picking a folder
ipcMain.handle('pick-repo-folder', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory'],
title: 'Select a Git Repository Folder',
});
if (result.canceled || result.filePaths.length === 0) return null;
const dirPath = result.filePaths[0];
try {
const remoteUrl = await git.gitRemoteUrl(dirPath);
const id = git.parseRepoId(remoteUrl);
if (!id) return { error: 'Could not parse org/repo from remote URL: ' + remoteUrl };
return { id, localPath: dirPath };
} catch (e) {
return { error: e.message };
}
});
// Scan a parent folder for git repos
ipcMain.handle('scan-folder', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory'],
title: 'Select a Parent Folder to Scan for Repositories',
});
if (result.canceled || result.filePaths.length === 0) return [];
const parentDir = result.filePaths[0];
const entries = fs.readdirSync(parentDir, { withFileTypes: true });
const found = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const fullPath = path.join(parentDir, entry.name);
const gitDir = path.join(fullPath, '.git');
if (!fs.existsSync(gitDir)) continue;
try {
const remoteUrl = await git.gitRemoteUrl(fullPath);
const id = git.parseRepoId(remoteUrl);
if (id) found.push({ id, localPath: fullPath });
} catch (_) {}
}
return found;
});
// Get status of a single repo (current branch + dirty)
ipcMain.handle('repo-status', async (_e, localPath) => {
try {
if (!fs.existsSync(localPath)) return { error: 'Path not found' };
const [currentBranch, status] = await Promise.all([
git.gitCurrentBranch(localPath),
git.gitStatus(localPath),
]);
return { currentBranch, dirty: status.length > 0 };
} catch (e) {
return { error: e.message };
}
});
// Get branch list for a repo
ipcMain.handle('repo-branches', async (_e, localPath) => {
try {
return await git.gitBranchList(localPath);
} catch (e) {
return [];
}
});
// Sync a single repo: fetch, optionally checkout, pull
ipcMain.handle('sync-repo', async (_e, localPath, branch) => {
const steps = [];
try {
steps.push('Fetching...');
await git.gitFetch(localPath);
steps.push('Fetched');
if (branch) {
steps.push('Checking out ' + branch + '...');
await git.gitCheckout(localPath, branch);
steps.push('Checked out ' + branch);
}
steps.push('Pulling...');
await git.gitPull(localPath);
steps.push('Pulled');
return { success: true, steps };
} catch (e) {
return { success: false, error: e.message, steps };
}
});
// Export workspace dialog
ipcMain.handle('export-workspace', async (_e, data) => {
const result = await dialog.showSaveDialog(mainWindow, {
title: 'Export Workspace',
defaultPath: data.name.replace(/[^a-zA-Z0-9]/g, '-') + '.json',
filters: [{ name: 'JSON', extensions: ['json'] }],
});
if (result.canceled) return false;
fs.writeFileSync(result.filePath, JSON.stringify(data, null, 2));
return true;
});
// Import workspace dialog
ipcMain.handle('import-workspace', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
title: 'Import Workspace',
filters: [{ name: 'JSON', extensions: ['json'] }],
properties: ['openFile'],
});
if (result.canceled || result.filePaths.length === 0) return null;
try {
const raw = fs.readFileSync(result.filePaths[0], 'utf-8');
return JSON.parse(raw);
} catch (e) {
return { error: 'Invalid JSON file: ' + e.message };
}
});
// Check git availability on startup
ipcMain.handle('check-git', async () => {
try {
await git.gitVersion();
return true;
} catch (_) {
return false;
}
});