Skip to content

Commit 1e0a028

Browse files
authored
Add tsup for ESM support
Add tsup
2 parents 2c9b065 + 7e5691f commit 1e0a028

File tree

21 files changed

+979
-190
lines changed

21 files changed

+979
-190
lines changed

.github/workflows/publish.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
name: 'publish'
1+
name: Publish to NPM
22

33
on:
4-
push:
5-
branches: [master]
4+
release:
5+
types: [created]
66

77
jobs:
88
release:
9-
name: 🚀 publish
9+
name: 🚀 Publish
1010
runs-on: ubuntu-latest
1111
steps:
12-
- name: 📚 checkout
12+
- name: 📚 Checkout
1313
uses: actions/checkout@v3
1414
- name: 🟢 node
1515
uses: actions/setup-node@v2
1616
with:
1717
node-version: 16
1818
registry-url: https://registry.npmjs.org
19-
- name: 🍳 prepare
19+
- name: 🍳 Prepare
2020
run: |
2121
npm install
2222
npm run build
23-
- name: 🚚 publish
23+
- name: 🚚 Publish
2424
run: npm publish
2525
env:
2626
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}

.npmignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
/node_modules
22
/src
3+
/.github
4+
/.vscode
5+
/.git
6+
37
.DS_Store
4-
.vscode
8+
yarn.lock
9+
tsconfig.json
10+
.prettierrc.json
11+
tsup.config.ts

.prettierrc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 4
4+
}

package.json

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
{
22
"name": "commandkit",
33
"version": "0.0.9",
4-
"main": "dist/index.js",
54
"license": "MIT",
5+
"main": "./dist/index.js",
6+
"module": "./dist/index.mjs",
7+
"types": "./dist/index.d.ts",
8+
"exports": {
9+
".": {
10+
"require": "./dist/index.js",
11+
"import": "./dist/index.mjs",
12+
"types": "./dist/index.d.ts"
13+
}
14+
},
615
"scripts": {
7-
"build": "tsc"
16+
"lint": "tsc",
17+
"build": "yarn lint && tsup"
818
},
919
"repository": {
1020
"type": "git",
11-
"url": "https://github.com/notunderctrl/commandkit"
21+
"url": "https://github.com/underctrl-io/commandkit"
1222
},
1323
"homepage": "https://commandkit.underctrl.io",
1424
"keywords": [
@@ -17,8 +27,15 @@
1727
"event handler",
1828
"command validations"
1929
],
30+
"dependencies": {
31+
"colors": "^1.4.0"
32+
},
2033
"devDependencies": {
2134
"discord.js": "^14.12.1",
35+
"tsup": "^7.2.0",
2236
"typescript": "^5.1.6"
37+
},
38+
"peerDependencies": {
39+
"discord.js": "^14"
2340
}
2441
}

src/CommandKit.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,71 @@
1-
import { CommandHandler, EventHandler, ValidationHandler } from './handlers';
2-
import { CommandKitData, CommandKitOptions } from '../typings';
1+
import { CommandHandler, EventHandler, ValidationHandler } from "./handlers";
2+
import { CommandKitData, CommandKitOptions } from "./typings";
3+
import "colors";
34

