-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathBaseGenerator.mjs
More file actions
122 lines (95 loc) · 4.02 KB
/
BaseGenerator.mjs
File metadata and controls
122 lines (95 loc) · 4.02 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
import path from 'path';
import fs from 'fs';
import { exec } from 'child_process';
const fsPromises = fs.promises;
class BaseGenerator {
constructor({ config, model, logger }) {
this.config = config;
this.logger = logger;
this.model = model;
this.modelNames = this.generateModelNameVariants(model.name, config.namespace);
}
generateModelNameVariants = (modelName, namespace = '') => {
return {
'original' : modelName,
'plural' : `${modelName}s`,
'plural_lc' : `${modelName.toLowerCase()}s`,
'lcc' : modelName[0].toLowerCase() + modelName.slice(1), // lowerCamelCase
'plural_lcc' : `${modelName[0].toLowerCase() + modelName.slice(1) }s`, // lowerCamelCase
// eslint-disable-next-line prefer-template
'namespaced' : namespace
.split('/')
.map(name => name ? `${name[0].toUpperCase()}${name.slice(1)}` : '')
.join('') + modelName,
// eslint-disable-next-line prefer-template
'namespaced_plural' : namespace
.split('/')
.map(name => name ? `${name[0].toUpperCase()}${name.slice(1)}` : '')
.join('') + modelName + 's'
};
}
fileExists(filePath) {
const absolutePath = path.join(process.cwd(), filePath);
// eslint-disable-next-line no-sync
return fs.existsSync(absolutePath);
}
async readFileString(filePath) {
const absolutePath = path.join(process.cwd(), filePath);
const fileBuffer = await fsPromises.readFile(absolutePath);
return fileBuffer.toString();
}
async writeFile(filePath, data, mode = 'write') {
const absolutePath = path.join(process.cwd(), filePath);
if (mode === 'append') {
const fileData = await fsPromises.readFile(absolutePath, 'utf8');
if (!fileData.includes(this.config.replaceTag)) throw new Error(`${absolutePath} file must contain ${this.config.replaceTag} comment for successful execution`);
await fsPromises.writeFile(
absolutePath,
fileData.replace(this.config.replaceTag, `${this.config.replaceTag}\n${data}`)
);
} else if (mode === 'write') {
await fsPromises.writeFile(absolutePath, data);
}
}
async fixEsLint(filePath) {
const absolutePath = path.join(process.cwd(), filePath);
exec(`npx eslint ${absolutePath} --fix`);
}
async writeFileAndFixEslint(filePath, data, mode) {
await this.writeFile(filePath, data, mode);
this.fixEsLint(filePath);
}
async appendLines(filePath, lines) {
let line = lines.shift();
while (lines.length !== 0) {
await fsPromises.appendFile(filePath, `${line}\n`);
line = lines.shift();
}
}
async createFolderIfNotExists(folderPath) {
const absolutePath = path.join(process.cwd(), folderPath);
// eslint-disable-next-line no-sync
if (!fs.existsSync(absolutePath)) {
await fsPromises.mkdir(absolutePath, { recursive: true });
}
}
async copyFileIfNotExists(targetFile, sourceFile) {
const targetAbsolutePath = path.join(process.cwd(), targetFile);
const sourceAbsolutePath = path.join(process.cwd(), sourceFile);
// eslint-disable-next-line no-sync
if (!fs.existsSync(targetAbsolutePath)) {
await fsPromises.copyFile(sourceAbsolutePath, targetAbsolutePath);
this.logger.created(targetFile);
}
}
fillTemplateWithData(template, data = {}) {
let result = template;
for (const [ templateKey, fillData ] of Object.entries(data)) {
// eslint-disable-next-line security/detect-non-literal-regexp
const regExp = new RegExp(`{{${ templateKey }}}`, 'g');
result = result.replace(regExp, fillData);
}
return result;
}
}
export default BaseGenerator;