|
1
|
import { fdir } from "fdir" |
|
2
|
import MagicString from "magic-string" |
|
3
|
import crypto from "node:crypto" |
|
4
|
import path from "path" |
|
5
|
import { |
|
6
|
File, |
|
7
|
isCallExpression, |
|
8
|
isIdentifier, |
|
9
|
isObjectExpression, |
|
10
|
isVariableDeclaration, |
|
11
|
isVariableDeclarator, |
|
12
|
ParseResult, |
|
13
|
traverse, |
|
14
|
type ExportNamedDeclaration, |
|
15
|
type NodePath, |
|
16
|
type ParserOptions, |
|
17
|
type VariableDeclarator, |
|
18
|
} from "./babel" |
|
19
|
|
|
20
|
export function normalizePath(file: string) { |
|
21
|
return path.normalize(file).replace(/\\/g, "/") |
|
22
|
} |
|
23
|
|
|
24
|
/** |
|
25
|
* Returns the parser options for a given file. |
|
26
|
*/ |
|
27
|
export function getParserOptions(file: string): ParserOptions { |
|
28
|
const options: ParserOptions = { |
|
29
|
sourceType: "module", |
|
30
|
plugins: ["jsx"], |
|
31
|
} |
|
32
|
|
|
33
|
if (file.endsWith(".tsx")) { |
|
34
|
options.plugins?.push("typescript") |
|
35
|
} |
|
36
|
|
|
37
|
return options |
|
38
|
} |
|
39
|
|
|
40
|
/** |
|
41
|
* Generates a module with a source map from a code string |
|
42
|
*/ |
|
43
|
export function generateModule(code: string) { |
|
44
|
const magicString = new MagicString(code) |
|
45
|
|
|
46
|
return { |
|
47
|
code: magicString.toString(), |
|
48
|
map: magicString.generateMap({ hires: true }), |
|
49
|
} |
|
50
|
} |
|
51
|
|
|
52
|
export const VALID_FILE_EXTENSIONS = [".tsx", ".jsx", ".js", ".ts"] |
|
53
|
|
|
54
|
/** |
|
55
|
* Crawls a directory and returns all files that match the criteria. |
|
56
|
*/ |
|
57
|
export async function crawl( |
|
58
|
dir: string, |
|
59
|
file?: string, |
|
60
|
depth?: { min: number; max?: number } |
|
61
|
) { |
|
62
|
const dirDepth = dir.split(path.sep).length |
|
63
|
|
|
64
|
const crawler = new fdir() |
|
65
|
.withBasePath() |
|
66
|
.exclude((dirName) => dirName.startsWith("_")) |
|
67
|
.filter((path) => { |
|
68
|
return VALID_FILE_EXTENSIONS.some((ext) => path.endsWith(ext)) |
|
69
|
}) |
|
70
|
|
|
71
|
if (file) { |
|
72
|
crawler.filter((path) => { |
|
73
|
return VALID_FILE_EXTENSIONS.some((ext) => path.endsWith(file + ext)) |
|
74
|
}) |
|
75
|
} |
|
76
|
|
|
77
|
if (depth) { |
|
78
|
crawler.filter((file) => { |
|
79
|
const pathDepth = file.split(path.sep).length - 1 |
|
80
|
|
|
81
|
if (depth.max && pathDepth > dirDepth + depth.max) { |
|
82
|
return false |
|
83
|
} |
|
84
|
|
|
85
|
if (pathDepth < dirDepth + depth.min) { |
|
86
|
return false |
|
87
|
} |
|
88
|
|
|
89
|
return true |
|
90
|
}) |
|
91
|
} |
|
92
|
|
|
93
|
return crawler.crawl(dir).withPromise() |
|
94
|
} |
|
95
|
|
|
96
|
/** |
|
97
|
* Extracts and returns the properties of a `config` object from a named export declaration. |
|
98
|
*/ |
|
99
|
export function getConfigObjectProperties( |
|
100
|
path: NodePath<ExportNamedDeclaration | VariableDeclarator> |
|
101
|
) { |
|
102
|
if (isVariableDeclarator(path.node)) { |
|
103
|
const configDeclaration = isIdentifier(path.node.id, { name: "config" }) |
|
104
|
? path.node |
|
105
|
: null |
|
106
|
|
|
107
|
if ( |
|
108
|
configDeclaration && |
|
109
|
isCallExpression(configDeclaration.init) && |
|
110
|
configDeclaration.init.arguments.length > 0 && |
|
111
|
isObjectExpression(configDeclaration.init.arguments[0]) |
|
112
|
) { |
|
113
|
return configDeclaration.init.arguments[0].properties |
|
114
|
} |
|
115
|
|
|
116
|
return null |
|
117
|
} |
|
118
|
|
|
119
|
const declaration = path.node.declaration |
|
120
|
|
|
121
|
if (isVariableDeclaration(declaration)) { |
|
122
|
const configDeclaration = declaration.declarations.find( |
|
123
|
(d) => isVariableDeclarator(d) && isIdentifier(d.id, { name: "config" }) |
|
124
|
) |
|
125
|
|
|
126
|
if ( |
|
127
|
configDeclaration && |
|
128
|
isCallExpression(configDeclaration.init) && |
|
129
|
configDeclaration.init.arguments.length > 0 && |
|
130
|
isObjectExpression(configDeclaration.init.arguments[0]) |
|
131
|
) { |
|
132
|
return configDeclaration.init.arguments[0].properties |
|
133
|
} |
|
134
|
} |
|
135
|
|
|
136
|
return null |
|
137
|
} |
|
138
|
|
|
139
|
export async function hasDefaultExport( |
|
140
|
ast: ParseResult<File> |
|
141
|
): Promise<boolean> { |
|
142
|
let hasDefaultExport = false |
|
143
|
traverse(ast, { |
|
144
|
ExportDefaultDeclaration() { |
|
145
|
hasDefaultExport = true |
|
146
|
}, |
|
147
|
AssignmentExpression(path) { |
|
148
|
if ( |
|
149
|
path.node.left.type === "MemberExpression" && |
|
150
|
path.node.left.object.type === "Identifier" && |
|
151
|
path.node.left.object.name === "exports" && |
|
152
|
path.node.left.property.type === "Identifier" && |
|
153
|
path.node.left.property.name === "default" |
|
154
|
) { |
|
155
|
hasDefaultExport = true |
|
156
|
} |
|
157
|
}, |
|
158
|
ExportNamedDeclaration(path) { |
|
159
|
const specifiers = path.node.specifiers |
|
160
|
if ( |
|
161
|
specifiers?.some( |
|
162
|
(s) => |
|
163
|
s.type === "ExportSpecifier" && |
|
164
|
s.exported.type === "Identifier" && |
|
165
|
s.exported.name === "default" |
|
166
|
) |
|
167
|
) { |
|
168
|
hasDefaultExport = true |
|
169
|
} |
|
170
|
}, |
|
171
|
}) |
|
172
|
return hasDefaultExport |
|
173
|
} |
|
174
|
|
|
175
|
export function generateHash(content: string) { |
|
176
|
return crypto.createHash("md5").update(content).digest("hex") |
|
177
|
} |
|
178
|
|
|
179
|
const ADMIN_SUBDIRECTORIES = [ |
|
180
|
"routes", |
|
181
|
"custom-fields", |
|
182
|
"widgets", |
|
183
|
"i18n", |
|
184
|
"layouts", |
|
185
|
"cell-renderers.tsx", |
|
186
|
"search-entities.tsx", |
|
187
|
"search-entities.ts", |
|
188
|
] as const |
|
189
|
|
|
190
|
export type AdminSubdirectory = (typeof ADMIN_SUBDIRECTORIES)[number] |
|
191
|
|
|
192
|
export function isFileInAdminSubdirectory( |
|
193
|
file: string, |
|
194
|
subdirectory: AdminSubdirectory |
|
195
|
): boolean { |
|
196
|
const normalizedPath = normalizePath(file) |
|
197
|
// Handle file-based subdirectories (e.g., cell-renderers.tsx) |
|
198
|
if (subdirectory.endsWith(".tsx") || subdirectory.endsWith(".ts") || subdirectory.endsWith(".jsx") || subdirectory.endsWith(".js")) { |
|
199
|
return normalizedPath.endsWith(`/src/admin/${subdirectory}`) |
|
200
|
} |
|
201
|
// Handle directory-based subdirectories |
|
202
|
return normalizedPath.includes(`/src/admin/${subdirectory}/`) |
|
203
|
} |
|
204
|
|
|
205
|
/** |
|
206
|
* Test util to normalize strings, so they can be compared without taking |
|
207
|
* whitespace into account. |
|
208
|
*/ |
|
209
|
export function normalizeString(str: string): string { |
|
210
|
return str.replace(/\s+/g, " ").trim() |
|
211
|
} |