@@ -64,14 +64,47 @@ module.exports = {
64
64
break ;
65
65
}
66
66
67
- await fs . writeFile ( targetPath , controllerCode ) ;
67
+ await fs . outputFile ( targetPath , controllerCode ) ;
68
68
console . log ( `Controller ${ name } created successfully at ${ targetPath } ` ) ;
69
69
} catch ( error ) {
70
70
console . error ( `Error generating controller: ${ error . message } ` ) ;
71
71
throw error ;
72
72
}
73
73
}
74
74
75
+ /**
76
+ * Generates a channel file.
77
+ * @param {string } targetPath - The path where the channel file will be created.
78
+ * @param {string } name - The name of the channel.
79
+ * @returns {Promise<void> }
80
+ */
81
+ async function generateChannel ( targetPath , name ) {
82
+ try {
83
+ let content ;
84
+ const stubPath = path . join ( elementPath , 'channel.stub' ) ;
85
+ if ( await fs . pathExists ( stubPath ) ) {
86
+ content = await fs . readFile ( stubPath , 'utf8' ) ;
87
+ } else {
88
+ console . log ( 'No template found, using default content.' ) ;
89
+ content = `
90
+ "use strict";
91
+
92
+ module.exports = {
93
+ index: async function (io, socket) {
94
+ }
95
+ }` ;
96
+ }
97
+ let channelCode = content ;
98
+
99
+
100
+ await fs . outputFile ( targetPath , channelCode ) ;
101
+ console . log ( `Channel ${ name } created successfully at ${ targetPath } ` ) ;
102
+ } catch ( error ) {
103
+ console . error ( `Error generating channel: ${ error . message } ` ) ;
104
+ throw error ;
105
+ }
106
+ }
107
+
75
108
/**
76
109
* Generates a middleware file.
77
110
* @param {string } targetPath - The path where the middleware file will be created.
@@ -97,7 +130,7 @@ module.exports = async (req, res, next) => {
97
130
}
98
131
99
132
content = content . replace ( '// more' , `// ${ name } implementation` ) ;
100
- await fs . writeFile ( targetPath , content ) ;
133
+ await fs . outputFile ( targetPath , content ) ;
101
134
console . log ( `${ type } ${ name } created successfully at ${ targetPath } ` ) ;
102
135
} catch ( error ) {
103
136
console . error ( `Error generating ${ type } : ${ error . message } ` ) ;
@@ -130,7 +163,7 @@ module.exports = [
130
163
}
131
164
132
165
content = content . replace ( '// more' , `// ${ name } implementation` ) ;
133
- await fs . writeFile ( targetPath , content ) ;
166
+ await fs . outputFile ( targetPath , content ) ;
134
167
console . log ( `Request ${ name } created successfully at ${ targetPath } ` ) ;
135
168
} catch ( error ) {
136
169
console . error ( `Error generating request: ${ error . message } ` ) ;
@@ -173,7 +206,7 @@ async function generateEmail(targetPath, name, { templateEmail, job }) {
173
206
174
207
moreReplace = moreReplace . replace ( '// more' , more ) ;
175
208
176
- await fs . writeFile ( templateEmail . path , contentTemplate ) ;
209
+ await fs . outputFile ( templateEmail . path , contentTemplate ) ;
177
210
console . log ( `Email template ${ templateEmail . name } created successfully at ${ templateEmail . path } ` ) ;
178
211
}
179
212
@@ -193,7 +226,7 @@ async function generateEmail(targetPath, name, { templateEmail, job }) {
193
226
content = content . replace ( '// queue\n' , `` ) ;
194
227
content = content . replace ( '// queueContent\n' , `` ) ;
195
228
196
- await fs . writeFile ( targetPath , content ) ;
229
+ await fs . outputFile ( targetPath , content ) ;
197
230
console . log ( `Email ${ name } created successfully at ${ targetPath } ` ) ;
198
231
} catch ( error ) {
199
232
console . error ( `Error generating email: ${ error . message } ` ) ;
@@ -239,7 +272,7 @@ async function generateJob(targetPath, name, options = { importModule, handle, i
239
272
content = content . replace ( '// jobName' , name ) ;
240
273
content = content . replace ( '// queueName' , name ) ;
241
274
242
- await fs . writeFile ( targetPath , content ) ;
275
+ await fs . outputFile ( targetPath , content ) ;
243
276
console . log ( `Job ${ name } created successfully at ${ targetPath } ` ) ;
244
277
} catch ( error ) {
245
278
console . error ( `Error generating job: ${ error . message } ` ) ;
@@ -264,7 +297,7 @@ async function generateInterface(targetPath, name) {
264
297
console . error ( 'No template found.' ) ;
265
298
}
266
299
267
- await fs . writeFile ( targetPath , content ) ;
300
+ await fs . outputFile ( targetPath , content ) ;
268
301
console . log ( `Interface ${ name } created successfully at ${ targetPath } ` ) ;
269
302
} catch ( error ) {
270
303
console . error ( `Error generating interface: ${ error . message } ` ) ;
@@ -402,7 +435,7 @@ async function generateSQLConfig() {
402
435
403
436
const userOrm = await fs . readFile ( userOrmPath , 'utf8' ) ;
404
437
const targetPath = path . join ( process . cwd ( ) , 'src/app/ORMs/User.js' )
405
- await fs . writeFile ( targetPath , userOrm ) ;
438
+ await fs . outputFile ( targetPath , userOrm ) ;
406
439
407
440
const packageJsonPath = path . join ( process . cwd ( ) , 'package.json' ) ;
408
441
const packageJson = require ( packageJsonPath ) ;
@@ -483,6 +516,7 @@ async function __checkPackageLock() {
483
516
484
517
module . exports = {
485
518
generateController,
519
+ generateChannel,
486
520
generateMiddleware,
487
521
generateRequest,
488
522
generateEmail,
0 commit comments