Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
1168 lines (971 loc) · 35.1 KB
/
Copy pathmain.js
File metadata and controls
1168 lines (971 loc) · 35.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
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Workaround for Electron 38+ Wayland/X11 drag-and-drop crash
// Must set env var BEFORE requiring electron for it to take effect
if (process.platform === 'linux') {
process.env.ELECTRON_OZONE_PLATFORM_HINT = 'x11';
process.env.GDK_BACKEND = 'x11';
}
const { app, BrowserWindow, ipcMain, dialog, nativeImage } = require('electron');
const path = require('path');
const fs = require('fs').promises;
const fsSync = require('fs');
const archiver = require('archiver');
const extract = require('extract-zip');
const { z } = require('zod');
// Additional GPU flags to prevent atom cache / SIGILL crashes on Linux
if (process.platform === 'linux') {
app.disableHardwareAcceleration();
app.commandLine.appendSwitch('disable-gpu');
app.commandLine.appendSwitch('disable-gpu-compositing');
}
let mainWindow;
const trackedTempFiles = new Set();
const ZOOM_STEP = 0.1;
const MIN_ZOOM_FACTOR = 0.5;
const MAX_ZOOM_FACTOR = 2;
// Input validation schemas
const skillDataSchema = z.object({
name: z.string().min(1).max(255),
description: z.string().max(500).default(''),
content: z.string()
});
const saveSkillSchema = z.object({
skillPath: z.string().min(1),
content: z.string()
});
const createZipSchema = z.object({
skillPath: z.string().min(1),
skillName: z.string().min(1).max(255)
});
const deleteSkillSchema = z.object({
skillPath: z.string().min(1)
});
const createFileSchema = z.object({
skillPath: z.string().min(1),
filePath: z.string().min(1),
content: z.string().default('')
});
const createFolderSchema = z.object({
skillPath: z.string().min(1),
folderPath: z.string().min(1)
});
const deleteFileOrFolderSchema = z.object({
skillPath: z.string().min(1),
targetPath: z.string().min(1)
});
const renameFileOrFolderSchema = z.object({
skillPath: z.string().min(1),
oldPath: z.string().min(1),
newName: z.string().min(1).max(255)
});
const moveFileSchema = z.object({
skillPath: z.string().min(1),
oldPath: z.string().min(1),
newPath: z.string().min(1)
});
const loadFileSchema = z.object({
filePath: z.string().min(1)
});
const importSkillPackageSchema = z.object({
filePath: z.string().min(1).optional(),
fileName: z.string().min(1).optional(),
data: z.any().optional() // Uint8Array from renderer
});
const confirmImportSkillSchema = z.object({
tempPath: z.string().min(1),
skillName: z.string().max(255).optional(),
action: z.enum(['overwrite', 'rename', 'cancel'])
});
// File utilities
const EDITABLE_EXTENSIONS = new Set([
'.md', '.txt', '.js', '.py', '.json', '.html', '.css', '.yaml', '.yml',
'.sh', '.bash', '.lua', '.rb', '.go', '.rs', '.ts', '.tsx', '.jsx'
]);
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
function isEditableFile(filePath) {
const ext = path.extname(filePath).toLowerCase();
return EDITABLE_EXTENSIONS.has(ext);
}
function sanitizeFileName(name) {
// Allow letters, numbers, hyphens, underscores, dots (for extensions)
// Preserve the extension
const sanitized = name.replace(/[^a-zA-Z0-9._\-]/g, '');
if (sanitized.length === 0) {
throw new Error('Invalid filename - must contain at least one valid character');
}
if (sanitized.length > 255) {
throw new Error('Filename is too long (max 255 characters)');
}
return sanitized;
}
function sanitizeRelativePath(relativePath) {
if (!relativePath) {
throw new Error('Invalid path');
}
const segments = relativePath.split(/[/\\]+/).filter(Boolean);
if (segments.length === 0) {
throw new Error('Invalid path');
}
const sanitizedSegments = segments.map((segment) => {
const sanitized = sanitizeFileName(segment);
if (sanitized === '.' || sanitized === '..') {
throw new Error('Invalid path segment');
}
return sanitized;
});
return sanitizedSegments.join(path.sep);
}
function getFileType(filePath) {
const ext = path.extname(filePath).toLowerCase();
if (['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg'].includes(ext)) {
return 'image';
}
if (['.pdf', '.doc', '.docx', '.xls', '.xlsx'].includes(ext)) {
return 'document';
}
if (['.mp4', '.webm', '.mov', '.avi'].includes(ext)) {
return 'video';
}
if (['.mp3', '.wav', '.flac'].includes(ext)) {
return 'audio';
}
if (isEditableFile(filePath)) {
return 'text';
}
return 'binary';
}
async function buildFileTree(dirPath, skillDirPath, relativePath = '') {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
const tree = [];
for (const entry of entries) {
// Skip node_modules and hidden files/folders
if (entry.name.startsWith('.') || entry.name === 'node_modules') {
continue;
}
const fullPath = path.join(dirPath, entry.name);
const relPath = relativePath ? path.join(relativePath, entry.name) : entry.name;
if (entry.isDirectory()) {
const subtree = await buildFileTree(fullPath, skillDirPath, relPath);
tree.push({
name: entry.name,
path: relPath,
type: 'folder',
children: subtree
});
} else {
const stats = await fs.stat(fullPath);
tree.push({
name: entry.name,
path: relPath,
type: 'file',
fileType: getFileType(entry.name),
size: stats.size,
editable: isEditableFile(entry.name)
});
}
}
return tree.sort((a, b) => {
// Folders first, then alphabetical
if (a.type !== b.type) return a.type === 'folder' ? -1 : 1;
return a.name.localeCompare(b.name);
});
}
// Security utility functions
function getSkillsDir() {
return path.join(app.getPath('userData'), 'skills');
}
function clampZoomFactor(zoomFactor) {
return Math.min(MAX_ZOOM_FACTOR, Math.max(MIN_ZOOM_FACTOR, zoomFactor));
}
function adjustWindowZoom(window, direction) {
const webContents = window.webContents;
const currentZoom = webContents.getZoomFactor();
const nextZoom = clampZoomFactor(currentZoom + (direction * ZOOM_STEP));
webContents.setZoomFactor(Math.round(nextZoom * 100) / 100);
}
function validateSkillPath(requestedPath, baseDir = getSkillsDir()) {
// Normalize the path to resolve .. and . references
const normalized = path.normalize(requestedPath);
// If relative path provided, join with base directory
const fullPath = path.isAbsolute(normalized) ? normalized : path.join(baseDir, normalized);
// Ensure the resolved path is within the base directory
const resolvedPath = path.resolve(fullPath);
const resolvedBase = path.resolve(baseDir);
if (!resolvedPath.startsWith(resolvedBase + path.sep) && resolvedPath !== resolvedBase) {
throw new Error('Invalid path - access denied');
}
return resolvedPath;
}
function sanitizeSkillName(name) {
// Allow only lowercase letters, numbers, and hyphens (Claude Desktop requirement)
const sanitized = name.toLowerCase().replace(/[^a-z0-9-]/g, '');
if (sanitized.length === 0) {
throw new Error('Skill name must contain at least one valid character (lowercase letters, numbers, or hyphens)');
}
if (sanitized.length > 255) {
throw new Error('Skill name is too long (max 255 characters)');
}
// Disallow reserved names
if (sanitized === 'skill' || sanitized === '') {
throw new Error('Invalid skill name');
}
return sanitized;
}
// Cleanup temporary files on app exit
app.on('before-quit', async () => {
for (const filePath of trackedTempFiles) {
try {
await fs.unlink(filePath);
} catch (error) {
console.error(`Failed to cleanup temp file ${filePath}:`, error);
}
}
});
// Clean up old temp files on startup
async function cleanupOldTempFiles() {
try {
const tempDir = app.getPath('temp');
const files = await fs.readdir(tempDir);
const skillZips = files.filter(f => f.match(/^.+\.zip$/) && f.includes('skill'));
// Clean up skill ZIPs older than 1 hour
const oneHourAgo = Date.now() - 60 * 60 * 1000;
for (const file of skillZips) {
const filePath = path.join(tempDir, file);
try {
const stats = await fs.stat(filePath);
if (stats.mtimeMs < oneHourAgo) {
await fs.unlink(filePath);
}
} catch (error) {
// Ignore errors for individual files
}
}
} catch (error) {
console.error('Failed to cleanup old temp files:', error);
}
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1400,
height: 900,
backgroundColor: '#141413',
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
},
titleBarStyle: 'hidden',
titleBarOverlay: {
color: '#141413',
symbolColor: '#faf9f5',
height: 40
}
});
mainWindow.loadFile('index.html');
mainWindow.webContents.on('before-input-event', (event, input) => {
if (!input.control && !input.meta) {
return;
}
if (input.key === '+' || input.key === '=') {
event.preventDefault();
adjustWindowZoom(mainWindow, 1);
return;
}
if (input.key === '-' || input.key === '_') {
event.preventDefault();
adjustWindowZoom(mainWindow, -1);
}
});
}
app.whenReady().then(async () => {
await cleanupOldTempFiles();
createWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// IPC Handlers
ipcMain.handle('create-skill', async (event, skillData) => {
try {
// Validate input
const validated = skillDataSchema.parse(skillData);
const sanitizedName = sanitizeSkillName(validated.name);
const skillsDir = getSkillsDir();
const skillDir = path.join(skillsDir, sanitizedName);
// Validate the resulting path is within skills directory
validateSkillPath(skillDir, skillsDir);
await fs.mkdir(skillDir, { recursive: true });
await fs.writeFile(path.join(skillDir, 'SKILL.md'), validated.content, 'utf-8');
return { success: true, path: skillDir };
} catch (error) {
console.error('Error creating skill:', error);
return { success: false, error: 'Failed to create skill' };
}
});
ipcMain.handle('load-skill', async (event, skillPath) => {
try {
const skillsDir = getSkillsDir();
await fs.mkdir(skillsDir, { recursive: true });
try {
const validatedPath = validateSkillPath(skillPath, skillsDir);
if (!fsSync.existsSync(validatedPath)) {
throw new Error('Skill file does not exist');
}
const content = await fs.readFile(validatedPath, 'utf-8');
return { success: true, content, path: validatedPath };
} catch (validationError) {
if (!validationError || validationError.message !== 'Invalid path - access denied') {
throw validationError;
}
// Allow importing external SKILL.md files
const resolvedPath = path.resolve(skillPath);
if (!fsSync.existsSync(resolvedPath)) {
throw new Error('Skill file does not exist');
}
if (path.basename(resolvedPath).toLowerCase() !== 'skill.md') {
throw new Error('Only SKILL.md files can be imported');
}
const content = await fs.readFile(resolvedPath, 'utf-8');
let sanitizedName;
try {
sanitizedName = sanitizeSkillName(path.basename(path.dirname(resolvedPath)));
} catch (nameError) {
sanitizedName = sanitizeSkillName(`imported-skill-${Date.now()}`);
}
let candidateName = sanitizedName;
let targetDir = path.join(skillsDir, candidateName);
let suffix = 1;
while (fsSync.existsSync(path.join(targetDir, 'SKILL.md'))) {
candidateName = `${sanitizedName}-${suffix}`;
targetDir = path.join(skillsDir, candidateName);
suffix += 1;
}
const targetPath = path.join(targetDir, 'SKILL.md');
await fs.mkdir(targetDir, { recursive: true });
await fs.writeFile(targetPath, content, 'utf-8');
return {
success: true,
content,
path: targetPath,
imported: true,
skillName: candidateName
};
}
} catch (error) {
console.error('Error loading skill:', error);
return { success: false, error: error.message || 'Failed to load skill' };
}
});
ipcMain.handle('save-skill', async (event, data) => {
try {
// Validate input
const validated = saveSkillSchema.parse(data);
// Validate path is within skills directory
const validatedPath = validateSkillPath(validated.skillPath);
await fs.writeFile(validatedPath, validated.content, 'utf-8');
return { success: true };
} catch (error) {
console.error('Error saving skill:', error);
return { success: false, error: 'Failed to save skill' };
}
});
ipcMain.handle('create-zip', async (event, data) => {
return new Promise((resolve, reject) => {
try {
// Validate input
const validated = createZipSchema.parse(data);
// Validate path is within skills directory
const validatedPath = validateSkillPath(validated.skillPath);
const skillDir = path.dirname(validatedPath);
// Ensure skill directory exists and is readable
if (!fsSync.existsSync(skillDir)) {
throw new Error('Skill directory does not exist');
}
// Sanitize filename for ZIP
const sanitizedName = validated.skillName.replace(/[^a-zA-Z0-9\-_.]/g, '');
const zipPath = path.join(app.getPath('temp'), `skill-${sanitizedName}-${Date.now()}.zip`);
// Track temp file for cleanup
trackedTempFiles.add(zipPath);
const output = fsSync.createWriteStream(zipPath);
const archive = archiver('zip', { zlib: { level: 9 } });
output.on('close', () => {
resolve({ success: true, zipPath });
});
output.on('error', (err) => {
trackedTempFiles.delete(zipPath);
reject({ success: false, error: 'Failed to create ZIP' });
});
archive.on('error', (err) => {
trackedTempFiles.delete(zipPath);
reject({ success: false, error: 'Failed to create ZIP' });
});
archive.pipe(output);
// Include folder structure in ZIP so Claude Desktop recognizes it as a skill
archive.directory(skillDir, validated.skillName);
archive.finalize();
} catch (error) {
console.error('Error creating ZIP:', error);
reject({ success: false, error: 'Failed to create ZIP' });
}
});
});
// Handle native file drag operation
ipcMain.on('start-drag', (event, zipPath, skillName) => {
try {
// Verify the file exists
if (!fsSync.existsSync(zipPath)) {
console.error('ZIP file does not exist:', zipPath);
event.sender.send('drag-error', 'ZIP file not found');
return;
}
// Resolve an icon for the drag payload
const candidateIconPaths = [
path.join(process.resourcesPath, 'drag-icon.png'), // Packaged builds (extraResources)
path.join(app.getAppPath(), 'icon.png'),
path.join(app.getAppPath(), 'build', 'icons', '64x64.png'),
path.join(__dirname, 'icon.png'),
path.join(__dirname, 'build', 'icons', '64x64.png'),
path.join(__dirname, 'node_modules/app-builder-lib/templates/icons/electron-linux/64x64.png')
];
let dragIcon = null;
for (const iconPath of candidateIconPaths) {
if (!iconPath || !fsSync.existsSync(iconPath)) {
continue;
}
const image = nativeImage.createFromPath(iconPath);
if (image && !image.isEmpty()) {
dragIcon = image;
break;
}
}
if (!dragIcon) {
// Fallback to a 1x1 transparent pixel so Linux has a valid icon
const transparentPixel = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGD4DwABBAEA6drdzQAAAABJRU5ErkJggg==',
'base64'
);
dragIcon = nativeImage.createFromBuffer(transparentPixel);
} else {
const { width, height } = dragIcon.getSize();
const MAX_DRAG_ICON_DIM = 48;
if (width > MAX_DRAG_ICON_DIM || height > MAX_DRAG_ICON_DIM) {
dragIcon = dragIcon.resize({ width: MAX_DRAG_ICON_DIM, height: MAX_DRAG_ICON_DIM, quality: 'best' });
}
}
if (!dragIcon || dragIcon.isEmpty()) {
console.error('Failed to create drag icon');
event.sender.send('drag-error', 'Failed to start drag operation');
return;
}
// Start the native drag operation
event.sender.startDrag({
file: zipPath,
icon: dragIcon
});
} catch (error) {
console.error('Error starting drag:', error);
event.sender.send('drag-error', 'Failed to start drag operation');
}
});
ipcMain.handle('open-file-dialog', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'Skill Files', extensions: ['md'] }
]
});
if (!result.canceled && result.filePaths.length > 0) {
return { success: true, filePath: result.filePaths[0] };
}
return { success: false };
});
ipcMain.handle('list-skills', async () => {
const skillsDir = getSkillsDir();
try {
await fs.mkdir(skillsDir, { recursive: true });
const entries = await fs.readdir(skillsDir, { withFileTypes: true });
const skillList = await Promise.all(
entries
.filter(entry => entry.isDirectory())
.map(async (entry) => {
const skillName = entry.name;
const skillPath = path.join(skillsDir, skillName, 'SKILL.md');
try {
// Validate skill name
sanitizeSkillName(skillName);
// Validate path is within skills directory
validateSkillPath(skillPath, skillsDir);
const content = await fs.readFile(skillPath, 'utf-8');
// Extract description from YAML frontmatter
const descMatch = content.match(/^description:\s*(.+?)(?:\n|$)/m);
const description = descMatch ? descMatch[1].trim() : 'No description';
return {
name: skillName,
path: skillPath,
description: description.substring(0, 200) // Limit description length
};
} catch (error) {
console.error(`Error loading skill ${skillName}:`, error);
return null;
}
})
);
return skillList.filter(Boolean);
} catch (error) {
console.error('Error listing skills:', error);
return [];
}
});
ipcMain.handle('delete-skill', async (event, data) => {
try {
// Validate input
const validated = deleteSkillSchema.parse(data);
// Validate path is within skills directory
const validatedPath = validateSkillPath(validated.skillPath);
// Get the skill directory (parent of SKILL.md file)
const skillDir = path.dirname(validatedPath);
// Ensure skill directory exists
if (!fsSync.existsSync(skillDir)) {
return { success: false, error: 'Skill directory does not exist' };
}
// Delete the entire skill directory
await fs.rm(skillDir, { recursive: true, force: true });
return { success: true };
} catch (error) {
console.error('Error deleting skill:', error);
return { success: false, error: 'Failed to delete skill' };
}
});
// List files in a skill directory with tree structure
ipcMain.handle('list-skill-files', async (event, skillPath) => {
try {
// Validate the skill path first
const validatedPath = validateSkillPath(skillPath);
const skillDir = path.dirname(validatedPath);
if (!fsSync.existsSync(skillDir)) {
return { success: false, error: 'Skill directory does not exist' };
}
const fileTree = await buildFileTree(skillDir, skillDir);
return { success: true, files: fileTree };
} catch (error) {
console.error('Error listing skill files:', error);
return { success: false, error: 'Failed to list skill files' };
}
});
// Create a new file in a skill
ipcMain.handle('create-file', async (event, data) => {
try {
// Validate input
const validated = createFileSchema.parse(data);
// Validate skill path
const skillDirPath = validateSkillPath(validated.skillPath);
const skillDir = path.dirname(skillDirPath);
// Build the full file path
const sanitizedFileName = sanitizeFileName(path.basename(validated.filePath));
const fileDir = path.dirname(validated.filePath);
const fullPath = path.join(skillDir, fileDir, sanitizedFileName);
// Validate the resulting path is within skill directory
const validatedFilePath = validateSkillPath(fullPath, skillDir);
// Ensure parent directory exists
await fs.mkdir(path.dirname(validatedFilePath), { recursive: true });
// Write the file
await fs.writeFile(validatedFilePath, validated.content, 'utf-8');
return { success: true, path: validated.filePath };
} catch (error) {
console.error('Error creating file:', error);
return { success: false, error: 'Failed to create file' };
}
});
// Create a new folder in a skill
ipcMain.handle('create-folder', async (event, data) => {
try {
// Validate input
const validated = createFolderSchema.parse(data);
// Validate skill path
const skillDirPath = validateSkillPath(validated.skillPath);
const skillDir = path.dirname(skillDirPath);
// Build the full folder path
const sanitizedFolderName = sanitizeFileName(path.basename(validated.folderPath));
const folderDir = path.dirname(validated.folderPath);
const fullPath = path.join(skillDir, folderDir, sanitizedFolderName);
// Validate the resulting path is within skill directory
const validatedFolderPath = validateSkillPath(fullPath, skillDir);
// Create the folder
await fs.mkdir(validatedFolderPath, { recursive: true });
return { success: true, path: validated.folderPath };
} catch (error) {
console.error('Error creating folder:', error);
return { success: false, error: 'Failed to create folder' };
}
});
// Delete a file or folder
ipcMain.handle('delete-file-or-folder', async (event, data) => {
try {
// Validate input
const validated = deleteFileOrFolderSchema.parse(data);
// Validate skill path
const skillDirPath = validateSkillPath(validated.skillPath);
const skillDir = path.dirname(skillDirPath);
// Build the full path
const fullPath = path.join(skillDir, validated.targetPath);
// Validate the resulting path is within skill directory
const validatedPath = validateSkillPath(fullPath, skillDir);
// Prevent deletion of SKILL.md
if (path.basename(validatedPath) === 'SKILL.md') {
return { success: false, error: 'Cannot delete SKILL.md - it is required' };
}
// Check if exists
if (!fsSync.existsSync(validatedPath)) {
return { success: false, error: 'File or folder does not exist' };
}
// Delete
await fs.rm(validatedPath, { recursive: true, force: true });
return { success: true };
} catch (error) {
console.error('Error deleting file or folder:', error);
return { success: false, error: 'Failed to delete file or folder' };
}
});
// Rename a file or folder
ipcMain.handle('rename-file-or-folder', async (event, data) => {
try {
// Validate input
const validated = renameFileOrFolderSchema.parse(data);
// Validate skill path
const skillDirPath = validateSkillPath(validated.skillPath);
const skillDir = path.dirname(skillDirPath);
// Build the full paths
const oldFullPath = path.join(skillDir, validated.oldPath);
const validatedOldPath = validateSkillPath(oldFullPath, skillDir);
// Sanitize new name
const sanitizedNewName = sanitizeFileName(validated.newName);
// Prevent renaming SKILL.md
if (path.basename(validatedOldPath) === 'SKILL.md') {
return { success: false, error: 'Cannot rename SKILL.md' };
}
// Build new path (same directory, new name)
const newFullPath = path.join(path.dirname(validatedOldPath), sanitizedNewName);
const validatedNewPath = validateSkillPath(newFullPath, skillDir);
// Check if old exists
if (!fsSync.existsSync(validatedOldPath)) {
return { success: false, error: 'File or folder does not exist' };
}
// Check if new path already exists
if (fsSync.existsSync(validatedNewPath)) {
return { success: false, error: 'A file or folder with that name already exists' };
}
// Rename
await fs.rename(validatedOldPath, validatedNewPath);
return { success: true };
} catch (error) {
console.error('Error renaming file or folder:', error);
return { success: false, error: 'Failed to rename file or folder' };
}
});
// Load a file with metadata
ipcMain.handle('load-file', async (event, data) => {
try {
// Validate input
const validated = loadFileSchema.parse(data);
// Validate path is within skills directory
const validatedPath = validateSkillPath(validated.filePath);
// Check if exists
if (!fsSync.existsSync(validatedPath)) {
return { success: false, error: 'File does not exist' };
}
// Get file stats
const stats = await fs.stat(validatedPath);
// Check file size
if (stats.size > MAX_FILE_SIZE) {
return {
success: true,
content: '',
metadata: {
name: path.basename(validatedPath),
path: validated.filePath,
type: getFileType(validated.filePath),
size: stats.size,
editable: false,
tooBig: true,
error: 'File is too large to edit (max 10MB)'
}
};
}
// Check if editable
const isEditable = isEditableFile(validatedPath);
if (isEditable) {
const content = await fs.readFile(validatedPath, 'utf-8');
return {
success: true,
content,
metadata: {
name: path.basename(validatedPath),
path: validated.filePath,
type: getFileType(validated.filePath),
size: stats.size,
editable: true
}
};
} else {
return {
success: true,
content: '',
metadata: {
name: path.basename(validatedPath),
path: validated.filePath,
type: getFileType(validated.filePath),
size: stats.size,
editable: false
}
};
}
} catch (error) {
console.error('Error loading file:', error);
return { success: false, error: 'Failed to load file' };
}
});
// Upload files (supporting multiple files and folder structure preservation)
ipcMain.handle('upload-files', async (event, data) => {
try {
const { skillPath, files, targetFolder = '' } = data;
// Validate skill path
const skillDirPath = validateSkillPath(skillPath);
const skillDir = path.dirname(skillDirPath);
// Build target directory
const targetPath = targetFolder ? path.join(skillDir, targetFolder) : skillDir;
const validatedTargetPath = validateSkillPath(targetPath, skillDir);
// Ensure target directory exists
await fs.mkdir(validatedTargetPath, { recursive: true });
const uploadedFiles = [];
// Process each file
for (const file of files) {
try {
const sanitizedRelative = sanitizeRelativePath(file.name);
const destinationPath = path.join(validatedTargetPath, sanitizedRelative);
const validatedDestination = validateSkillPath(destinationPath, skillDir);
await fs.mkdir(path.dirname(validatedDestination), { recursive: true });
await fs.writeFile(validatedDestination, file.data);
uploadedFiles.push(sanitizedRelative.split(path.sep).join('/'));
} catch (fileError) {
console.error(`Error uploading file ${file.name}:`, fileError);
}
}
return { success: true, uploadedFiles, count: uploadedFiles.length };
} catch (error) {
console.error('Error uploading files:', error);
return { success: false, error: 'Failed to upload files' };
}
});
// Move file to a different folder
ipcMain.handle('move-file', async (event, data) => {
try {
// Validate input
const validated = moveFileSchema.parse(data);
// Validate skill path
const skillDirPath = validateSkillPath(validated.skillPath);
const skillDir = path.dirname(skillDirPath);
// Build full paths
const oldFullPath = path.join(skillDir, validated.oldPath);
const newFullPath = path.join(skillDir, validated.newPath);
// Validate both paths are within skill directory
const validatedOldPath = validateSkillPath(oldFullPath, skillDir);
const validatedNewPath = validateSkillPath(newFullPath, skillDir);
// Prevent moving SKILL.md
if (path.basename(validatedOldPath) === 'SKILL.md') {
return { success: false, error: 'Cannot move SKILL.md' };
}
// Check if old path exists
if (!fsSync.existsSync(validatedOldPath)) {
return { success: false, error: 'Source file does not exist' };
}
// Check if new path already exists
if (fsSync.existsSync(validatedNewPath)) {
return { success: false, error: 'A file with that name already exists in the target folder' };
}
// Ensure target directory exists
const targetDir = path.dirname(validatedNewPath);
await fs.mkdir(targetDir, { recursive: true });
// Move the file
await fs.rename(validatedOldPath, validatedNewPath);
return { success: true };
} catch (error) {
console.error('Error moving file:', error);
return { success: false, error: 'Failed to move file' };
}
});
// Open file dialog for .skill/.zip files
ipcMain.handle('open-skill-file-dialog', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'Skill Packages', extensions: ['skill', 'zip'] }
]
});
if (!result.canceled && result.filePaths.length > 0) {
return { success: true, filePath: result.filePaths[0] };
}
return { success: false };
});
// Import a skill package (.skill/.zip file)
ipcMain.handle('import-skill-package', async (event, data) => {
try {
// Validate input
const validated = importSkillPackageSchema.parse(data);
// Create temp directory for extraction
const tempDir = path.join(app.getPath('temp'), `skill-import-${Date.now()}`);
await fs.mkdir(tempDir, { recursive: true });
let zipFilePath;
// Handle file data from drag & drop