-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtao
executable file
·144 lines (119 loc) · 5.3 KB
/
tao
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
#!/bin/node
const path = require('node:path');
const fs = require('node:fs');
const args = process.argv.splice(2);
const pc = require('picocolors');
const modelsDir = path.join(__dirname, 'src/app/Models');
const commandsDir = path.join(__dirname, 'src/app/Commands/commands');
const help = () => {
console.log(`${pc.bold('Welcome to Tao CLI. This is the list of commands avaiable:')}`);
console.table({
newModel: {
name: 'new Model',
options: 'name',
description: 'Creates a new Model Module',
example: 'new Model User'
},
newCommand: {
name: 'new Command',
options: 'name',
description: 'Creates a new Command',
example: 'new Command ping'
}
});
}
const validateNew = () => {
if (args[2] === undefined) {
console.error(pc.red(` Expected 3 args. Given ${args.length} args `));
process.exit();
}
}
const commandTemplate = (CommandName) => `import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js";
export default {
data: new SlashCommandBuilder()
.setName("${CommandName}")
.setDescription("${CommandName}'s description"),
async execute(interaction: ChatInputCommandInteraction) {
await interaction.reply("here is the reply");
},
}`;
const modelTemplate = (ModelName) => `export class ${ModelName}Entity {
id: string;
constructor(${ModelName.toLowerCase()}: {id: string}) {
this.id = ${ModelName.toLowerCase()}.id;
}
public static ${ModelName.toLowerCase()}FromPrisma(${ModelName.toLowerCase()}: { id: string }): ${ModelName}Entity {
return new ${ModelName}Entity(${ModelName.toLowerCase()});
}
}`;
const repositoryTemplate = (ModelName) => `import { ${ModelName}Entity } from "..";
export abstract class I${ModelName}Repository {
abstract get(condition?: { where: object }): Promise<${ModelName}Entity[] | null>;
abstract getById(id: string): Promise<${ModelName}Entity | null>;
abstract create(entity: ${ModelName}Entity): Promise<${ModelName}Entity | null>;
abstract update(id: string, entity: ${ModelName}Entity): Promise<${ModelName}Entity | null>;
abstract deleteById(id: string): Promise<${ModelName}Entity | null>;
abstract delete(condition?: { where: object }): Promise<void>;
}
export class ${ModelName}Repository extends I${ModelName}Repository {
constructor(private readonly ${ModelName.toLowerCase()}Service: I${ModelName}Repository) {
super()
}
async get(condition?: { where: object, include?: {} } | undefined): Promise<${ModelName}Entity[] | ${ModelName}Entity | null> {
return (await this.${ModelName.toLowerCase()}Service.get(condition));
}
async getById(id: string): Promise<${ModelName}Entity | null> {
return (await this.${ModelName.toLowerCase()}Service.getById(id));
}
async create(entity: ${ModelName}Entity): Promise<${ModelName}Entity | null> {
return (await this.${ModelName.toLowerCase()}Service.create(entity));
}
async update(id: string, entity: ${ModelName}Entity): Promise<${ModelName}Entity | null> {
return (await this.${ModelName.toLowerCase()}Service.update(id, entity));
}
async deleteById(id: string): Promise<${ModelName}Entity | null> {
return (await this.${ModelName.toLowerCase()}Service.deleteById(id));
}
async delete(condition?: { where: object; } | undefined): Promise<void> {
await this.${ModelName.toLowerCase()}Service.delete(condition);
}
}`;
const indexModelTemplate = (ModelName) => `import { ${ModelName}Entity } from "./Entity/${ModelName}Entity";
import { ${ModelName}Repository, I${ModelName}Repository } from "./Repositories/${ModelName}Repository";
export {
${ModelName}Entity,
${ModelName}Repository,
I${ModelName}Repository,
}`;
const makeCommand = (CommandName) => {
CommandName = CommandName.toLowerCase();
fs.writeFileSync(path.join(commandsDir, `${CommandName}.ts`), commandTemplate(CommandName), { encoding: 'utf-8' });
}
const makeModel = (ModelName) => {
const modelDir = path.join(modelsDir, ModelName);
fs.mkdirSync(path.join(modelDir, 'Entity'), { recursive: true });
fs.writeFileSync(path.join(modelDir, 'Entity', `${ModelName}Entity.ts`), modelTemplate(ModelName), { encoding: 'utf-8' });
fs.mkdirSync(path.join(modelDir, 'Repositories'), { recursive: true });
fs.writeFileSync(path.join(modelDir, 'Repositories', `${ModelName}Repository.ts`), repositoryTemplate(ModelName), { encoding: 'utf-8' });
fs.writeFileSync(path.join(modelDir, 'index.ts'), indexModelTemplate(ModelName), { encoding: 'utf-8' });
fs.mkdirSync(path.join(modelDir, 'Services'), { recursive: true });
}
if (args[0] === undefined || args[0] === 'help') {
help();
}
if (args[0] === 'new') {
validateNew();
switch (args[1]) {
case 'Model':
makeModel(args[2]);
console.log(pc.green(`${args[2]} Module created succesfully`));
break;
case 'Command':
makeCommand(args[2]);
console.log(pc.green(`${args[2]} Command created succesfully`));
break;
default:
console.error(pc.red(`${args[1]} is not a valid command`));
console.log(pc.magenta('Use "node tao help" to get the list of commands'));
}
}