-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron-main.js
More file actions
734 lines (663 loc) · 26.8 KB
/
electron-main.js
File metadata and controls
734 lines (663 loc) · 26.8 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
const { app, BrowserWindow, ipcMain, shell, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const os = require('os');
const { spawn } = require('child_process');
// CSS Processing
const postcss = require('postcss');
const tailwindcss = require('tailwindcss');
const autoprefixer = require('autoprefixer');
let mainWindow;
let rendererRecoveryAttempts = 0;
let rendererRecoveryWindowStart = 0;
const RENDERER_RECOVERY_WINDOW_MS = 60000;
const MAX_RENDERER_RECOVERY_ATTEMPTS = 1;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1400,
height: 900,
titleBarStyle: 'hiddenInset',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
sandbox: false
},
backgroundColor: '#0f172a',
title: "LocalShip"
});
const isDev = !app.isPackaged && process.env.NODE_ENV !== 'production';
if (isDev) {
mainWindow.loadURL('http://localhost:5173');
} else {
mainWindow.loadFile(path.join(__dirname, 'dist/index.html'));
}
mainWindow.webContents.on('did-fail-load', (_event, code, desc, url) => {
dialog.showErrorBox('Renderer Load Failed', `${code} ${desc}\n${url}`);
});
mainWindow.webContents.on('render-process-gone', (_event, details) => {
const reason = details?.reason || 'unknown';
const now = Date.now();
if (!rendererRecoveryWindowStart || (now - rendererRecoveryWindowStart) > RENDERER_RECOVERY_WINDOW_MS) {
rendererRecoveryWindowStart = now;
rendererRecoveryAttempts = 0;
}
const recoverable = reason === 'oom' || reason === 'crashed';
if (recoverable && rendererRecoveryAttempts < MAX_RENDERER_RECOVERY_ATTEMPTS && mainWindow && !mainWindow.isDestroyed()) {
rendererRecoveryAttempts += 1;
setTimeout(() => {
if (mainWindow && !mainWindow.isDestroyed()) {
try {
mainWindow.webContents.reloadIgnoringCache();
} catch {}
}
}, 300);
return;
}
dialog.showErrorBox(
'Renderer Recovery Failed',
`Renderer process exited (${reason}). Please restart LocalShip if this repeats.`
);
});
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
// --- TAILWIND JIT COMPILATION ---
ipcMain.handle('compile-tailwind', async (event, { files, configCode, cssInput }) => {
try {
// Transform files into Tailwind's expected content format
const content = Object.entries(files || {})
.filter(([filename, code]) => {
const ext = path.extname(filename).toLowerCase();
if (ASSET_EXTENSIONS.has(ext)) return false;
if (isEncodedAssetText(String(code || ''))) return false;
return true;
})
.map(([filename, code]) => ({
raw: code,
extension: filename.split('.').pop()
}))
.filter((entry) => typeof entry.raw === 'string' && entry.raw.trim().length > 0);
// Simple config parsing
let userConfig = { content: ["./**/*.{js,ts,jsx,tsx,html}"] };
try {
const configMatch = configCode.match(/module\.exports\s*=\s*(\{[\s\S]*\})/);
if (configMatch) {
const parsed = Function(`"use strict"; return (${configMatch[1]})`)();
if (parsed && typeof parsed === 'object') userConfig = parsed;
}
} catch (e) {
console.warn("Config parse failed, using default.");
}
if (content.length === 0) {
content.push({ raw: '<div class="p-2 text-sm"></div>', extension: 'html' });
}
userConfig.content = content;
const result = await postcss([
tailwindcss(userConfig),
autoprefixer
]).process(cssInput, { from: 'input.css' });
return { css: result.css, warning: content.length === 1 && content[0].raw === '<div class="p-2 text-sm"></div>' ? 'tailwind_fallback_content_used' : undefined };
} catch (error) {
return { error: error.message };
}
});
// --- DIALOGS & BUILD SYSTEM ---
ipcMain.handle('select-directory', async () => {
const result = await dialog.showOpenDialog(mainWindow, { properties: ['openDirectory', 'createDirectory'] });
return result.canceled ? null : result.filePaths[0];
});
// --- PROJECT STORAGE ---
const safeSlug = (name) => name.toLowerCase().replace(/[^a-z0-9-_]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '') || 'localship-project';
const ensureDir = (dir) => fs.mkdirSync(dir, { recursive: true });
const versionsRoot = (projectDir) => path.join(projectDir, '.localship');
const versionsIndex = (projectDir) => path.join(versionsRoot(projectDir), 'versions.json');
const versionsDir = (projectDir) => path.join(versionsRoot(projectDir), 'versions');
const workspaceIndex = (projectDir) => path.join(versionsRoot(projectDir), 'workspace.json');
const isIgnoredPath = (segment) => {
return ['node_modules', '.localship', '.git', 'dist', 'build', 'release', 'out'].includes(segment);
};
const ASSET_PREFIX = '__LOCALSHIP_ASSET_V1__:';
const ASSET_EXTENSIONS = new Set([
'.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.ico', '.bmp',
'.mp3', '.wav', '.ogg', '.flac', '.aac', '.m4a',
'.mp4', '.webm', '.mov',
'.glb', '.gltf', '.bin',
'.woff', '.woff2', '.ttf', '.otf',
'.pdf'
]);
const TEXT_EXTENSIONS = new Set(['.html', '.tsx', '.ts', '.js', '.jsx', '.css', '.json', '.md', '.txt']);
const MIME_BY_EXTENSION = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.bmp': 'image/bmp',
'.mp3': 'audio/mpeg',
'.wav': 'audio/wav',
'.ogg': 'audio/ogg',
'.flac': 'audio/flac',
'.aac': 'audio/aac',
'.m4a': 'audio/mp4',
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.mov': 'video/quicktime',
'.glb': 'model/gltf-binary',
'.gltf': 'model/gltf+json',
'.bin': 'application/octet-stream',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.otf': 'font/otf',
'.pdf': 'application/pdf'
};
const inferMimeType = (filename) => MIME_BY_EXTENSION[path.extname(filename).toLowerCase()] || 'application/octet-stream';
const isEncodedAssetText = (value) => typeof value === 'string' && value.startsWith(ASSET_PREFIX);
const encodeBinaryAsset = (filename, buffer) => {
const meta = {
mime: inferMimeType(filename),
name: path.basename(filename),
size: buffer.length
};
return `${ASSET_PREFIX}${JSON.stringify(meta)}\n${buffer.toString('base64')}`;
};
const decodeBinaryAsset = (content) => {
if (!isEncodedAssetText(content)) return null;
const split = content.indexOf('\n');
if (split < 0) return null;
const metaRaw = content.slice(ASSET_PREFIX.length, split).trim();
const base64 = content.slice(split + 1).trim();
if (!metaRaw || !base64) return null;
try {
const meta = JSON.parse(metaRaw);
const buffer = Buffer.from(base64, 'base64');
return { meta, buffer };
} catch {
return null;
}
};
const readProjectFiles = (projectDir) => {
const files = {};
const walk = (dir) => {
const entries = fs.readdirSync(dir, { withFileTypes: true });
entries.forEach((entry) => {
if (entry.name.startsWith('.DS_Store')) return;
if (isIgnoredPath(entry.name)) return;
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
walk(fullPath);
return;
}
const rel = path.relative(projectDir, fullPath);
const ext = path.extname(entry.name).toLowerCase();
if (!TEXT_EXTENSIONS.has(ext) && !ASSET_EXTENSIONS.has(ext)) return;
try {
if (TEXT_EXTENSIONS.has(ext)) {
files[rel] = fs.readFileSync(fullPath, 'utf-8');
} else {
files[rel] = encodeBinaryAsset(rel, fs.readFileSync(fullPath));
}
} catch {}
});
};
walk(projectDir);
return files;
};
ipcMain.handle('project-init', async (_event, { baseDir, name }) => {
const projectDir = path.join(baseDir, safeSlug(name));
ensureDir(projectDir);
ensureDir(versionsRoot(projectDir));
ensureDir(versionsDir(projectDir));
if (!fs.existsSync(versionsIndex(projectDir))) {
fs.writeFileSync(versionsIndex(projectDir), JSON.stringify([], null, 2), 'utf-8');
}
return { projectDir };
});
ipcMain.handle('project-validate', async (_event, { projectDir }) => {
try {
return { exists: !!projectDir && fs.existsSync(projectDir) };
} catch {
return { exists: false };
}
});
ipcMain.handle('project-save-workspace', async (_event, { projectDir, files }) => {
try {
if (!projectDir) return { success: false, error: 'Missing projectDir' };
ensureDir(projectDir);
ensureDir(versionsRoot(projectDir));
const previousIndex = fs.existsSync(workspaceIndex(projectDir))
? JSON.parse(fs.readFileSync(workspaceIndex(projectDir), 'utf-8'))
: { files: [] };
const previousFiles = Array.isArray(previousIndex.files) ? previousIndex.files : [];
const nextFiles = Object.keys(files || {});
// Remove files that were previously managed but are no longer present.
previousFiles.forEach((filename) => {
if (!nextFiles.includes(filename)) {
const targetPath = path.join(projectDir, filename);
if (fs.existsSync(targetPath)) {
fs.unlinkSync(targetPath);
}
}
});
for (const [filename, content] of Object.entries(files || {})) {
const targetPath = path.join(projectDir, filename);
ensureDir(path.dirname(targetPath));
fs.writeFileSync(targetPath, content, 'utf-8');
}
fs.writeFileSync(workspaceIndex(projectDir), JSON.stringify({ files: nextFiles, savedAt: new Date().toISOString() }, null, 2), 'utf-8');
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('project-load-workspace', async (_event, { projectDir }) => {
try {
if (!projectDir) return { files: null };
if (!fs.existsSync(workspaceIndex(projectDir))) return { files: null };
const index = JSON.parse(fs.readFileSync(workspaceIndex(projectDir), 'utf-8'));
const fileList = Array.isArray(index.files) ? index.files : [];
const files = {};
fileList.forEach((filename) => {
const targetPath = path.join(projectDir, filename);
if (fs.existsSync(targetPath)) {
files[filename] = fs.readFileSync(targetPath, 'utf-8');
}
});
return { files };
} catch {
return { files: null };
}
});
ipcMain.handle('project-import', async (_event, { projectDir }) => {
try {
if (!projectDir || !fs.existsSync(projectDir)) return { files: {} };
ensureDir(versionsRoot(projectDir));
ensureDir(versionsDir(projectDir));
if (!fs.existsSync(versionsIndex(projectDir))) {
fs.writeFileSync(versionsIndex(projectDir), JSON.stringify([], null, 2), 'utf-8');
}
const files = readProjectFiles(projectDir);
fs.writeFileSync(workspaceIndex(projectDir), JSON.stringify({ files: Object.keys(files), savedAt: new Date().toISOString() }, null, 2), 'utf-8');
return { files };
} catch {
return { files: {} };
}
});
ipcMain.handle('project-list-versions', async (_event, { projectDir }) => {
try {
if (!fs.existsSync(versionsIndex(projectDir))) return [];
const raw = fs.readFileSync(versionsIndex(projectDir), 'utf-8');
const list = JSON.parse(raw);
return Array.isArray(list) ? list : [];
} catch {
return [];
}
});
ipcMain.handle('project-save-version', async (_event, { projectDir, version }) => {
try {
ensureDir(versionsRoot(projectDir));
ensureDir(versionsDir(projectDir));
const id = version.id;
const filePath = path.join(versionsDir(projectDir), `${id}.json`);
fs.writeFileSync(filePath, JSON.stringify({ files: version.files, meta: { id, label: version.label, createdAt: version.createdAt, summary: version.summary } }, null, 2), 'utf-8');
const listRaw = fs.existsSync(versionsIndex(projectDir)) ? fs.readFileSync(versionsIndex(projectDir), 'utf-8') : '[]';
const list = Array.isArray(JSON.parse(listRaw)) ? JSON.parse(listRaw) : [];
const existing = list.find((v) => v.id === id);
if (!existing) {
list.push({ id, label: version.label, createdAt: version.createdAt, summary: version.summary });
fs.writeFileSync(versionsIndex(projectDir), JSON.stringify(list, null, 2), 'utf-8');
}
return { success: true };
} catch (e) {
return { success: false, error: e.message };
}
});
ipcMain.handle('project-load-version', async (_event, { projectDir, id }) => {
const filePath = path.join(versionsDir(projectDir), `${id}.json`);
const raw = fs.readFileSync(filePath, 'utf-8');
const parsed = JSON.parse(raw);
return { files: parsed.files || {}, meta: parsed.meta || { id } };
});
ipcMain.handle('build-app', async (event, data) => {
const log = (msg) => event.sender.send('build-log', msg);
const err = (msg) => event.sender.send('build-error', msg);
let buildRoot = null;
let outputDir = null;
const cleanupArtifacts = (dir, targetPlatform) => {
if (!dir || !fs.existsSync(dir)) return [];
const entries = fs.readdirSync(dir, { withFileTypes: true });
const keepExts = targetPlatform?.startsWith('mac')
? ['.dmg']
: targetPlatform === 'windows'
? ['.exe']
: targetPlatform === 'linux'
? ['.appimage']
: [];
const removed = [];
entries.forEach((entry) => {
if (!entry.isFile()) return;
const fullPath = path.join(dir, entry.name);
const lower = entry.name.toLowerCase();
const ext = path.extname(lower);
if (keepExts.length > 0) {
if (!keepExts.includes(ext)) {
fs.unlinkSync(fullPath);
removed.push(entry.name);
}
return;
}
if (lower.endsWith('.blockmap') || lower.endsWith('.zip') || lower.endsWith('.yml') || lower.endsWith('.yaml')) {
fs.unlinkSync(fullPath);
removed.push(entry.name);
}
});
return removed;
};
const findArtifacts = (dir, depth = 2) => {
if (!dir || !fs.existsSync(dir) || depth < 0) return [];
const entries = fs.readdirSync(dir, { withFileTypes: true });
const artifacts = [];
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
artifacts.push(...findArtifacts(fullPath, depth - 1));
} else {
const lower = entry.name.toLowerCase();
if (lower.endsWith('.dmg') || lower.endsWith('.exe') || lower.endsWith('.appimage')) {
artifacts.push(fullPath);
}
}
}
return artifacts;
};
const selectPrimaryArtifact = (artifacts, targetPlatform) => {
if (!artifacts || artifacts.length === 0) return null;
const preferExt = targetPlatform?.startsWith('mac')
? '.dmg'
: targetPlatform === 'windows'
? '.exe'
: targetPlatform === 'linux'
? '.appimage'
: '';
const sorted = [...artifacts].sort((a, b) => {
try {
return fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs;
} catch {
return 0;
}
});
if (preferExt) {
const preferred = sorted.find((p) => p.toLowerCase().endsWith(preferExt));
if (preferred) return preferred;
}
return sorted[0];
};
try {
if (!data || !data.files || !data.appName) {
return { success: false, error: "Invalid build payload." };
}
buildRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'localship-build-'));
log(`Build root: ${buildRoot}`);
const projectFiles = { ...(data.files || {}) };
const run = (cmd, args) => new Promise((resolve, reject) => {
log(`$ ${cmd} ${args.join(' ')}`);
const child = spawn(cmd, args, { cwd: buildRoot, shell: false });
child.stdout.on('data', (d) => log(d.toString()));
child.stderr.on('data', (d) => log(d.toString()));
child.on('error', (e) => reject(e));
child.on('close', (code) => {
if (code === 0) resolve();
else reject(new Error(`${cmd} exited with code ${code}`));
});
});
const writeFile = (filename, content) => {
const targetPath = path.join(buildRoot, filename);
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
const decodedAsset = decodeBinaryAsset(typeof content === 'string' ? content : '');
if (decodedAsset) {
fs.writeFileSync(targetPath, decodedAsset.buffer);
return;
}
fs.writeFileSync(targetPath, typeof content === 'string' ? content : String(content ?? ''), 'utf-8');
};
const resolveEntry = () => {
const candidates = [
'index.tsx', 'index.jsx', 'main.tsx', 'main.jsx',
'src/index.tsx', 'src/index.jsx', 'src/main.tsx', 'src/main.jsx'
];
return candidates.find((name) => projectFiles[name] !== undefined) || '';
};
const ensureIndexHtml = (entryFile) => {
if (projectFiles['index.html']) return;
const entrySrc = `/${entryFile.replace(/\\/g, '/')}`;
projectFiles['index.html'] = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>${data.appName || 'LocalShip App'}</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="${entrySrc}"></script>
</body>
</html>`;
};
const ensureBuildConfig = () => {
if (!projectFiles['vite.config.ts'] && !projectFiles['vite.config.js']) {
projectFiles['vite.config.ts'] = `import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
base: './',
plugins: [react()],
});
`;
}
};
const normalizeDistAssetPaths = () => {
const distDir = path.join(buildRoot, 'dist');
const indexPath = path.join(distDir, 'index.html');
if (!fs.existsSync(indexPath)) return;
let html = fs.readFileSync(indexPath, 'utf-8');
html = html.replace(/(src|href)=["']\/(?!\/)/g, '$1="./');
fs.writeFileSync(indexPath, html, 'utf-8');
const assetsDir = path.join(distDir, 'assets');
if (!fs.existsSync(assetsDir)) return;
const entries = fs.readdirSync(assetsDir);
entries.forEach((name) => {
if (!name.endsWith('.css')) return;
const cssPath = path.join(assetsDir, name);
let css = fs.readFileSync(cssPath, 'utf-8');
css = css.replace(/url\((['"]?)\/(?!\/)/g, 'url($1./');
fs.writeFileSync(cssPath, css, 'utf-8');
});
};
const parsePackage = () => {
let parsed = {};
try {
parsed = JSON.parse(data.packageJson || '{}');
} catch {
parsed = {};
}
const dependencies = { ...(parsed.dependencies || {}) };
const devDependencies = { ...(parsed.devDependencies || {}) };
dependencies.react = dependencies.react || '^18.2.0';
dependencies['react-dom'] = dependencies['react-dom'] || '^18.2.0';
dependencies.three = dependencies.three || '^0.179.1';
dependencies.postcss = dependencies.postcss || '^8.4.38';
dependencies.tailwindcss = dependencies.tailwindcss || '^3.4.17';
dependencies.autoprefixer = dependencies.autoprefixer || '^10.4.19';
devDependencies.electron = devDependencies.electron || '^30.0.0';
devDependencies['electron-builder'] = devDependencies['electron-builder'] || '^24.0.0';
devDependencies.vite = devDependencies.vite || '^5.4.8';
devDependencies['@vitejs/plugin-react'] = devDependencies['@vitejs/plugin-react'] || '^4.3.2';
devDependencies.typescript = devDependencies.typescript || '^5.5.4';
devDependencies['@types/react'] = devDependencies['@types/react'] || '^18.3.3';
devDependencies['@types/react-dom'] = devDependencies['@types/react-dom'] || '^18.3.0';
return {
...parsed,
name: (parsed.name || data.appName || 'localship-app').toLowerCase().replace(/[^a-z0-9-_]/g, '-'),
private: true,
main: 'main.js',
scripts: {
...(parsed.scripts || {}),
dev: parsed.scripts?.dev || 'vite',
'build:web': parsed.scripts?.['build:web'] || 'vite build',
start: parsed.scripts?.start || 'electron .',
dist: parsed.scripts?.dist || 'npm run build:web && electron-builder'
},
dependencies,
devDependencies
};
};
const compileCssForBuild = async () => {
const cssInput = projectFiles['input.css'] || '@tailwind base;\n@tailwind components;\n@tailwind utilities;';
const content = Object.entries(projectFiles)
.filter(([filename, code]) => {
const ext = path.extname(filename).toLowerCase();
if (ASSET_EXTENSIONS.has(ext)) return false;
if (isEncodedAssetText(String(code || ''))) return false;
return true;
})
.map(([filename, code]) => ({
raw: code,
extension: filename.split('.').pop()
}));
let userConfig = { content: ["./**/*.{js,ts,jsx,tsx,html}"] };
const configCode = projectFiles['tailwind.config.js'] || '';
try {
const configMatch = configCode.match(/module\.exports\s*=\s*(\{[\s\S]*\})/);
if (configMatch) {
userConfig = Function(`"use strict"; return (${configMatch[1]})`)();
}
} catch (e) {
log("Tailwind config parse failed during build; using default.");
}
userConfig.content = content;
const result = await postcss([tailwindcss(userConfig), autoprefixer]).process(cssInput, { from: undefined });
return result.css;
};
const ensureCssImport = (entryFile) => {
const importTarget = '__localship.generated.css';
const entryDir = path.dirname(entryFile);
const relative = path.relative(entryDir, importTarget).replace(/\\/g, '/');
const importPath = relative.startsWith('.') ? relative : `./${relative}`;
const source = projectFiles[entryFile] || '';
if (source.includes(importTarget)) return;
projectFiles[entryFile] = `import '${importPath}';\n${source}`;
};
let entry = resolveEntry();
if (!entry) {
projectFiles['App.tsx'] = projectFiles['App.tsx'] || `import React from 'react';
const App: React.FC = () => <div style={{ padding: 24 }}>LocalShip App</div>;
export default App;`;
projectFiles['index.tsx'] = `import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.tsx';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);`;
entry = 'index.tsx';
}
ensureIndexHtml(entry);
ensureBuildConfig();
projectFiles['__localship.generated.css'] = await compileCssForBuild();
ensureCssImport(entry);
for (const [filename, content] of Object.entries(projectFiles)) {
writeFile(filename, content);
}
writeFile('main.js', data.mainJs || '');
writeFile('preload.js', data.preloadJs || '');
writeFile('package.json', JSON.stringify(parsePackage(), null, 2));
writeFile('README.md', data.readme || '');
if (data.iconBuffer) {
const buildDir = path.join(buildRoot, 'build');
fs.mkdirSync(buildDir, { recursive: true });
fs.writeFileSync(path.join(buildDir, 'icon.png'), Buffer.from(data.iconBuffer));
}
log("Installing dependencies...");
await run(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['install', '--silent']);
log("Building web assets...");
await run(process.platform === 'win32' ? 'npx.cmd' : 'npx', ['vite', 'build', '--base', './']);
normalizeDistAssetPaths();
const distIndexPath = path.join(buildRoot, 'dist', 'index.html');
if (!fs.existsSync(distIndexPath)) {
throw new Error('Web build validation failed: dist/index.html missing.');
}
const distIndex = fs.readFileSync(distIndexPath, 'utf-8');
if (!/id=["']root["']/.test(distIndex) || !/<script/i.test(distIndex)) {
throw new Error('Web build validation failed: dist/index.html missing root/script wiring.');
}
outputDir = data.outputDir || path.join(buildRoot, 'release');
const config = {
appId: "com.localship.app",
productName: data.appName,
directories: { output: outputDir },
files: ["dist/**/*", "main.js", "preload.js", "package.json"],
mac: { target: ["dmg"], category: "public.app-category.developer-tools" },
win: { target: "nsis" },
linux: { target: "AppImage" },
asar: true
};
if (data.iconBuffer) {
config.mac.icon = "build/icon.png";
config.win.icon = "build/icon.png";
config.linux.icon = "build/icon.png";
}
fs.writeFileSync(path.join(buildRoot, 'electron-builder.json'), JSON.stringify(config, null, 2), 'utf-8');
log("Running electron-builder...");
const platform = data.targetPlatform || '';
const args = ['electron-builder', '--config', 'electron-builder.json'];
if (platform.startsWith('mac')) {
args.push('--mac');
if (platform.includes('arm')) args.push('--arm64');
if (platform.includes('intel')) args.push('--x64');
} else if (platform === 'windows') {
args.push('--win');
} else if (platform === 'linux') {
args.push('--linux');
}
await run(process.platform === 'win32' ? 'npx.cmd' : 'npx', args);
const removed = cleanupArtifacts(outputDir, data.targetPlatform);
if (removed.length > 0) {
log(`Cleaned extra artifacts: ${removed.join(', ')}`);
}
const artifacts = findArtifacts(outputDir);
const primaryArtifact = selectPrimaryArtifact(artifacts, data.targetPlatform);
if (!primaryArtifact) {
throw new Error('Build completed but no installable artifact was found.');
}
log(`Build complete. Output: ${outputDir}`);
return {
success: true,
path: outputDir,
primaryArtifact,
artifacts
};
} catch (e) {
const resolvedOutputDir = outputDir || data?.outputDir || null;
const artifacts = resolvedOutputDir ? findArtifacts(resolvedOutputDir) : [];
if (artifacts.length > 0) {
const primaryArtifact = selectPrimaryArtifact(artifacts, data?.targetPlatform);
const warning = `Builder reported an error, but artifacts were produced: ${artifacts.map(p => path.basename(p)).join(', ')}`;
err(`Build warning: ${e.message}`);
log(`Build warning: ${warning}`);
return {
success: true,
path: resolvedOutputDir || undefined,
warning: `${e.message}. ${warning}`,
primaryArtifact: primaryArtifact || undefined,
artifacts
};
}
err(`Build failed: ${e.message}`);
return { success: false, error: e.message };
}
});