-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateModule.js
More file actions
201 lines (178 loc) · 5 KB
/
createModule.js
File metadata and controls
201 lines (178 loc) · 5 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
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
// eslint-disable-next-line @typescript-eslint/no-var-requires,no-undef
const fs = require("fs");
// eslint-disable-next-line no-undef
const moduleNameArg = process.argv[2];
if (!moduleNameArg) {
console.error("Укажите название модуля! Н-р: npm run module test");
throw new Error("Укажите название модуля! Н-р: npm run module test");
}
/**
* Вернет строку с заглавной первой буквой.
*
* @param {string} name - строка.
* @returns {string}
*/
function capitalizeFirstLetter(name) {
return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
}
/**
* Обработчик ошибок.
* @param err
*/
function errorCallback(err) {
if (err) {
return console.log(err);
}
}
const builder = {
entity: {
getClassFilePath(name) {
return `${this.getDirectoryPath(name)}/index.ts`;
},
getTypeFilePath(name) {
return `${this.getDirectoryPath(name)}/types.ts`;
},
getDirectoryPath: (name) => {
return `./src/data/${name}/entity`;
},
getClassTemplate: (name) => {
const className = name;
const typeName = "I" + className;
return `import { ${typeName} } from "./types";
export class ${className} implements ${typeName} {}
`;
},
getTypeTemplate: (name) => {
return `export interface I${name} {}
`;
},
},
service: {
getClassFilePath(name) {
return `${this.getDirectoryPath(name)}/index.ts`;
},
getTypeFilePath(name) {
return `${this.getDirectoryPath(name)}/types.ts`;
},
getDirectoryPath: (name) => {
return `./src/data/${name}/service`;
},
getClassTemplate: (name) => {
const className = name + "Service";
const typeName = "I" + className;
return `import { ${typeName} } from "./types";
import { I${name}Repository } from "../repository/types";
export class ${className} implements ${typeName} {
constructor(private repository: I${name}Repository) {}
}
`;
},
getTypeTemplate: (name) =>
`export interface I${name}Service {}
`,
},
repository: {
getClassFilePath(name) {
return `${this.getDirectoryPath(name)}/index.ts`;
},
getTypeFilePath(name) {
return `${this.getDirectoryPath(name)}/types.ts`;
},
getDirectoryPath: (name) => {
return `./src/data/${name}/repository`;
},
getClassTemplate: (name) => {
const className = name + "Repository";
const typeName = "I" + className;
return `import { BaseRepository } from "../../BaseRepository";
import { ${typeName} } from "./types";
export class ${className} extends BaseRepository implements ${typeName} {}
`;
},
getTypeTemplate: (name) =>
`export interface I${name}Repository {}
`,
},
VM: {
getClassFilePath(name) {
return `${this.getDirectoryPath(name)}/index.ts`;
},
getTypeFilePath(name) {
return `${this.getDirectoryPath(name)}/types.ts`;
},
getDirectoryPath(name) {
return `./src/view/viewModels/${name}`;
},
getClassTemplate(name) {
const className = name + "VM";
const typeName = "I" + className;
return `import { makeObservable } from "mobx";
import { I${name}Service } from "../../../data/${name}/service/types";
import { INotificationsVM } from "../types";
import { BaseVM } from "../BaseVM";
import { ${typeName} } from "./types";
export class ${className} extends BaseVM implements ${typeName} {
constructor(
notificationsVM: INotificationsVM,
private service: I${name}Service
) {
super(notificationsVM);
makeObservable(this, {});
}
}
`;
},
getTypeTemplate: (name) => `import { IBaseVM } from "../types";
export interface I${name}VM extends IBaseVM {}
`,
},
getIndexPath: (name) => `./src/data/${name}/index.ts`,
indexTemplate: `export * from "./entity";
export * from "./service";
export * from "./repository";
export * from "./entity/types";
export * from "./repository/types";
export * from "./service/types";
`,
};
/**
* Название модуля.
* @type {string}
*/
const moduleName = capitalizeFirstLetter(moduleNameArg);
/**
* Создаваемые типы.
* @type {string[]}
*/
const types = ["entity", "repository", "service", "VM"];
types.forEach((type) => {
/**
* Создает соответствующую папку.
*/
fs.mkdirSync(builder[type].getDirectoryPath(moduleName), { recursive: true });
/**
* Создает соответствующий класс.
*/
fs.writeFile(
builder[type].getClassFilePath(moduleName),
builder[type].getClassTemplate(moduleName),
errorCallback
);
/**
* Создает соответствующий интерфейс.
*/
fs.writeFile(
builder[type].getTypeFilePath(moduleName),
builder[type].getTypeTemplate(moduleName),
errorCallback
);
/**
* Создает корневой файл.
*/
fs.writeFile(
builder.getIndexPath(moduleName),
builder.indexTemplate,
errorCallback
);
});
console.log(`The module ${moduleName} was created!`);