Skip to content

Commit 55e0fb9

Browse files
authored
chore: update parser exports (#2904)
1 parent d7d9581 commit 55e0fb9

37 files changed

+186
-125
lines changed

apps/web/client/src/app/projects/import/local/_context/index.tsx

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
'use client';
22

3-
import { ProcessedFileType, type NextJsProjectValidation, type ProcessedFile } from '@/app/projects/types';
4-
import { api } from '@/trpc/react';
5-
import { Routes } from '@/utils/constants';
6-
import { CodeProvider, createCodeProviderClient, Provider } from '@onlook/code-provider';
3+
import type { ReactNode } from 'react';
4+
import { createContext, useContext, useState } from 'react';
5+
import { useRouter } from 'next/navigation';
6+
7+
import type { Provider } from '@onlook/code-provider';
8+
import { CodeProvider, createCodeProviderClient } from '@onlook/code-provider';
79
import { NEXT_JS_FILE_EXTENSIONS, SandboxTemplates, Templates } from '@onlook/constants';
810
import { RouterType } from '@onlook/models';
911
import { generate, getAstFromContent, injectPreloadScript } from '@onlook/parser';
1012
import { isRootLayoutFile, isTargetFile } from '@onlook/utility';
11-
import { useRouter } from 'next/navigation';
12-
import type { ReactNode } from 'react';
13-
import { createContext, useContext, useState } from 'react';
13+
14+
import type { NextJsProjectValidation, ProcessedFile } from '@/app/projects/types';
15+
import { ProcessedFileType } from '@/app/projects/types';
16+
import { api } from '@/trpc/react';
17+
import { Routes } from '@/utils/constants';
1418

1519
export interface Project {
1620
name: string;
@@ -44,7 +48,11 @@ const ProjectCreationContext = createContext<ProjectCreationContextValue | undef
4448
export function detectPortFromPackageJson(packageJsonFile: ProcessedFile | undefined): number {
4549
const defaultPort = 3000;
4650

47-
if (!packageJsonFile || typeof packageJsonFile.content !== 'string' || packageJsonFile.type !== ProcessedFileType.TEXT) {
51+
if (
52+
!packageJsonFile ||
53+
typeof packageJsonFile.content !== 'string' ||
54+
packageJsonFile.type !== ProcessedFileType.TEXT
55+
) {
4856
return defaultPort;
4957
}
5058

@@ -80,10 +88,7 @@ interface ProjectCreationProviderProps {
8088
totalSteps: number;
8189
}
8290

83-
export const ProjectCreationProvider = ({
84-
children,
85-
totalSteps,
86-
}: ProjectCreationProviderProps) => {
91+
export const ProjectCreationProvider = ({ children, totalSteps }: ProjectCreationProviderProps) => {
8792
const router = useRouter();
8893
const [currentStep, setCurrentStep] = useState(0);
8994
const [projectData, setProjectDataState] = useState<Partial<Project>>({
@@ -116,7 +121,7 @@ export const ProjectCreationProvider = ({
116121
}
117122

118123
const packageJsonFile = projectData.files.find(
119-
(f) => f.path.endsWith('package.json') && f.type === ProcessedFileType.TEXT
124+
(f) => f.path.endsWith('package.json') && f.type === ProcessedFileType.TEXT,
120125
);
121126

122127
const template = SandboxTemplates[Templates.BLANK];
@@ -176,32 +181,38 @@ export const ProjectCreationProvider = ({
176181
const validateNextJsProject = async (
177182
files: ProcessedFile[],
178183
): Promise<NextJsProjectValidation> => {
179-
const packageJsonFile = files.find((f) => f.path.endsWith('package.json') && f.type === ProcessedFileType.TEXT);
184+
const packageJsonFile = files.find(
185+
(f) => f.path.endsWith('package.json') && f.type === ProcessedFileType.TEXT,
186+
);
180187

181-
if (!packageJsonFile) {
188+
if (!packageJsonFile?.content || typeof packageJsonFile.content !== 'string') {
182189
return { isValid: false, error: 'No package.json found' };
183190
}
184191

185192
try {
186-
const packageJson = JSON.parse(packageJsonFile.content as string);
187-
const hasNext = packageJson.dependencies?.next || packageJson.devDependencies?.next;
193+
const packageJson = JSON.parse(packageJsonFile.content) as Record<string, unknown>;
194+
const dependencies = packageJson.dependencies as Record<string, string> | undefined;
195+
const devDependencies = packageJson.devDependencies as
196+
| Record<string, string>
197+
| undefined;
198+
const hasNext = dependencies?.next ?? devDependencies?.next;
188199
if (!hasNext) {
189200
return { isValid: false, error: 'Next.js not found in dependencies' };
190201
}
191202

192-
const hasReact = packageJson.dependencies?.react || packageJson.devDependencies?.react;
203+
const hasReact = dependencies?.react ?? devDependencies?.react;
193204
if (!hasReact) {
194205
return { isValid: false, error: 'React not found in dependencies' };
195206
}
196207

197208
let routerType: RouterType = RouterType.PAGES;
198209

199-
const hasAppLayout = files.some(
200-
(f) => isTargetFile(f.path, {
210+
const hasAppLayout = files.some((f) =>
211+
isTargetFile(f.path, {
201212
fileName: 'layout',
202213
targetExtensions: NEXT_JS_FILE_EXTENSIONS,
203214
potentialPaths: ['app', 'src/app'],
204-
})
215+
}),
205216
);
206217

207218
if (hasAppLayout) {
@@ -226,7 +237,6 @@ export const ProjectCreationProvider = ({
226237
}
227238
};
228239

229-
230240
const nextStep = () => {
231241
if (currentStep < totalSteps - 2) {
232242
// -2 because we have 2 final steps
@@ -235,7 +245,7 @@ export const ProjectCreationProvider = ({
235245
} else {
236246
// This is the final step, so we should finalize the project
237247
setCurrentStep((prev) => prev + 1);
238-
finalizeProject();
248+
void finalizeProject();
239249
}
240250
};
241251

@@ -260,7 +270,7 @@ export const ProjectCreationProvider = ({
260270

261271
const retry = () => {
262272
setError(null);
263-
finalizeProject();
273+
void finalizeProject();
264274
};
265275

266276
const cancel = () => {
@@ -323,10 +333,7 @@ export const uploadToSandbox = async (files: ProcessedFile[], provider: Provider
323333
const modifiedAst = injectPreloadScript(ast);
324334
content = generate(modifiedAst, {}, content).code;
325335
} catch (parseError) {
326-
console.warn(
327-
'Failed to add script config to layout.tsx:',
328-
parseError,
329-
);
336+
console.warn('Failed to add script config to layout.tsx:', parseError);
330337
}
331338
}
332339
await provider.writeFile({

apps/web/client/src/components/store/editor/font/font-config-manager.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { camelCase } from 'lodash';
2+
import { makeAutoObservable, reaction } from 'mobx';
3+
4+
import type { CodeDiff, Font } from '@onlook/models';
5+
import type { T } from '@onlook/parser';
16
import {
27
addGoogleFontSpecifier,
38
generateFontVariableExport,
@@ -6,10 +11,9 @@ import {
611
removeFontDeclaration,
712
validateGoogleFontSetup,
813
} from '@onlook/fonts';
9-
import { RouterType, type CodeDiff, type Font } from '@onlook/models';
10-
import { generate, getAstFromContent, types as t, type t as T } from '@onlook/parser';
11-
import { camelCase } from 'lodash';
12-
import { makeAutoObservable, reaction } from 'mobx';
14+
import { RouterType } from '@onlook/models';
15+
import { generate, getAstFromContent, t } from '@onlook/parser';
16+
1317
import type { EditorEngine } from '../engine';
1418
import { normalizePath } from '../sandbox/helpers';
1519

@@ -143,7 +147,10 @@ export class FontConfigManager {
143147
return false;
144148
}
145149

146-
const success = await this.editorEngine.activeSandbox.writeFile(this.fontConfigPath, code);
150+
const success = await this.editorEngine.activeSandbox.writeFile(
151+
this.fontConfigPath,
152+
code,
153+
);
147154

148155
if (!success) {
149156
throw new Error('Failed to write font configuration');

apps/web/client/src/components/store/editor/font/font-upload-manager.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import * as pathModule from 'path';
2+
import { camelCase } from 'lodash';
3+
import { makeAutoObservable } from 'mobx';
4+
5+
import type { FontConfig, FontUploadFile } from '@onlook/models';
6+
import type { T } from '@onlook/parser';
17
import { DefaultSettings } from '@onlook/constants';
28
import {
39
createFontSrcObjects,
@@ -6,12 +12,9 @@ import {
612
hasLocalFontImport,
713
mergeLocalFontSources,
814
} from '@onlook/fonts';
9-
import type { FontConfig, FontUploadFile } from '@onlook/models';
10-
import { types as t, type t as T } from '@onlook/parser';
15+
import { t } from '@onlook/parser';
1116
import { getFontFileName } from '@onlook/utility';
12-
import { camelCase } from 'lodash';
13-
import { makeAutoObservable } from 'mobx';
14-
import * as pathModule from 'path';
17+
1518
import type { EditorEngine } from '../engine';
1619

1720
export class FontUploadManager {
@@ -45,15 +48,18 @@ export class FontUploadManager {
4548

4649
const fontName = camelCase(`custom-${baseFontName}`);
4750

48-
const { fontNameExists, existingFontNode } = findFontExportDeclaration(fontConfigAst, fontName);
51+
const { fontNameExists, existingFontNode } = findFontExportDeclaration(
52+
fontConfigAst,
53+
fontName,
54+
);
4955

5056
const fontConfigs = await this.processFontFiles(fontFiles, baseFontName, basePath);
5157
const fontsSrc = createFontSrcObjects(fontConfigs);
5258

5359
await this.updateAstWithFontConfig(
5460
fontConfigAst,
5561
fontName,
56-
fontsSrc,
62+
fontsSrc as T.ObjectExpression[],
5763
fontNameExists,
5864
existingFontNode,
5965
);

apps/web/client/src/components/store/editor/font/layout-manager.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { camelCase } from 'lodash';
2+
import { makeAutoObservable } from 'mobx';
3+
4+
import type { CodeDiff, Font } from '@onlook/models';
5+
import type { T } from '@onlook/parser';
16
import {
27
addFontImportToFile,
38
createStringLiteralWithFont,
@@ -8,10 +13,8 @@ import {
813
updateClassNameWithFontVar,
914
updateTemplateLiteralWithFontClass,
1015
} from '@onlook/fonts';
11-
import type { CodeDiff, Font } from '@onlook/models';
12-
import { generate, getAstFromContent, types as t, traverse, type t as T } from '@onlook/parser';
13-
import { camelCase } from 'lodash';
14-
import { makeAutoObservable } from 'mobx';
16+
import { generate, getAstFromContent, t, traverse } from '@onlook/parser';
17+
1518
import type { EditorEngine } from '../engine';
1619
import { normalizePath } from '../sandbox/helpers';
1720

apps/web/client/src/components/store/editor/pages/helper.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { nanoid } from 'nanoid';
2+
13
import type { ListFilesOutputFile } from '@onlook/code-provider';
24
import type { PageMetadata, PageNode, SandboxFile } from '@onlook/models';
5+
import type { T } from '@onlook/parser';
36
import { RouterType } from '@onlook/models';
4-
import { generate, getAstFromContent, types as t, traverse, type t as T } from '@onlook/parser';
5-
import { nanoid } from 'nanoid';
7+
import { generate, getAstFromContent, t, traverse } from '@onlook/parser';
8+
69
import type { SandboxManager } from '../sandbox';
710
import { formatContent } from '../sandbox/helpers';
811

apps/web/client/src/components/store/editor/theme/util.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import type { Root, Rule } from 'postcss';
2+
import postcss from 'postcss';
3+
4+
import type { T } from '@onlook/parser';
15
import { DEFAULT_COLOR_NAME } from '@onlook/constants';
26
import { SystemTheme } from '@onlook/models/assets';
3-
import { generate, getAstFromContent, parse, traverse, type t as T } from '@onlook/parser';
7+
import { generate, getAstFromContent, parse, traverse } from '@onlook/parser';
48
import { parseHslValue } from '@onlook/utility';
5-
import type { Root, Rule } from 'postcss';
6-
import postcss from 'postcss';
79

810
export function addTailwindNestedColor(
911
colorObj: T.ObjectExpression,

packages/fonts/src/helpers/ast-generators.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { Font, FontConfig } from '@onlook/models';
2-
import { types as t, type t as T } from '@onlook/parser';
31
import { camelCase } from 'lodash';
42

3+
import type { Font, FontConfig } from '@onlook/models';
4+
import type { T } from '@onlook/parser';
5+
import { t } from '@onlook/parser';
6+
57
/**
68
* Creates an AST object expression containing font configuration properties for Google Fonts.
79
* Generates the configuration object with subsets, weights, styles, CSS variable name, and display strategy.

packages/fonts/src/helpers/ast-manipulators.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import { createAndInsertImport } from '@onlook/fonts';
21
import type { Font } from '@onlook/models';
3-
import {
4-
generate,
5-
getAstFromContent,
6-
types as t,
7-
traverse,
8-
type NodePath,
9-
type t as T,
10-
} from '@onlook/parser';
2+
import type { NodePath, T } from '@onlook/parser';
3+
import { createAndInsertImport } from '@onlook/fonts';
4+
import { generate, getAstFromContent, t, traverse } from '@onlook/parser';
5+
116
import { createFontFamilyProperty } from './ast-generators';
127
import {
138
hasPropertyName,

packages/fonts/src/helpers/class-utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { types as t, type t as T } from '@onlook/parser';
1+
import type { T } from '@onlook/parser';
2+
import { t } from '@onlook/parser';
23

34
const FONT_WEIGHT_REGEX =
45
/font-(thin|extralight|light|normal|medium|semibold|bold|extrabold|black)/;
@@ -21,7 +22,7 @@ export function findFontClass(classString: string): string | null {
2122
* Filters out font-related classes from a className string, keeping only font weight classes
2223
*/
2324
export function filterFontClasses(className: string): string[] {
24-
return className.split(' ').filter((c) => !c.startsWith('font-') || c.match(FONT_WEIGHT_REGEX));
25+
return className.split(' ').filter((c) => !c.startsWith('font-') || FONT_WEIGHT_REGEX.exec(c));
2526
}
2627

2728
/**
@@ -86,7 +87,7 @@ export function removeFontsFromClassName(
8687
removeAll?: boolean;
8788
},
8889
): boolean {
89-
if (!classNameAttr || !classNameAttr.value) {
90+
if (!classNameAttr?.value) {
9091
return false;
9192
}
9293

packages/fonts/src/helpers/font-extractors.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Font } from '@onlook/models';
2-
import { generate, getAstFromContent, type t as T, types as t, traverse } from '@onlook/parser';
2+
import type { T } from '@onlook/parser';
3+
import { generate, getAstFromContent, t, traverse } from '@onlook/parser';
4+
35
import { removeFontsFromClassName } from './class-utils';
46

57
/**

0 commit comments

Comments
 (0)