-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathutils.ts
More file actions
89 lines (81 loc) · 2.5 KB
/
utils.ts
File metadata and controls
89 lines (81 loc) · 2.5 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
import type { IncomingMessage, ServerResponse } from "node:http"
import { type Connect, normalizePath } from "vite"
export async function processPlugins(pluginDirectoryPath: string) {
const fs = await import("node:fs")
const { join } = await import("node:path")
const files = fs.readdirSync(pluginDirectoryPath)
const allExports: { name: string; path: string }[] = []
for (const file of files) {
const filePath = join(pluginDirectoryPath, file)
const fileCode = fs.readFileSync(filePath, "utf8")
const lines = fileCode.split("\n")
for (const line of lines) {
if (line.includes("export const")) {
const [name] = line.split("export const ")[1].split(" =")
allExports.push({ name, path: join("..", filePath).replaceAll("\\", "/") })
}
}
}
return allExports
}
export const handleDevToolsViteRequest = (
req: Connect.IncomingMessage,
res: ServerResponse<IncomingMessage>,
next: Connect.NextFunction,
cb: (data: any) => void
) => {
if (req.url?.includes("open-source")) {
const searchParams = new URLSearchParams(req.url.split("?")[1])
const source = searchParams.get("source")
const line = searchParams.get("line")
const column = searchParams.get("column")
cb({
type: "open-source",
routine: "open-source",
data: {
source: source ? normalizePath(`${process.cwd()}/${source}`) : undefined,
line,
column,
},
})
res.setHeader("Content-Type", "text/html")
res.write(`<script>
window.close();
</script>`)
res.end()
return
}
if (!req.url?.includes("react-router-devtools-request")) {
return next()
}
const chunks: any[] = []
req.on("data", (chunk) => {
chunks.push(chunk)
})
req.on("end", () => {
const dataToParse = Buffer.concat(chunks)
try {
const parsedData = JSON.parse(dataToParse.toString())
cb(parsedData)
} catch (e) {
// eslint-disable-next-line no-empty
}
res.write("OK")
})
}
export async function checkPath(routePath: string, extensions = [".tsx", ".jsx", ".ts", ".js"]) {
const fs = await import("node:fs")
// Check if the path exists as a directory
if (fs.existsSync(routePath) && fs.lstatSync(routePath).isDirectory()) {
return { validPath: routePath, type: "directory" } as const
}
// Check if the path exists as a file with one of the given extensions
for (const ext of extensions) {
const filePath = `${routePath}${ext}`
if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
return { validPath: filePath, type: "file" } as const
}
}
// If neither a file nor a directory is found
return null
}