Skip to content

Commit 3959ad9

Browse files
refactor: add watcher to file
1 parent 4e4a9c5 commit 3959ad9

File tree

8 files changed

+638
-493
lines changed

8 files changed

+638
-493
lines changed

apps/studio/electron/main/assets/fonts/font.ts

-65
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ import * as pathModule from 'path';
22
import { DefaultSettings } from '@onlook/models/constants';
33
import type { Font } from '@onlook/models/assets';
44
import { camelCase } from 'lodash';
5-
import {
6-
addFontVariableToAppLayout,
7-
addFontVariableToPageApp,
8-
removeFontVariableFromLayout,
9-
} from './layout';
105
import { parse } from '@babel/parser';
116
import traverse from '@babel/traverse';
127
import * as t from '@babel/types';
138
import generate from '@babel/generator';
14-
import { removeFontFromTailwindConfig, updateTailwindFontConfig } from './tailwind';
15-
import { detectRouterType } from '../../pages';
169
import { readFile } from '../../code/files';
1710
import fs from 'fs';
1811
import { extractFontName, getFontFileName } from '@onlook/utility';
@@ -89,19 +82,6 @@ export const ${fontName} = ${importName}({
8982
newContent += fontConfig;
9083

9184
fs.writeFileSync(fontPath, newContent);
92-
93-
await updateTailwindFontConfig(projectRoot, font);
94-
95-
const routerConfig = await detectRouterType(projectRoot);
96-
if (routerConfig) {
97-
if (routerConfig.type === 'app') {
98-
const layoutPath = pathModule.join(routerConfig.basePath, 'layout.tsx');
99-
await addFontVariableToAppLayout(layoutPath, fontName);
100-
} else {
101-
const appPath = pathModule.join(routerConfig.basePath, '_app.tsx');
102-
await addFontVariableToPageApp(appPath, fontName);
103-
}
104-
}
10585
} catch (error) {
10686
console.error('Error adding font:', error);
10787
}
@@ -251,24 +231,6 @@ export async function removeFont(projectRoot: string, font: Font) {
251231

252232
await fs.writeFileSync(fontPath, code);
253233

254-
await removeFontFromTailwindConfig(projectRoot, font);
255-
256-
const routerConfig = await detectRouterType(projectRoot);
257-
if (routerConfig) {
258-
if (routerConfig.type === 'app') {
259-
const layoutPath = pathModule.join(routerConfig.basePath, 'layout.tsx');
260-
await removeFontVariableFromLayout(layoutPath, font.id, ['html']);
261-
} else {
262-
const appPath = pathModule.join(routerConfig.basePath, '_app.tsx');
263-
await removeFontVariableFromLayout(appPath, font.id, [
264-
'div',
265-
'main',
266-
'section',
267-
'body',
268-
]);
269-
}
270-
}
271-
272234
// Delete font files if found
273235
if (fontFilesToDelete.length > 0) {
274236
for (const fileRelativePath of fontFilesToDelete) {
@@ -303,8 +265,6 @@ export async function addFonts(projectRoot: string, fonts: Font[]) {
303265
* Adds a local font to the project by:
304266
* 1. Saving the font files to the fonts folder
305267
* 2. Adding the font configuration to fonts.ts using next/font/local
306-
* 3. Updating Tailwind config with the new font family
307-
* 4. Adding the font variable to the appropriate layout file
308268
*/
309269
export async function addLocalFont(
310270
projectRoot: string,
@@ -403,31 +363,6 @@ export const ${fontName} = localFont({
403363

404364
fs.writeFileSync(fontPath, newContent);
405365

406-
// Update Tailwind config
407-
const font: Font = {
408-
id: fontName,
409-
family: baseFontName,
410-
subsets: ['latin'],
411-
weight: fontConfigs.map((config) => config.weight),
412-
styles: fontConfigs.map((config) => config.style),
413-
variable: `--font-${fontName}`,
414-
type: 'local',
415-
};
416-
417-
await updateTailwindFontConfig(projectRoot, font);
418-
419-
// Update layout file
420-
const routerConfig = await detectRouterType(projectRoot);
421-
if (routerConfig) {
422-
if (routerConfig.type === 'app') {
423-
const layoutPath = pathModule.join(routerConfig.basePath, 'layout.tsx');
424-
await addFontVariableToAppLayout(layoutPath, fontName);
425-
} else {
426-
const appPath = pathModule.join(routerConfig.basePath, '_app.tsx');
427-
await addFontVariableToPageApp(appPath, fontName);
428-
}
429-
}
430-
431366
return fontName;
432367
} catch (error) {
433368
console.error('Error adding local font:', error);

apps/studio/electron/main/assets/fonts/layout.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { parse, type ParseResult } from '@babel/parser';
44
import traverse from '@babel/traverse';
55
import generate from '@babel/generator';
66
import * as pathModule from 'path';
7-
import { readFile, writeFile } from '../../code/files';
7+
import { readFile } from '../../code/files';
88
import { DefaultSettings } from '@onlook/models/constants';
99
import type { Font } from '@onlook/models/assets';
1010
import { detectRouterType } from '../../pages/helpers';
1111
import {
1212
createStringLiteralWithFont,
1313
createTemplateLiteralWithFont,
1414
findFontClass,
15-
filterFontClasses,
1615
removeFontsFromClassName,
1716
} from './utils';
1817
import type { TraverseCallback } from './types';
@@ -105,7 +104,7 @@ async function updateFileWithImport(
105104
newContent = fontImport + '\n' + newContent;
106105
}
107106

108-
await writeFile(filePath, newContent);
107+
fs.writeFileSync(filePath, newContent);
109108
}
110109

111110
/**
@@ -196,15 +195,17 @@ export async function addFontVariableToElement(
196195
}
197196
}
198197

199-
export async function addFontVariableToAppLayout(
200-
layoutPath: string,
201-
fontName: string,
202-
): Promise<void> {
203-
await addFontVariableToElement(layoutPath, fontName, ['html']);
204-
}
205-
206-
export async function addFontVariableToPageApp(appPath: string, fontName: string): Promise<void> {
207-
await addFontVariableToElement(appPath, fontName, ['div', 'main', 'section', 'body']);
198+
export async function addFontVariableToLayout(projectRoot: string, fontName: string) {
199+
const routerConfig = await detectRouterType(projectRoot);
200+
if (routerConfig) {
201+
if (routerConfig.type === 'app') {
202+
const layoutPath = pathModule.join(routerConfig.basePath, 'layout.tsx');
203+
await addFontVariableToElement(layoutPath, fontName, ['html']);
204+
} else {
205+
const appPath = pathModule.join(routerConfig.basePath, '_app.tsx');
206+
await addFontVariableToElement(appPath, fontName, ['div', 'main', 'section', 'body']);
207+
}
208+
}
208209
}
209210

210211
/**
@@ -267,7 +268,7 @@ export async function removeFontVariableFromLayout(
267268
}
268269
}
269270

270-
await writeFile(filePath, newContent);
271+
fs.writeFileSync(filePath, newContent);
271272
}
272273
} catch (error) {
273274
console.error(`Error removing font variable from ${filePath}:`, error);
@@ -316,7 +317,7 @@ export async function updateFontInLayout(
316317
});
317318

318319
if (result) {
319-
await writeFile(filePath, result);
320+
fs.writeFileSync(filePath, result);
320321
}
321322
}
322323

0 commit comments

Comments
 (0)