@diagramers/cli 1.0.12 → 1.0.14
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/dist/commands/api.d.ts +3 -0
- package/dist/commands/api.d.ts.map +1 -0
- package/dist/commands/api.js +46 -0
- package/dist/commands/api.js.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/services/api-generator.d.ts +2 -0
- package/dist/services/api-generator.d.ts.map +1 -0
- package/dist/services/api-generator.js +387 -0
- package/dist/services/api-generator.js.map +1 -0
- package/dist/services/project-initializer.d.ts +2 -0
- package/dist/services/project-initializer.d.ts.map +1 -1
- package/dist/services/project-initializer.js +55 -0
- package/dist/services/project-initializer.js.map +1 -1
- package/dist/services/template-processor.d.ts +2 -0
- package/dist/services/template-processor.d.ts.map +1 -0
- package/dist/services/template-processor.js +162 -0
- package/dist/services/template-processor.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/api.ts +42 -0
- package/src/index.ts +4 -0
- package/src/services/api-generator.ts +376 -0
- package/src/services/project-initializer.ts +64 -0
- package/src/services/template-processor.ts +161 -0
@@ -0,0 +1,162 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
36
|
+
exports.processTemplate = processTemplate;
|
37
|
+
const fs = __importStar(require("fs"));
|
38
|
+
const path = __importStar(require("path"));
|
39
|
+
async function processTemplate(projectName) {
|
40
|
+
if (!projectName) {
|
41
|
+
throw new Error('Project name is required');
|
42
|
+
}
|
43
|
+
const currentDir = process.cwd();
|
44
|
+
// Check if we're in a diagramers API project
|
45
|
+
const packageJsonPath = path.join(currentDir, 'package.json');
|
46
|
+
if (!fs.existsSync(packageJsonPath)) {
|
47
|
+
throw new Error('package.json not found. Please run this command from a diagramers API project root.');
|
48
|
+
}
|
49
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
50
|
+
if (!packageJson.name?.includes('diagramers') && !packageJson.name?.includes('api')) {
|
51
|
+
throw new Error('This command should be run from a diagramers API project.');
|
52
|
+
}
|
53
|
+
// Convert project name to database-friendly format
|
54
|
+
const dbName = projectName
|
55
|
+
.toLowerCase()
|
56
|
+
.replace(/[^a-zA-Z0-9]/g, '_')
|
57
|
+
.replace(/_+/g, '_')
|
58
|
+
.replace(/^_|_$/g, '');
|
59
|
+
console.log(`🗄️ Database name will be: ${dbName}`);
|
60
|
+
// Process configuration files
|
61
|
+
console.log('📝 Updating configuration files...');
|
62
|
+
// Update development config
|
63
|
+
const devConfigPath = path.join(currentDir, 'src/config/development.ts');
|
64
|
+
if (fs.existsSync(devConfigPath)) {
|
65
|
+
let devConfig = fs.readFileSync(devConfigPath, 'utf8');
|
66
|
+
devConfig = devConfig.replace(/mongodb:\/\/127\.0\.0\.1:27017\/[^,\s]+/g, `mongodb://127.0.0.1:27017/${dbName}-development`);
|
67
|
+
fs.writeFileSync(devConfigPath, devConfig);
|
68
|
+
console.log(' ✅ Updated development.ts');
|
69
|
+
}
|
70
|
+
// Update staging config
|
71
|
+
const stagingConfigPath = path.join(currentDir, 'src/config/staging.ts');
|
72
|
+
if (fs.existsSync(stagingConfigPath)) {
|
73
|
+
let stagingConfig = fs.readFileSync(stagingConfigPath, 'utf8');
|
74
|
+
stagingConfig = stagingConfig.replace(/sendifier-staging/g, `${dbName}-staging`);
|
75
|
+
fs.writeFileSync(stagingConfigPath, stagingConfig);
|
76
|
+
console.log(' ✅ Updated staging.ts');
|
77
|
+
}
|
78
|
+
// Update production config
|
79
|
+
const prodConfigPath = path.join(currentDir, 'src/config/production.ts');
|
80
|
+
if (fs.existsSync(prodConfigPath)) {
|
81
|
+
let prodConfig = fs.readFileSync(prodConfigPath, 'utf8');
|
82
|
+
prodConfig = prodConfig.replace(/sendifier-production/g, `${dbName}-production`);
|
83
|
+
fs.writeFileSync(prodConfigPath, prodConfig);
|
84
|
+
console.log(' ✅ Updated production.ts');
|
85
|
+
}
|
86
|
+
// Update package.json
|
87
|
+
let packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
|
88
|
+
packageJsonContent = packageJsonContent.replace(/"name":\s*"@diagramers\/api"/g, `"name": "${projectName}"`);
|
89
|
+
packageJsonContent = packageJsonContent.replace(/"description":\s*"Diagramers API - A comprehensive Node\.js API template with TypeScript, Firebase Functions, and Socket\.io"/g, `"description": "${projectName} - API project"`);
|
90
|
+
fs.writeFileSync(packageJsonPath, packageJsonContent);
|
91
|
+
console.log(' ✅ Updated package.json');
|
92
|
+
// Update README.md
|
93
|
+
const readmePath = path.join(currentDir, 'README.md');
|
94
|
+
if (fs.existsSync(readmePath)) {
|
95
|
+
let readmeContent = fs.readFileSync(readmePath, 'utf8');
|
96
|
+
readmeContent = readmeContent.replace(/# @diagramers\/api/g, `# ${projectName}`);
|
97
|
+
readmeContent = readmeContent.replace(/A comprehensive Node\.js API template with TypeScript, Firebase Functions, and Socket\.io\./g, `${projectName} - API project.`);
|
98
|
+
fs.writeFileSync(readmePath, readmeContent);
|
99
|
+
console.log(' ✅ Updated README.md');
|
100
|
+
}
|
101
|
+
// Create .env file template
|
102
|
+
console.log('📄 Creating .env template...');
|
103
|
+
const envTemplate = generateEnvTemplate(dbName);
|
104
|
+
fs.writeFileSync(path.join(currentDir, '.env.example'), envTemplate);
|
105
|
+
console.log('✅ Template processing completed!');
|
106
|
+
console.log('');
|
107
|
+
console.log('📋 Summary:');
|
108
|
+
console.log(` Project Name: ${projectName}`);
|
109
|
+
console.log(` Database Name: ${dbName}`);
|
110
|
+
console.log(` Development DB: ${dbName}-development`);
|
111
|
+
console.log(` Staging DB: ${dbName}-staging`);
|
112
|
+
console.log(` Production DB: ${dbName}-production`);
|
113
|
+
console.log('');
|
114
|
+
console.log('🚀 Next steps:');
|
115
|
+
console.log(' 1. Copy .env.example to .env and configure your environment variables');
|
116
|
+
console.log(' 2. Install dependencies: npm install');
|
117
|
+
console.log(' 3. Build the project: npm run build:dev');
|
118
|
+
console.log(' 4. Start the server: npm run serve');
|
119
|
+
}
|
120
|
+
function generateEnvTemplate(dbName) {
|
121
|
+
return `# Environment Configuration
|
122
|
+
NODE_ENV=development
|
123
|
+
|
124
|
+
# Server Configuration
|
125
|
+
PORT=4000
|
126
|
+
HOST=localhost
|
127
|
+
|
128
|
+
# Database Configuration
|
129
|
+
MONGODB_URI=mongodb://127.0.0.1:27017/${dbName}-development
|
130
|
+
|
131
|
+
# Firebase Configuration
|
132
|
+
FIREBASE_API_KEY=your-api-key
|
133
|
+
FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
|
134
|
+
FIREBASE_PROJECT_ID=your-project-id
|
135
|
+
FIREBASE_STORAGE_BUCKET=your-project.appspot.com
|
136
|
+
FIREBASE_MESSAGING_SENDER_ID=your-sender-id
|
137
|
+
FIREBASE_APP_ID=your-app-id
|
138
|
+
|
139
|
+
# JWT Configuration
|
140
|
+
JWT_SECRET=your-jwt-secret-key
|
141
|
+
|
142
|
+
# Twilio Configuration (for SMS)
|
143
|
+
TWILIO_ACCOUNT_SID=your-account-sid
|
144
|
+
TWILIO_AUTH_TOKEN=your-auth-token
|
145
|
+
|
146
|
+
# Email Configuration
|
147
|
+
SMTP_HOST=smtp.gmail.com
|
148
|
+
SMTP_PORT=587
|
149
|
+
SMTP_USER=your-email@gmail.com
|
150
|
+
SMTP_PASS=your-app-password
|
151
|
+
|
152
|
+
# Internal API Configuration
|
153
|
+
INTERNAL_REQUEST_HEADER_VALUE=your-secret-header-value
|
154
|
+
|
155
|
+
# Optional Configuration
|
156
|
+
BASE_SITE_URL=http://localhost:3000
|
157
|
+
LOCAL_API_TUNNEL_URL=https://your-ngrok-url.ngrok-free.app
|
158
|
+
LOCAL_UI_TUNNEL_URL=https://your-ui-ngrok-url.ngrok-free.app
|
159
|
+
BACKUP_DATABASES_FOLDER_PATH=/path/to/backup/folder
|
160
|
+
EXTERNAL_HTTP_REQUEST_TIMEOUT=5000`;
|
161
|
+
}
|
162
|
+
//# sourceMappingURL=template-processor.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"template-processor.js","sourceRoot":"","sources":["../../src/services/template-processor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,0CAkHC;AArHD,uCAAyB;AACzB,2CAA6B;AAEtB,KAAK,UAAU,eAAe,CAAC,WAAmB;IACvD,IAAI,CAAC,WAAW,EAAE,CAA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/C,OAAO,CAAC,GAAG,CAAC,qBAAqB,MAAM,EAAE,CAAC,CAAC;IAC3C,OAAO,CAAC,GAAG,CAAC,sBAAsB,MAAM,cAAc,CAAC,CAAC;IACxD,OAAO,CAAC,GAAG,CAAC,kBAAkB,MAAM,UAAU,CAAC,CAAC;IAChD,OAAO,CAAC,GAAG,CAAC,qBAAqB,MAAM,aAAa,CAAC,CAAC;IACtD,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAChB,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAC9B,OAAO,CAAC,GAAG,CAAC,0EAA0E,CAAC,CAAC;IACxF,OAAO,CAAC,GAAG,CAAC,yCAAyC,CAAC,CAAC;IACvD,OAAO,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAC;IAC1D,OAAO,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,mBAAmB,CAAC,MAAc;IACzC,OAAO;;;;;;;;wCAQ+B,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mCA+BX,CAAC;AACpC,CAAC"}
|
package/package.json
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
import { Command } from 'commander';
|
2
|
+
import { generateModule } from '../services/api-generator';
|
3
|
+
import { processTemplate } from '../services/template-processor';
|
4
|
+
import chalk from 'chalk';
|
5
|
+
|
6
|
+
export function apiCommand(program: Command) {
|
7
|
+
const api = program
|
8
|
+
.command('api')
|
9
|
+
.description('API-specific commands for diagramers projects');
|
10
|
+
|
11
|
+
// Generate module command
|
12
|
+
api
|
13
|
+
.command('generate:module <name>')
|
14
|
+
.description('Generate a new module with entity, schema, service, controller, and routes')
|
15
|
+
.action(async (name: string) => {
|
16
|
+
try {
|
17
|
+
console.log(chalk.blue(`🚀 Generating module: ${name}`));
|
18
|
+
await generateModule(name);
|
19
|
+
console.log(chalk.green(`✅ Module '${name}' generated successfully!`));
|
20
|
+
} catch (error: any) {
|
21
|
+
console.error(chalk.red(`❌ Error generating module: ${error.message}`));
|
22
|
+
process.exit(1);
|
23
|
+
}
|
24
|
+
});
|
25
|
+
|
26
|
+
// Process template command
|
27
|
+
api
|
28
|
+
.command('process:template <name>')
|
29
|
+
.description('Process template files for a new project')
|
30
|
+
.action(async (name: string) => {
|
31
|
+
try {
|
32
|
+
console.log(chalk.blue(`🔧 Processing template for project: ${name}`));
|
33
|
+
await processTemplate(name);
|
34
|
+
console.log(chalk.green(`✅ Template processing completed for '${name}'!`));
|
35
|
+
} catch (error: any) {
|
36
|
+
console.error(chalk.red(`❌ Error processing template: ${error.message}`));
|
37
|
+
process.exit(1);
|
38
|
+
}
|
39
|
+
});
|
40
|
+
|
41
|
+
return api;
|
42
|
+
}
|
package/src/index.ts
CHANGED
@@ -4,6 +4,7 @@ import { Command } from 'commander';
|
|
4
4
|
import { initCommand } from './commands/init';
|
5
5
|
import { updateCommand } from './commands/update';
|
6
6
|
import { extendCommand } from './commands/extend';
|
7
|
+
import { apiCommand } from './commands/api';
|
7
8
|
import chalk from 'chalk';
|
8
9
|
|
9
10
|
const program = new Command();
|
@@ -17,6 +18,7 @@ program
|
|
17
18
|
initCommand(program);
|
18
19
|
updateCommand(program);
|
19
20
|
extendCommand(program);
|
21
|
+
apiCommand(program);
|
20
22
|
|
21
23
|
// Add help text
|
22
24
|
program.addHelpText('after', `
|
@@ -25,6 +27,8 @@ Examples:
|
|
25
27
|
$ diagramers init admin my-admin-dashboard
|
26
28
|
$ diagramers update
|
27
29
|
$ diagramers extend --feature auth
|
30
|
+
$ diagramers api generate:module product
|
31
|
+
$ diagramers api process:template my-api-project
|
28
32
|
`);
|
29
33
|
|
30
34
|
program.parse();
|
@@ -0,0 +1,376 @@
|
|
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 if we're in a diagramers API project
|
14
|
+
const packageJsonPath = path.join(currentDir, 'package.json');
|
15
|
+
if (!fs.existsSync(packageJsonPath)) {
|
16
|
+
throw new Error('package.json not found. Please run this command from a diagramers API project root.');
|
17
|
+
}
|
18
|
+
|
19
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
20
|
+
if (!packageJson.name?.includes('diagramers') && !packageJson.name?.includes('api')) {
|
21
|
+
throw new Error('This command should be run from a diagramers API project.');
|
22
|
+
}
|
23
|
+
|
24
|
+
console.log('📝 Creating entity...');
|
25
|
+
const entityContent = generateEntityContent(moduleName, moduleNameCapitalized);
|
26
|
+
fs.writeFileSync(path.join(currentDir, 'src/entities', `${moduleName}.ts`), entityContent);
|
27
|
+
|
28
|
+
console.log('📋 Creating schema...');
|
29
|
+
const schemaContent = generateSchemaContent(moduleName, moduleNameCapitalized);
|
30
|
+
fs.writeFileSync(path.join(currentDir, 'src/schemas', `${moduleName}.ts`), schemaContent);
|
31
|
+
|
32
|
+
console.log('🔧 Creating service...');
|
33
|
+
const serviceContent = generateServiceContent(moduleName, moduleNameCapitalized);
|
34
|
+
fs.writeFileSync(path.join(currentDir, 'src/services', `${moduleName}-service.ts`), serviceContent);
|
35
|
+
|
36
|
+
console.log('🎮 Creating controller...');
|
37
|
+
const controllerContent = generateControllerContent(moduleName, moduleNameCapitalized);
|
38
|
+
fs.writeFileSync(path.join(currentDir, 'src/controllers', `${moduleName}-controller.ts`), controllerContent);
|
39
|
+
|
40
|
+
console.log('🛣️ Creating routes...');
|
41
|
+
const routesContent = generateRoutesContent(moduleName, moduleNameCapitalized);
|
42
|
+
fs.writeFileSync(path.join(currentDir, 'src/routes', `${moduleName}-routes.ts`), routesContent);
|
43
|
+
|
44
|
+
// Update dbcontext to include the new entity
|
45
|
+
updateDbContext(currentDir, moduleName, moduleNameCapitalized);
|
46
|
+
|
47
|
+
// Update routes index to include the new routes
|
48
|
+
updateRoutesIndex(currentDir, moduleName);
|
49
|
+
}
|
50
|
+
|
51
|
+
function generateEntityContent(moduleName: string, moduleNameCapitalized: string): string {
|
52
|
+
return `import * as mongoose from 'mongoose';
|
53
|
+
import { ObjectId } from "bson";
|
54
|
+
|
55
|
+
export interface I${moduleNameCapitalized} extends mongoose.Document {
|
56
|
+
_id: ObjectId,
|
57
|
+
name: string,
|
58
|
+
description?: string,
|
59
|
+
status: number,
|
60
|
+
createdAt: Date,
|
61
|
+
updatedAt: Date
|
62
|
+
}`;
|
63
|
+
}
|
64
|
+
|
65
|
+
function generateSchemaContent(moduleName: string, moduleNameCapitalized: string): string {
|
66
|
+
return `import * as mongoose from 'mongoose';
|
67
|
+
import { I${moduleNameCapitalized} } from '../entities/${moduleName}';
|
68
|
+
|
69
|
+
export const ${moduleName}Schema = new mongoose.Schema(
|
70
|
+
{
|
71
|
+
name: {
|
72
|
+
type: mongoose.SchemaTypes.String,
|
73
|
+
required: true,
|
74
|
+
},
|
75
|
+
description: {
|
76
|
+
type: mongoose.SchemaTypes.String,
|
77
|
+
required: false,
|
78
|
+
},
|
79
|
+
status: {
|
80
|
+
type: mongoose.SchemaTypes.Number,
|
81
|
+
default: 1,
|
82
|
+
}
|
83
|
+
},
|
84
|
+
{ timestamps: true, suppressReservedKeysWarning: true },
|
85
|
+
);
|
86
|
+
|
87
|
+
export const ${moduleNameCapitalized}Entity = mongoose.model<I${moduleNameCapitalized}>('${moduleName}', ${moduleName}Schema);`;
|
88
|
+
}
|
89
|
+
|
90
|
+
function generateServiceContent(moduleName: string, moduleNameCapitalized: string): string {
|
91
|
+
return `import { ObjectId } from "bson";
|
92
|
+
import dbcontext from "../helpers/dbcontext";
|
93
|
+
import { ResponseCode } from "../helpers/enums";
|
94
|
+
import { Result } from "../helpers/result";
|
95
|
+
import { ${moduleNameCapitalized}Entity } from "../schemas/${moduleName}";
|
96
|
+
|
97
|
+
export class ${moduleNameCapitalized}Service {
|
98
|
+
result: Result;
|
99
|
+
className = '${moduleNameCapitalized}Service';
|
100
|
+
|
101
|
+
constructor(result: Result) {
|
102
|
+
this.result = result;
|
103
|
+
}
|
104
|
+
|
105
|
+
async getAll(): Promise<Result> {
|
106
|
+
try {
|
107
|
+
const ${moduleName}s = await dbcontext.${moduleNameCapitalized}Entity.find({ status: 1 });
|
108
|
+
this.result.Data = ${moduleName}s;
|
109
|
+
this.result.Status = ResponseCode.Ok;
|
110
|
+
return this.result;
|
111
|
+
} catch (ex) {
|
112
|
+
this.result.addException(this.className, 'GetAll', ex);
|
113
|
+
return this.result;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
async getById(id: string): Promise<Result> {
|
118
|
+
try {
|
119
|
+
const ${moduleName} = await dbcontext.${moduleNameCapitalized}Entity.findById(id);
|
120
|
+
if (${moduleName}) {
|
121
|
+
this.result.Data = ${moduleName};
|
122
|
+
this.result.Status = ResponseCode.Ok;
|
123
|
+
} else {
|
124
|
+
this.result.Status = ResponseCode.NotExist;
|
125
|
+
}
|
126
|
+
return this.result;
|
127
|
+
} catch (ex) {
|
128
|
+
this.result.addException(this.className, 'GetById', ex);
|
129
|
+
return this.result;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
async create(${moduleName}Data: any): Promise<Result> {
|
134
|
+
try {
|
135
|
+
const ${moduleName}Entity = new ${moduleNameCapitalized}Entity({
|
136
|
+
_id: new ObjectId(),
|
137
|
+
...${moduleName}Data,
|
138
|
+
status: 1
|
139
|
+
});
|
140
|
+
const created${moduleNameCapitalized} = await dbcontext.${moduleNameCapitalized}Entity.create(${moduleName}Entity);
|
141
|
+
if (created${moduleNameCapitalized}) {
|
142
|
+
this.result.Data = created${moduleNameCapitalized};
|
143
|
+
this.result.Status = ResponseCode.Ok;
|
144
|
+
} else {
|
145
|
+
this.result.Status = ResponseCode.Error;
|
146
|
+
this.result.Errors.push('Failed to create ${moduleName}');
|
147
|
+
}
|
148
|
+
return this.result;
|
149
|
+
} catch (ex) {
|
150
|
+
this.result.addException(this.className, 'Create', ex);
|
151
|
+
return this.result;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
async update(id: string, ${moduleName}Data: any): Promise<Result> {
|
156
|
+
try {
|
157
|
+
const updated${moduleNameCapitalized} = await dbcontext.${moduleNameCapitalized}Entity.findByIdAndUpdate(
|
158
|
+
id,
|
159
|
+
{ ...${moduleName}Data },
|
160
|
+
{ new: true }
|
161
|
+
);
|
162
|
+
if (updated${moduleNameCapitalized}) {
|
163
|
+
this.result.Data = updated${moduleNameCapitalized};
|
164
|
+
this.result.Status = ResponseCode.Ok;
|
165
|
+
} else {
|
166
|
+
this.result.Status = ResponseCode.NotExist;
|
167
|
+
}
|
168
|
+
return this.result;
|
169
|
+
} catch (ex) {
|
170
|
+
this.result.addException(this.className, 'Update', ex);
|
171
|
+
return this.result;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
async delete(id: string): Promise<Result> {
|
176
|
+
try {
|
177
|
+
const deleted${moduleNameCapitalized} = await dbcontext.${moduleNameCapitalized}Entity.findByIdAndUpdate(
|
178
|
+
id,
|
179
|
+
{ status: 0 },
|
180
|
+
{ new: true }
|
181
|
+
);
|
182
|
+
if (deleted${moduleNameCapitalized}) {
|
183
|
+
this.result.Data = deleted${moduleNameCapitalized};
|
184
|
+
this.result.Status = ResponseCode.Ok;
|
185
|
+
} else {
|
186
|
+
this.result.Status = ResponseCode.NotExist;
|
187
|
+
}
|
188
|
+
return this.result;
|
189
|
+
} catch (ex) {
|
190
|
+
this.result.addException(this.className, 'Delete', ex);
|
191
|
+
return this.result;
|
192
|
+
}
|
193
|
+
}
|
194
|
+
}`;
|
195
|
+
}
|
196
|
+
|
197
|
+
function generateControllerContent(moduleName: string, moduleNameCapitalized: string): string {
|
198
|
+
return `import { AuditMessageType, ResponseCode } from "../helpers/enums";
|
199
|
+
import handleResponse from "../helpers/handle-response";
|
200
|
+
import { Result } from "../helpers/result";
|
201
|
+
import { ${moduleNameCapitalized}Service } from "../services/${moduleName}-service";
|
202
|
+
|
203
|
+
export default class ${moduleNameCapitalized}Controller {
|
204
|
+
|
205
|
+
async getAll(req, res) {
|
206
|
+
const result = res.locals.result;
|
207
|
+
try {
|
208
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'getAll', 'Started');
|
209
|
+
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
210
|
+
const serviceResult = await ${moduleName}Service.getAll();
|
211
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'getAll', 'Finished');
|
212
|
+
return handleResponse(req, res, serviceResult);
|
213
|
+
} catch (ex) {
|
214
|
+
result.addException(req.baseUrl, 'getAll', ex);
|
215
|
+
return handleResponse(req, res, result);
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
async getById(req, res) {
|
220
|
+
const result = res.locals.result;
|
221
|
+
try {
|
222
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'getById', 'Started');
|
223
|
+
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
224
|
+
const serviceResult = await ${moduleName}Service.getById(req.params.id);
|
225
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'getById', 'Finished');
|
226
|
+
return handleResponse(req, res, serviceResult);
|
227
|
+
} catch (ex) {
|
228
|
+
result.addException(req.baseUrl, 'getById', ex);
|
229
|
+
return handleResponse(req, res, result);
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
async create(req, res) {
|
234
|
+
const result = res.locals.result;
|
235
|
+
try {
|
236
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'create', 'Started');
|
237
|
+
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
238
|
+
const serviceResult = await ${moduleName}Service.create(req.body);
|
239
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'create', 'Finished');
|
240
|
+
return handleResponse(req, res, serviceResult);
|
241
|
+
} catch (ex) {
|
242
|
+
result.addException(req.baseUrl, 'create', ex);
|
243
|
+
return handleResponse(req, res, result);
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
async update(req, res) {
|
248
|
+
const result = res.locals.result;
|
249
|
+
try {
|
250
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'update', 'Started');
|
251
|
+
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
252
|
+
const serviceResult = await ${moduleName}Service.update(req.params.id, req.body);
|
253
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'update', 'Finished');
|
254
|
+
return handleResponse(req, res, serviceResult);
|
255
|
+
} catch (ex) {
|
256
|
+
result.addException(req.baseUrl, 'update', ex);
|
257
|
+
return handleResponse(req, res, result);
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
async delete(req, res) {
|
262
|
+
const result = res.locals.result;
|
263
|
+
try {
|
264
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'delete', 'Started');
|
265
|
+
const ${moduleName}Service = new ${moduleNameCapitalized}Service(result);
|
266
|
+
const serviceResult = await ${moduleName}Service.delete(req.params.id);
|
267
|
+
result.addMessage(AuditMessageType.info, req.baseUrl, 'delete', 'Finished');
|
268
|
+
return handleResponse(req, res, serviceResult);
|
269
|
+
} catch (ex) {
|
270
|
+
result.addException(req.baseUrl, 'delete', ex);
|
271
|
+
return handleResponse(req, res, result);
|
272
|
+
}
|
273
|
+
}
|
274
|
+
}`;
|
275
|
+
}
|
276
|
+
|
277
|
+
function generateRoutesContent(moduleName: string, moduleNameCapitalized: string): string {
|
278
|
+
return `import express from 'express';
|
279
|
+
import ${moduleNameCapitalized}Controller from '../controllers/${moduleName}-controller';
|
280
|
+
|
281
|
+
const router = express.Router();
|
282
|
+
const ${moduleName}Controller = new ${moduleNameCapitalized}Controller();
|
283
|
+
|
284
|
+
// GET /api/${moduleName}s
|
285
|
+
router.get('/', ${moduleName}Controller.getAll.bind(${moduleName}Controller));
|
286
|
+
|
287
|
+
// GET /api/${moduleName}s/:id
|
288
|
+
router.get('/:id', ${moduleName}Controller.getById.bind(${moduleName}Controller));
|
289
|
+
|
290
|
+
// POST /api/${moduleName}s
|
291
|
+
router.post('/', ${moduleName}Controller.create.bind(${moduleName}Controller));
|
292
|
+
|
293
|
+
// PUT /api/${moduleName}s/:id
|
294
|
+
router.put('/:id', ${moduleName}Controller.update.bind(${moduleName}Controller));
|
295
|
+
|
296
|
+
// DELETE /api/${moduleName}s/:id
|
297
|
+
router.delete('/:id', ${moduleName}Controller.delete.bind(${moduleName}Controller));
|
298
|
+
|
299
|
+
export default router;`;
|
300
|
+
}
|
301
|
+
|
302
|
+
function updateDbContext(currentDir: string, moduleName: string, moduleNameCapitalized: string): void {
|
303
|
+
const dbcontextPath = path.join(currentDir, 'src/helpers/dbcontext.ts');
|
304
|
+
if (!fs.existsSync(dbcontextPath)) {
|
305
|
+
console.log('⚠️ dbcontext.ts not found, skipping dbcontext update');
|
306
|
+
return;
|
307
|
+
}
|
308
|
+
|
309
|
+
let dbcontextContent = fs.readFileSync(dbcontextPath, 'utf8');
|
310
|
+
|
311
|
+
// Add import
|
312
|
+
const importStatement = `import { ${moduleNameCapitalized}Entity } from '../schemas/${moduleName}';`;
|
313
|
+
if (!dbcontextContent.includes(importStatement)) {
|
314
|
+
// Find the last import statement and add after it
|
315
|
+
const importRegex = /import.*from.*['"];?\s*$/gm;
|
316
|
+
const matches = [...dbcontextContent.matchAll(importRegex)];
|
317
|
+
if (matches.length > 0) {
|
318
|
+
const lastImport = matches[matches.length - 1];
|
319
|
+
const insertIndex = lastImport.index! + lastImport[0].length;
|
320
|
+
dbcontextContent = dbcontextContent.slice(0, insertIndex) + '\n' + importStatement + dbcontextContent.slice(insertIndex);
|
321
|
+
}
|
322
|
+
}
|
323
|
+
|
324
|
+
// Add to dbcontext object
|
325
|
+
const dbcontextObjectRegex = /export\s+default\s*\{([^}]*)\}/s;
|
326
|
+
const match = dbcontextContent.match(dbcontextObjectRegex);
|
327
|
+
if (match) {
|
328
|
+
const objectContent = match[1];
|
329
|
+
if (!objectContent.includes(`${moduleNameCapitalized}Entity`)) {
|
330
|
+
const newObjectContent = objectContent.trim() + `,\n ${moduleNameCapitalized}Entity`;
|
331
|
+
dbcontextContent = dbcontextContent.replace(dbcontextObjectRegex, `export default {$1${newObjectContent}\n}`);
|
332
|
+
}
|
333
|
+
}
|
334
|
+
|
335
|
+
fs.writeFileSync(dbcontextPath, dbcontextContent);
|
336
|
+
console.log('📊 Updated dbcontext.ts');
|
337
|
+
}
|
338
|
+
|
339
|
+
function updateRoutesIndex(currentDir: string, moduleName: string): void {
|
340
|
+
const routesIndexPath = path.join(currentDir, 'src/routes/index.ts');
|
341
|
+
if (!fs.existsSync(routesIndexPath)) {
|
342
|
+
console.log('⚠️ routes/index.ts not found, skipping routes index update');
|
343
|
+
return;
|
344
|
+
}
|
345
|
+
|
346
|
+
let routesContent = fs.readFileSync(routesIndexPath, 'utf8');
|
347
|
+
|
348
|
+
// Add import
|
349
|
+
const importStatement = `import ${moduleName}Routes from './${moduleName}-routes';`;
|
350
|
+
if (!routesContent.includes(importStatement)) {
|
351
|
+
// Find the last import statement and add after it
|
352
|
+
const importRegex = /import.*from.*['"];?\s*$/gm;
|
353
|
+
const matches = [...routesContent.matchAll(importRegex)];
|
354
|
+
if (matches.length > 0) {
|
355
|
+
const lastImport = matches[matches.length - 1];
|
356
|
+
const insertIndex = lastImport.index! + lastImport[0].length;
|
357
|
+
routesContent = routesContent.slice(0, insertIndex) + '\n' + importStatement + routesContent.slice(insertIndex);
|
358
|
+
}
|
359
|
+
}
|
360
|
+
|
361
|
+
// Add route registration
|
362
|
+
const routeRegistration = `app.use('/api/${moduleName}s', ${moduleName}Routes);`;
|
363
|
+
if (!routesContent.includes(routeRegistration)) {
|
364
|
+
// Find where routes are registered (usually after imports)
|
365
|
+
const routeRegex = /app\.use\('\/api\/.*',.*\);?\s*$/gm;
|
366
|
+
const matches = [...routesContent.matchAll(routeRegex)];
|
367
|
+
if (matches.length > 0) {
|
368
|
+
const lastRoute = matches[matches.length - 1];
|
369
|
+
const insertIndex = lastRoute.index! + lastRoute[0].length;
|
370
|
+
routesContent = routesContent.slice(0, insertIndex) + '\n' + routeRegistration + routesContent.slice(insertIndex);
|
371
|
+
}
|
372
|
+
}
|
373
|
+
|
374
|
+
fs.writeFileSync(routesIndexPath, routesContent);
|
375
|
+
console.log('🛣️ Updated routes/index.ts');
|
376
|
+
}
|