-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
173 lines (139 loc) · 5.43 KB
/
cli.ts
File metadata and controls
173 lines (139 loc) · 5.43 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/env node
// B68/CLI command tool
// Usage: b68 <command>
// Commands:
// help Show this help
// cs Create new service along with controller and routes
// cso Create new service only
// cp Create new provider
// ch Create new helper of type factory or client
// ct Create new type
// cv Create new view
// ci Create new interface
import fs from "node:fs";
import path from "node:path";
const args = process.argv.slice(2);
const __dirname = path.resolve();
const commands = {
help: () => {
console.log("B68/CLI command tool");
console.log("Usage: b68 <command>");
console.log("Commands:");
console.log(" help Show this help");
console.log(" cs Create new service along with controller and routes");
console.log(" cso Create new service only");
console.log(" cp Create new provider");
console.log(" ch Create new helper of type factory or client");
console.log(" ct Create new type");
console.log(" ci Create new interface");
process.exit(0);
},
cs: () => {
const serviceName = args[1];
const servicePath = path.join(
__dirname,
"services",
serviceName.toLocaleLowerCase() + ".service.ts",
);
const controllerPath = path.join(
__dirname,
"controllers",
serviceName.toLocaleLowerCase() + ".controller.ts",
);
const routesPath = path.join(
__dirname,
"routes",
serviceName.toLocaleLowerCase() + ".routes.ts",
);
const serviceTemplate = `export default class ${serviceName} {}`;
const controllerTemplate = `import ${serviceName}Service from '../services/${serviceName.toLocaleLowerCase()}.service';\nimport { Context } from 'hono'\nimport { makeResponse } from '../libs'\n\nexport default class ${serviceName}Controller extends ${serviceName}Service {}\n`;
const routesTemplate = `import { Hono } from 'hono';\nimport ${serviceName}Controller from '../controllers/${serviceName.toLocaleLowerCase()}.controller';\nconst router = new Hono()\n\nexport default router\n`;
if (fs.existsSync(servicePath || controllerPath || routesPath)) {
console.log("Already exists");
process.exit(1);
}
fs.writeFileSync(servicePath, serviceTemplate);
fs.writeFileSync(controllerPath, controllerTemplate);
fs.writeFileSync(routesPath, routesTemplate);
console.log("Service created successfully");
},
cso: () => {
const serviceName = args[1];
const servicePath = path.join(
__dirname,
"services",
serviceName.toLocaleLowerCase() + ".service.ts",
);
const serviceTemplate = `export default class ${serviceName} {}`;
if (fs.existsSync(servicePath)) {
console.log("Already exists");
process.exit(1);
}
fs.writeFileSync(servicePath, serviceTemplate);
console.log("Service created successfully");
},
cp: () => {
const providerName = args[1];
const providerPath = path.join(
__dirname,
"providers",
providerName.toLocaleLowerCase() + ".provider.ts",
);
const providerTemplate = `export default class ${providerName} {}`;
if (fs.existsSync(providerPath)) {
console.log("Already exists");
process.exit(1);
}
fs.writeFileSync(providerPath, providerTemplate);
console.log("Provider created successfully");
},
ch: () => {
const helperName = args[1];
let helperType = args[2];
if (!helperType || (helperType !== "--factory" && helperType !== "--client")) {
console.log("Invalid helper type");
}
helperType = helperType === "--factory" ? "factory" : "client";
const isFactory = helperType === "factory";
const helperPath = path.join(
__dirname,
"helpers",
helperName.toLocaleLowerCase() + isFactory ? ".factory" + ".ts" : "_client" + ".ts",
);
const helperTemplate = `export default class ${helperName} {}`;
if (fs.existsSync(helperPath)) {
console.log("Already exists");
process.exit(1);
}
fs.writeFileSync(helperPath, helperTemplate);
console.log("Helper created successfully");
},
ct: () => {
const typeName = args[1];
let typeCase = args[2];
if (!typeCase || (typeCase !== "--interface" && typeCase !== "--type")) {
console.log("Invalid type case");
process.exit(1);
}
typeCase = typeCase === "--interface" ? "interface" : "type";
const isInterface = typeCase === "interface";
const typePath = path.join(__dirname, "types", "index.d.ts");
const typeTemplate = `\nexport ${typeCase} ${typeName} ${isInterface ? "{}" : "= any"}`;
fs.appendFileSync(typePath, typeTemplate);
console.log("Type/Interface created successfully");
},
ci: () => {
console.log("Not implemented yet");
},
};
const command = args[0];
if (!command) {
console.log("No command provided");
process.exit(1);
}
if (!commands[command]) {
console.log("Invalid command");
process.exit(1);
}
commands[command]();
// Path: packages/cli/index.ts