commandkit 0.1.11-dev.20250306152436 → 0.1.11-dev.20250308073818

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1011,81 +1011,6 @@ var init_components = __esm({
1011
1011
  }
1012
1012
  });
1013
1013
 
1014
- // src/app/i18n/DefaultLocalizationStrategy.ts
1015
- var import_promises, import_node_path, _DefaultLocalizationStrategy, DefaultLocalizationStrategy;
1016
- var init_DefaultLocalizationStrategy = __esm({
1017
- "src/app/i18n/DefaultLocalizationStrategy.ts"() {
1018
- "use strict";
1019
- init_cjs_shims();
1020
- import_promises = require("fs/promises");
1021
- import_node_path = require("path");
1022
- _DefaultLocalizationStrategy = class _DefaultLocalizationStrategy {
1023
- constructor(commandkit) {
1024
- this.commandkit = commandkit;
1025
- }
1026
- translations = /* @__PURE__ */ new Map();
1027
- async locateTranslation(scope, locale) {
1028
- const localesPath = this.commandkit.getPath("locales");
1029
- if (!localesPath) return null;
1030
- const path3 = (0, import_node_path.join)(localesPath, locale, `${scope}.json`);
1031
- try {
1032
- const data = await (0, import_promises.readFile)(path3, "utf-8");
1033
- return JSON.parse(data);
1034
- } catch {
1035
- return null;
1036
- }
1037
- }
1038
- async getTranslationStrict(scope, locale) {
1039
- const key = `${scope}:${locale}`;
1040
- if (!this.translations.has(key)) {
1041
- const translation = await this.locateTranslation(scope, locale);
1042
- if (!translation) {
1043
- throw new Error(`Translation not found for ${key}`);
1044
- }
1045
- this.translations.set(key, translation);
1046
- return translation;
1047
- }
1048
- return this.translations.get(key);
1049
- }
1050
- async getTranslation(scope, locale) {
1051
- try {
1052
- return this.getTranslationStrict(scope, locale);
1053
- } catch (e) {
1054
- return null;
1055
- }
1056
- }
1057
- async translate(request) {
1058
- const { scope, key, args, locale } = request;
1059
- const translation = await this.getTranslation(scope, locale);
1060
- if (!translation) return `${scope}.${key}`;
1061
- const value = translation.translations[key];
1062
- const translated = this.applyTranslation(value, args);
1063
- return translated || `${scope}.${key}`;
1064
- }
1065
- applyTranslation(translation, args) {
1066
- if (!translation || !args) return translation;
1067
- return translation.replace(/{([^}]+)}/g, (_, key) => {
1068
- return String(args[key] ?? `{${key}}`);
1069
- });
1070
- }
1071
- };
1072
- __name(_DefaultLocalizationStrategy, "DefaultLocalizationStrategy");
1073
- DefaultLocalizationStrategy = _DefaultLocalizationStrategy;
1074
- }
1075
- });
1076
-
1077
- // src/utils/constants.ts
1078
- var COMMANDKIT_CACHE_TAG, COMMANDKIT_IS_DEV, COMMANDKIT_IS_TEST;
1079
- var init_constants = __esm({
1080
- "src/utils/constants.ts"() {
1081
- "use strict";
1082
- init_cjs_shims();
1083
- COMMANDKIT_CACHE_TAG = Symbol("kCommandKitCacheTag");
1084
- COMMANDKIT_IS_DEV = !!process.env.COMMANDKIT_IS_DEV;
1085
- COMMANDKIT_IS_TEST = process.env.COMMANDKIT_IS_TEST === "true";
1086
- }
1087
- });
1088
-
1089
1014
  // src/plugins/PluginCommon.ts
1090
1015
  var _PluginCommon, PluginCommon;
