forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
177 lines (151 loc) · 5.21 KB
/
helpers.ts
File metadata and controls
177 lines (151 loc) · 5.21 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
import { type GeneratorOptions } from '@babel/generator';
import type { NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import type { DetectedPortResults } from '@onlook/models';
import { CUSTOM_OUTPUT_DIR } from '@onlook/models/constants';
import type {
CoreElementType,
DynamicType,
TemplateNode,
TemplateTag,
} from '@onlook/models/element';
import { detect } from 'detect-port';
import * as fs from 'fs';
import { customAlphabet } from 'nanoid/non-secure';
import * as nodePath from 'path';
import { VALID_DATA_ATTR_CHARS } from '/common/helpers/ids';
export const ALLOWED_EXTENSIONS = ['.jsx', '.tsx'];
export const IGNORED_DIRECTORIES = [
'node_modules',
'dist',
'build',
'.next',
'.git',
'.onlook',
CUSTOM_OUTPUT_DIR,
];
export const GENERATE_CODE_OPTIONS: GeneratorOptions = {
compact: false, // Keep normal spacing
retainLines: true, // Retain original line numbers
jsescOption: {
minimal: true, // Nice string escaping
},
jsonCompatibleStrings: true, // Readable string literals
};
export const generateId = customAlphabet(VALID_DATA_ATTR_CHARS, 7);
export async function getValidFiles(dirPath: string): Promise<string[]> {
const validFiles: string[] = [];
function scanDirectory(currentPath: string) {
const files = fs.readdirSync(currentPath);
for (const file of files) {
const filepath = nodePath.join(currentPath, file);
const stat = fs.statSync(filepath);
if (stat.isDirectory()) {
if (!IGNORED_DIRECTORIES.includes(file)) {
scanDirectory(filepath);
}
} else {
const fileExt = nodePath.extname(file);
if (ALLOWED_EXTENSIONS.includes(fileExt)) {
validFiles.push(filepath);
}
}
}
}
scanDirectory(dirPath);
return validFiles;
}
export function isReactFragment(openingElement: any): boolean {
const name = openingElement.name;
if (t.isJSXIdentifier(name)) {
return name.name === 'Fragment';
}
if (t.isJSXMemberExpression(name)) {
return (
t.isJSXIdentifier(name.object) &&
name.object.name === 'React' &&
t.isJSXIdentifier(name.property) &&
name.property.name === 'Fragment'
);
}
return false;
}
export function getTemplateNode(
path: any,
filename: string,
componentStack: string[],
dynamicType?: DynamicType,
coreElementType?: CoreElementType,
): TemplateNode {
const startTag: TemplateTag = getTemplateTag(path.node.openingElement);
const endTag: TemplateTag | null = path.node.closingElement
? getTemplateTag(path.node.closingElement)
: null;
const component = componentStack.length > 0 ? componentStack[componentStack.length - 1] : null;
const domNode: TemplateNode = {
path: filename,
startTag,
endTag,
component,
dynamicType,
coreElementType,
};
return domNode;
}
function getTemplateTag(element: any): TemplateTag {
return {
start: {
line: element.loc.start.line,
column: element.loc.start.column + 1,
},
end: {
line: element.loc.end.line,
column: element.loc.end.column,
},
};
}
export function isNodeElementArray(node: t.CallExpression): boolean {
return (
t.isMemberExpression(node.callee) &&
t.isIdentifier(node.callee.property) &&
node.callee.property.name === 'map'
);
}
export function getDynamicTypeInfo(path: NodePath<t.JSXElement>): DynamicType | undefined {
const parent = path.parent;
const grandParent = path.parentPath?.parent;
// Check for conditional root element
const isConditionalRoot =
(t.isConditionalExpression(parent) || t.isLogicalExpression(parent)) &&
t.isJSXExpressionContainer(grandParent);
// Check for array map root element
const isArrayMapRoot =
t.isArrowFunctionExpression(parent) ||
(t.isJSXFragment(parent) && path.parentPath?.parentPath?.isArrowFunctionExpression());
const dynamicType = isConditionalRoot ? 'conditional' : isArrayMapRoot ? 'array' : undefined;
return dynamicType;
}
export function getCoreElementInfo(path: NodePath<t.JSXElement>): CoreElementType | undefined {
const parent = path.parent;
const isComponentRoot = t.isReturnStatement(parent) || t.isArrowFunctionExpression(parent);
const isBodyTag =
t.isJSXIdentifier(path.node.openingElement.name) &&
path.node.openingElement.name.name.toLocaleLowerCase() === 'body';
const coreElementType = isComponentRoot ? 'component-root' : isBodyTag ? 'body-tag' : undefined;
return coreElementType;
}
export async function isPortAvailable(port: number): Promise<DetectedPortResults> {
try {
const availablePort = await detect(port);
return {
isPortAvailable: port === availablePort,
availablePort: availablePort,
};
} catch (error) {
console.error('Error detecting port:', error);
return {
isPortAvailable: false,
availablePort: 3000,
};
}
}