-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
118 lines (105 loc) · 3.66 KB
/
Copy pathindex.ts
File metadata and controls
118 lines (105 loc) · 3.66 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
import inquirer from 'inquirer'
import chalk from 'chalk'
import fs from 'fs'
import { nestedFileSearch } from './utils/file'
import path from 'path'
const main = async () => {
console.log(chalk.green('This command creates a sub-router module.'))
const { rootNameOfKebabCase } = await inquirer.prompt({
type: 'input',
name: 'rootNameOfKebabCase',
message: `Enter the name of the parent root folder. ${chalk.gray(
'(Just below the src folder)'
)}`
})
const rootNameOfPascalCase = rootNameOfKebabCase.replace(
/-([a-z])/g,
(match, group1) => group1.toUpperCase()
)
const { routeNameOfKebabCase } = await inquirer.prompt({
type: 'input',
name: 'routeNameOfKebabCase',
message: `Please enter the subroutine name you want to create. ${chalk.gray(
'(e.g. user/token/check -> user-token-check)'
)}`
})
const routeNameOfPascalCase = (routeNameOfKebabCase as string)
.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('')
const routeNameOfCamelCase =
routeNameOfPascalCase.charAt(0).toLowerCase() +
routeNameOfPascalCase.slice(1)
const routeGeneratePath = path.resolve(
`${process.cwd()}/src/${rootNameOfKebabCase}/${routeNameOfKebabCase}`
)
const addRouteTemplatePath = path.resolve(
__dirname,
'../src/template/add-route'
)
fs.mkdirSync(routeGeneratePath, {
recursive: true
})
const rootRouteFilePath = path.resolve(
`${process.cwd()}/src/${rootNameOfKebabCase}/${rootNameOfKebabCase}.routes.ts`
)
if (fs.existsSync(rootRouteFilePath)) {
const content = fs.readFileSync(rootRouteFilePath, 'utf8')
const newContent = content
.replace(
`\/\/ * (nest-route) import - do not remove this comment.`,
`import { ${routeNameOfPascalCase}Module } from './${routeNameOfKebabCase}/index.module';\n` +
`\/\/ * (nest-route) import - do not remove this comment.`
)
.replace(
`\/\/ * (nest-route) define - do not remove this comment.`,
`{\n path: '/${routeNameOfKebabCase.replace(
/-/g,
'/'
)}',\n module: ${routeNameOfPascalCase}Module,\n },\n ` +
`\/\/ * (nest-route) define - do not remove this comment.`
)
fs.writeFileSync(rootRouteFilePath, newContent)
}
const files = nestedFileSearch(addRouteTemplatePath)
for (const { subPath } of files) {
const templateFilePath = path.join(addRouteTemplatePath, subPath)
const content = fs
.readFileSync(templateFilePath, 'utf8')
.replace(/TemplateRouteName/g, routeNameOfPascalCase)
.replace(/templateRouteName/g, routeNameOfCamelCase)
.replace(/TemplateParentRouteName/g, rootNameOfPascalCase)
.replace(/sub-route-path/g, routeNameOfKebabCase.replace(/-/g, '/'))
.replace(/sub-route-name/g, routeNameOfKebabCase)
.replace(/root-name/g, rootNameOfKebabCase)
const generateFilePath = path.join(
`${process.cwd()}/src/${rootNameOfKebabCase}`,
subPath
.replace(/sub-route-name/g, routeNameOfKebabCase)
.replace(/root-name/g, rootNameOfKebabCase)
)
if (!fs.existsSync(generateFilePath))
fs.writeFileSync(generateFilePath, content, 'utf8')
}
console.log(``)
console.log(
chalk.green(
`The sub-router module has been created. ${chalk.gray(routeGeneratePath)}`
)
)
console.log(
chalk.green(
`Please add the sub-router module to the app.module. ${chalk.gray(
path.resolve(`${process.cwd()}/src/app.module.ts`)
)}`
)
)
console.log(
chalk.green(
`(Example code: ${chalk.gray(
`RouterModule.register([...${rootNameOfPascalCase}Module])`
)})`
)
)
}
main()