Skip to content

Commit 4d6d7cb

Browse files
author
khanh2906
committed
update
1 parent 4bace86 commit 4d6d7cb

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

bin/generate/generate-channel.cli.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const path = require('path');
2+
const {
3+
generateChannel,
4+
} = require('../../lib/handlers/generateElements');
5+
6+
const fs = require('fs-extra');
7+
module.exports = (runCommand) => {
8+
runCommand
9+
.command('channel:generate <channelName>')
10+
.description('Generate a new channel')
11+
.option('-p, --path <path>', 'Path for the channel (if you don\'t want to use the default)')
12+
.action(async (channelName, cmdObj) => {
13+
const { path: channelPath } = cmdObj;
14+
let fileName = `${channelName}.channel.js`;
15+
const targetPath = channelPath ? path.join(process.cwd(), './src', channelPath, fileName) : path.join(process.cwd(), './src/app/channels', fileName);
16+
if (await fs.pathExists(targetPath)) {
17+
console.log('Channel already exists');
18+
process.exit(1);
19+
}
20+
21+
await generateChannel(targetPath, channelName);
22+
});
23+
}

bin/generate/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module.exports = (runCommand) => {
33
require("./generate-app.cli")(runCommand)
44
require("./generate-controller.cli")(runCommand)
5+
require("./generate-channel.cli")(runCommand)
56
require("./generate-docker.cli")(runCommand)
67
require("./generate-email.cli")(runCommand)
78
require("./generate-interface.cli")(runCommand)

lib/handlers/generateElements.js

+42-8
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,47 @@ module.exports = {
6464
break;
6565
}
6666

67-
await fs.writeFile(targetPath, controllerCode);
67+
await fs.outputFile(targetPath, controllerCode);
6868
console.log(`Controller ${name} created successfully at ${targetPath}`);
6969
} catch (error) {
7070
console.error(`Error generating controller: ${error.message}`);
7171
throw error;
7272
}
7373
}
7474

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+
75108
/**
76109
* Generates a middleware file.
77110
* @param {string} targetPath - The path where the middleware file will be created.
@@ -97,7 +130,7 @@ module.exports = async (req, res, next) => {
97130
}
98131

99132
content = content.replace('// more', `// ${name} implementation`);
100-
await fs.writeFile(targetPath, content);
133+
await fs.outputFile(targetPath, content);
101134
console.log(`${type} ${name} created successfully at ${targetPath}`);
102135
} catch (error) {
103136
console.error(`Error generating ${type}: ${error.message}`);
@@ -130,7 +163,7 @@ module.exports = [
130163
}
131164

132165
content = content.replace('// more', `// ${name} implementation`);
133-
await fs.writeFile(targetPath, content);
166+
await fs.outputFile(targetPath, content);
134167
console.log(`Request ${name} created successfully at ${targetPath}`);
135168
} catch (error) {
136169
console.error(`Error generating request: ${error.message}`);
@@ -173,7 +206,7 @@ async function generateEmail(targetPath, name, { templateEmail, job }) {
173206

174207
moreReplace = moreReplace.replace('// more', more);
175208

176-
await fs.writeFile(templateEmail.path, contentTemplate);
209+
await fs.outputFile(templateEmail.path, contentTemplate);
177210
console.log(`Email template ${templateEmail.name} created successfully at ${templateEmail.path}`);
178211
}
179212

@@ -193,7 +226,7 @@ async function generateEmail(targetPath, name, { templateEmail, job }) {
193226
content = content.replace('// queue\n', ``);
194227
content = content.replace('// queueContent\n', ``);
195228

196-
await fs.writeFile(targetPath, content);
229+
await fs.outputFile(targetPath, content);
197230
console.log(`Email ${name} created successfully at ${targetPath}`);
198231
} catch (error) {
199232
console.error(`Error generating email: ${error.message}`);
@@ -239,7 +272,7 @@ async function generateJob(targetPath, name, options = { importModule, handle, i
239272
content = content.replace('// jobName', name);
240273
content = content.replace('// queueName', name);
241274

242-
await fs.writeFile(targetPath, content);
275+
await fs.outputFile(targetPath, content);
243276
console.log(`Job ${name} created successfully at ${targetPath}`);
244277
} catch (error) {
245278
console.error(`Error generating job: ${error.message}`);
@@ -264,7 +297,7 @@ async function generateInterface(targetPath, name) {
264297
console.error('No template found.');
265298
}
266299

267-
await fs.writeFile(targetPath, content);
300+
await fs.outputFile(targetPath, content);
268301
console.log(`Interface ${name} created successfully at ${targetPath}`);
269302
} catch (error) {
270303
console.error(`Error generating interface: ${error.message}`);
@@ -402,7 +435,7 @@ async function generateSQLConfig() {
402435

403436
const userOrm = await fs.readFile(userOrmPath, 'utf8');
404437
const targetPath = path.join(process.cwd(), 'src/app/ORMs/User.js')
405-
await fs.writeFile(targetPath, userOrm);
438+
await fs.outputFile(targetPath, userOrm);
406439

407440
const packageJsonPath = path.join(process.cwd(), 'package.json');
408441
const packageJson = require(packageJsonPath);
@@ -483,6 +516,7 @@ async function __checkPackageLock() {
483516

484517
module.exports = {
485518
generateController,
519+
generateChannel,
486520
generateMiddleware,
487521
generateRequest,
488522
generateEmail,

lib/stubs/elements/channel.stub

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
module.exports = {
4+
index: async function (io, socket) {
5+
}
6+
}

tests/units/generate-element.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('Element Generator', () => {
6060
},
6161
};`;
6262

63-
const [capturedPath, capturedContent] = fs.writeFile.mock.calls[0];
63+
const [capturedPath, capturedContent] = fs.outputFile.mock.calls[0];
6464

6565
expect(capturedPath).toBe(targetPath);
6666
});
@@ -86,7 +86,7 @@ describe('Element Generator', () => {
8686
// TestMiddleware implementation
8787
};`;
8888

89-
const [capturedPath, capturedContent] = fs.writeFile.mock.calls[0];
89+
const [capturedPath, capturedContent] = fs.outputFile.mock.calls[0];
9090

9191
expect(capturedPath).toBe(targetPath);
9292

@@ -117,7 +117,7 @@ describe('Element Generator', () => {
117117
// TestRequest implementation
118118
]`;
119119

120-
const [capturedPath, capturedContent] = fs.writeFile.mock.calls[0];
120+
const [capturedPath, capturedContent] = fs.outputFile.mock.calls[0];
121121

122122
// Assert the target path is correct
123123
expect(capturedPath).toBe(targetPath);
@@ -169,8 +169,8 @@ module.exports = (data) => {
169169
};
170170
`;
171171

172-
// Capture the arguments passed to fs.writeFile
173-
const [capturedPath, capturedContent] = fs.writeFile.mock.calls[0];
172+
// Capture the arguments passed to fs.outputFile
173+
const [capturedPath, capturedContent] = fs.outputFile.mock.calls[0];
174174

175175
// Assert the target path is correct
176176
expect(capturedPath).toBe(targetPath);
@@ -214,7 +214,7 @@ module.exports = {
214214
}
215215
};`;
216216

217-
const [capturedPath, capturedContent] = fs.writeFile.mock.calls[0];
217+
const [capturedPath, capturedContent] = fs.outputFile.mock.calls[0];
218218

219219
expect(capturedPath).toBe(targetPath);
220220

0 commit comments

Comments
 (0)