@diagramers/cli 1.0.22 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -3
- package/dist/services/template-updater.d.ts +2 -0
- package/dist/services/template-updater.d.ts.map +1 -1
- package/dist/services/template-updater.js +66 -14
- package/dist/services/template-updater.js.map +1 -1
- package/package.json +5 -1
- package/templates/api/certs/auth-app-cert.json +13 -0
- package/templates/api/main.ts +10 -0
- package/templates/api/package.json +70 -0
- package/templates/api/src/assets/css/email-template.css +8 -0
- package/templates/api/src/assets/images/logo_large.png +0 -0
- package/templates/api/src/assets/keys/certificate.pem +22 -0
- package/templates/api/src/assets/keys/private-key.pem +28 -0
- package/templates/api/src/config/config-interface.ts +191 -0
- package/templates/api/src/config/development.ts +145 -0
- package/templates/api/src/config/index.ts +59 -0
- package/templates/api/src/config/production.ts +145 -0
- package/templates/api/src/config/staging.ts +144 -0
- package/templates/api/src/config/uat.ts +144 -0
- package/templates/api/src/controllers/account-controller.ts +162 -0
- package/templates/api/src/entities/audit.ts +12 -0
- package/templates/api/src/entities/base-entity.ts +10 -0
- package/templates/api/src/entities/user.ts +71 -0
- package/templates/api/src/helpers/FrameworkHelper.ts +157 -0
- package/templates/api/src/helpers/auth.ts +971 -0
- package/templates/api/src/helpers/cronHelper.ts +170 -0
- package/templates/api/src/helpers/dbcontext.ts +83 -0
- package/templates/api/src/helpers/encryptionHelper.ts +76 -0
- package/templates/api/src/helpers/enums.ts +258 -0
- package/templates/api/src/helpers/handle-response.ts +49 -0
- package/templates/api/src/helpers/httpHelper.ts +75 -0
- package/templates/api/src/helpers/mailer.ts +152 -0
- package/templates/api/src/helpers/result.ts +47 -0
- package/templates/api/src/helpers/string-helper.ts +27 -0
- package/templates/api/src/routes/account-routes.ts +37 -0
- package/templates/api/src/routes/auth-routes.ts +286 -0
- package/templates/api/src/routes/index.ts +92 -0
- package/templates/api/src/schemas/audit.ts +36 -0
- package/templates/api/src/schemas/otp.ts +52 -0
- package/templates/api/src/schemas/session.ts +57 -0
- package/templates/api/src/schemas/user.ts +125 -0
- package/templates/api/src/server/index.ts +86 -0
- package/templates/api/src/server/socket-server-provider.ts +209 -0
- package/templates/api/src/services/account-service.ts +243 -0
- package/templates/api/src/services/audit-service.ts +56 -0
- package/templates/api/tsconfig.json +16 -0
- package/templates/api/webpack.config.js +66 -0
- package/scripts/publish.sh +0 -58
- package/scripts/setup.sh +0 -38
- package/scripts/version.sh +0 -80
- package/src/commands/api.ts +0 -76
- package/src/commands/extend.ts +0 -35
- package/src/commands/init.ts +0 -32
- package/src/commands/update.ts +0 -25
- package/src/config/template-config.ts +0 -111
- package/src/index.ts +0 -41
- package/src/services/api-generator.ts +0 -378
- package/src/services/project-extender.ts +0 -330
- package/src/services/project-initializer.ts +0 -335
- package/src/services/project-updater.ts +0 -117
- package/src/services/relation-generator.ts +0 -203
- package/src/services/table-generator.ts +0 -114
- package/src/services/template-processor.ts +0 -166
- package/src/services/template-updater.ts +0 -184
- package/tsconfig.json +0 -19
package/src/commands/init.ts
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
import { Command } from 'commander';
|
2
|
-
import { ProjectInitializer } from '../services/project-initializer';
|
3
|
-
import chalk from 'chalk';
|
4
|
-
|
5
|
-
export function initCommand(program: Command) {
|
6
|
-
program
|
7
|
-
.command('init <template-type> <project-name>')
|
8
|
-
.description('Initialize a new project from the template')
|
9
|
-
.option('-t, --template <template>', 'Template version to use', 'latest')
|
10
|
-
.option('-y, --yes', 'Skip prompts and use defaults')
|
11
|
-
.action(async (templateType: string, projectName: string, options: any) => {
|
12
|
-
try {
|
13
|
-
console.log(chalk.blue(`🚀 Initializing new ${templateType} project: ${projectName}`));
|
14
|
-
|
15
|
-
// Validate template type
|
16
|
-
if (templateType !== 'api' && templateType !== 'admin') {
|
17
|
-
throw new Error(`Unsupported template type: ${templateType}. Supported types: api, admin`);
|
18
|
-
}
|
19
|
-
|
20
|
-
const initializer = new ProjectInitializer();
|
21
|
-
await initializer.initialize(projectName, { ...options, template: templateType });
|
22
|
-
|
23
|
-
console.log(chalk.green(`✅ Project ${projectName} initialized successfully!`));
|
24
|
-
console.log(chalk.yellow(`📁 Navigate to the project: cd ${projectName}`));
|
25
|
-
console.log(chalk.yellow(`🔧 Install dependencies: npm install`));
|
26
|
-
console.log(chalk.yellow(`🚀 Start development: npm run serve`));
|
27
|
-
} catch (error: any) {
|
28
|
-
console.error(chalk.red(`❌ Failed to initialize project: ${error.message}`));
|
29
|
-
process.exit(1);
|
30
|
-
}
|
31
|
-
});
|
32
|
-
}
|
package/src/commands/update.ts
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
import { Command } from 'commander';
|
2
|
-
import { TemplateUpdater } from '../services/template-updater';
|
3
|
-
import chalk from 'chalk';
|
4
|
-
|
5
|
-
export function updateCommand(program: Command) {
|
6
|
-
program
|
7
|
-
.command('update')
|
8
|
-
.description('Update existing project with latest template features')
|
9
|
-
.option('-f, --force', 'Force update even if conflicts detected')
|
10
|
-
.option('-b, --backup', 'Create backup before updating')
|
11
|
-
.action(async (options: any) => {
|
12
|
-
try {
|
13
|
-
console.log(chalk.blue('🔄 Checking for template updates...'));
|
14
|
-
|
15
|
-
const updater = new TemplateUpdater();
|
16
|
-
await updater.update(options);
|
17
|
-
|
18
|
-
console.log(chalk.green('✅ Project updated successfully!'));
|
19
|
-
console.log(chalk.yellow('📝 Review the changes and test your application'));
|
20
|
-
} catch (error: any) {
|
21
|
-
console.error(chalk.red(`❌ Failed to update project: ${error.message}`));
|
22
|
-
process.exit(1);
|
23
|
-
}
|
24
|
-
});
|
25
|
-
}
|
@@ -1,111 +0,0 @@
|
|
1
|
-
export interface TemplateConfig {
|
2
|
-
name: string;
|
3
|
-
version: string;
|
4
|
-
description: string;
|
5
|
-
files: {
|
6
|
-
include: string[];
|
7
|
-
exclude: string[];
|
8
|
-
updateable: string[];
|
9
|
-
customizable: string[];
|
10
|
-
};
|
11
|
-
features: {
|
12
|
-
[key: string]: {
|
13
|
-
name: string;
|
14
|
-
description: string;
|
15
|
-
dependencies: string[];
|
16
|
-
files: string[];
|
17
|
-
};
|
18
|
-
};
|
19
|
-
}
|
20
|
-
|
21
|
-
export const templateConfig: TemplateConfig = {
|
22
|
-
name: 'diagramers-api-template',
|
23
|
-
version: '1.0.0',
|
24
|
-
description: 'Node.js API template with TypeScript, Firebase Functions, and Socket.io',
|
25
|
-
|
26
|
-
files: {
|
27
|
-
include: [
|
28
|
-
'src/**/*',
|
29
|
-
'package.json',
|
30
|
-
'tsconfig.json',
|
31
|
-
'webpack.config.js',
|
32
|
-
'main.ts',
|
33
|
-
'certs/**/*'
|
34
|
-
],
|
35
|
-
exclude: [
|
36
|
-
'node_modules/**',
|
37
|
-
'dist/**',
|
38
|
-
'cli/**',
|
39
|
-
'.git/**',
|
40
|
-
'backup-*/**',
|
41
|
-
'*.log',
|
42
|
-
'.env*'
|
43
|
-
],
|
44
|
-
updateable: [
|
45
|
-
'src/helpers/**/*',
|
46
|
-
'src/config/**/*',
|
47
|
-
'src/server/**/*',
|
48
|
-
'webpack.config.js',
|
49
|
-
'tsconfig.json'
|
50
|
-
],
|
51
|
-
customizable: [
|
52
|
-
'src/controllers/**/*',
|
53
|
-
'src/entities/**/*',
|
54
|
-
'src/routes/**/*',
|
55
|
-
'src/services/**/*',
|
56
|
-
'src/schemas/**/*',
|
57
|
-
'main.ts'
|
58
|
-
]
|
59
|
-
},
|
60
|
-
|
61
|
-
features: {
|
62
|
-
auth: {
|
63
|
-
name: 'Authentication',
|
64
|
-
description: 'JWT-based authentication system',
|
65
|
-
dependencies: ['jsonwebtoken', 'bcryptjs', '@types/jsonwebtoken', '@types/bcryptjs'],
|
66
|
-
files: [
|
67
|
-
'src/features/auth/**/*',
|
68
|
-
'src/helpers/auth.ts'
|
69
|
-
]
|
70
|
-
},
|
71
|
-
email: {
|
72
|
-
name: 'Email System',
|
73
|
-
description: 'Email notification system with templates',
|
74
|
-
dependencies: ['nodemailer', 'handlebars', '@types/nodemailer'],
|
75
|
-
files: [
|
76
|
-
'src/features/email/**/*',
|
77
|
-
'src/helpers/mailer.ts',
|
78
|
-
'src/assets/css/email-template.css'
|
79
|
-
]
|
80
|
-
},
|
81
|
-
socket: {
|
82
|
-
name: 'WebSocket',
|
83
|
-
description: 'Real-time communication with Socket.io',
|
84
|
-
dependencies: ['socket.io', '@types/socket.io'],
|
85
|
-
files: [
|
86
|
-
'src/features/socket/**/*',
|
87
|
-
'src/server/socket-server-provider.ts'
|
88
|
-
]
|
89
|
-
},
|
90
|
-
cron: {
|
91
|
-
name: 'Scheduled Tasks',
|
92
|
-
description: 'Cron job system for scheduled tasks',
|
93
|
-
dependencies: ['node-cron', '@types/node-cron'],
|
94
|
-
files: [
|
95
|
-
'src/features/cron/**/*',
|
96
|
-
'src/helpers/cronHelper.ts'
|
97
|
-
]
|
98
|
-
},
|
99
|
-
audit: {
|
100
|
-
name: 'Audit Logging',
|
101
|
-
description: 'Comprehensive audit logging system',
|
102
|
-
dependencies: [],
|
103
|
-
files: [
|
104
|
-
'src/features/audit/**/*',
|
105
|
-
'src/entities/audit.ts',
|
106
|
-
'src/schemas/audit.ts',
|
107
|
-
'src/services/audit-service.ts'
|
108
|
-
]
|
109
|
-
}
|
110
|
-
}
|
111
|
-
};
|
package/src/index.ts
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
|
3
|
-
import { Command } from 'commander';
|
4
|
-
import { initCommand } from './commands/init';
|
5
|
-
import { updateCommand } from './commands/update';
|
6
|
-
import { extendCommand } from './commands/extend';
|
7
|
-
import { apiCommand } from './commands/api';
|
8
|
-
import chalk from 'chalk';
|
9
|
-
|
10
|
-
const program = new Command();
|
11
|
-
|
12
|
-
program
|
13
|
-
.name('diagramers')
|
14
|
-
.description('CLI tool for managing diagramers projects')
|
15
|
-
.version('1.0.0');
|
16
|
-
|
17
|
-
// Add commands
|
18
|
-
initCommand(program);
|
19
|
-
updateCommand(program);
|
20
|
-
extendCommand(program);
|
21
|
-
apiCommand(program);
|
22
|
-
|
23
|
-
// Add help text
|
24
|
-
program.addHelpText('after', `
|
25
|
-
Examples:
|
26
|
-
$ diagramers init api my-new-project
|
27
|
-
$ diagramers init admin my-admin-dashboard
|
28
|
-
$ diagramers update
|
29
|
-
$ diagramers extend --feature auth
|
30
|
-
$ diagramers api generate:module product
|
31
|
-
$ diagramers api generate:table category
|
32
|
-
$ diagramers api generate:relation product category one-to-many
|
33
|
-
$ diagramers api process:template my-api-project
|
34
|
-
`);
|
35
|
-
|
36
|
-
program.parse();
|
37
|
-
|
38
|
-
// If no command is provided, show help
|
39
|
-
if (!process.argv.slice(2).length) {
|
40
|
-
program.outputHelp();
|
41
|
-
}
|
@@ -1,378 +0,0 @@
|
|
1
|
-
import * as fs from 'fs';
|
2
|
-
import * as path from 'path';
|
3
|
-
|
4
|
-
export async function generateModule(moduleName: string): Promise<void> {
|
5
|
-
// Validate module name
|
6
|
-
if (!moduleName || !/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(moduleName)) {
|
7
|
-
throw new Error('Invalid module name. Use only letters, numbers, hyphens, and underscores. Must start with a letter.');
|
8
|
-
}
|
9
|
-
|
10
|
-
const moduleNameCapitalized = moduleName.charAt(0).toUpperCase() + moduleName.slice(1);
|
11
|
-
const currentDir = process.cwd();
|
12
|
-
|
13
|
-
// Check for required API project structure
|
14
|
-
const requiredDirs = [
|
15
|
-
'src/entities',
|
16
|
-
'src/schemas',
|
17
|
-
'src/services',
|
18
|
-
'src/controllers',
|
19
|
-
'src/routes'
|
20
|
-
];
|
21
|
-
const missingDirs = requiredDirs.filter(dir => !fs.existsSync(path.join(currentDir, dir)));
|
22
|
-
if (missingDirs.length > 0) {
|
23
|
-
throw new Error(`This command should be run from a diagramers API project. Missing directories: ${missingDirs.join(', ')}`);
|
24
|
-
}
|
25
|
-
|
26
|
-
console.log('📝 Creating entity...');
|
27
|
-
const entityContent = generateEntityContent(moduleName, moduleNameCapitalized);
|
28
|
-
fs.writeFileSync(path.join(currentDir, 'src/entities', `${moduleName}.ts`), entityContent);
|
29
|
-
|
30
|
-
console.log('📋 Creating schema...');
|
31
|
-
const schemaContent = generateSchemaContent(moduleName, moduleNameCapitalized);
|
32
|
-
fs.writeFileSync(path.join(currentDir, 'src/schemas', `${moduleName}.ts`), schemaContent);
|
33
|
-
|
34
|
-
console.log('🔧 Creating service...');
|
35
|
-
const serviceContent = generateServiceContent(moduleName, moduleNameCapitalized);
|
36
|
-
fs.writeFileSync(path.join(currentDir, 'src/services', `${moduleName}-service.ts`), serviceContent);
|
37
|
-
|
38
|
-
console.log('🎮 Creating controller...');
|
39
|
-
const controllerContent = generateControllerContent(moduleName, moduleNameCapitalized);
|
40
|
-
fs.writeFileSync(path.join(currentDir, 'src/controllers', `${moduleName}-controller.ts`), controllerContent);
|
41
|
-
|
42
|
-
console.log('🛣️ Creating routes...');
|
43
|
-
const routesContent = generateRoutesContent(moduleName, moduleNameCapitalized);
|
44
|
-
fs.writeFileSync(path.join(currentDir, 'src/routes', `${moduleName}-routes.ts`), routesContent);
|
45
|
-
|
46
|
-
// Update dbcontext to include the new entity
|
47
|
-
updateDbContext(currentDir, moduleName, moduleNameCapitalized);
|
48
|
-
|
49
|
-
// Update routes index to include the new routes
|
50
|
-
updateRoutesIndex(currentDir, moduleName);
|
51
|
-
}
|
52
|
-
|
53
|
-
function generateEntityContent(moduleName: string, moduleNameCapitalized: string): string {
|
54
|
-
return `import * as mongoose from 'mongoose';
|
55
|
-
import { ObjectId } from "bson";
|
56
|
-
|
57
|
-
export interface I${moduleNameCapitalized} extends mongoose.Document {
|
58
|
-
_id: ObjectId,
|
59
|
-
name: string,
|
60
|
-
description?: string,
|
61
|
-
status: number,
|
62
|
-
createdAt: Date,
|
63
|
-
updatedAt: Date
|
64
|
-
}`;
|
65
|
-
}
|
66
|
-
|
67
|
-
function generateSchemaContent(moduleName: string, moduleNameCapitalized: string): string {
|
68
|
-
return `import * as mongoose from 'mongoose';
|
69
|
-
import { I${moduleNameCapitalized} } from '../entities/${moduleName}';
|
70
|
-
|
71
|
-
export const ${moduleName}Schema = new mongoose.Schema(
|
72
|
-
{
|
73
|
-
name: {
|
74
|
-
type: mongoose.SchemaTypes.String,
|
75
|
-
required: true,
|
76
|
-
},
|
77
|
-
description: {
|
78
|
-
type: mongoose.SchemaTypes.String,
|
79
|
-
required: false,
|
80
|
-
},
|
81
|
-
status: {
|
82
|
-
type: mongoose.SchemaTypes.Number,
|
83
|
-
default: 1,
|
84
|
-
}
|
85
|
-
},
|
86
|
-
{ timestamps: true, suppressReservedKeysWarning: true },
|
87
|
-
);
|
88
|
-
|
89
|
-
export const ${moduleNameCapitalized}Entity = mongoose.model<I${moduleNameCapitalized}>('${moduleName}', ${moduleName}Schema);`;
|
90
|
-
}
|
91
|
-
|
92
|
-
function generateServiceContent(moduleName: string, moduleNameCapitalized: string): string {
|
93
|
-
return `import { ObjectId } from "bson";
|
94
|
-
import dbcontext from "../helpers/dbcontext";
|
95
|
-
import { ResponseCode } from "../helpers/enums";
|
96
|
-
import { Result } from "../helpers/result";
|
97
|
-
import { ${moduleNameCapitalized}Entity } from "../schemas/${moduleName}";
|
98
|
-
|
99
|
-
export class ${moduleNameCapitalized}Service {
|
100
|
-
result: Result;
|
101
|
-
className = '${moduleNameCapitalized}Service';
|
102
|
-
|
103
|
-
constructor(result: Result) {
|
104
|
-
this.result = result;
|
105
|
-
}
|
106
|
-
|
107
|
-
async getAll(): Promise<Result> {
|
108
|
-
try {
|
109
|
-
const ${moduleName}s = await dbcontext.${moduleNameCapitalized}Entity.find({ status: 1 });
|
110
|
-
this.result.Data = ${moduleName}s;
|
111
|
-
this.result.Status = ResponseCode.Ok;
|
112
|
-
return this.result;
|
113
|
-
} catch (ex) {
|
114
|
-
this.result.addException(this.className, 'GetAll', ex);
|
115
|
-
return this.result;
|
116
|
-
}
|
117
|
-
}
|
118
|
-
|
119
|
-
async getById(id: string): Promise<Result> {
|
120
|
-
try {
|
121
|
-
const ${moduleName} = await dbcontext.${moduleNameCapitalized}Entity.findById(id);
|
122
|
-
if (${moduleName}) {
|
123
|
-
this.result.Data = ${moduleName};
|
124
|
-
this.result.Status = ResponseCode.Ok;
|
125
|
-
} else {
|
126
|
-
this.result.Status = ResponseCode.NotExist;
|
127
|
-
}
|
128
|
-
return this.result;
|
129
|
-
} catch (ex) {
|
130
|
-
this.result.addException(this.className, 'GetById', ex);
|
131
|
-
return this.result;
|
132
|
-
}
|
133
|
-
}
|
134
|
-
|
135
|
-
async create(${moduleName}Data: any): Promise<Result> {
|
136
|
-
try {
|
137
|
-
const ${moduleName}Entity = new ${moduleNameCapitalized}Entity({
|
138
|
-
_id: new ObjectId(),
|
139
|
-
...${moduleName}Data,
|
140
|
-
status: 1
|
141
|
-
});
|
142
|
-
const created${moduleNameCapitalized} = await dbcontext.${moduleNameCapitalized}Entity.create(${moduleName}Entity);
|
143
|
-
if (created${moduleNameCapitalized}) {
|
144
|
-
this.result.Data = created${moduleNameCapitalized};
|
145
|
-
this.result.Status = ResponseCode.Ok;
|
146
|
-
} else {
|
147
|
-
this.result.Status = ResponseCode.Error;
|
148
|
-
this.result.Errors.push('Failed to create ${moduleName}');
|
149
|
-
}
|
150
|
-
return this.result;
|
151
|
-
} catch (ex) {
|
152
|
-
this.result.addException(this.className, 'Create', ex);
|
153
|
-
return this.result;
|
154
|
-
}
|
155
|
-
}
|
156
|
-
|
157
|
-
async update(id: string, ${moduleName}Data: any): Promise<Result> {
|
158
|
-
try {
|
159
|
-
const updated${moduleNameCapitalized} = await dbcontext.${moduleNameCapitalized}Entity.findByIdAndUpdate(
|
160
|
-
id,
|
161
|
-
{ ...${moduleName}Data },
|
162
|
-
{ new: true }
|
163
|
-
);
|
164
|
-
if (updated${moduleNameCapitalized}) {
|
165
|
-
this.result.Data = updated${moduleNameCapitalized};
|
166
|
-
this.result.Status = ResponseCode.Ok;
|
167
|
-
} else {
|
168
|
-
this.result.Status = ResponseCode.NotExist;
|
169
|
-
}
|
170
|
-
return this.result;
|
171
|
-
} catch (ex) {
|
172
|
-
this.result.addException(this.className, 'Update', ex);
|
173
|
-
return this.result;
|
174
|
-
}
|
175
|
-
}
|
176
|
-
|
177
|
-
async delete(id: string): Promise<Result> {
|
178
|
-
try {
|
179
|
-
const deleted${moduleNameCapitalized} = await dbcontext.${moduleNameCapitalized}Entity.findByIdAndUpdate(
|
180
|
-
id,
|
181
|
-
{ status: 0 },
|
182
|
-
{ new: true }
|
183
|
-
);
|
184
|
-
if (deleted${moduleNameCapitalized}) {
|
185
|
-
this.result.Data = deleted${moduleNameCapitalized};
|
186
|
-
this.result.Status = ResponseCode.Ok;
|
187
|
-
} else {
|
188
|
-
this.result.Status = ResponseCode.NotExist;
|
189
|
-
}
|
190
|
-
return this.result;
|
191
|
-
} catch (ex) {
|
192
|
-
this.result.addException(this.className, 'Delete', ex);
|
193
|
-
return this.result;
|
194
|
-
}
|
195
|
-
}
|
196
|
-
}`;
|
197
|
-
}
|
198
|
-
|
199
|
-
function generateControllerContent(moduleName: string, moduleNameCapitalized: string): string {
|
200
|
-
return `import { AuditMessageType, ResponseCode } from "../helpers/enums";
|
201
|
-
import handleResponse from "../helpers/handle-response";
|
202
|
-
import { Result } from "../helpers/result";
|
203
|
-
import { ${moduleNameCapitalized}Service } from "../services/${moduleName}-service";
|
204
|
-
|
205
|
-
export default class ${moduleNameCapitalized}Controller {
|
206
|
-
|
207
|
-
async getAll(req, res) {
|
208
|
-
const result = res.locals.result;
|
209
|
-
try {
|
210
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'getAll', 'Started');
|
211
|
-
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
212
|
-
const serviceResult = await ${moduleName}Service.getAll();
|
213
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'getAll', 'Finished');
|
214
|
-
return handleResponse(req, res, serviceResult);
|
215
|
-
} catch (ex) {
|
216
|
-
result.addException(req.baseUrl, 'getAll', ex);
|
217
|
-
return handleResponse(req, res, result);
|
218
|
-
}
|
219
|
-
}
|
220
|
-
|
221
|
-
async getById(req, res) {
|
222
|
-
const result = res.locals.result;
|
223
|
-
try {
|
224
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'getById', 'Started');
|
225
|
-
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
226
|
-
const serviceResult = await ${moduleName}Service.getById(req.params.id);
|
227
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'getById', 'Finished');
|
228
|
-
return handleResponse(req, res, serviceResult);
|
229
|
-
} catch (ex) {
|
230
|
-
result.addException(req.baseUrl, 'getById', ex);
|
231
|
-
return handleResponse(req, res, result);
|
232
|
-
}
|
233
|
-
}
|
234
|
-
|
235
|
-
async create(req, res) {
|
236
|
-
const result = res.locals.result;
|
237
|
-
try {
|
238
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'create', 'Started');
|
239
|
-
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
240
|
-
const serviceResult = await ${moduleName}Service.create(req.body);
|
241
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'create', 'Finished');
|
242
|
-
return handleResponse(req, res, serviceResult);
|
243
|
-
} catch (ex) {
|
244
|
-
result.addException(req.baseUrl, 'create', ex);
|
245
|
-
return handleResponse(req, res, result);
|
246
|
-
}
|
247
|
-
}
|
248
|
-
|
249
|
-
async update(req, res) {
|
250
|
-
const result = res.locals.result;
|
251
|
-
try {
|
252
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'update', 'Started');
|
253
|
-
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
254
|
-
const serviceResult = await ${moduleName}Service.update(req.params.id, req.body);
|
255
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'update', 'Finished');
|
256
|
-
return handleResponse(req, res, serviceResult);
|
257
|
-
} catch (ex) {
|
258
|
-
result.addException(req.baseUrl, 'update', ex);
|
259
|
-
return handleResponse(req, res, result);
|
260
|
-
}
|
261
|
-
}
|
262
|
-
|
263
|
-
async delete(req, res) {
|
264
|
-
const result = res.locals.result;
|
265
|
-
try {
|
266
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'delete', 'Started');
|
267
|
-
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
268
|
-
const serviceResult = await ${moduleName}Service.delete(req.params.id);
|
269
|
-
result.addMessage(AuditMessageType.info, req.baseUrl, 'delete', 'Finished');
|
270
|
-
return handleResponse(req, res, serviceResult);
|
271
|
-
} catch (ex) {
|
272
|
-
result.addException(req.baseUrl, 'delete', ex);
|
273
|
-
return handleResponse(req, res, result);
|
274
|
-
}
|
275
|
-
}
|
276
|
-
}`;
|
277
|
-
}
|
278
|
-
|
279
|
-
function generateRoutesContent(moduleName: string, moduleNameCapitalized: string): string {
|
280
|
-
return `import express from 'express';
|
281
|
-
import ${moduleNameCapitalized}Controller from '../controllers/${moduleName}-controller';
|
282
|
-
|
283
|
-
const router = express.Router();
|
284
|
-
const ${moduleName}Controller = new ${moduleNameCapitalized}Controller();
|
285
|
-
|
286
|
-
// GET /api/${moduleName}s
|
287
|
-
router.get('/', ${moduleName}Controller.getAll.bind(${moduleName}Controller));
|
288
|
-
|
289
|
-
// GET /api/${moduleName}s/:id
|
290
|
-
router.get('/:id', ${moduleName}Controller.getById.bind(${moduleName}Controller));
|
291
|
-
|
292
|
-
// POST /api/${moduleName}s
|
293
|
-
router.post('/', ${moduleName}Controller.create.bind(${moduleName}Controller));
|
294
|
-
|
295
|
-
// PUT /api/${moduleName}s/:id
|
296
|
-
router.put('/:id', ${moduleName}Controller.update.bind(${moduleName}Controller));
|
297
|
-
|
298
|
-
// DELETE /api/${moduleName}s/:id
|
299
|
-
router.delete('/:id', ${moduleName}Controller.delete.bind(${moduleName}Controller));
|
300
|
-
|
301
|
-
export default router;`;
|
302
|
-
}
|
303
|
-
|
304
|
-
function updateDbContext(currentDir: string, moduleName: string, moduleNameCapitalized: string): void {
|
305
|
-
const dbcontextPath = path.join(currentDir, 'src/helpers/dbcontext.ts');
|
306
|
-
if (!fs.existsSync(dbcontextPath)) {
|
307
|
-
console.log('⚠️ dbcontext.ts not found, skipping dbcontext update');
|
308
|
-
return;
|
309
|
-
}
|
310
|
-
|
311
|
-
let dbcontextContent = fs.readFileSync(dbcontextPath, 'utf8');
|
312
|
-
|
313
|
-
// Add import
|
314
|
-
const importStatement = `import { ${moduleNameCapitalized}Entity } from '../schemas/${moduleName}';`;
|
315
|
-
if (!dbcontextContent.includes(importStatement)) {
|
316
|
-
// Find the last import statement and add after it
|
317
|
-
const importRegex = /import.*from.*['"];?\s*$/gm;
|
318
|
-
const matches = [...dbcontextContent.matchAll(importRegex)];
|
319
|
-
if (matches.length > 0) {
|
320
|
-
const lastImport = matches[matches.length - 1];
|
321
|
-
const insertIndex = lastImport.index! + lastImport[0].length;
|
322
|
-
dbcontextContent = dbcontextContent.slice(0, insertIndex) + '\n' + importStatement + dbcontextContent.slice(insertIndex);
|
323
|
-
}
|
324
|
-
}
|
325
|
-
|
326
|
-
// Add to dbcontext object
|
327
|
-
const dbcontextObjectRegex = /export\s+default\s*\{([^}]*)\}/s;
|
328
|
-
const match = dbcontextContent.match(dbcontextObjectRegex);
|
329
|
-
if (match) {
|
330
|
-
const objectContent = match[1];
|
331
|
-
if (!objectContent.includes(`${moduleNameCapitalized}Entity`)) {
|
332
|
-
const newObjectContent = objectContent.trim() + `,\n ${moduleNameCapitalized}Entity`;
|
333
|
-
dbcontextContent = dbcontextContent.replace(dbcontextObjectRegex, `export default {$1${newObjectContent}\n}`);
|
334
|
-
}
|
335
|
-
}
|
336
|
-
|
337
|
-
fs.writeFileSync(dbcontextPath, dbcontextContent);
|
338
|
-
console.log('📊 Updated dbcontext.ts');
|
339
|
-
}
|
340
|
-
|
341
|
-
function updateRoutesIndex(currentDir: string, moduleName: string): void {
|
342
|
-
const routesIndexPath = path.join(currentDir, 'src/routes/index.ts');
|
343
|
-
if (!fs.existsSync(routesIndexPath)) {
|
344
|
-
console.log('⚠️ routes/index.ts not found, skipping routes index update');
|
345
|
-
return;
|
346
|
-
}
|
347
|
-
|
348
|
-
let routesContent = fs.readFileSync(routesIndexPath, 'utf8');
|
349
|
-
|
350
|
-
// Add import
|
351
|
-
const importStatement = `import ${moduleName}Routes from './${moduleName}-routes';`;
|
352
|
-
if (!routesContent.includes(importStatement)) {
|
353
|
-
// Find the last import statement and add after it
|
354
|
-
const importRegex = /import.*from.*['"];?\s*$/gm;
|
355
|
-
const matches = [...routesContent.matchAll(importRegex)];
|
356
|
-
if (matches.length > 0) {
|
357
|
-
const lastImport = matches[matches.length - 1];
|
358
|
-
const insertIndex = lastImport.index! + lastImport[0].length;
|
359
|
-
routesContent = routesContent.slice(0, insertIndex) + '\n' + importStatement + routesContent.slice(insertIndex);
|
360
|
-
}
|
361
|
-
}
|
362
|
-
|
363
|
-
// Add route registration
|
364
|
-
const routeRegistration = `app.use('/api/${moduleName}s', ${moduleName}Routes);`;
|
365
|
-
if (!routesContent.includes(routeRegistration)) {
|
366
|
-
// Find where routes are registered (usually after imports)
|
367
|
-
const routeRegex = /app\.use\('\/api\/.*',.*\);?\s*$/gm;
|
368
|
-
const matches = [...routesContent.matchAll(routeRegex)];
|
369
|
-
if (matches.length > 0) {
|
370
|
-
const lastRoute = matches[matches.length - 1];
|
371
|
-
const insertIndex = lastRoute.index! + lastRoute[0].length;
|
372
|
-
routesContent = routesContent.slice(0, insertIndex) + '\n' + routeRegistration + routesContent.slice(insertIndex);
|
373
|
-
}
|
374
|
-
}
|
375
|
-
|
376
|
-
fs.writeFileSync(routesIndexPath, routesContent);
|
377
|
-
console.log('🛣️ Updated routes/index.ts');
|
378
|
-
}
|