forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcode.ts
More file actions
241 lines (211 loc) · 8.04 KB
/
code.ts
File metadata and controls
241 lines (211 loc) · 8.04 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
import type { CodeDiff, CodeDiffRequest } from '@onlook/models/code';
import { MainChannels } from '@onlook/models/constants';
import type { TemplateNode } from '@onlook/models/element';
import { ipcMain } from 'electron';
import {
addFont,
addLocalFont,
getDefaultFont,
removeFont,
scanFonts,
setDefaultFont,
} from '../assets/fonts/index';
import { FontFileWatcher } from '../assets/fonts/watcher';
import {
deleteTailwindColorGroup,
scanTailwindConfig,
updateTailwindColorConfig,
} from '../assets/styles';
import {
moveFolderPath,
openFileInIde,
openInIde,
pickDirectory,
readCodeBlock,
writeCode,
} from '../code/';
import { getTemplateNodeClass } from '../code/classes';
import { extractComponentsFromDirectory } from '../code/components';
import { getCodeDiffs } from '../code/diff';
import { isChildTextEditable } from '../code/diff/text';
import { readFile } from '../code/files';
import { fileWatcher } from '../code/fileWatcher';
import { getTemplateNodeProps } from '../code/props';
import { getTemplateNodeChild } from '../code/templateNode';
import runManager from '../run';
import { getFileContentWithoutIds } from '../run/cleanup';
import type { CopyCallback, CopyStage } from '@onlook/models';
import { mainWindow } from '..';
const fontFileWatcher = new FontFileWatcher();
export function listenForCodeMessages() {
ipcMain.handle(MainChannels.VIEW_SOURCE_CODE, (e: Electron.IpcMainInvokeEvent, args) => {
const oid = args as string;
const templateNode = runManager.getTemplateNode(oid);
if (!templateNode) {
console.error('Failed to get code block. No template node found.');
return;
}
openInIde(templateNode);
});
ipcMain.handle(MainChannels.VIEW_SOURCE_FILE, (e: Electron.IpcMainInvokeEvent, args) => {
const { filePath, line } = args as {
filePath: string;
line?: number;
};
openFileInIde(filePath, line);
});
ipcMain.handle(MainChannels.GET_CODE_BLOCK, (e: Electron.IpcMainInvokeEvent, args) => {
const { oid, stripIds } = args as {
oid: string;
stripIds: boolean;
};
const templateNode = runManager.getTemplateNode(oid);
if (!templateNode) {
console.error('Failed to get code block. No template node found.');
return null;
}
return readCodeBlock(templateNode, stripIds);
});
ipcMain.handle(MainChannels.GET_FILE_CONTENT, (e: Electron.IpcMainInvokeEvent, args) => {
const { filePath, stripIds } = args as {
filePath: string;
stripIds: boolean;
};
if (stripIds) {
return getFileContentWithoutIds(filePath);
}
return readFile(filePath);
});
ipcMain.handle(MainChannels.GET_TEMPLATE_NODE_CLASS, (e: Electron.IpcMainInvokeEvent, args) => {
const templateNode = args as TemplateNode;
return getTemplateNodeClass(templateNode);
});
ipcMain.handle(MainChannels.WRITE_CODE_DIFFS, async (e: Electron.IpcMainInvokeEvent, args) => {
const codeResults = args as CodeDiff[];
const res = await writeCode(codeResults);
return res;
});
ipcMain.handle(
MainChannels.GET_AND_WRITE_CODE_DIFFS,
async (e: Electron.IpcMainInvokeEvent, args) => {
const { requests, write } = args as { requests: CodeDiffRequest[]; write: boolean };
const codeDiffs = await getCodeDiffs(requests);
if (write) {
return writeCode(codeDiffs);
}
return codeDiffs;
},
);
ipcMain.handle(MainChannels.GET_TEMPLATE_NODE_CHILD, (e: Electron.IpcMainInvokeEvent, args) => {
const { parent, child, index } = args as {
parent: TemplateNode;
child: TemplateNode;
index: number;
};
return getTemplateNodeChild(parent, child, index);
});
ipcMain.handle(MainChannels.PICK_COMPONENTS_DIRECTORY, async () => {
const result = await pickDirectory();
if (result.canceled) {
return null;
}
return result.filePaths.at(0) ?? null;
});
ipcMain.handle(
MainChannels.UPDATE_PROJECT_PATH,
async (e: Electron.IpcMainInvokeEvent, args) => {
const progressCallback: CopyCallback = (stage: CopyStage) => {
mainWindow?.webContents.send(MainChannels.COPY_PROJECT_CALLBACK, {
stage,
});
};
const { currentPath, updatedPath } = args as {
currentPath: string;
updatedPath: string;
};
return moveFolderPath(currentPath, updatedPath, progressCallback);
},
);
ipcMain.handle(MainChannels.GET_COMPONENTS, async (_, args) => {
if (typeof args !== 'string') {
throw new Error('`args` must be a string');
}
const result = extractComponentsFromDirectory(args);
return result;
});
ipcMain.handle(
MainChannels.IS_CHILD_TEXT_EDITABLE,
async (e: Electron.IpcMainInvokeEvent, args) => {
const { oid } = args as { oid: string };
return isChildTextEditable(oid);
},
);
ipcMain.handle(MainChannels.GET_TEMPLATE_NODE_PROPS, (e: Electron.IpcMainInvokeEvent, args) => {
const templateNode = args as TemplateNode;
return getTemplateNodeProps(templateNode);
});
ipcMain.handle(
MainChannels.SCAN_TAILWIND_CONFIG,
async (e: Electron.IpcMainInvokeEvent, args) => {
const { projectRoot } = args as { projectRoot: string };
return scanTailwindConfig(projectRoot);
},
);
ipcMain.handle(MainChannels.UPDATE_TAILWIND_CONFIG, async (e, args) => {
const { projectRoot, originalKey, newColor, newName, parentName, theme } = args;
return updateTailwindColorConfig(
projectRoot,
originalKey,
newColor,
newName,
theme,
parentName,
);
});
ipcMain.handle(MainChannels.DELETE_TAILWIND_CONFIG, async (_, args) => {
const { projectRoot, groupName, colorName } = args;
return deleteTailwindColorGroup(projectRoot, groupName, colorName);
});
ipcMain.handle(MainChannels.SCAN_FONTS, async (_, args) => {
const { projectRoot } = args;
return scanFonts(projectRoot);
});
ipcMain.handle(MainChannels.ADD_FONT, async (_, args) => {
const { projectRoot, font } = args;
return addFont(projectRoot, font);
});
ipcMain.handle(MainChannels.REMOVE_FONT, async (_, args) => {
const { projectRoot, font } = args;
return removeFont(projectRoot, font);
});
ipcMain.handle(MainChannels.SET_FONT, async (_, args) => {
const { projectRoot, font } = args;
return setDefaultFont(projectRoot, font);
});
ipcMain.handle(MainChannels.GET_DEFAULT_FONT, async (_, args) => {
const { projectRoot } = args;
return getDefaultFont(projectRoot);
});
ipcMain.handle(MainChannels.UPLOAD_FONTS, async (_, args) => {
const { projectRoot, fontFiles } = args;
return addLocalFont(projectRoot, fontFiles);
});
ipcMain.handle(MainChannels.WATCH_FONT_FILE, async (_, args) => {
const { projectRoot } = args;
return fontFileWatcher.watch(projectRoot);
});
ipcMain.handle(MainChannels.WATCH_FILE, async (e: Electron.IpcMainInvokeEvent, args) => {
const { filePath } = args as { filePath: string };
return fileWatcher.watchFile(filePath);
});
ipcMain.handle(MainChannels.UNWATCH_FILE, (e: Electron.IpcMainInvokeEvent, args) => {
const { filePath } = args as { filePath: string };
fileWatcher.unwatchFile(filePath);
return true;
});
ipcMain.handle(MainChannels.MARK_FILE_MODIFIED, (e: Electron.IpcMainInvokeEvent, args) => {
const { filePath } = args as { filePath: string };
fileWatcher.markFileAsModified(filePath);
return true;
});
}