Skip to content

Commit f8cc07d

Browse files
committed
fix: cli generators
1 parent bae8b74 commit f8cc07d

File tree

7 files changed

+65
-15
lines changed

7 files changed

+65
-15
lines changed

apps/test-bot/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"type": "module",
55
"scripts": {
6+
"commandkit": "commandkit",
67
"dev": "commandkit dev",
78
"build": "commandkit build",
89
"start": "commandkit start"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { CommandData, SlashCommand, MessageCommand } from 'commandkit';
2+
3+
export const data: CommandData = {
4+
name: 'cat',
5+
description: 'cat command',
6+
};
7+
8+
export const chatInput: SlashCommand = async (ctx) => {
9+
await ctx.interaction.reply('Hello from cat!');
10+
};
11+
12+
export const message: MessageCommand = async (ctx) => {
13+
await ctx.message.reply('Hello from cat!');
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { CommandData, SlashCommand, MessageCommand } from 'commandkit';
2+
3+
export const data: CommandData = {
4+
name: 'dog',
5+
description: 'dog command',
6+
};
7+
8+
export const chatInput: SlashCommand = async (ctx) => {
9+
await ctx.interaction.reply('Hello from dog!');
10+
};
11+
12+
export const message: MessageCommand = async (ctx) => {
13+
await ctx.message.reply('Hello from dog!');
14+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default async function onMessageCreate() {
2+
console.log('messageCreate event fired!');
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"command": {
3+
"name": "cat",
4+
"description": "cat command"
5+
},
6+
"translations": {}
7+
}

packages/commandkit/src/cli/generators.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const COMMANDS_DIR = join(BASE_PATH, 'src/app/commands');
1010
const EVENTS_DIR = join(BASE_PATH, 'src/app/events');
1111
const LOCALES_DIR = join(BASE_PATH, 'src/app/locales');
1212

13+
const formatPath = (path: string) =>
14+
path.replace(process.cwd(), '.').replace(/\\/g, '/');
15+
1316
export async function generateCommand(name: string, customPath?: string) {
1417
const cmdPath = join(customPath || COMMANDS_DIR, name);
1518
if (!existsSync(cmdPath)) await mkdir(cmdPath, { recursive: true });
@@ -39,19 +42,19 @@ export const message: MessageCommand = async (ctx) => {
3942

4043
console.log(
4144
colors.green(
42-
`Command ${colors.magenta(name)} created at ${colors.blue(cmdPath)}/command.ts`,
45+
`Command ${colors.magenta(name)} created at ${colors.blue(formatPath(cmdPath))}/command.ts`,
4346
),
4447
);
4548
}
4649

4750
export async function generateEvent(name: string, customPath?: string) {
4851
const eventPath = join(customPath || EVENTS_DIR, name);
49-
if (!existsSync) await mkdir(eventPath, { recursive: true });
52+
if (!existsSync(eventPath)) await mkdir(eventPath, { recursive: true });
5053

5154
let filename = 'event.ts';
5255
if (existsSync(join(eventPath, filename))) {
5356
const count = (await readdir(eventPath)).length;
54-
filename = `${String(count).padStart(2, '0')}_${filename}.ts`;
57+
filename = `${String(count).padStart(2, '0')}_${filename}`;
5558
}
5659

5760
const eventFile = `
@@ -64,7 +67,7 @@ export default async function on${name[0].toUpperCase() + name.slice(1)}() {
6467

6568
console.log(
6669
colors.green(
67-
`Event ${colors.magenta(name)} created at ${colors.blue(eventPath)}/${colors.magenta(filename)}`,
70+
`Event ${colors.magenta(name)} created at ${colors.blue(formatPath(eventPath))}/${colors.magenta(filename)}`,
6871
),
6972
);
7073
}
@@ -74,7 +77,11 @@ export async function generateLocale(
7477
commandName: string,
7578
customPath?: string,
7679
) {
77-
if (!Locale[locale] || /^\d+$/.test(locale)) {
80+
const localeNames = Object.fromEntries(
81+
Object.entries(Locale).map(([k, v]) => [v, k]),
82+
);
83+
84+
if (!localeNames[locale]) {
7885
panic(`Invalid locale: ${locale}`);
7986
}
8087

@@ -106,7 +113,7 @@ export async function generateLocale(
106113
console.log(
107114
colors.green(
108115
`Locale file for ${colors.magenta(commandName)} created at ${colors.blue(
109-
localePath,
116+
formatPath(localePath),
110117
)}/${colors.magenta(`${commandName}.json`)}`,
111118
),
112119
);

packages/commandkit/src/cli/init.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,24 @@ export async function bootstrapCommandkitCLI(
6161
program
6262
.command('create')
6363
.description('Create new commands, events, or locale files')
64-
.option('-c, --command <name>', 'Create a new command')
65-
.option('-e, --event <name>', 'Create a new event')
64+
.option('-c, --command', 'Create a new command')
65+
.option('-e, --event', 'Create a new event')
6666
.option(
67-
'-l, --locale <locale> <command>',
68-
'Create a new locale file for the given command',
67+
'-l, --locale <locale>',
68+
'Specify the locale code (e.g. nl, es, fr)',
6969
)
70-
.action(async (options) => {
70+
.argument('<name>', 'The name of the command or event')
71+
.action(async (name, options) => {
7172
if (options.command) {
72-
await generateCommand(options.command);
73+
await generateCommand(name);
7374
} else if (options.event) {
74-
await generateEvent(options.event);
75+
await generateEvent(name);
7576
} else if (options.locale) {
76-
const [locale, command] = options.locale;
77-
await generateLocale(locale, command);
77+
if (!name) {
78+
console.error('Please specify a command name for the locale file');
79+
return;
80+
}
81+
await generateLocale(options.locale, name);
7882
} else {
7983
console.error(
8084
'Please specify what to create: --command, --event, or --locale',

0 commit comments

Comments
 (0)