-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-module.mjs
More file actions
112 lines (98 loc) · 3.85 KB
/
generate-module.mjs
File metadata and controls
112 lines (98 loc) · 3.85 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
import fs from 'fs';
import path from 'path';
// --- Utilitários de String ---
const toPascalCase = (str) => str.replace(/(^\w|-\w)/g, (m) => m.replace(/-/, '').toUpperCase());
const toCamelCase = (str) => str.replace(/-(\w)/g, (m, c) => c.toUpperCase());
const toKebabCase = (str) => str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
const toSnakeCase = (str) => str.replace(/-/g, '_');
const pluralize = (word) => {
if (word.endsWith('y')) return word.slice(0, -1) + 'ies';
if (word.endsWith('r') || word.endsWith('l')) return word + 'es';
return word + 's';
};
async function generateModule() {
const args = process.argv.slice(2);
const nameArg = args.find(a => a.startsWith('--name='))?.split('=')[1];
const labelPt = args.find(a => a.startsWith('--pt='))?.split('=')[1] || nameArg;
if (!nameArg) {
console.error("❌ Erro: Use --name=nome-do-modulo");
process.exit(1);
}
if (!labelPt) {
console.error("❌ Erro: Use --pt=nome-do-modulo");
process.exit(1);
}
const nameKebab = toKebabCase(nameArg);
const config = {
single: {
kebab: nameKebab,
pascal: toPascalCase(nameKebab),
camel: toCamelCase(nameKebab),
snake: toSnakeCase(nameKebab)
},
plural: {
kebab: pluralize(nameKebab),
pascal: pluralize(toPascalCase(nameKebab)),
camel: pluralize(toCamelCase(nameKebab)),
snake: toSnakeCase(pluralize(nameKebab))
},
labelPt
};
const template = {
kebab: '__nameKebab__',
pascal: '__namePascal__',
camel: '{{nameCamel}}',
snake: '{{nameSnake}}',
label: '{{labelPt}}'
};
const paths = {
source: path.join(process.cwd(), 'src/modules/template-base'),
targetModule: path.join(process.cwd(), 'src/modules', config.single.kebab),
targetApp: path.join(process.cwd(), 'src/app/_private', labelPt)
};
const processFiles = (source, target, isRouteFolder = false) => {
if (!fs.existsSync(target)) fs.mkdirSync(target, { recursive: true });
fs.readdirSync(source).forEach(item => {
const sourcePath = path.join(source, item);
if (!isRouteFolder && item === '_app-routes') return;
// Renomeia o arquivo usando as chaves como busca
let targetItemName = item
.replaceAll(template.kebab, config.single.kebab)
.replaceAll(template.pascal, config.single.pascal)
.replaceAll(template.camel, config.single.camel)
.replaceAll(template.snake, config.single.snake);
const targetPath = path.join(target, targetItemName);
if (fs.lstatSync(sourcePath).isDirectory()) {
processFiles(sourcePath, targetPath, isRouteFolder);
} else {
let content = fs.readFileSync(sourcePath, 'utf8');
content = content
// Plurais (Se você usar {{nameKebabPlural}} no futuro)
.replaceAll('{{nameKebabPlural}}', config.plural.kebab)
.replaceAll('{{namePascalPlural}}', config.plural.pascal)
// Singulares
.replaceAll(template.kebab, config.single.kebab)
.replaceAll(template.pascal, config.single.pascal)
.replaceAll(template.camel, config.single.camel)
.replaceAll(template.snake, config.single.snake)
.replaceAll(template.label, config.labelPt);
fs.writeFileSync(targetPath, content);
}
});
};
try {
processFiles(paths.source, paths.targetModule);
const routeSource = path.join(paths.source, '_app-routes');
if (fs.existsSync(routeSource)) {
processFiles(routeSource, paths.targetApp, true);
}
console.info(`✅ Sucesso! Arquivos renomeados e conteúdo gerado para "${config.single.pascal}".`);
} catch (err) {
console.error('❌ Erro:', err);
}
}
generateModule();
// Comando para rodar abaixo
// npm run gen:module -- --name=product --pt='produto'
// Em seguida rode o eslint para corrigir os arquivos gerados
// npx eslint --fix src/modules/product