-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
169 lines (142 loc) · 5.51 KB
/
cli.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
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
#!/usr/bin/env node
/**
* SEQUELIZE GENERATOR ROUTER
*/
'use strict';
let inquirer = require('inquirer');
let fs = require('fs');
let mkdirp = require('mkdirp');
const modelsFolder = './models/';
String.prototype.capitalize = function () {
return this.replace(/(^|\s)([a-z])/g, function (m, p1, p2) {
return p1 + p2.toUpperCase();
});
};
fs.readdir(modelsFolder, (err, files) => {
let filesName = [];
files.forEach(function (file) {
filesName.push(file.replace(/.js/g, ""));
});
let index = filesName.indexOf("index");
if (index !== -1) filesName.splice(index, 1);
let questions = [
{
type: 'list',
name: 'router',
message: 'Which sequelize route whould you like to generate ? :',
choices: filesName
}
];
inquirer.prompt(questions).then(answers => {
let routerName = answers.router;
fs.readFile('./models/' + routerName + '.js', "utf8", function (err, fileData) {
if (err) {
console.log("\n The model named " + routerName + ".js doesn't exist !");
}
let postData = getPostData(routerName, fileData);
let routerNameObject = prepareRouteNameObject(routerName, postData);
fs.readFile(__dirname+'/base/baseRouter.js', "utf8", function (baseErr, baseFileData) {
for (let key in routerNameObject) {
let stringToReplace = "{{routeName." + key + "}}";
baseFileData = baseFileData.replace(new RegExp(stringToReplace, 'g'), routerNameObject[key]);
}
fs.readFile(__dirname+'/base/baseRouterIndex.js', "utf8", function (baseRouterIndexErr, baseRouterIndexFileData) {
mkdirp('./router', function (err) {
fs.readFile('./router/' + routerName + '.js', "utf8", function (fileError, fileExist) {
fs.readFile('./router/index.js', "utf8", function (indexErr, indexFile) {
if(indexErr){
fs.appendFile('./router/index.js', baseRouterIndexFileData, function (err) {
if (err) console.log("\n error, try again...\n", err);
indexFile = baseRouterIndexFileData;
});
}
if (fileError) {
fs.appendFile('./router/' + routerName + '.js', baseFileData, function (err) {
if (err) console.log("\n error, try again...\n", err);
addRouterIndexRequire(indexFile, routerName);
});
} else {
console.log("\n " + routerName + ".js was selected !\n");
let exist = [
{
type: 'list',
name: 'exist',
message: 'Route named ' + routerName + ' already exist.\n Do you want to erase and generate it again ? (DANGER):',
choices: [
"yes",
"no"
]
}
];
inquirer.prompt(exist).then(answers => {
let isExist = answers.exist;
if (isExist === "yes") {
fs.appendFile('./router/' + routerName + '.js', baseFileData, function (err) {
if (err) console.log("\n error, try again...\n", err);
addRouterIndexRequire(indexFile, routerName);
});
} else {
console.log("\n Ok, it's maybe a good choice ;)");
}
});
}
});
});
});
});
});
});
});
});
function addRouterIndexRequire(indexFile, routerName){
let beginString = "const routes = [",
String = indexFile.substring(indexFile.lastIndexOf(beginString) + beginString.length + 1 , indexFile.lastIndexOf("];")),
newString = [];
String = String.trim().split();
let includeString = "require('./"+routerName+"')";
if(!String.toString().includes(includeString)){
if(String && String[0] !== "]"){
newString = String.slice();
newString.push("\n require('./"+routerName+"')");
}else{
newString.push("\n require('./"+routerName+"')");
newString = newString.toString() + "\n]";
}
indexFile = indexFile.replace(String, newString);
fs.writeFile('./router/index.js', indexFile, function (err) {
console.log("\n The route " + routerName + '.js was create with success :)');
});
}else{
console.log("\n The route " + routerName + '.js was create with success :)');
}
}
function getPostData(routerName, fileData){
let beginString = "sequelize.define('"+routerName.capitalize()+"', {",
String = fileData.substring(fileData.lastIndexOf(beginString) + beginString.length + 1 , fileData.lastIndexOf("}, {")),
arrayOfItem = String.match(/\S+(?=:)/g),
postItems = '',
arrayLength = arrayOfItem.length;
for(let i = 0; i < arrayLength; i++){
postItems += arrayOfItem[i]+": req.body."+arrayOfItem[i];
if(i !== arrayLength - 1){
postItems += ",\n\t\t\t";
}
}
return postItems;
}
function prepareRouteNameObject(routerName, postData){
let singular = routerName;
let lastChar = routerName.substr(routerName.length - 1);
if(lastChar === 's'){
singular = routerName.substring(0, routerName.length - 1);
}
return {
singular: singular,
singularCap: singular.capitalize(),
singularUp: singular.toUpperCase(),
plurial: routerName,
plurialCap: routerName.capitalize(),
plurialUp: routerName.toUpperCase(),
postData: postData
}
}