-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathfile.ts
More file actions
70 lines (69 loc) · 1.88 KB
/
file.ts
File metadata and controls
70 lines (69 loc) · 1.88 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
import { writeFile } from "node:fs/promises"
import { resolve } from "node:path"
import { GENERATORS, type Generator } from "./generators"
export type WriteFileData = {
type: "write-file"
path: string
openInEditor: (path: string, lineNum: string | undefined, columnNum: string | undefined) => void
options: {
loader: boolean
clientLoader: boolean
action: boolean
clientAction: boolean
headers: boolean
errorBoundary: boolean
revalidate: boolean
handler: boolean
meta: boolean
links: boolean
}
appDir: string
}
const defaultGenerationOrder: Exclude<Generator, "dependencies">[] = [
"links",
"meta",
"handler",
"headers",
"loader",
"clientLoader",
"action",
"clientAction",
"component",
"errorBoundary",
"revalidate",
]
export const handleWriteFile = async ({ path, options, openInEditor, appDir }: WriteFileData) => {
const generatorOptions = Object.entries(options)
.map(([key, value]) => {
if (value) {
return { key }
}
})
.filter(Boolean) as unknown as { key: Exclude<Generator, "dependencies"> }[]
let outputFile = `${resolve(appDir, "routes", path)}`
const extensions = [".tsx", ".jsx", ".ts", ".js"]
if (!extensions.some((ext) => outputFile.endsWith(ext))) {
outputFile = `${outputFile}.tsx`
}
const selectedGenerators: Exclude<Generator, "dependencies">[] = [
"component",
...generatorOptions.map((option) => option.key),
]
const withLoader = selectedGenerators.includes("loader" as any)
const fileContent = [
GENERATORS.dependencies(selectedGenerators),
...defaultGenerationOrder.map((generatorKey) => {
if (generatorKey === "component") {
return GENERATORS[generatorKey](withLoader)
}
if (selectedGenerators.includes(generatorKey)) {
return GENERATORS[generatorKey]()
}
return undefined
}),
]
.filter(Boolean)
.join("\n\n")
await writeFile(outputFile, fileContent)
openInEditor(outputFile, undefined, undefined)
}