45
export class CommandKit {
5-
private _data: CommandKitData;
6+
#data: CommandKitData;
67

78
constructor({ ...options }: CommandKitOptions) {
89
if (!options.client) {
9-
throw new Error('"client" is required when instantiating CommandKit.');
10+
throw new Error('"client" is required when instantiating CommandKit.'.red);
1011
}
1112

1213
if (options.validationsPath && !options.commandsPath) {
13-
throw new Error('"commandsPath" is required when "validationsPath" is set.');
14+
throw new Error('"commandsPath" is required when "validationsPath" is set.'.red);
1415
}
1516

16-
this._data = {
17+
this.#data = {
1718
...options,
1819
commands: [],
1920
};
2021

21-
this._init();
22+
this.#init();
2223
}
2324

24-
private _init() {
25+
async #init() {
2526
// Event handler
26-
if (this._data.eventsPath) {
27-
new EventHandler({
28-
client: this._data.client,
29-
eventsPath: this._data.eventsPath,
27+
if (this.#data.eventsPath) {
28+
const eventHandler = new EventHandler({
29+
client: this.#data.client,
30+
eventsPath: this.#data.eventsPath,
3031
});
32+
33+
await eventHandler.init();
3134
}
3235

3336
// Validation handler
3437
let validationFunctions: Function[] = [];
3538

36-
if (this._data.validationsPath) {
39+
if (this.#data.validationsPath) {
3740
const validationHandler = new ValidationHandler({
38-
validationsPath: this._data.validationsPath,
41+
validationsPath: this.#data.validationsPath,
3942
});
4043

41-
validationHandler.getValidations().forEach((v) => validationFunctions.push(v));
44+
await validationHandler.init();
45+
46+
validationHandler.validations.forEach((v) => validationFunctions.push(v));
4247
}
4348

4449
// Command handler
45-
if (this._data.commandsPath) {
50+
if (this.#data.commandsPath) {
4651
const commandHandler = new CommandHandler({
47-
client: this._data.client,
48-
commandsPath: this._data.commandsPath,
49-
devGuildIds: this._data.devGuildIds || [],
50-
devUserIds: this._data.devUserIds || [],
51-
devRoleIds: this._data.devRoleIds || [],
52+
client: this.#data.client,
53+
commandsPath: this.#data.commandsPath,
54+
devGuildIds: this.#data.devGuildIds || [],
55+
devUserIds: this.#data.devUserIds || [],
56+
devRoleIds: this.#data.devRoleIds || [],
5257
customValidations: validationFunctions,
53-
skipBuiltInValidations: this._data.skipBuiltInValidations || false,
58+
skipBuiltInValidations: this.#data.skipBuiltInValidations || false,
5459
});
5560

56-
this._data.commands = commandHandler.getCommands();
61+
await commandHandler.init();
62+
63+
this.#data.commands = commandHandler.commands;
5764
}
5865
}
5966

6067
get commands() {
61-
return this._data.commands.map((cmd) => {
68+
return this.#data.commands.map((cmd) => {
6269
const { run, ...command } = cmd;
6370
return command;
6471
});
Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { BuiltInValidation, CommandHandlerData, CommandHandlerOptions } from './typings';
2-
import { ContextCommandObject, SlashCommandObject } from '../../../typings';
3-
import { getFilePaths } from '../../utils/get-paths';
4-
import registerCommands from './functions/registerCommands';
5-
import handleCommands from './functions/handleCommands';
6-
import path from 'path';
1+
import { CommandHandlerData, CommandHandlerOptions } from "./typings";
2+
import { getFilePaths } from "../../utils/get-paths";
3+
import builtInValidations from "./validations";
4+
import registerCommands from "./functions/registerCommands";
5+
import handleCommands from "./functions/handleCommands";
6+
import "colors";
77

88
export class CommandHandler {
99
_data: CommandHandlerData;
@@ -14,64 +14,55 @@ export class CommandHandler {
1414
builtInValidations: [],
1515
commands: [],
1616
};
17-
18-
this._init();
1917
}
2018

21-
_init() {
22-
this._buildCommands();
23-
this._buildValidations();
24-
this._registerCommands();
25-
this._handleCommands();
19+
async init() {
20+
await this.#buildCommands();
21+
this.#buildValidations();
22+
await this.#registerCommands();
23+
this.#handleCommands();
2624
}
2725

28-
_buildCommands() {
26+
async #buildCommands() {
2927
const commandFilePaths = getFilePaths(this._data.commandsPath, true).filter(
30-
(path) => path.endsWith('.js') || path.endsWith('.ts')
28+
(path) => path.endsWith(".js") || path.endsWith(".ts")
3129
);
3230

3331
for (const commandFilePath of commandFilePaths) {
34-
const commandObj: SlashCommandObject | ContextCommandObject = require(commandFilePath);
32+
let commandObj = await import(commandFilePath);
33+
const compactFilePath = commandFilePath.split(process.cwd())[1] || commandFilePath;
34+
35+
if (commandObj.default) commandObj = commandObj.default;
3536

3637
if (!commandObj.data) {
37-
console.log(`⏩ Ignoring: Command ${commandFilePath} does not export "data".`);
38+
console.log(`⏩ Ignoring: Command ${compactFilePath} does not export "data".`.yellow);
3839
continue;
3940
}
4041

4142
if (!commandObj.run) {
42-
console.log(`⏩ Ignoring: Command ${commandFilePath} does not export "run".`);
43+
console.log(`⏩ Ignoring: Command ${compactFilePath} does not export "run".`.yellow);
4344
continue;
4445
}
4546

4647
this._data.commands.push(commandObj);
4748
}
4849
}
4950

50-
_buildValidations() {
51-
const validationFilePaths = getFilePaths(path.join(__dirname, 'validations'), true).filter(
52-
(path) => path.endsWith('.js')
53-
);
54-
55-
for (const validationFilePath of validationFilePaths) {
56-
const validationFunction: Function = require(validationFilePath);
57-
58-
if (typeof validationFunction !== 'function') {
59-
continue;
60-
}
61-
62-
this._data.builtInValidations.push(validationFunction as BuiltInValidation);
51+
#buildValidations() {
52+
for (const validationFunction of builtInValidations) {
53+
this._data.builtInValidations.push(validationFunction);
6354
}
6455
}
6556

66-
_registerCommands() {
67-
registerCommands(this);
57+
async #registerCommands() {
58+
await registerCommands(this);
6859
}
6960

70-
_handleCommands() {
61+
#handleCommands() {
7162
handleCommands(this);
7263
}
7364

74-
getCommands() {
65+
get commands() {
7566
return this._data.commands;
7667
}
7768
}

src/handlers/command-handler/functions/handleCommands.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import { CommandHandler } from '../CommandHandler';
1+
import { CommandHandler } from "../CommandHandler";
22

33
export default function handleCommands(commandHandler: CommandHandler) {
44
const client = commandHandler._data.client;
55

6-
client.on('interactionCreate', async (interaction) => {
6+
client.on("interactionCreate", async (interaction) => {
77
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand()) return;
88

9-
const targetCommand = commandHandler._data.commands.find(
10-
(cmd) => cmd.data.name === interaction.commandName
11-
);
9+
const targetCommand = commandHandler._data.commands.find((cmd) => cmd.data.name === interaction.commandName);
1210

1311
if (!targetCommand) return;
1412

0 commit comments

Comments
 (0)