-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
86 lines (70 loc) · 2.3 KB
/
cli.js
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const args = process.argv.slice(2);
const [fullCommand, name] = args;
const [namespace, action, type] = fullCommand.split(':');
if (namespace !== 'expressway' || action !== 'make') {
console.log('❌ Unknown command. Try: expressway:make:<type>');
process.exit(1);
}
const toPascal = str => str.charAt(0).toUpperCase() + str.slice(1);
const toKebab = str => str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
const Name = toPascal(name);
const kebabName = toKebab(name);
function writeFile(filePath, content) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, content);
}
switch (type) {
case 'controller':
writeFile(`app/controllers/${name}Controller.js`,
`const Controller = require('../../expressway/core/controller');
class ${Name}Controller extends Controller {
index(req, res) {
this.render(res, '${kebabName}', { message: 'Hello from ${Name}Controller!' });
}
}
module.exports = new ${Name}Controller();`);
console.log(`✅ Controller '${name}' created.`);
break;
case 'model':
writeFile(`app/models/${name}Model.js`,
`const { Model, DataTypes } = require('../../expressway/core/model');
class ${Name}Model extends Model {}
${Name}Model.initModel({
// Define attributes here
});
module.exports = ${Name}Model;`);
console.log(`✅ Model '${name}' created.`);
break;
case 'view':
writeFile(`app/views/${kebabName}.ejs`,
`<h1>${Name} View</h1>
<p>Generated by expressway CLI.</p>`);
console.log(`✅ View '${name}' created.`);
break;
case 'migration':
const timestamp = Date.now();
writeFile(`expressway/database/migrations/${timestamp}-${kebabName}.js`,
`module.exports = {
up: async ({ DataTypes, sequelize }) => {
await sequelize.getQueryInterface().createTable('${kebabName}s', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
});
},
down: async ({ sequelize }) => {
await sequelize.getQueryInterface().dropTable('${kebabName}s');
}
};`);
console.log(`✅ Migration '${name}' created.`);
break;
default:
console.log('❌ Unknown type. Use: controller | model | view | migration');
}