1091
1016
  var init_PluginCommon = __esm({
@@ -1157,11 +1082,83 @@ var init_CompilerPlugin = __esm({
1157
1082
  });
1158
1083
 
1159
1084
  // src/plugins/RuntimePlugin.ts
1085
+ function isRuntimePlugin(plugin) {
1086
+ return plugin instanceof RuntimePlugin;
1087
+ }
1088
+ var _RuntimePlugin, RuntimePlugin;
1160
1089
  var init_RuntimePlugin = __esm({
1161
1090
  "src/plugins/RuntimePlugin.ts"() {
1162
1091
  "use strict";
1163
1092
  init_cjs_shims();
1164
1093
  init_PluginCommon();
1094
+ _RuntimePlugin = class _RuntimePlugin extends PluginCommon {
1095
+ /**
1096
+ * Called before commands are loaded
1097
+ */
1098
+ async onBeforeCommandsLoad(ctx) {
1099
+ }
1100
+ /**
1101
+ * Called after commands are loaded
1102
+ */
1103
+ async onAfterCommandsLoad(ctx) {
1104
+ }
1105
+ /**
1106
+ * Called before events are loaded
1107
+ */
1108
+ async onBeforeEventsLoad(ctx) {
1109
+ }
1110
+ /**
1111
+ * Called after events are loaded
1112
+ */
1113
+ async onAfterEventsLoad(ctx) {
1114
+ }
1115
+ /**
1116
+ * Called before the client logs in
1117
+ */
1118
+ async onBeforeClientLogin(ctx) {
1119
+ }
1120
+ /**
1121
+ * Called after the client logs in
1122
+ */
1123
+ async onAfterClientLogin(ctx) {
1124
+ }
1125
+ /**
1126
+ * Called before interaction is handled
1127
+ * @param interaction The interaction
1128
+ */
1129
+ async onBeforeInteraction(ctx, interaction) {
1130
+ }
1131
+ /**
1132
+ * Called after interaction is handled
1133
+ * @param interaction The interaction that was handled
1134
+ */
1135
+ async onAfterInteraction(ctx, interaction) {
1136
+ }
1137
+ /**
1138
+ * Called before message command is processed
1139
+ * @param message The message
1140
+ */
1141
+ async onBeforeMessageCommand(ctx, message) {
1142
+ }
1143
+ /**
1144
+ * Called before message update command is processed
1145
+ * @param message The message
1146
+ */
1147
+ async onBeforeMessageUpdateCommand(ctx, oldMessage, newMessage) {
1148
+ }
1149
+ /**
1150
+ * Called before command is executed. This method can execute the command itself.
1151
+ * @param source The source that triggered the command
1152
+ * @param command The command
1153
+ * @param execute The function that executes the command
1154
+ */
1155
+ async executeCommand(ctx, source, command, execute) {
1156
+ return false;
1157
+ }
1158
+ };
1159
+ __name(_RuntimePlugin, "RuntimePlugin");
1160
+ RuntimePlugin = _RuntimePlugin;
1161
+ __name(isRuntimePlugin, "isRuntimePlugin");
1165
1162
  }
1166
1163
  });
1167
1164
 
@@ -1386,6 +1383,18 @@ var init_CommandKitPluginRuntime = __esm({
1386
1383
  getPlugin(pluginName) {
1387
1384
  return this.plugins.get(pluginName) ?? null;
1388
1385
  }
1386
+ async softRegisterPlugin(plugin) {
1387
+ const pluginName = plugin.name;
1388
+ if (this.plugins.has(pluginName)) return;
1389
+ try {
1390
+ await plugin.activate(this);
1391
+ this.plugins.set(pluginName, plugin);
1392
+ } catch (e) {
1393
+ throw new Error(
1394
+ `Failed to activate plugin "${pluginName}": ${(e == null ? void 0 : e.stack) || e}`
1395
+ );
1396
+ }
1397
+ }
1389
1398
  async registerPlugin(plugin) {
1390
1399
  const pluginName = plugin.name;
1391
1400
  if (this.plugins.has(pluginName)) {
@@ -1467,12 +1476,12 @@ function fromEsbuildPlugin(plugin) {
1467
1476
  let EsbuildPluginCompat = _EsbuildPluginCompat;
1468
1477
  return EsbuildPluginCompat;
1469
1478
  }
1470
- var import_promises2, pattern, _CompilerPluginRuntime, CompilerPluginRuntime;
1479
+ var import_promises, pattern, _CompilerPluginRuntime, CompilerPluginRuntime;
1471
1480
  var init_CompilerPluginRuntime = __esm({
1472
1481
  "src/plugins/runtime/CompilerPluginRuntime.ts"() {
1473
1482
  "use strict";
1474
1483
  init_cjs_shims();
1475
- import_promises2 = require("fs/promises");
1484
+ import_promises = require("fs/promises");
1476
1485
  init_plugins();
1477
1486
  pattern = /\.(c|m)?(t|j)sx?$/;
1478
1487
  _CompilerPluginRuntime = class _CompilerPluginRuntime {
@@ -1485,7 +1494,7 @@ var init_CompilerPluginRuntime = __esm({
1485
1494
  }
1486
1495
  async onLoad(args) {
1487
1496
  var _a;
1488
- const source = await (0, import_promises2.readFile)(args.path, "utf8");
1497
+ const source = await (0, import_promises.readFile)(args.path, "utf8");
1489
1498
  let contents = source, loader = args.path.split(".").pop();
1490
1499
  for (const plugin of this.plugins) {
1491
1500
  try {
@@ -1956,6 +1965,134 @@ var init_config = __esm({
1956
1965
  }
1957
1966
  });
1958
1967
 
1968
+ // src/app/i18n/DefaultLocalizationStrategy.ts
1969
+ var import_promises2, import_node_path, import_node_fs, _DefaultLocalizationStrategy, DefaultLocalizationStrategy;
1970
+ var init_DefaultLocalizationStrategy = __esm({
1971
+ "src/app/i18n/DefaultLocalizationStrategy.ts"() {
1972
+ "use strict";
1973
+ init_cjs_shims();
1974
+ import_promises2 = require("fs/promises");
1975
+ import_node_path = require("path");
1976
+ init_config();
1977
+ import_node_fs = require("fs");
1978
+ _DefaultLocalizationStrategy = class _DefaultLocalizationStrategy {
1979
+ constructor(commandkit) {
1980
+ this.commandkit = commandkit;
1981
+ }
1982
+ translations = /* @__PURE__ */ new Map();
1983
+ async locateTranslation(scope, locale) {
1984
+ const localesPath = this.commandkit.getPath("locales");
1985
+ if (!localesPath) return null;
1986
+ const path3 = (0, import_node_path.join)(localesPath, locale, `${scope}.json`);
1987
+ try {
1988
+ const data = await (0, import_promises2.readFile)(path3, "utf-8");
1989
+ const localeData = JSON.parse(data);
1990
+ await this.generateLocaleTypes(localeData);
1991
+ return localeData;
1992
+ } catch {
1993
+ return null;
1994
+ }
1995
+ }
1996
+ async generateLocaleTypes(localeData) {
1997
+ const { typedLocales } = getConfig();
1998
+ if (!typedLocales) return;
1999
+ const file = (0, import_node_path.join)(
2000
+ process.cwd(),
2001
+ "node_modules",
2002
+ "commandkit-types",
2003
+ "locale_types.d.ts"
2004
+ );
2005
+ const header = `// auto generated file do not edit
2006
+ declare module 'commandkit' {
2007
+ export interface CommandLocalizationTypeData {
2008
+ `;
2009
+ const footer = ` }
2010
+ }`;
2011
+ const generateType = /* @__PURE__ */ __name((locale) => {
2012
+ return `"${locale.command}": ${JSON.stringify(
2013
+ Object.entries(locale.translations).map(([key, value]) => {
2014
+ const args = value.match(/{([^}]+)}/g);
2015
+ if (!args) return `${JSON.stringify(key)}: null`;
2016
+ return `${JSON.stringify(key)}: ${args.map((arg) => arg.slice(1, -1)).join(" | ")}`;
2017
+ }),
2018
+ null,
2019
+ 2
2020
+ )}`;
2021
+ }, "generateType");
2022
+ if (!(0, import_node_fs.existsSync)(file)) {
2023
+ const generated = generateType(localeData);
2024
+ await (0, import_promises2.writeFile)(file, `${header}${generated}${footer}`);
2025
+ } else {
2026
+ const data = await (0, import_promises2.readFile)(file, "utf-8");
2027
+ const lines = data.split("\n");
2028
+ const index = lines.findIndex(
2029
+ (line) => line.includes("CommandLocalizationTypeData")
2030
+ );
2031
+ if (index === -1) {
2032
+ for (const locale of this.translations.values()) {
2033
+ const generated2 = generateType(locale);
2034
+ await (0, import_promises2.writeFile)(file, `${header}${generated2}${footer}`);
2035
+ }
2036
+ return;
2037
+ }
2038
+ const start = index + 2;
2039
+ const end = lines.findIndex((line) => line.includes("}"));
2040
+ const generated = generateType(localeData);
2041
+ lines.splice(start, end - start, generated);
2042
+ await (0, import_promises2.writeFile)(file, lines.join("\n"));
2043
+ }
2044
+ }
2045
+ async getTranslationStrict(scope, locale) {
2046
+ const key = `${scope}:${locale}`;
2047
+ if (!this.translations.has(key)) {
2048
+ const translation = await this.locateTranslation(scope, locale);
2049
+ if (!translation) {
2050
+ throw new Error(`Translation not found for ${key}`);
2051
+ }
2052
+ this.translations.set(key, translation);
2053
+ return translation;
2054
+ }
2055
+ return this.translations.get(key);
2056
+ }
2057
+ async getTranslation(scope, locale) {
2058
+ try {
2059
+ return this.getTranslationStrict(scope, locale);
2060
+ } catch (e) {
2061
+ return null;
2062
+ }
2063
+ }
2064
+ async translate(request) {
2065
+ const { scope, key, args, locale } = request;
2066
+ const translation = await this.getTranslation(scope, locale);
2067
+ if (!translation) return `${scope}.${key}`;
2068
+ const value = translation.translations[key];
2069
+ const translated = this.applyTranslation(value, args);
2070
+ return translated || `${scope}.${key}`;
2071
+ }
2072
+ applyTranslation(translation, args) {
2073
+ if (!translation || !args) return translation;
2074
+ return translation.replace(/{([^}]+)}/g, (_, key) => {
2075
+ return String(args[key] ?? `{${key}}`);
2076
+ });
2077
+ }
2078
+ };
2079
+ __name(_DefaultLocalizationStrategy, "DefaultLocalizationStrategy");
2080
+ DefaultLocalizationStrategy = _DefaultLocalizationStrategy;
2081
+ }
2082
+ });
2083
+
2084
+ // src/utils/constants.ts
2085
+ var COMMANDKIT_CACHE_TAG, COMMANDKIT_IS_DEV, COMMANDKIT_IS_TEST;
2086
+ var init_constants = __esm({
2087
+ "src/utils/constants.ts"() {
2088
+ "use strict";
2089
+ init_cjs_shims();
2090
+ COMMANDKIT_CACHE_TAG = Symbol("kCommandKitCacheTag");
2091
+ COMMANDKIT_IS_DEV = !!process.env.COMMANDKIT_IS_DEV;
2092
+ COMMANDKIT_IS_TEST = process.env.COMMANDKIT_IS_TEST === "true";
2093
+ }
2094
+ });
2095
+
1959
2096
  // src/utils/utilities.ts
1960
2097
  function findAppDirectory() {
1961
2098
  if (appDir) return appDir;
@@ -1963,10 +2100,10 @@ function findAppDirectory() {
1963
2100
  process.cwd(),
1964
2101
  COMMANDKIT_IS_DEV ? ".commandkit" : getConfig().distDir
1965
2102
  );
1966
- if (!(0, import_node_fs.existsSync)(root)) root = process.cwd();
2103
+ if (!(0, import_node_fs2.existsSync)(root)) root = process.cwd();
1967
2104
  const dirs = ["app"].map((dir) => (0, import_node_path2.join)(root, dir));
1968
2105
  for (const dir of dirs) {
1969
- if ((0, import_node_fs.existsSync)(dir)) {
2106
+ if ((0, import_node_fs2.existsSync)(dir)) {
1970
2107
  appDir = dir;
1971
2108
  return dir;
1972
2109
  }
@@ -1994,12 +2131,12 @@ function debounce(fn, ms2) {
1994
2131
  });
1995
2132
  };
1996
2133
  }
1997
- var import_node_fs, import_node_path2, appDir;
2134
+ var import_node_fs2, import_node_path2, appDir;
1998
2135
  var init_utilities = __esm({
1999
2136
  "src/utils/utilities.ts"() {
2000
2137
  "use strict";
2001
2138
  init_cjs_shims();
2002
- import_node_fs = require("fs");
2139
+ import_node_fs2 = require("fs");
2003
2140
  import_node_path2 = require("path");
2004
2141
  init_constants();
2005
2142
  init_config();
@@ -3635,256 +3772,303 @@ var init_AppCommandHandler = __esm({
3635
3772
  });
3636
3773
 
3637
3774
  // src/app/router/CommandsRouter.ts
3638
- var import_discord11, import_node_fs2, import_promises3, import_node_path3, import_node_crypto3, _CommandsRouter, CommandsRouter;
3775
+ var import_discord11, import_node_fs3, import_promises3, path2, _CommandsRouter, CommandsRouter;
3639
3776
  var init_CommandsRouter = __esm({
3640
3777
  "src/app/router/CommandsRouter.ts"() {
3641
3778
  "use strict";
3642
3779
  init_cjs_shims();
3643
3780
  import_discord11 = require("discord.js");
3644
- import_node_fs2 = require("fs");
3781
+ import_node_fs3 = require("fs");
3645
3782
  import_promises3 = require("fs/promises");
3646
- import_node_path3 = __toESM(require("path"));
3647
- import_node_crypto3 = require("crypto");
3783
+ path2 = __toESM(require("path"));
3648
3784
  _CommandsRouter = class _CommandsRouter {
3649
3785
  constructor(options) {
3650
3786
  this.options = options;
3651
3787
  }
3652
3788
  commands = new import_discord11.Collection();
3653
- subcommands = new import_discord11.Collection();
3654
3789
  middlewares = new import_discord11.Collection();
3790
+ subcommands = new import_discord11.Collection();
3791
+ isValidPath() {
3792
+ return (0, import_node_fs3.existsSync)(this.entrypoint);
3793
+ }
3655
3794
  get entrypoint() {
3656
3795
  return this.options.entrypoint;
3657
3796
  }
3658
- isValidPath() {
3659
- return (0, import_node_fs2.existsSync)(this.entrypoint);
3797
+ clear() {
3798
+ this.commands.clear();
3799
+ this.middlewares.clear();
3800
+ this.subcommands.clear();
3801
+ }
3802
+ async reload() {
3803
+ this.clear();
3804
+ return this.scan();
3805
+ }
3806
+ visualize() {
3807
+ let output = "";
3808
+ const commands = [...this.commands.values()];
3809
+ commands.forEach((command, index) => {
3810
+ const isLast = index === commands.length - 1;
3811
+ const prefix = isLast ? "\u2514\u2500" : "\u251C\u2500";
3812
+ const childPrefix = isLast ? " " : "\u2502 ";
3813
+ const categoryPrefix = command.category ? `[${command.category}] ` : "";
3814
+ output += `${prefix} ${categoryPrefix}${command.name}
3815
+ `;
3816
+ const subcommands = command.subcommands.map((id) => this.subcommands.get(id)).filter(Boolean);
3817
+ const groupedSubs = /* @__PURE__ */ new Map();
3818
+ subcommands.forEach((sub) => {
3819
+ if (!groupedSubs.has(sub.group)) {
3820
+ groupedSubs.set(sub.group, []);
3821
+ }
3822
+ groupedSubs.get(sub.group).push(sub);
3823
+ });
3824
+ const groups = [...groupedSubs.entries()];
3825
+ groups.forEach(([group, subs], groupIndex) => {
3826
+ const isLastGroup = groupIndex === groups.length - 1;
3827
+ if (group) {
3828
+ const groupPrefix = isLastGroup ? `${childPrefix}\u2514\u2500` : `${childPrefix}\u251C\u2500`;
3829
+ output += `${groupPrefix} Group: ${group}
3830
+ `;
3831
+ }
3832
+ const subPrefix = group ? `${childPrefix}${isLastGroup ? " " : "\u2502 "}` : childPrefix;
3833
+ subs.forEach((subcommand, subIndex) => {
3834
+ const isLastSub = subIndex === subs.length - 1;
3835
+ const subCommandPrefix = isLastSub ? `${subPrefix}\u2514\u2500` : `${subPrefix}\u251C\u2500`;
3836
+ output += `${subCommandPrefix} ${subcommand.name}
3837
+ `;
3838
+ const middlewares = subcommand.middlewares.map((id) => this.middlewares.get(id)).filter(Boolean);
3839
+ middlewares.forEach((middleware, mwIndex) => {
3840
+ const isLastMw = mwIndex === middlewares.length - 1;
3841
+ const mwPrefix = isLastMw ? `${subPrefix}${isLastSub ? " " : "\u2502 "}\u2514\u2500` : `${subPrefix}${isLastSub ? " " : "\u2502 "}\u251C\u2500`;
3842
+ output += `${mwPrefix} \u2699\uFE0F ${middleware.name}
3843
+ `;
3844
+ });
3845
+ });
3846
+ });
3847
+ const commandMiddlewares = command.middlewares.map((id) => this.middlewares.get(id)).filter(Boolean);
3848
+ if (commandMiddlewares.length > 0) {
3849
+ output += `${childPrefix}\u2514\u2500 Middlewares:
3850
+ `;
3851
+ commandMiddlewares.forEach((middleware, mwIndex) => {
3852
+ const isLastMw = mwIndex === commandMiddlewares.length - 1;
3853
+ const mwPrefix = isLastMw ? `${childPrefix} \u2514\u2500` : `${childPrefix} \u251C\u2500`;
3854
+ output += `${mwPrefix} \u2699\uFE0F ${middleware.name}
3855
+ `;
3856
+ });
3857
+ }
3858
+ output += "\n";
3859
+ });
3860
+ return output;
3660
3861
  }
3661
3862
  getData() {
3662
3863
  return {
3663
3864
  commands: this.commands,
3664
- subcommands: this.subcommands,
3665
- middlewares: this.middlewares
3865
+ middlewares: this.middlewares,
3866
+ subcommands: this.subcommands
3666
3867
  };
3667
3868
  }
3668
3869
  toJSON() {
3669
3870
  return {
3670
3871
  commands: Object.fromEntries(this.commands.entries()),
3671
- subcommands: Object.fromEntries(this.subcommands.entries()),
3672
- middlewares: Object.fromEntries(this.middlewares.entries())
3872
+ middlewares: Object.fromEntries(this.middlewares.entries()),
3873
+ subcommands: Object.fromEntries(this.subcommands.entries())
3673
3874
  };
3674
3875
  }
3675
- clear() {
3676
- this.commands.clear();
3677
- this.subcommands.clear();
3678
- this.middlewares.clear();
3679
- }
3680
- async reload() {
3681
- this.clear();
3682
- return this.scan();
3683
- }
3684
3876
  async scan() {
3685
- await this.scanDirectory(this.entrypoint);
3877
+ if (!this.isValidPath()) return this.toJSON();
3878
+ const root = this.entrypoint;
3879
+ await this.scanDeep(root);
3686
3880
  return this.toJSON();
3687
3881
  }
3688
- async scanDirectory(dir, parentPath = "", categories = []) {
3689
- const entries = await (0, import_promises3.readdir)(dir);
3690
- const directoryStats = /* @__PURE__ */ new Map();
3691
- for (const entry of entries) {
3692
- if (entry.startsWith("_")) continue;
3693
- const fullPath = import_node_path3.default.join(dir, entry);
3694
- const stats = await (0, import_promises3.stat)(fullPath);
3695
- directoryStats.set(entry, stats.isDirectory());
3696
- }
3697
- await this.processMiddlewares(dir, entries, parentPath, categories);
3698
- for (const entry of entries) {
3699
- if (entry.startsWith("_")) continue;
3700
- if (entry.includes(".middleware.")) continue;
3701
- const fullPath = import_node_path3.default.join(dir, entry);
3702
- const isDirectory = directoryStats.get(entry) || false;
3703
- if (isDirectory) {
3704
- const { name, parsedCategories } = this.parseNameAndCategory(entry);
3705
- const newCategories = [...categories, ...parsedCategories];
3706
- const isCategoryContainer = /^\([^)]+\)$/.test(entry);
3707
- if (isCategoryContainer) {
3708
- await this.scanDirectory(fullPath, parentPath, newCategories);
3709
- } else {
3710
- const newParentPath = parentPath ? `${parentPath}/${name}` : name;
3711
- await this.processCommandDirectory(
3712
- fullPath,
3713
- name,
3714
- newParentPath,
3715
- newCategories
3716
- );
3882
+ async scanDeep(entry, currentDepth = 3) {
3883
+ const normalizedPath = path2.normalize(entry);
3884
+ const content = await (0, import_promises3.readdir)(normalizedPath, { withFileTypes: true });
3885
+ const isRootOrDirectSubdir = normalizedPath === this.entrypoint || path2.dirname(normalizedPath) === this.entrypoint || // Check if parent directories between this and entrypoint are all category directories
3886
+ this.getPathSegmentsBetween(normalizedPath, this.entrypoint).every(
3887
+ (segment) => this.isCategoryDirectory(segment)
3888
+ );
3889
+ const effectiveDepth = isRootOrDirectSubdir ? 3 : currentDepth;
3890
+ for (const item of content) {
3891
+ const itemPath = path2.join(normalizedPath, item.name);
3892
+ if (this.shouldIgnore(itemPath, item.isDirectory())) continue;
3893
+ if (item.isDirectory()) {
3894
+ const isCategoryDir = this.isCategoryDirectory(item.name);
3895
+ if (isCategoryDir || effectiveDepth >= 0) {
3896
+ const nextDepth = isCategoryDir ? effectiveDepth : effectiveDepth - 1;
3897
+ await this.scanDeep(itemPath, nextDepth);
3717
3898
  }
3718
3899
  } else {
3719
- if (!/\.(m|c)?(j|t)sx?$/.test(entry)) {
3720
- continue;
3721
- }
3722
- const extname = import_node_path3.default.extname(entry);
3723
- const basename = import_node_path3.default.basename(entry, extname);
3724
- if (basename === "index") {
3725
- continue;
3726
- }
3727
- const { name, parsedCategories } = this.parseNameAndCategory(basename);
3728
- const newCategories = [...categories, ...parsedCategories];
3729
- if (parentPath) {
3730
- await this.processSubCommand(
3731
- fullPath,
3732
- name,
3733
- parentPath,
3734
- newCategories
3900
+ const category = this.extractCategory(itemPath);
3901
+ const fileName = this.extractName(itemPath);
3902
+ const isIndexFile = fileName === "index";
3903
+ if (this.isMiddleware(itemPath)) {
3904
+ this.processMiddleware(itemPath, category);
3905
+ } else if (effectiveDepth <= 1) {
3906
+ this.processSubcommand(
3907
+ itemPath,
3908
+ path2.basename(normalizedPath),
3909
+ effectiveDepth === 0,
3910
+ // isSubCommandGroup when depth is 0
3911
+ isIndexFile,
3912
+ effectiveDepth,
3913
+ category
3735
3914
  );
3736
3915
  } else {
3737
- await this.processCommand(fullPath, name, newCategories);
3916
+ this.processCommand(
3917
+ itemPath,
3918
+ category,
3919
+ isIndexFile ? normalizedPath : null
3920
+ );
3738
3921
  }
3739
3922
  }
3740
3923
  }
3741
3924
  }
3742
- async processMiddlewares(dir, entries, parentPath, categories) {
3743
- for (const entry of entries) {
3744
- if (entry.startsWith("_")) continue;
3745
- if (!entry.includes(".middleware.")) continue;
3746
- const fullPath = import_node_path3.default.join(dir, entry);
3747
- const stats = await (0, import_promises3.stat)(fullPath);
3748
- if (stats.isDirectory()) continue;
3749
- if (!/\.(m|c)?(j|t)sx?$/.test(entry)) {
3750
- continue;
3925
+ /**
3926
+ * Gets path segments between two paths, excluding the start and end paths
3927
+ * Used to check if all directories between a path and the entrypoint are category directories
3928
+ */
3929
+ getPathSegmentsBetween(path1, path22) {
3930
+ const normalizedPath1 = path2.normalize(path1);
3931
+ const normalizedPath2 = path2.normalize(path22);
3932
+ if (normalizedPath1 === normalizedPath2) return [];
3933
+ const relativePath = path2.relative(normalizedPath2, normalizedPath1);
3934
+ return relativePath.split(path2.sep).filter((segment) => segment !== "." && segment !== "..");
3935
+ }
3936
+ processMiddleware(filePath, category) {
3937
+ const middleware = {
3938
+ id: crypto.randomUUID(),
3939
+ name: this.getAppropriateFileName(filePath),
3940
+ path: filePath,
3941
+ relativePath: this.getRelativePath(filePath),
3942
+ category
3943
+ };
3944
+ this.middlewares.set(middleware.id, middleware);
3945
+ this.applyMiddlewareToCommands(middleware);
3946
+ }
3947
+ processSubcommand(filePath, parentDirName, isGrouped, isIndexFile = false, depth, category) {
3948
+ if (isIndexFile) {
3949
+ const cleanParentDirName = this.extractDirOmitCategory(parentDirName);
3950
+ const parentCommand = this.commands.find((cmd) => {
3951
+ return cmd.name === cleanParentDirName;
3952
+ });
3953
+ if (!parentCommand) {
3954
+ const command = {
3955
+ id: crypto.randomUUID(),
3956
+ name: cleanParentDirName,
3957
+ category,
3958
+ middlewares: [],
3959
+ path: filePath,
3960
+ relativePath: this.getRelativePath(filePath),
3961
+ subcommands: []
3962
+ };
3963
+ this.commands.set(command.id, command);
3751
3964
  }
3752
- const extname = import_node_path3.default.extname(entry);
3753
- const basename = import_node_path3.default.basename(entry, extname);
3754
- const middlewareSegments = basename.split(".middleware");
3755
- const middlewareName = middlewareSegments[0];
3756
- const { name, parsedCategories } = this.parseNameAndCategory(middlewareName);
3757
- const newCategories = [...categories, ...parsedCategories];
3758
- const category = newCategories.length ? newCategories.join(":") : null;
3759
- const middleware = {
3760
- name,
3761
- id: (0, import_node_crypto3.randomUUID)(),
3762
- // Using randomUUID for unique ID generation
3763
- path: fullPath,
3764
- category
3965
+ } else {
3966
+ const subcommand = {
3967
+ id: crypto.randomUUID(),
3968
+ name: this.extractName(filePath),
3969
+ // For subcommands, the group is the directory name (excluding category markers)
3970
+ group: isGrouped ? this.extractDirOmitCategory(parentDirName) : null,
3971
+ middlewares: [],
3972
+ path: filePath,
3973
+ category,
3974
+ relativePath: this.getRelativePath(filePath)
3765
3975
  };
3766
- this.middlewares.set(middleware.id, middleware);
3976
+ this.subcommands.set(subcommand.id, subcommand);
3977
+ this.applySubcommandToCommands(subcommand);
3767
3978
  }
3768
3979
  }
3769
- async processCommand(filePath, name, categories) {
3770
- const category = categories.length ? categories.join(":") : null;
3771
- const id = (0, import_node_crypto3.randomUUID)();
3772
- const middlewares = this.findAssociatedMiddlewares(name, category);
3980
+ processCommand(filePath, category, parentPath = null) {
3981
+ console.log({ filePath, category, parentPath });
3773
3982
  const command = {
3774
- id,
3775
- name,
3983
+ id: crypto.randomUUID(),
3984
+ name: parentPath ? this.getDirectoryName(parentPath) : this.extractName(filePath),
3776
3985
  path: filePath,
3986
+ relativePath: this.getRelativePath(filePath),
3777
3987
  category,
3778
- subcommands: [],
3779
- middlewares
3988
+ middlewares: [],
3989
+ subcommands: []
3780
3990
  };
3781
- this.commands.set(id, command);
3782
- }
3783
- async processCommandDirectory(dirPath, name, parentPath, categories) {
3784
- const category = categories.length ? categories.join(":") : null;
3785
- const id = (0, import_node_crypto3.randomUUID)();
3786
- let indexFile = null;
3787
- const possibleExtensions = [
3788
- ".js",
3789
- ".ts",
3790
- ".jsx",
3791
- ".tsx",
3792
- ".mjs",
3793
- ".cjs",
3794
- ".mts",
3795
- ".cts"
3796
- ];
3797
- for (const ext of possibleExtensions) {
3798
- const testPath = import_node_path3.default.join(dirPath, `index${ext}`);
3799
- if ((0, import_node_fs2.existsSync)(testPath)) {
3800
- indexFile = testPath;
3801
- break;
3802
- }
3991
+ this.commands.set(command.id, command);
3992
+ }
3993
+ extractDirOmitCategory(dirPath) {
3994
+ const segments = path2.normalize(dirPath).split(path2.sep).reverse();
3995
+ let name = "";
3996
+ while (segments.length > 0) {
3997
+ const segment = segments.pop();
3998
+ if (this.isCategoryDirectory(segment)) continue;
3999
+ name = segment;
4000
+ break;
3803
4001
  }
3804
- const middlewares = this.findAssociatedMiddlewares(name, category);
3805
- const entries = await (0, import_promises3.readdir)(dirPath);
3806
- const subcommands = [];
3807
- for (const entry of entries) {
3808
- if (entry.startsWith("_") || entry.startsWith("index.")) continue;
3809
- const fullPath = import_node_path3.default.join(dirPath, entry);
3810
- const stats = await (0, import_promises3.stat)(fullPath);
3811
- const isDirectory = stats.isDirectory();
3812
- if (!isDirectory && !/\.(m|c)?(j|t)sx?$/.test(entry)) {
3813
- continue;
3814
- }
3815
- if (!isDirectory || isDirectory && !entry.includes(".")) {
3816
- const extname = isDirectory ? "" : import_node_path3.default.extname(entry);
3817
- const baseName = isDirectory ? entry : import_node_path3.default.basename(entry, extname);
3818
- if (!baseName.includes(".middleware")) {
3819
- const { name: subName } = this.parseNameAndCategory(baseName);
3820
- subcommands.push(subName);
3821
- }
3822
- }
4002
+ return name;
4003
+ }
4004
+ /**
4005
+ * Gets the appropriate file name, handling index files specially
4006
+ * For index files, it returns the parent directory name (excluding category markers)
4007
+ */
4008
+ getAppropriateFileName(filePath) {
4009
+ const fileName = this.extractName(filePath);
4010
+ if (fileName === "index" && path2.dirname(filePath) !== this.entrypoint) {
4011
+ return this.getDirectoryName(filePath);
3823
4012
  }
3824
- const command = {
3825
- id,
3826
- name,
3827
- path: indexFile,
3828
- // This can be null if no index file exists
3829
- category,
3830
- subcommands,
3831
- middlewares
3832
- };
3833
- this.commands.set(id, command);
3834
- await this.scanDirectory(dirPath, id, categories);
4013
+ return fileName;
3835
4014
  }
3836
- async processSubCommand(filePath, name, parentPath, categories) {
3837
- if (!/\.(m|c)?(j|t)sx?$/.test(filePath)) {
3838
- return;
4015
+ /**
4016
+ * Extract the clean directory name, removing any category markers
4017
+ */
4018
+ getDirectoryName(filePath) {
4019
+ const dirName = path2.basename(path2.dirname(filePath));
4020
+ return dirName.replace(/^\((\w+)\)$/, "$1");
4021
+ }
4022
+ // Helper method to identify category directories
4023
+ isCategoryDirectory(dirName) {
4024
+ return /^\(\w+\)$/.test(dirName);
4025
+ }
4026
+ applyMiddlewareToCommands(middleware) {
4027
+ for (const command of this.commands.values()) {
4028
+ const middlewareFileName = path2.basename(middleware.path);
4029
+ const match = middlewareFileName.startsWith(command.name) || middlewareFileName === "middleware";
4030
+ if (!match) continue;
4031
+ if (command.middlewares.includes(middleware.id)) continue;
4032
+ command.middlewares.push(middleware.id);
3839
4033
  }
3840
- const pathSegments = parentPath.split("/");
3841
- let group = null;
3842
- if (pathSegments.length > 1) {
3843
- group = pathSegments[1];
4034
+ }
4035
+ applySubcommandToCommands(subcommand) {
4036
+ for (const command of this.commands.values()) {
4037
+ const subcommandPathParts = path2.normalize(subcommand.path).split(path2.sep);
4038
+ const commandName = command.name;
4039
+ const relevantDirectories = subcommandPathParts.map((part) => path2.basename(part)).filter((part) => !this.isCategoryDirectory(part));
4040
+ if (relevantDirectories.includes(commandName)) {
4041
+ if (!command.subcommands.includes(subcommand.id)) {
4042
+ command.subcommands.push(subcommand.id);
4043
+ }
4044
+ }
3844
4045
  }
3845
- const category = categories.length ? categories.join(":") : null;
3846
- const id = (0, import_node_crypto3.randomUUID)();
3847
- const middlewares = this.findAssociatedMiddlewares(name, category);
3848
- const subcommand = {
3849
- id,
3850
- name,
3851
- path: filePath,
3852
- category,
3853
- group,
3854
- middlewares
3855
- };
3856
- this.subcommands.set(id, subcommand);
3857
- }
3858
- parseNameAndCategory(name) {
3859
- const parsedCategories = [];
3860
- const regex = /\(([^)]+)\)/g;
3861
- let match;
3862
- let cleanName = name;
3863
- while ((match = regex.exec(name)) !== null) {
3864
- parsedCategories.push(match[1]);
3865
- cleanName = cleanName.replace(match[0], "");
4046
+ }
4047
+ extractCategory(entry) {
4048
+ return path2.normalize(entry).split(path2.sep).map((segment) => {
4049
+ const match = segment.match(/^\((\w+)\)$/);
4050
+ return match ? match[1] : null;
4051
+ }).filter(Boolean).join(":") || null;
4052
+ }
4053
+ getRelativePath(entry) {
4054
+ return path2.relative(this.entrypoint, entry);
4055
+ }
4056
+ isMiddleware(entry) {
4057
+ const fileName = path2.basename(entry);
4058
+ return fileName === "middleware.js" || fileName === "middleware.ts" || fileName === "middleware.mjs" || fileName === "middleware.cjs" || fileName === "middleware.jsx" || fileName === "middleware.tsx" || /\.middleware\.(c|m)?(j|t)sx?$/.test(fileName);
4059
+ }
4060
+ shouldIgnore(entry, dir = false) {
4061
+ const fileName = path2.basename(entry);
4062
+ if (fileName.startsWith("_")) return true;
4063
+ if (!dir) {
4064
+ const ext = path2.extname(fileName).toLowerCase();
4065
+ return ![".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"].includes(ext);
3866
4066
  }
3867
- return {
3868
- name: cleanName.trim(),
3869
- parsedCategories
3870
- };
4067
+ return false;
3871
4068
  }
3872
- findAssociatedMiddlewares(commandName, category) {
3873
- const middlewares = [];
3874
- this.middlewares.forEach((middleware) => {
3875
- if (middleware.name === "middleware") {
3876
- middlewares.push(middleware.id);
3877
- return;
3878
- }
3879
- if (middleware.name === commandName) {
3880
- middlewares.push(middleware.id);
3881
- return;
3882
- }
3883
- if (category && middleware.category === category) {
3884
- middlewares.push(middleware.id);
3885
- }
3886
- });
3887
- return middlewares;
4069
+ extractName(entry) {
4070
+ const fileName = path2.basename(entry);
4071
+ return path2.parse(fileName).name;
3888
4072
  }
3889
4073
  };
3890
4074
  __name(_CommandsRouter, "CommandsRouter");
@@ -3893,14 +4077,14 @@ var init_CommandsRouter = __esm({
3893
4077
  });
3894
4078
 
3895
4079
  // src/app/router/EventsRouter.ts
3896
- var import_node_fs3, import_promises4, import_node_path4, _EventsRouter, EventsRouter;
4080
+ var import_node_fs4, import_promises4, import_node_path3, _EventsRouter, EventsRouter;
3897
4081
  var init_EventsRouter = __esm({
3898
4082
  "src/app/router/EventsRouter.ts"() {
3899
4083
  "use strict";
3900
4084
  init_cjs_shims();
3901
- import_node_fs3 = require("fs");
4085
+ import_node_fs4 = require("fs");
3902
4086
  import_promises4 = require("fs/promises");
3903
- import_node_path4 = require("path");
4087
+ import_node_path3 = require("path");
3904
4088
  _EventsRouter = class _EventsRouter {
3905
4089
  /**
3906
4090
  * Creates a new EventsRouter instance
@@ -3934,7 +4118,7 @@ var init_EventsRouter = __esm({
3934
4118
  * Checks if the entrypoint path is valid
3935
4119
  */
3936
4120
  isValidPath() {
3937
- return (0, import_node_fs3.existsSync)(this.entrypoint);
4121
+ return (0, import_node_fs4.existsSync)(this.entrypoint);
3938
4122
  }
3939
4123
  /**
3940
4124
  * Clear all parsed events
@@ -3958,7 +4142,7 @@ var init_EventsRouter = __esm({
3958
4142
  const dirs = await (0, import_promises4.readdir)(this.entrypoint, { withFileTypes: true });
3959
4143
  for (const dir of dirs) {
3960
4144
  if (dir.isDirectory()) {
3961
- const path3 = (0, import_node_path4.join)(this.entrypoint, dir.name);
4145
+ const path3 = (0, import_node_path3.join)(this.entrypoint, dir.name);
3962
4146
  const event = await this.scanEvent(dir.name, path3);
3963
4147
  this.events.set(event.event, event);
3964
4148
  }
@@ -3985,12 +4169,12 @@ var init_EventsRouter = __esm({
3985
4169
  for (const file of files) {
3986
4170
  if (/node_modules/.test(file.name)) continue;
3987
4171
  if (file.isDirectory()) {
3988
- const nextPath = (0, import_node_path4.join)(path3, file.name);
4172
+ const nextPath = (0, import_node_path3.join)(path3, file.name);
3989
4173
  await this.scanEvent(event, nextPath, listeners);
3990
4174
  continue;
3991
4175
  }
3992
4176
  if (file.isFile() && /\.(m|c)?(j|t)sx?$/.test(file.name)) {
3993
- listeners.push((0, import_node_path4.join)(file.parentPath, file.name));
4177
+ listeners.push((0, import_node_path3.join)(file.parentPath, file.name));
3994
4178
  }
3995
4179
  }
3996
4180
  const maybeNamespace = path3.replace(this.entrypoint, "").replaceAll("\\", "/").split("/").shift();
@@ -4042,7 +4226,10 @@ var init_AppEventsHandler = __esm({
4042
4226
  `Event handler for ${event.event}${event.namespace ? ` of namespace ${event.namespace}` : ""} does not have a default export or is not a function`
4043
4227
  );
4044
4228
  }
4045
- listeners.push(handler.default);
4229
+ listeners.push({
4230
+ handler: handler.default,
4231
+ once: !!handler.once
4232
+ });
4046
4233
  }
4047
4234
  const len = listeners.length;
4048
4235
  if (!len) {
@@ -4070,11 +4257,28 @@ var init_AppEventsHandler = __esm({
4070
4257
  registerAllClientEvents() {
4071
4258
  const client = this.commandkit.client;
4072
4259
  for (const [key, data] of this.loadedEvents.entries()) {
4073
- const { name, listeners, namespace, mainListener } = data;
4074
- const main = mainListener || (async (...args) => {
4075
- for (const listener of listeners) {
4260
+ const { name, listeners, namespace } = data;
4261
+ const onceListeners = listeners.filter((listener) => listener.once);
4262
+ const onListeners = listeners.filter((listener) => !listener.once);
4263
+ const executedOnceListeners = /* @__PURE__ */ new Set();
4264
+ const mainHandler = /* @__PURE__ */ __name(async (...args) => {
4265
+ for (const listener of onListeners) {
4266
+ try {
4267
+ await listener.handler(...args);
4268
+ } catch (e) {
4269
+ Logger.error(
4270
+ `Error handling event ${name}${namespace ? ` of namespace ${namespace}` : ""}`,
4271
+ e
4272
+ );
4273
+ }
4274
+ }
4275
+ }, "mainHandler");
4276
+ const onceHandler = /* @__PURE__ */ __name(async (...args) => {
4277
+ for (const listener of onceListeners) {
4076
4278
  try {
4077
- await listener(...args);
4279
+ if (executedOnceListeners.has(listener.handler)) continue;
4280
+ await listener.handler(...args);
4281
+ executedOnceListeners.add(listener.handler);
4078
4282
  } catch (e) {
4079
4283
  Logger.error(
4080
4284
  `Error handling event ${name}${namespace ? ` of namespace ${namespace}` : ""}`,
@@ -4082,16 +4286,35 @@ var init_AppEventsHandler = __esm({
4082
4286
  );
4083
4287
  }
4084
4288
  }
4289
+ if (executedOnceListeners.size === onceListeners.length && onListeners.length === 0) {
4290
+ this.loadedEvents.delete(key);
4291
+ Logger.info(
4292
+ `\u{1F9F9} Cleaned up completed once-only event ${name}${namespace ? ` of namespace ${namespace}` : ""}`
4293
+ );
4294
+ }
4295
+ }, "onceHandler");
4296
+ this.loadedEvents.set(key, {
4297
+ ...data,
4298
+ mainListener: onListeners.length > 0 ? { handler: mainHandler, once: false } : void 0,
4299
+ executedOnceListeners
4085
4300
  });
4086
- if (!mainListener) {
4087
- this.loadedEvents.set(key, {
4088
- ...data,
4089
- mainListener: main
4090
- });
4301
+ if (namespace) {
4302
+ if (onListeners.length > 0) {
4303
+ this.commandkit.events.on(namespace, name, mainHandler);
4304
+ }
4305
+ if (onceListeners.length > 0) {
4306
+ this.commandkit.events.once(namespace, name, onceHandler);
4307
+ }
4308
+ } else {
4309
+ if (onListeners.length > 0) {
4310
+ client.on(name, mainHandler);
4311
+ }
4312
+ if (onceListeners.length > 0) {
4313
+ client.once(name, onceHandler);
4314
+ }
4091
4315
  }
4092
- client.on(name, main);
4093
4316
  Logger.info(
4094
- `\u{1F50C} Registered event ${name}${namespace ? ` of namespace ${namespace}` : ""}`
4317
+ `\u{1F50C} Registered event ${name}${namespace ? ` of namespace ${namespace}` : ""} (${onListeners.length} regular, ${onceListeners.length} once-only)`
4095
4318
  );
4096
4319
  }
4097
4320
  }
@@ -4102,9 +4325,17 @@ var init_AppEventsHandler = __esm({
4102
4325
  { name, mainListener, namespace }
4103
4326
  ] of this.loadedEvents.entries()) {
4104
4327
  if (mainListener) {
4105
- client.off(name, mainListener);
4328
+ if (namespace) {
4329
+ this.commandkit.events.off(namespace, name, mainListener.handler);
4330
+ } else {
4331
+ client.off(name, mainListener.handler);
4332
+ }
4106
4333
  } else {
4107
- client.removeAllListeners(name);
4334
+ if (namespace) {
4335
+ this.commandkit.events.removeAllListeners(namespace, name);
4336
+ } else {
4337
+ client.removeAllListeners(name);
4338
+ }
4108
4339
  }
4109
4340
  this.loadedEvents.delete(key);
4110
4341
  Logger.info(
@@ -4118,6 +4349,54 @@ var init_AppEventsHandler = __esm({
4118
4349
  }
4119
4350
  });
4120
4351
 
4352
+ // src/utils/types-package.ts
4353
+ async function generateTypesPackage() {
4354
+ const location = (0, import_node_path4.join)(process.cwd(), "node_modules", "commandkit-types");
4355
+ const packageJSON = (0, import_node_path4.join)(location, "package.json");
4356
+ const index = (0, import_node_path4.join)(location, "index.js");
4357
+ const types = (0, import_node_path4.join)(location, "index.d.ts");
4358
+ const locale = (0, import_node_path4.join)(location, "locale.d.ts");
4359
+ const command = (0, import_node_path4.join)(location, "command.d.ts");
4360
+ const exists = (0, import_node_fs5.existsSync)(packageJSON);
4361
+ if (exists) return location;
4362
+ const packageJSONContent = {
4363
+ name: "commandkit-types",
4364
+ version: "1.0.0",
4365
+ description: "CommandKit types package",
4366
+ type: "commonjs",
4367
+ main: "index.js",
4368
+ types: "index.d.ts"
4369
+ };
4370
+ const indexContent = `module.exports = {};`;
4371
+ const typesContent = `import { CommandLocalizationTypeData } from './locale.d.ts';
4372
+ import { CommandTypeData } from './command.d.ts';
4373
+
4374
+ declare module 'commandkit' {
4375
+ export interface CommandKitTypeReferences {
4376
+ locale: CommandLocalizationTypeData;
4377
+ command: CommandTypeData;
4378
+ }
4379
+ }`;
4380
+ await (0, import_promises5.mkdir)(location, { recursive: true });
4381
+ await (0, import_promises5.writeFile)(packageJSON, JSON.stringify(packageJSONContent, null, 2));
4382
+ await (0, import_promises5.writeFile)(index, indexContent);
4383
+ await (0, import_promises5.writeFile)(types, typesContent);
4384
+ await (0, import_promises5.writeFile)(locale, "");
4385
+ await (0, import_promises5.writeFile)(command, "");
4386
+ return location;
4387
+ }
4388
+ var import_node_fs5, import_promises5, import_node_path4;
4389
+ var init_types_package = __esm({
4390
+ "src/utils/types-package.ts"() {
4391
+ "use strict";
4392
+ init_cjs_shims();
4393
+ import_node_fs5 = require("fs");
4394
+ import_promises5 = require("fs/promises");
4395
+ import_node_path4 = require("path");
4396
+ __name(generateTypesPackage, "generateTypesPackage");
4397
+ }
4398
+ });
4399
+
4121
4400
  // src/cli/common.ts
4122
4401
  function write(message) {
4123
4402
  process.stdout.write(message);
@@ -4145,7 +4424,7 @@ async function loadConfigFileFromPath(target) {
4145
4424
  const isTs = await ensureTypeScript(target);
4146
4425
  if (isTs && ts) {
4147
4426
  const { transpileModule } = ts;
4148
- const src = import_node_fs4.default.readFileSync(target, "utf8");
4427
+ const src = import_node_fs6.default.readFileSync(target, "utf8");
4149
4428
  const { outputText } = transpileModule(src, {
4150
4429
  compilerOptions: {
4151
4430
  module: ts.ModuleKind.ESNext,
@@ -4154,14 +4433,9 @@ async function loadConfigFileFromPath(target) {
4154
4433
  },
4155
4434
  fileName: target
4156
4435
  });
4157
- const nodeModulesPath = (0, import_node_path5.join)(
4158
- process.cwd(),
4159
- "node_modules",
4160
- ".commandkit_tmp"
4161
- );
4162
- import_node_fs4.default.mkdirSync(nodeModulesPath, { recursive: true });
4436
+ const nodeModulesPath = await generateTypesPackage();
4163
4437
  const tmpFile = (0, import_node_path5.join)(nodeModulesPath, "compiled-commandkit.config.mjs");
4164
- import_node_fs4.default.writeFileSync(tmpFile, outputText);
4438
+ import_node_fs6.default.writeFileSync(tmpFile, outputText);
4165
4439
  target = tmpFile;
4166
4440
  }
4167
4441
  const config = await import(`file://${target}`).then(
@@ -4170,20 +4444,21 @@ async function loadConfigFileFromPath(target) {
4170
4444
  return config;
4171
4445
  }
4172
4446
  async function ensureExists(loc) {
4173
- const exists = import_node_fs4.default.existsSync(loc);
4447
+ const exists = import_node_fs6.default.existsSync(loc);
4174
4448
  if (!exists) {
4175
4449
  throw new Error(`File not found: ${loc}`);
4176
4450
  }
4177
4451
  }
4178
- var import_rimraf, import_node_path5, import_node_fs4, ts;
4452
+ var import_rimraf, import_node_path5, import_node_fs6, ts;
4179
4453
  var init_common = __esm({
4180
4454
  "src/cli/common.ts"() {
4181
4455
  "use strict";
4182
4456
  init_cjs_shims();
4183
4457
  import_rimraf = require("rimraf");
4184
4458
  import_node_path5 = require("path");
4185
- import_node_fs4 = __toESM(require("fs"));
4459
+ import_node_fs6 = __toESM(require("fs"));
4186
4460
  init_colors();
4461
+ init_types_package();
4187
4462
  __name(write, "write");
4188
4463
  __name(panic, "panic");
4189
4464
  __name(ensureTypeScript, "ensureTypeScript");
@@ -4196,7 +4471,7 @@ var init_common = __esm({
4196
4471
  function findConfigFile(cwd) {
4197
4472
  const locations = CONFIG_FILE_NAMES.map((name) => (0, import_node_path6.join)(cwd, name));
4198
4473
  for (const location of locations) {
4199
- if ((0, import_node_fs5.existsSync)(location)) {
4474
+ if ((0, import_node_fs7.existsSync)(location)) {
4200
4475
  return {
4201
4476
  path: location,
4202
4477
  isTypeScript: /\.ts$/.test(location)
@@ -4213,12 +4488,12 @@ async function loadConfigFile(entrypoint = process.cwd()) {
4213
4488
  loadedConfig = config;
4214
4489
  return config;
4215
4490
  }
4216
- var import_node_fs5, import_node_path6, CONFIG_FILE_NAMES, loadedConfig;
4491
+ var import_node_fs7, import_node_path6, CONFIG_FILE_NAMES, loadedConfig;
4217
4492
  var init_loader = __esm({
4218
4493
  "src/config/loader.ts"() {
4219
4494
  "use strict";
4220
4495
  init_cjs_shims();
4221
- import_node_fs5 = require("fs");
4496
+ import_node_fs7 = require("fs");
4222
4497
  import_node_path6 = require("path");
4223
4498
  init_common();
4224
4499
  init_config();
@@ -4267,13 +4542,59 @@ var init_dev_hooks = __esm({
4267
4542
  }
4268
4543
  });
4269
4544
 
4545
+ // src/events/CommandKitEventsChannel.ts
4546
+ var import_node_events, _CommandKitEventsChannel, CommandKitEventsChannel;
4547
+ var init_CommandKitEventsChannel = __esm({
4548
+ "src/events/CommandKitEventsChannel.ts"() {
4549
+ "use strict";
4550
+ init_cjs_shims();
4551
+ import_node_events = __toESM(require("events"));
4552
+ _CommandKitEventsChannel = class _CommandKitEventsChannel {
4553
+ constructor(commandkit) {
4554
+ this.commandkit = commandkit;
4555
+ }
4556
+ emitter = new import_node_events.default();
4557
+ to(namespace) {
4558
+ return {
4559
+ on: this.on.bind(this, namespace),
4560
+ off: this.off.bind(this, namespace),
4561
+ once: this.once.bind(this, namespace),
4562
+ emit: this.emit.bind(this, namespace),
4563
+ removeAllListeners: this.removeAllListeners.bind(this, namespace)
4564
+ };
4565
+ }
4566
+ on(namespace, event, listener) {
4567
+ this.emitter.on(`${namespace}:${event}`, listener);
4568
+ }
4569
+ off(namespace, event, listener) {
4570
+ this.emitter.off(`${namespace}:${event}`, listener);
4571
+ }
4572
+ once(namespace, event, listener) {
4573
+ this.emitter.once(`${namespace}:${event}`, listener);
4574
+ }
4575
+ emit(namespace, event, ...args) {
4576
+ return this.emitter.emit(`${namespace}:${event}`, ...args);
4577
+ }
4578
+ removeAllListeners(namespace, event) {
4579
+ if (event) {
4580
+ this.emitter.removeAllListeners(`${namespace}:${event}`);
4581
+ } else {
4582
+ this.emitter.removeAllListeners(namespace);
4583
+ }
4584
+ }
4585
+ };
4586
+ __name(_CommandKitEventsChannel, "CommandKitEventsChannel");
4587
+ CommandKitEventsChannel = _CommandKitEventsChannel;
4588
+ }
4589
+ });
4590
+
4270
4591
  // src/CommandKit.ts
4271
- var import_node_events, import_discord12, import_node_path7, import_node_fs6, _started, _CommandKit_instances, init_fn, initCommands_fn, initEvents_fn, _CommandKit, CommandKit;
4592
+ var import_node_events2, import_discord12, import_node_path7, import_node_fs8, _started, _CommandKit_instances, init_fn, initCommands_fn, initEvents_fn, _CommandKit, CommandKit;
4272
4593
  var init_CommandKit = __esm({
4273
4594
  "src/CommandKit.ts"() {
4274
4595
  "use strict";
4275
4596
  init_cjs_shims();
4276
- import_node_events = __toESM(require("events"));
4597
+ import_node_events2 = __toESM(require("events"));
4277
4598
  init_colors();
4278
4599
  init_CacheProvider();
4279
4600
  init_MemoryCache();
@@ -4290,8 +4611,12 @@ var init_CommandKit = __esm({
4290
4611
  init_loader();
4291
4612
  init_constants();
4292
4613
  init_dev_hooks();
4293
- import_node_fs6 = require("fs");
4294
- _CommandKit = class _CommandKit extends import_node_events.default {
4614
+ import_node_fs8 = require("fs");
4615
+ init_CommandKitEventsChannel();
4616
+ init_plugins();
4617
+ init_types_package();
4618
+ init_Logger();
4619
+ _CommandKit = class _CommandKit extends import_node_events2.default {
4295
4620
  /**
4296
4621
  * Create a new command and event handler with CommandKit.
4297
4622
  *
@@ -4332,6 +4657,7 @@ var init_CommandKit = __esm({
4332
4657
  __publicField(this, "commandHandler", new AppCommandHandler(this));
4333
4658
  __publicField(this, "eventHandler", new AppEventsHandler(this));
4334
4659
  __publicField(this, "plugins");
4660
+ __publicField(this, "events", new CommandKitEventsChannel(this));
4335
4661
  options.debugCommands ??= process.env.NODE_ENV !== "production";
4336
4662
  if (options.cacheProvider !== null && (!options.cacheProvider || !(options.cacheProvider instanceof CacheProvider))) {
4337
4663
  options.cacheProvider = new MemoryCache();
@@ -4347,8 +4673,18 @@ var init_CommandKit = __esm({
4347
4673
  * @param token The application token to connect to the discord gateway. If not provided, it will use the `TOKEN` or `DISCORD_TOKEN` environment variable. If set to `false`, it will not login.
4348
4674
  */
4349
4675
  async start(token) {
4350
- await loadConfigFile();
4351
4676
  if (__privateGet(this, _started)) return;
4677
+ if (COMMANDKIT_IS_DEV) {
4678
+ try {
4679
+ registerDevHooks(this);
4680
+ await generateTypesPackage();
4681
+ } catch (e) {
4682
+ if (process.env.COMMANDKIT_DEBUG_TYPEGEN) {
4683
+ Logger.error(e);
4684
+ }
4685
+ }
4686
+ }
4687
+ await this.loadPlugins();
4352
4688
  await __privateMethod(this, _CommandKit_instances, init_fn).call(this);
4353
4689
  this.incrementClientListenersCount();
4354
4690
  if (token !== false && !this.options.client.isReady()) {
@@ -4357,11 +4693,19 @@ var init_CommandKit = __esm({
4357
4693
  );
4358
4694
  }
4359
4695
  __privateSet(this, _started, true);
4360
- if (COMMANDKIT_IS_DEV) {
4361
- registerDevHooks(this);
4362
- }
4363
4696
  await this.commandHandler.registrar.register();
4364
4697
  }
4698
+ /**
4699
+ * Loads all the plugins.
4700
+ */
4701
+ async loadPlugins() {
4702
+ const config = await loadConfigFile();
4703
+ const plugins = config.plugins.filter((p) => isRuntimePlugin(p));
4704
+ if (!plugins.length) return;
4705
+ for (const plugin of plugins) {
4706
+ await this.plugins.softRegisterPlugin(plugin);
4707
+ }
4708
+ }
4365
4709
  /**
4366
4710
  * Whether or not the commandkit application has started.
4367
4711
  */
@@ -4517,8 +4861,15 @@ var init_CommandKit = __esm({
4517
4861
  }, "#init");
4518
4862
  initCommands_fn = /* @__PURE__ */ __name(async function() {
4519
4863
  if (this.commandsRouter.isValidPath()) {
4520
- const commands = await this.commandsRouter.scan();
4521
- await (0, import_node_fs6.writeFileSync)("./commands.json", JSON.stringify(commands, null, 2));
4864
+ const result = await this.commandsRouter.scan();
4865
+ if (COMMANDKIT_IS_DEV) {
4866
+ const visual = this.commandsRouter.visualize();
4867
+ Logger.info(visual);
4868
+ (0, import_node_fs8.writeFileSync)(
4869
+ "./.commandkit/commands.json",
4870
+ JSON.stringify(result, null, 2)
4871
+ );
4872
+ }
4522
4873
  }
4523
4874
  await this.commandHandler.loadCommands();
4524
4875
  }, "#initCommands");
@@ -4547,7 +4898,7 @@ var init_version = __esm({
4547
4898
  "use strict";
4548
4899
  init_cjs_shims();
4549
4900
  version = /* @__MACRO__ $version */
4550
- "0.1.11-dev.20250306152436";
4901
+ "0.1.11-dev.20250308073818";
4551
4902
  }
4552
4903
  });
4553
4904
 
@@ -4589,7 +4940,7 @@ function getStdio(supportsCommands) {
4589
4940
  }
4590
4941
  function createAppProcess(fileName, cwd, isDev) {
4591
4942
  var _a, _b;
4592
- if (!(0, import_node_fs7.existsSync)((0, import_node_path8.join)(cwd, fileName))) {
4943
+ if (!(0, import_node_fs9.existsSync)((0, import_node_path8.join)(cwd, fileName))) {
4593
4944
  panic(`Could not locate the entrypoint file: ${fileName}`);
4594
4945
  }
4595
4946
  const stdio = getStdio(isDev);
@@ -4611,7 +4962,7 @@ function createAppProcess(fileName, cwd, isDev) {
4611
4962
  (_b = ps.stderr) == null ? void 0 : _b.pipe(process.stderr);
4612
4963
  return ps;
4613
4964
  }
4614
- var import_node_child_process, import_node_path8, import_node_fs7;
4965
+ var import_node_child_process, import_node_path8, import_node_fs9;
4615
4966
  var init_app_process = __esm({
4616
4967
  "src/cli/app-process.ts"() {
4617
4968
  "use strict";
@@ -4619,7 +4970,7 @@ var init_app_process = __esm({
4619
4970
  import_node_child_process = require("child_process");
4620
4971
  init_env();
4621
4972
  import_node_path8 = require("path");
4622
- import_node_fs7 = require("fs");
4973
+ import_node_fs9 = require("fs");
4623
4974
  init_common();
4624
4975
  __name(getStdio, "getStdio");
4625
4976
  __name(createAppProcess, "createAppProcess");
@@ -4706,9 +5057,9 @@ async function main() {
4706
5057
  await main();
4707
5058
  `;
4708
5059
  const dist = isDev ? ".commandkit" : distDir || "dist";
4709
- await (0, import_promises5.writeFile)((0, import_node_path9.join)(configPath, dist, "index.js"), code);
5060
+ await (0, import_promises6.writeFile)((0, import_node_path9.join)(configPath, dist, "index.js"), code);
4710
5061
  }
4711
- var import_tsup, import_promises5, import_node_path9, envScript, requireScript, antiCrashScript, wrapInAsyncIIFE;
5062
+ var import_tsup, import_promises6, import_node_path9, envScript, requireScript, antiCrashScript, wrapInAsyncIIFE;
4712
5063
  var init_build = __esm({
4713
5064
  "src/cli/build.ts"() {
4714
5065
  "use strict";
@@ -4716,7 +5067,7 @@ var init_build = __esm({
4716
5067
  import_tsup = require("tsup");
4717
5068
  init_plugins();
4718
5069
  init_loader();
4719
- import_promises5 = require("fs/promises");
5070
+ import_promises6 = require("fs/promises");
4720
5071
  import_node_path9 = require("path");
4721
5072
  init_env();
4722
5073
  __name(buildApplication, "buildApplication");
@@ -4861,7 +5212,7 @@ async function bootstrapDevelopmentServer(configPath) {
4861
5212
  watcher.on("add", hmrHandler);
4862
5213
  watcher.on("unlink", hmrHandler);
4863
5214
  watcher.on("unlinkDir", (path3) => {
4864
- const hasChild = (0, import_node_fs8.readdirSync)(path3).length > 0;
5215
+ const hasChild = (0, import_node_fs10.readdirSync)(path3).length > 0;
4865
5216
  if (hasChild) return hmrHandler(path3);
4866
5217
  });
4867
5218
  watcher.on("error", (e) => {
@@ -4880,7 +5231,7 @@ ${colors_default.yellowBright("re")} - Reload all events
4880
5231
  ${colors_default.yellowBright("rl")} - Reload all locales`
4881
5232
  );
4882
5233
  }
4883
- var import_path2, import_chokidar, import_node_fs8, isCommandSource, isEventSource, isLocaleSource;
5234
+ var import_path2, import_chokidar, import_node_fs10, isCommandSource, isEventSource, isLocaleSource;
4884
5235
  var init_development = __esm({
4885
5236
  "src/cli/development.ts"() {
4886
5237
  "use strict";
@@ -4891,7 +5242,7 @@ var init_development = __esm({
4891
5242
  init_app_process();
4892
5243
  init_build();
4893
5244
  import_chokidar = require("chokidar");
4894
- import_node_fs8 = require("fs");
5245
+ import_node_fs10 = require("fs");
4895
5246
  init_utilities();
4896
5247
  init_colors();
4897
5248
  __name(buildAndStart, "buildAndStart");
@@ -4978,7 +5329,7 @@ __export(generators_exports, {
4978
5329
  });
4979
5330
  async function generateCommand(name, customPath) {
4980
5331
  const cmdPath = (0, import_path4.join)(customPath || COMMANDS_DIR, name);
4981
- if (!(0, import_fs2.existsSync)(cmdPath)) await (0, import_promises6.mkdir)(cmdPath, { recursive: true });
5332
+ if (!(0, import_fs2.existsSync)(cmdPath)) await (0, import_promises7.mkdir)(cmdPath, { recursive: true });
4982
5333
  if ((0, import_fs2.existsSync)((0, import_path4.join)(cmdPath, "command.ts"))) {
4983
5334
  panic(`Command ${name} already exists.`);
4984
5335
  }
@@ -4998,7 +5349,7 @@ export const message: MessageCommand = async (ctx) => {
4998
5349
  await ctx.message.reply('Hello from ${name}!');
4999
5350
  };
5000
5351
  `.trim();
5001
- await (0, import_promises6.writeFile)((0, import_path4.join)(cmdPath, "command.ts"), commandFile);
5352
+ await (0, import_promises7.writeFile)((0, import_path4.join)(cmdPath, "command.ts"), commandFile);
5002
5353
  console.log(
5003
5354
  colors_default.green(
5004
5355
  `Command ${colors_default.magenta(name)} created at ${colors_default.blue(formatPath(cmdPath))}/command.ts`
@@ -5007,10 +5358,10 @@ export const message: MessageCommand = async (ctx) => {
5007
5358
  }
5008
5359
  async function generateEvent(name, customPath) {
5009
5360
  const eventPath = (0, import_path4.join)(customPath || EVENTS_DIR, name);
5010
- if (!(0, import_fs2.existsSync)(eventPath)) await (0, import_promises6.mkdir)(eventPath, { recursive: true });
5361
+ if (!(0, import_fs2.existsSync)(eventPath)) await (0, import_promises7.mkdir)(eventPath, { recursive: true });
5011
5362
  let filename = "event.ts";
5012
5363
  if ((0, import_fs2.existsSync)((0, import_path4.join)(eventPath, filename))) {
5013
- const count = (await (0, import_promises6.readdir)(eventPath)).length;
5364
+ const count = (await (0, import_promises7.readdir)(eventPath)).length;
5014
5365
  filename = `${String(count).padStart(2, "0")}_${filename}`;
5015
5366
  }
5016
5367
  const eventFile = `
@@ -5018,7 +5369,7 @@ export default async function on${name[0].toUpperCase() + name.slice(1)}() {
5018
5369
  console.log('${name} event fired!');
5019
5370
  };
5020
5371
  `.trim();
5021
- await (0, import_promises6.writeFile)((0, import_path4.join)(eventPath, filename), eventFile);
5372
+ await (0, import_promises7.writeFile)((0, import_path4.join)(eventPath, filename), eventFile);
5022
5373
  console.log(
5023
5374
  colors_default.green(
5024
5375
  `Event ${colors_default.magenta(name)} created at ${colors_default.blue(formatPath(eventPath))}/${colors_default.magenta(filename)}`
@@ -5036,7 +5387,7 @@ async function generateLocale(locale, commandName, customPath) {
5036
5387
  panic("Command name is required.");
5037
5388
  }
5038
5389
  const localePath = (0, import_path4.join)(customPath || LOCALES_DIR, locale);
5039
- if (!(0, import_fs2.existsSync)(localePath)) await (0, import_promises6.mkdir)(localePath, { recursive: true });
5390
+ if (!(0, import_fs2.existsSync)(localePath)) await (0, import_promises7.mkdir)(localePath, { recursive: true });
5040
5391
  if ((0, import_fs2.existsSync)((0, import_path4.join)(localePath, `${commandName}.json`))) {
5041
5392
  panic(`Locale file for ${commandName} already exists.`);
5042
5393
  }
@@ -5047,7 +5398,7 @@ async function generateLocale(locale, commandName, customPath) {
5047
5398
  },
5048
5399
  translations: {}
5049
5400
  };
5050
- await (0, import_promises6.writeFile)(
5401
+ await (0, import_promises7.writeFile)(
5051
5402
  (0, import_path4.join)(localePath, `${commandName}.json`),
5052
5403
  JSON.stringify(localeFile, null, 2)
5053
5404
  );
@@ -5059,12 +5410,12 @@ async function generateLocale(locale, commandName, customPath) {
5059
5410
  )
5060
5411
  );
5061
5412
  }
5062
- var import_promises6, import_path4, import_discord13, import_fs2, BASE_PATH, COMMANDS_DIR, EVENTS_DIR, LOCALES_DIR, formatPath;
5413
+ var import_promises7, import_path4, import_discord13, import_fs2, BASE_PATH, COMMANDS_DIR, EVENTS_DIR, LOCALES_DIR, formatPath;
5063
5414
  var init_generators = __esm({
5064
5415
  "src/cli/generators.ts"() {
5065
5416
  "use strict";
5066
5417
  init_cjs_shims();
5067
- import_promises6 = require("fs/promises");
5418
+ import_promises7 = require("fs/promises");
5068
5419
  import_path4 = require("path");
5069
5420
  import_discord13 = require("discord.js");
5070
5421
  init_common();