-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
61 lines (50 loc) · 1.88 KB
/
Copy pathroute.ts
File metadata and controls
61 lines (50 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
import fs from 'fs'
import path from 'path'
import { nestedFileSearch } from './file'
export const generateAppRouteFile = async () => {
const srcPath = path.resolve(process.cwd(), 'src')
if (!fs.existsSync(srcPath)) return
const projectFiles = nestedFileSearch(srcPath)
const routerFiles = projectFiles.filter((file) =>
file.subPath.endsWith('routes.ts')
)
let code = `\/\/ * (nest-route) index - This file was created automatically. \n`
const routes: string[] = []
const modules: string[] = []
for (const routerFile of routerFiles) {
const content = fs.readFileSync(routerFile.staticPath, 'utf-8')
if (!content.includes(`\/\/ * (nest-route) import`)) continue
const rootNameOfKebabCase = routerFile.subPath.split('/')[1]
const rootNameOfPascalCase = rootNameOfKebabCase
.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
const routesName = `${rootNameOfPascalCase}Routes`
const importStatement = `import { ${routesName} } from '.${routerFile.subPath.replace(
/\.ts$/,
''
)}';`
code += `${importStatement}\n`
routes.push(routesName)
for (const line of content.split('\n')) {
if (!line.endsWith(`index.module';`)) continue
const moduleName = /{(.*)}/.exec(line)![1].trim()
const modulePath = /['"](.*)['"]/.exec(line)![1].trim()
const relativeModulePath = `./${path.join(
`${rootNameOfKebabCase}/${modulePath}`
)}`
code += `import { ${moduleName} } from '${relativeModulePath}';\n`
modules.push(moduleName)
}
}
code += `\nexport const AppRoutes = [\n`
for (const route of routes) {
code += ` ...${route},\n`
}
code += `];\n`
code += `\nexport const AppRouteModules = [\n`
for (const module of modules) {
code += ` ${module},\n`
}
code += `];\n`
fs.writeFileSync(path.resolve(process.cwd(), 'src/app.routes.ts'), code)
}