-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·103 lines (89 loc) · 2.7 KB
/
index.js
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
#! /usr/bin/env node
// Folder Structure
/*
root
|-- models
|-- Model.js
|-- routes
|-- routename
|-- controllers.js
|-- index.js
|-- services.js
|-- index.js
|-- app.js
*/
const fs = require('fs');
const inquirer = require('inquirer');
const ejs = require('ejs');
// Helpers
const { makeInitialUppercase } = require('./helpers/string');
const { DIR_ROUTES, DIR_MODELS } = require('./helpers/constants');
//
function generate(dir, file, content) {
if (!fs.existsSync(`${dir}/${file}.js`)) {
fs.mkdirSync(dir, {
recursive: true,
});
fs.writeFileSync(`${dir}/${file}.js`, content, {
recursive: true,
});
} else {
console.log(`Directory already exists`);
}
}
function main() {
inquirer
.prompt([
{
type: 'input',
name: 'endpoint',
message: 'What is name of your api endpoint?',
},
])
.then((answers) => {
const { endpoint } = answers;
const MODEL = makeInitialUppercase(endpoint);
const FOLDER = endpoint.toLowerCase();
// Routes
const routeTemplate = fs.readFileSync(
`${__dirname}/ejs/route.ejs`,
'utf-8'
);
const renderedRoute = ejs.render(routeTemplate, {
modelName: MODEL,
});
// Controllers
const controllerTemplate = fs.readFileSync(
`${__dirname}/ejs/controller.ejs`,
'utf-8'
);
const renderedController = ejs.render(controllerTemplate, {
modelName: MODEL,
});
// Model
const modelTemplate = fs.readFileSync(
`${__dirname}/ejs/model.ejs`,
'utf-8'
);
const renderedModel = ejs.render(modelTemplate, {
modelName: MODEL,
});
// Services
const servicesTemplate = fs.readFileSync(
`${__dirname}/ejs/services.ejs`,
'utf-8'
);
const renderedServices = ejs.render(servicesTemplate, {
modelName: MODEL,
});
generate(`${DIR_ROUTES}/${FOLDER}`, 'index', renderedRoute);
generate(`${DIR_MODELS}`, `${MODEL}`, renderedModel);
generate(
`${DIR_ROUTES}/${FOLDER}`,
`controllers`,
renderedController
);
generate(`${DIR_ROUTES}/${FOLDER}`, `services`, renderedServices);
});
}
main();