-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherela.cli.ts
225 lines (207 loc) · 6.01 KB
/
erela.cli.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env node
/* tslint:disable:no-console */
import ora from 'ora'
import { PlainObject } from '@types'
import { stat, mkdir, readFile, writeFile, exists, copyFile } from 'fs'
import { copy } from 'fs-extra'
import { promisify } from 'util'
import 'colors'
import { prompt, RawListQuestion, Question } from 'inquirer'
import { join, parse, basename } from 'path'
import { textSync } from 'figlet'
const statAsync = promisify(stat)
const mkdirAsync = promisify(mkdir)
const readFileAsync = promisify(readFile)
const writeFileAsync = promisify(writeFile)
const existsAsync = promisify(exists)
const copyFileAsync = promisify(copyFile)
const { log, error, clear } = console
enum choices {
App = 'App',
Controller = 'Controller',
Entity = 'Entity',
Service = 'Service',
}
enum ServiceTypes {
Base = 'Base',
Custom = 'Custom',
}
;(async () => {
clear()
log(textSync('Erela Cli', { horizontalLayout: 'full' }))
const q01: RawListQuestion = {
type: 'rawlist',
message: 'What do you want to generate?',
choices: Object.keys(choices),
name: 'generate',
}
prompt([q01]).then((answer: PlainObject) => {
const [ans] = Object.values(answer) as Array<choices>
pipeGeneration(ans)
})
})()
const pipeGeneration = async (choice: choices): Promise<void> => {
switch (choice) {
case choices.Controller:
const controllers = './src/controllers'
await setup(controllers, choice)
generateControllerOrEntity(controllers, choices.Controller)
break
case choices.Entity:
const entities = './src/entities'
await setup(entities, choice)
generateControllerOrEntity(entities, choices.Entity)
break
case choices.Service:
const services = './src/services'
await setup(services, choice)
generateService(services)
break
case choices.App:
genreateApp()
break
default:
break
}
}
const setup = async (path: string, type: choices): Promise<void> => {
const fullPath = join('./', path)
log(`Will generate a ${type}`.blue.bold)
const spinner = ora(`Checking if a the ${path} exists ..`.white)
spinner.start()
const exits = await statAsync(fullPath)
.then(() => true)
.catch(() => false)
if (exits) {
spinner.succeed(`The ${path} directory already exists`)
} else {
spinner.fail(
`Can't find the ${path} directory, so I'll create it for you 🙂`
)
await mkdirAsync(fullPath)
spinner.succeed(`Created The ${path} directory`)
}
}
const generateControllerOrEntity = async (
distPath: string,
choice: choices
): Promise<void> => {
const name = await askAboutName(choice)
const buffer: Buffer = await readTempalte(
choice === choices.Controller ? 'controller' : 'entity'
)
const newContent = buffer
.toString()
.replace(/__NAME__/g, title(basename(name)))
.replace(
/__CHOICE_NAME__/g,
choice === choices.Controller
? `/${basename(name.toLowerCase())}`
: basename(name.toLowerCase())
)
await createFile(distPath, name, newContent, choice)
}
const generateService = async (distPath: string): Promise<void> => {
const wantCustom: boolean = await askAboutServiceChoice()
let name: string = 'Base'
if (wantCustom) {
name = await askAboutName(choices.Service)
}
const buffer: Buffer = await readTempalte(
`service${name === 'Base' ? '.base' : ''}`
)
await createFile(
distPath,
name,
buffer.toString().replace(/__NAME__/g, title(basename(name))),
choices.Service
)
}
const askAboutName = async (choice: choices): Promise<choices> => {
const q: Question = {
type: 'input',
name: 'name',
message: `What is the name of the ${choice}`,
}
return await prompt([q]).then((answer: PlainObject) => {
const [ans] = Object.values(answer) as Array<choices>
return ans
})
}
const askAboutServiceChoice = async (): Promise<boolean> => {
const q01: RawListQuestion = {
type: 'rawlist',
message: 'What service do you want?',
choices: Object.keys(ServiceTypes),
name: 'type',
}
return (
ServiceTypes.Custom ===
(await prompt([q01]).then((answer: PlainObject) => {
const [ans] = Object.values(answer) as Array<ServiceTypes>
return ans
}))
)
}
const createFile = async (
distPath: string,
nameThatMightContainPath: string,
content: string,
choice: choices
): Promise<void> => {
const { name, dir } = parse(nameThatMightContainPath)
let path = distPath
for (const folder of dir.split('/')) {
path = join(path, folder)
if (!(await existsAsync(path))) {
await mkdirAsync(`./${path}`)
}
}
const { cwd } = process
const dist = join(
cwd(),
path,
`${name.toLowerCase()}.${choice.toLowerCase()}.ts`
)
await writeFileAsync(join(dist), content)
log(`plz look at ${dist}`.yellow.bold)
}
const readTempalte = async (name: string): Promise<Buffer> =>
await readFileAsync(join(__dirname, `templates/${name}.template.txt`))
const title = (name: string) =>
`${name.charAt(0).toUpperCase()}${name.slice(1)}`
const genreateApp = async () => {
try {
const q: Question = {
type: 'input',
name: 'Type path',
message: 'Enter App name',
}
const [nameWithPath] = await prompt([q]).then(
(answer: PlainObject) => Object.values(answer) as Array<choices>
)
await mkdirAsync(nameWithPath)
log(`Created a project ${nameWithPath}`.green.bold)
const toCp = [
'normal-package.json',
'.env.example',
'tsconfig.json',
'tslint.json',
'README.md',
]
await copy(join(__dirname, 'src'), join(nameWithPath, 'src'))
log(`Created the src folder`.green.bold)
for (const file of toCp) {
await copyFileAsync(join(__dirname, file), join(nameWithPath, file === 'normal-package.json' ? 'package.json' : file))
}
log(`Done now cd ${nameWithPath} and run`.green.bold)
log('npm i'.bgWhite.black.bold)
} catch (e) {
error(
'Something went wrong while trying to generate the app'.red.bold,
'see the error messages',
'PRs are welcomed'
)
error(e)
}
}