-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
127 lines (109 loc) · 4.19 KB
/
generate.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
/* eslint-disable */
const inquirer = require('inquirer');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const QUESTIONS = [
{
name: 'api-choice',
type: 'list',
message: 'What kind of template would you like to generate?',
choices: CHOICES
},
{
name: 'entity-name',
type: 'input',
message: 'Entity name (PascalCase & singular) | example: Booking or BookingPayment :',
validate: function (input) {
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
else return 'Entity name may only include letters, numbers, underscores and hashes.';
}
},
{
name: 'file-name',
type: 'input',
message: 'File name (camelCase & singular) | example: booking or bookingPayment :',
validate: function (input) {
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
else return 'Entity name may only include letters, numbers, underscores and hashes.';
}
}
];
const CURR_DIR = process.cwd();
const SPACING = ` `;
inquirer.prompt(QUESTIONS)
.then(answers => {
const apiChoice = answers['api-choice'];
const entityName = answers['entity-name'];
const fileName = answers['file-name'];
const templatePath = `${__dirname}/templates/${apiChoice}`;
createDirectoryContents(templatePath, fileName, entityName, apiChoice);
addNewTypes(entityName);
addNewCollection(entityName, fileName);
});
function addNewTypes(entityName){
const typesPath = `${CURR_DIR}/src/types.ts`
const stats = fs.statSync(typesPath);
if (stats.isFile()) {
const content = fs.readFileSync(typesPath, 'utf8');
const symbol = "// SYMBOL //";
const newTypes = `${symbol}\n${SPACING}${entityName}Service: Symbol.for('${entityName}Service'),\n${SPACING}${entityName}Repository: Symbol.for('${entityName}Repository'),`
const newContent = content.replace(symbol, newTypes);
fs.writeFileSync(typesPath, newContent, 'utf8');
}
}
function addNewCollection(entityName, fileName){
const typesPath = `${CURR_DIR}/src/infra/database/config/collection.ts`
const stats = fs.statSync(typesPath);
if (stats.isFile()) {
const content = fs.readFileSync(typesPath, 'utf8');
const symbol = "// COLLECTIONS //";
const newTypes = `${symbol}\n${SPACING}${entityName}: '${fileName}s',`
const newContent = content.replace(symbol, newTypes);
fs.writeFileSync(typesPath, newContent, 'utf8');
}
}
function exportNewFile(originPath, fileName) {
fs.appendFile(`${originPath}/index.ts`, `export * from './${fileName}';\n`, function (err) {
if (err) throw err;
});
}
function rename(originFile, fileName) {
const originPath = path.dirname(originFile);
const originName = path.basename(originFile);
const newName = originName.replace('__', fileName);
exportNewFile(originPath, fileName);
fs.rename(originFile, originPath + '/' + newName, function(err) {
if(err) throw err
console.log(originPath + '/' + newName);
});
}
function replaceContent(content, fileName, entityName) {
const regexEntity = /####/gi;
const regexFileName = /FFFF/gi;
return content.replace(regexEntity, entityName).replace(regexFileName, fileName);
}
function createDirectoryContents (templatePath, fileName, entityName, apiChoice) {
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach(file => {
const origFilePath = `${templatePath}/${file}`;
const originPath = path.dirname(origFilePath);
// get stats about the current file
const stats = fs.statSync(origFilePath);
if (stats.isFile()) {
const content = fs.readFileSync(origFilePath, 'utf8');
const relativePath = path.relative(process.cwd(), originPath);
const srcPath = relativePath.substring(`templates/${apiChoice}/`.length);
const writePath = `${CURR_DIR}/${srcPath}/${file}`;
const newContent = replaceContent(content, fileName, entityName);
fs.writeFileSync(writePath, newContent, 'utf8');
rename(writePath, fileName);
} else if (stats.isDirectory()) {
mkdirp(`${originPath}/${file}`, {}, function(err) {
if(err) throw err
});
// recursive call
createDirectoryContents(`${templatePath}/${file}`, fileName, entityName, apiChoice);
}
});
}