commandkit 0.1.6-dev.20231124164249 → 0.1.6-dev.20231124164938

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/bin/build.mjs CHANGED
@@ -1,11 +1,20 @@
1
1
  // @ts-check
2
2
 
3
3
  import { build } from 'tsup';
4
- import { Colors, erase, findCommandKitJSON, panic, write } from './common.mjs';
4
+ import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
5
5
  import ora from 'ora';
6
+ import { appendFile } from 'node:fs/promises';
7
+ import { join } from 'node:path';
6
8
 
7
9
  export async function bootstrapProductionBuild(config) {
8
- const { minify = false, outDir = 'dist', main, src } = findCommandKitJSON(config);
10
+ const {
11
+ sourcemap = false,
12
+ minify = false,
13
+ outDir = 'dist',
14
+ antiCrash = true,
15
+ src,
16
+ main,
17
+ } = await findCommandKitConfig(config);
9
18
 
10
19
  const status = ora('Creating optimized production build...\n').start();
11
20
  const start = performance.now();
@@ -21,23 +30,23 @@ export async function bootstrapProductionBuild(config) {
21
30
  minify,
22
31
  shims: true,
23
32
  banner: {
24
- js: '/* Optimized production build of your project, generated by CommandKit */',
33
+ js: '/* Optimized production build generated by CommandKit */',
25
34
  },
26
- sourcemap: false,
35
+ sourcemap,
27
36
  keepNames: true,
28
37
  outDir,
29
38
  silent: true,
30
- entry: [src, '!dist', '!.commandkit'],
39
+ entry: [src, '!dist', '!.commandkit', `!${outDir}`],
31
40
  });
32
41
 
42
+ if (antiCrash) await injectAntiCrash(outDir, main);
43
+
33
44
  status.succeed(
34
45
  Colors.green(`Build completed in ${(performance.now() - start).toFixed(2)}ms!`),
35
46
  );
36
47
  write(
37
48
  Colors.green(
38
- `\nRun ${Colors.magenta(`node ${outDir}/${main}`)} ${Colors.green(
39
- 'to start your bot.',
40
- )}`,
49
+ `\nRun ${Colors.magenta(`commandkit start`)} ${Colors.green('to start your bot.')}`,
41
50
  ),
42
51
  );
43
52
  } catch (e) {
@@ -45,3 +54,28 @@ export async function bootstrapProductionBuild(config) {
45
54
  panic(e);
46
55
  }
47
56
  }
57
+
58
+ function injectAntiCrash(outDir, main) {
59
+ const path = join(process.cwd(), outDir, main);
60
+ const snippet = [
61
+ '\n\n// --- CommandKit Anti-Crash Monitor ---',
62
+ ';(()=>{',
63
+ " 'use strict';",
64
+ " // 'uncaughtException' event is supposed to be used to perform synchronous cleanup before shutting down the process",
65
+ ' // instead of using it as a means to resume operation.',
66
+ ' // But it exists here due to compatibility reasons with discord bot ecosystem.',
67
+ " const p = (t) => `\\x1b[33m${t}\\x1b[0m`, b = '[CommandKit Anti-Crash Monitor]', l = console.log, e1 = 'uncaughtException', e2 = 'unhandledRejection';",
68
+ ' if (!process.eventNames().includes(e1)) // skip if it is already handled',
69
+ ' process.on(e1, (e, o) => {',
70
+ ' l(p(`${b} Uncaught Exception`)); l(p(b), p(e.stack || e));',
71
+ ' })',
72
+ ' if (!process.eventNames().includes(e2)) // skip if it is already handled',
73
+ ' process.on(e2, (r) => {',
74
+ ' l(p(`${b} Unhandled promise rejection`)); l(p(`${b} ${r.stack || r}`));',
75
+ ' });',
76
+ '})();',
77
+ '// --- CommandKit Anti-Crash Monitor ---\n',
78
+ ].join('\n');
79
+
80
+ return appendFile(path, snippet);
81
+ }
package/bin/common.mjs CHANGED
@@ -58,15 +58,43 @@ export function findPackageJSON() {
58
58
  return JSON.parse(fs.readFileSync(target, 'utf8'));
59
59
  }
60
60
 
61
- export function findCommandKitJSON(src) {
61
+ const possibleFileNames = [
62
+ 'commandkit.json',
63
+ 'commandkit.config.json',
64
+ 'commandkit.js',
65
+ 'commandkit.config.js',
66
+ 'commandkit.mjs',
67
+ 'commandkit.config.mjs',
68
+ 'commandkit.cjs',
69
+ 'commandkit.config.cjs',
70
+ ];
71
+
72
+ export async function findCommandKitConfig(src) {
62
73
  const cwd = process.cwd();
63
- const target = src || join(cwd, 'commandkit.json');
74
+ const locations = src ? [join(cwd, src)] : possibleFileNames.map((name) => join(cwd, name));
64
75
 
65
- if (!fs.existsSync(target)) {
66
- panic('Could not find commandkit.json in current directory.');
76
+ for (const location of locations) {
77
+ try {
78
+ return await loadConfigInner(location);
79
+ } catch (e) {
80
+ continue;
81
+ }
67
82
  }
68
83
 
69
- return JSON.parse(fs.readFileSync(target, 'utf8'));
84
+ panic('Could not locate commandkit config.');
85
+ }
86
+
87
+ async function loadConfigInner(target) {
88
+ const isJSON = target.endsWith('.json');
89
+
90
+ /**
91
+ * @type {import('..').CommandKitConfig}
92
+ */
93
+ const config = await import(`file://${target}`, {
94
+ assert: isJSON ? { type: 'json' } : undefined,
95
+ }).then((conf) => conf.default || conf);
96
+
97
+ return config;
70
98
  }
71
99
 
72
100
  export function erase(dir) {
@@ -0,0 +1,163 @@
1
+ // @ts-check
2
+ import { config as dotenv } from 'dotenv';
3
+ import { build } from 'tsup';
4
+ import child_process from 'node:child_process';
5
+ import ora from 'ora';
6
+ import { join } from 'node:path';
7
+ import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
8
+ import { parseEnv } from './parse-env.mjs';
9
+
10
+ const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
11
+ const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;
12
+
13
+ export async function bootstrapDevelopmentServer(opts) {
14
+ const {
15
+ src,
16
+ main,
17
+ watch = Boolean(opts.noWatch),
18
+ nodeOptions = [],
19
+ envExtra = true,
20
+ clearRestartLogs = true,
21
+ outDir,
22
+ } = await findCommandKitConfig(opts.config);
23
+
24
+ if (!src) {
25
+ panic('Could not find src in commandkit.json');
26
+ }
27
+
28
+ if (!main) {
29
+ panic('Could not find main in commandkit.json');
30
+ }
31
+
32
+ const watchMode = watch;
33
+ const status = ora(Colors.green('Starting a development server...\n')).start();
34
+ const start = performance.now();
35
+
36
+ if (watchMode && !nodeOptions.includes('--watch')) {
37
+ nodeOptions.push('--watch');
38
+ } else if (!watchMode && nodeOptions.includes('--watch')) {
39
+ nodeOptions.splice(nodeOptions.indexOf('--watch'), 1);
40
+ }
41
+
42
+ if (!nodeOptions.includes('--enable-source-maps')) {
43
+ nodeOptions.push('--enable-source-maps');
44
+ }
45
+
46
+ erase('.commandkit');
47
+
48
+ try {
49
+ await build({
50
+ clean: true,
51
+ format: ['esm'],
52
+ dts: false,
53
+ skipNodeModulesBundle: true,
54
+ minify: false,
55
+ shims: true,
56
+ sourcemap: 'inline',
57
+ keepNames: true,
58
+ outDir: '.commandkit',
59
+ silent: true,
60
+ entry: [src, '!dist', '!.commandkit', `!${outDir}`].filter(Boolean),
61
+ watch: watchMode,
62
+ });
63
+
64
+ status.succeed(
65
+ Colors.green(`Dev server started in ${(performance.now() - start).toFixed(2)}ms!\n`),
66
+ );
67
+
68
+ if (watchMode) write(Colors.cyan('Watching for file changes...\n'));
69
+
70
+ const processEnv = {};
71
+
72
+ const env = dotenv({
73
+ path: join(process.cwd(), '.env'),
74
+ // @ts-expect-error
75
+ processEnv,
76
+ });
77
+
78
+ if (envExtra) {
79
+ parseEnv(processEnv);
80
+ }
81
+
82
+ if (env.error) {
83
+ write(Colors.yellow(`[DOTENV] Warning: ${env.error.message}`));
84
+ }
85
+
86
+ if (env.parsed) {
87
+ write(Colors.blue('[DOTENV] Loaded .env file!'));
88
+ }
89
+
90
+ /**
91
+ * @type {child_process.ChildProcessWithoutNullStreams}
92
+ */
93
+ const ps = child_process.spawn(
94
+ 'node',
95
+ [...nodeOptions, join(process.cwd(), '.commandkit', main)],
96
+ {
97
+ env: {
98
+ ...process.env,
99
+ ...processEnv,
100
+ NODE_ENV: 'development',
101
+ // @ts-expect-error
102
+ COMMANDKIT_DEV: true,
103
+ // @ts-expect-error
104
+ COMMANDKIT_PRODUCTION: false,
105
+ },
106
+ cwd: process.cwd(),
107
+ },
108
+ );
109
+
110
+ let isLastLogRestarting = false,
111
+ hasStarted = false;
112
+
113
+ ps.stdout.on('data', (data) => {
114
+ const message = data.toString();
115
+
116
+ if (FAILED_RUNNING_PATTERN.test(message)) {
117
+ write(Colors.cyan('Failed running the bot, waiting for changes...'));
118
+ isLastLogRestarting = false;
119
+ if (!hasStarted) hasStarted = true;
120
+ return;
121
+ }
122
+
123
+ if (clearRestartLogs && !RESTARTING_MSG_PATTERN.test(message)) {
124
+ write(message);
125
+ isLastLogRestarting = false;
126
+ } else {
127
+ if (isLastLogRestarting || !hasStarted) {
128
+ if (!hasStarted) hasStarted = true;
129
+ return;
130
+ }
131
+ write(Colors.cyan('⌀ Restarting the bot...'));
132
+ isLastLogRestarting = true;
133
+ }
134
+
135
+ if (!hasStarted) hasStarted = true;
136
+ });
137
+
138
+ ps.stderr.on('data', (data) => {
139
+ const message = data.toString();
140
+
141
+ if (
142
+ message.includes(
143
+ 'ExperimentalWarning: Watch mode is an experimental feature and might change at any time',
144
+ )
145
+ )
146
+ return;
147
+
148
+ write(Colors.red(message));
149
+ });
150
+
151
+ ps.on('close', (code) => {
152
+ write('\n');
153
+ process.exit(code ?? 0);
154
+ });
155
+
156
+ ps.on('error', (err) => {
157
+ panic(err);
158
+ });
159
+ } catch (e) {
160
+ status.fail(`Error occurred after ${(performance.now() - start).toFixed(2)}ms!\n`);
161
+ panic(e.stack ?? e);
162
+ }
163
+ }
package/bin/index.mjs CHANGED
@@ -3,18 +3,28 @@
3
3
  // @ts-check
4
4
 
5
5
  import { Command } from 'commander';
6
- import { bootstrapDevelopmentServer } from './dev-server.mjs';
6
+ import { bootstrapDevelopmentServer } from './development.mjs';
7
7
  import { bootstrapProductionBuild } from './build.mjs';
8
+ import { bootstrapProductionServer } from './production.mjs';
8
9
 
9
10
  const program = new Command('commandkit');
10
11
 
11
12
  program
12
13
  .command('dev')
13
14
  .description('Start your bot in development mode.')
14
- .option('-c, --config <path>', 'Path to your commandkit.json file.', './commandkit.json')
15
+ .option('-c, --config <path>', 'Path to your commandkit config file.', './commandkit.js')
16
+ .action(() => {
17
+ const options = program.opts();
18
+ bootstrapDevelopmentServer(options);
19
+ });
20
+
21
+ program
22
+ .command('start')
23
+ .description('Start your bot in production mode after running the build command.')
24
+ .option('-c, --config <path>', 'Path to your commandkit.json file.', './commandkit.js')
15
25
  .action(() => {
16
26
  const options = program.opts();
17
- bootstrapDevelopmentServer(options.config);
27
+ bootstrapProductionServer(options.config);
18
28
  });
19
29
 
20
30
  program
@@ -0,0 +1,52 @@
1
+ // @ts-check
2
+
3
+ import { randomUUID } from 'node:crypto';
4
+
5
+ const valuesMap = {
6
+ true: true,
7
+ false: false,
8
+ null: null,
9
+ undefined: undefined,
10
+ __UUIDv4__: () => randomUUID(),
11
+ };
12
+
13
+ const VALUE_PREFIXES = {
14
+ JSON: 'JSON::',
15
+ DATE: 'DATE::',
16
+ };
17
+
18
+ export function parseEnv(src) {
19
+ for (const key in src) {
20
+ const value = src[key];
21
+
22
+ if (typeof value !== 'string') continue;
23
+
24
+ if (value.startsWith(VALUE_PREFIXES.JSON)) {
25
+ src[key] = JSON.parse(value.replace(VALUE_PREFIXES.JSON, ''));
26
+ continue;
27
+ }
28
+
29
+ if (value.startsWith(VALUE_PREFIXES.DATE)) {
30
+ src[key] = new Date(value.replace(VALUE_PREFIXES.DATE, ''));
31
+ continue;
32
+ }
33
+
34
+ if (value.includes(',')) {
35
+ src[key] = value.split(',').map((v) => v.trim());
36
+ continue;
37
+ }
38
+
39
+ if (/^[0-9]+n$/.test(value)) {
40
+ src[key] = BigInt(value);
41
+ continue;
42
+ }
43
+
44
+ if (value in valuesMap) {
45
+ src[key] =
46
+ typeof valuesMap[value] === 'function' ? valuesMap[value]() : valuesMap[value];
47
+ continue;
48
+ }
49
+ }
50
+
51
+ return src;
52
+ }
@@ -0,0 +1,83 @@
1
+ // @ts-check
2
+ import { config as dotenv } from 'dotenv';
3
+ import child_process from 'node:child_process';
4
+ import { join } from 'node:path';
5
+ import { Colors, findCommandKitConfig, panic, write } from './common.mjs';
6
+ import { existsSync } from 'node:fs';
7
+ import { parseEnv } from './parse-env.mjs';
8
+
9
+ export async function bootstrapProductionServer(config) {
10
+ const {
11
+ main,
12
+ outDir = 'dist',
13
+ envExtra = true,
14
+ sourcemap,
15
+ } = await findCommandKitConfig(config);
16
+
17
+ if (!existsSync(join(process.cwd(), outDir, main))) {
18
+ panic('Could not find production build, maybe run `commandkit build` first?');
19
+ }
20
+
21
+ try {
22
+ const processEnv = {};
23
+
24
+ const env = dotenv({
25
+ path: join(process.cwd(), '.env'),
26
+ // @ts-expect-error
27
+ processEnv,
28
+ });
29
+
30
+ if (envExtra) {
31
+ parseEnv(processEnv);
32
+ }
33
+
34
+ if (env.error) {
35
+ write(Colors.yellow(`[DOTENV] Warning: ${env.error.message}`));
36
+ }
37
+
38
+ if (env.parsed) {
39
+ write(Colors.blue('[DOTENV] Loaded .env file!'));
40
+ }
41
+
42
+ /**
43
+ * @type {child_process.ChildProcessWithoutNullStreams}
44
+ */
45
+ const ps = child_process.spawn(
46
+ 'node',
47
+ [sourcemap ? '--enable-source-maps' : '', join(process.cwd(), outDir, main)].filter(
48
+ Boolean,
49
+ ),
50
+ {
51
+ env: {
52
+ ...process.env,
53
+ ...processEnv,
54
+ NODE_ENV: 'production',
55
+ // @ts-expect-error
56
+ COMMANDKIT_DEV: false,
57
+ // @ts-expect-error
58
+ COMMANDKIT_PROD: true,
59
+ },
60
+ cwd: process.cwd(),
61
+ },
62
+ );
63
+
64
+ ps.stdout.on('data', (data) => {
65
+ write(data.toString());
66
+ });
67
+
68
+ ps.stderr.on('data', (data) => {
69
+ write(Colors.red(data.toString()));
70
+ });
71
+
72
+ ps.on('close', (code) => {
73
+ write('\n');
74
+ process.exit(code ?? 0);
75
+ });
76
+
77
+ ps.on('error', (err) => {
78
+ panic(err);
79
+ });
80
+ } catch (e) {
81
+ panic(e);
82
+ }
83
+ }
package/dist/index.d.mts CHANGED
@@ -362,6 +362,54 @@ declare class ButtonKit extends ButtonBuilder {
362
362
  onClick(handler: CommandKitButtonBuilderInteractionCollectorDispatch, data: CommandKitButtonBuilderInteractionCollectorDispatchContextData): this;
363
363
  }
364
364
 
365
+ interface CommandKitConfig {
366
+ /**
367
+ * The source directory of the project.
368
+ */
369
+ src: string;
370
+ /**
371
+ * The main "javascript" file of the project.
372
+ */
373
+ main: string;
374
+ /**
375
+ * Whether or not to use the watch mode. Defaults to `true`.
376
+ */
377
+ watch: boolean;
378
+ /**
379
+ * The output directory of the project. Defaults to `dist`.
380
+ */
381
+ outDir: string;
382
+ /**
383
+ * Whether or not to include extra env utilities. Defaults to `true`.
384
+ */
385
+ envExtra: boolean;
386
+ /**
387
+ * Node.js cli options.
388
+ */
389
+ nodeOptions: string[];
390
+ /**
391
+ * Whether or not to clear default restart logs. Defaults to `true`.
392
+ */
393
+ clearRestartLogs: boolean;
394
+ /**
395
+ * Whether or not to minify the output. Defaults to `false`.
396
+ */
397
+ minify: boolean;
398
+ /**
399
+ * Whether or not to include sourcemaps in production build. Defaults to `false`.
400
+ */
401
+ sourcemap: boolean | 'inline';
402
+ /**
403
+ * Whether or not to include anti-crash handler in production. Defaults to `true`.
404
+ */
405
+ antiCrash: boolean;
406
+ }
407
+ declare function getConfig(): CommandKitConfig;
408
+ declare const requiredProps: readonly ["src", "main"];
409
+ type R = (typeof requiredProps)[number];
410
+ type PartialConfig<T extends CommandKitConfig> = Partial<Omit<T, R>> & Pick<T, R>;
411
+ declare function defineConfig(config: PartialConfig<CommandKitConfig>): Partial<CommandKitConfig>;
412
+
365
413
  type CommandKitEffectCallback = () => void;
366
414
  type CommandKitSignalInitializer<T> = T | (() => T);
367
415
  type CommandKitSignalUpdater<T> = T | ((prev: T) => T);
@@ -382,4 +430,4 @@ declare function createSignal<T = unknown>(value?: CommandKitSignalInitializer<T
382
430
  */
383
431
  declare function createEffect(callback: CommandKitEffectCallback): void;
384
432
 
385
- export { ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, CommandType, ContextMenuCommandProps, ReloadType, SlashCommandProps, ValidationFunctionProps, createEffect, createSignal };
433
+ export { ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitConfig, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, CommandType, ContextMenuCommandProps, ReloadType, SlashCommandProps, ValidationFunctionProps, createEffect, createSignal, defineConfig, getConfig };
package/dist/index.d.ts CHANGED
@@ -362,6 +362,54 @@ declare class ButtonKit extends ButtonBuilder {
362
362
  onClick(handler: CommandKitButtonBuilderInteractionCollectorDispatch, data: CommandKitButtonBuilderInteractionCollectorDispatchContextData): this;
363
363
  }
364
364
 
365
+ interface CommandKitConfig {
366
+ /**
367
+ * The source directory of the project.
368
+ */
369
+ src: string;
370
+ /**
371
+ * The main "javascript" file of the project.
372
+ */
373
+ main: string;
374
+ /**
375
+ * Whether or not to use the watch mode. Defaults to `true`.
376
+ */
377
+ watch: boolean;
378
+ /**
379
+ * The output directory of the project. Defaults to `dist`.
380
+ */
381
+ outDir: string;
382
+ /**
383
+ * Whether or not to include extra env utilities. Defaults to `true`.
384
+ */
385
+ envExtra: boolean;
386
+ /**
387
+ * Node.js cli options.
388
+ */
389
+ nodeOptions: string[];
390
+ /**
391
+ * Whether or not to clear default restart logs. Defaults to `true`.
392
+ */
393
+ clearRestartLogs: boolean;
394
+ /**
395
+ * Whether or not to minify the output. Defaults to `false`.
396
+ */
397
+ minify: boolean;
398
+ /**
399
+ * Whether or not to include sourcemaps in production build. Defaults to `false`.
400
+ */
401
+ sourcemap: boolean | 'inline';
402
+ /**
403
+ * Whether or not to include anti-crash handler in production. Defaults to `true`.
404
+ */
405
+ antiCrash: boolean;
406
+ }
407
+ declare function getConfig(): CommandKitConfig;
408
+ declare const requiredProps: readonly ["src", "main"];
409
+ type R = (typeof requiredProps)[number];
410
+ type PartialConfig<T extends CommandKitConfig> = Partial<Omit<T, R>> & Pick<T, R>;
411
+ declare function defineConfig(config: PartialConfig<CommandKitConfig>): Partial<CommandKitConfig>;
412
+
365
413
  type CommandKitEffectCallback = () => void;
366
414
  type CommandKitSignalInitializer<T> = T | (() => T);
367
415
  type CommandKitSignalUpdater<T> = T | ((prev: T) => T);
@@ -382,4 +430,4 @@ declare function createSignal<T = unknown>(value?: CommandKitSignalInitializer<T
382
430
  */
383
431
  declare function createEffect(callback: CommandKitEffectCallback): void;
384
432
 
385
- export { ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, CommandType, ContextMenuCommandProps, ReloadType, SlashCommandProps, ValidationFunctionProps, createEffect, createSignal };
433
+ export { ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitConfig, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, CommandType, ContextMenuCommandProps, ReloadType, SlashCommandProps, ValidationFunctionProps, createEffect, createSignal, defineConfig, getConfig };
package/dist/index.js CHANGED
@@ -33,7 +33,9 @@ __export(src_exports, {
33
33
  ButtonKit: () => ButtonKit,
34
34
  CommandKit: () => CommandKit,
35
35
  createEffect: () => createEffect,
36
- createSignal: () => createSignal
36
+ createSignal: () => createSignal,
37
+ defineConfig: () => defineConfig,
38
+ getConfig: () => getConfig
37
39
  });
38
40
  module.exports = __toCommonJS(src_exports);
39
41
 
@@ -977,6 +979,34 @@ var ButtonKit = class extends import_discord2.ButtonBuilder {
977
979
  }
978
980
  };
979
981
 
982
+ // src/config.ts
983
+ var globalConfig = {
984
+ envExtra: true,
985
+ outDir: "dist",
986
+ watch: true,
987
+ clearRestartLogs: true,
988
+ minify: false,
989
+ sourcemap: false,
990
+ nodeOptions: [],
991
+ antiCrash: true
992
+ };
993
+ function getConfig() {
994
+ return globalConfig;
995
+ }
996
+ var requiredProps = ["src", "main"];
997
+ function defineConfig(config) {
998
+ for (const prop of requiredProps) {
999
+ if (!config[prop]) {
1000
+ throw new Error(`[CommandKit Config] Missing required config property: ${prop}`);
1001
+ }
1002
+ }
1003
+ globalConfig = {
1004
+ ...globalConfig,
1005
+ ...config
1006
+ };
1007
+ return globalConfig;
1008
+ }
1009
+
980
1010
  // src/utils/signal.ts
981
1011
  var context = [];
982
1012
  function createSignal(value) {
@@ -1024,5 +1054,7 @@ function getCurrentObserver() {
1024
1054
  ButtonKit,
1025
1055
  CommandKit,
1026
1056
  createEffect,
1027
- createSignal
1057
+ createSignal,
1058
+ defineConfig,
1059
+ getConfig
1028
1060
  });
package/dist/index.mjs CHANGED
@@ -950,6 +950,34 @@ var ButtonKit = class extends ButtonBuilder {
950
950
  }
951
951
  };
952
952
 
953
+ // src/config.ts
954
+ var globalConfig = {
955
+ envExtra: true,
956
+ outDir: "dist",
957
+ watch: true,
958
+ clearRestartLogs: true,
959
+ minify: false,
960
+ sourcemap: false,
961
+ nodeOptions: [],
962
+ antiCrash: true
963
+ };
964
+ function getConfig() {
965
+ return globalConfig;
966
+ }
967
+ var requiredProps = ["src", "main"];
968
+ function defineConfig(config) {
969
+ for (const prop of requiredProps) {
970
+ if (!config[prop]) {
971
+ throw new Error(`[CommandKit Config] Missing required config property: ${prop}`);
972
+ }
973
+ }
974
+ globalConfig = {
975
+ ...globalConfig,
976
+ ...config
977
+ };
978
+ return globalConfig;
979
+ }
980
+
953
981
  // src/utils/signal.ts
954
982
  var context = [];
955
983
  function createSignal(value) {
@@ -996,5 +1024,7 @@ export {
996
1024
  ButtonKit,
997
1025
  CommandKit,
998
1026
  createEffect,
999
- createSignal
1027
+ createSignal,
1028
+ defineConfig,
1029
+ getConfig
1000
1030
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "commandkit",
3
3
  "description": "Beginner friendly command & event handler for Discord.js",
4
- "version": "0.1.6-dev.20231124164249",
4
+ "version": "0.1.6-dev.20231124164938",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
@@ -24,8 +24,9 @@
24
24
  "build": "tsup",
25
25
  "deploy": "npm publish",
26
26
  "deploy-dev": "npm publish --access public --tag dev",
27
- "test": "tsx tests/index.ts",
28
- "test:watch": "tsx watch tests/index.ts"
27
+ "test": "cd ./tests && node ../bin/index.mjs dev",
28
+ "test:build": "cd ./tests && node ../bin/index.mjs build",
29
+ "test:prod": "cd ./tests && node ../bin/index.mjs start"
29
30
  },
30
31
  "repository": {
31
32
  "url": "git+https://github.com/underctrl-io/commandkit.git"
@@ -1,91 +0,0 @@
1
- // @ts-check
2
- import { config as dotenv } from 'dotenv';
3
- import { build } from 'tsup';
4
- import child_process from 'node:child_process';
5
- import ora from 'ora';
6
- import { join } from 'node:path';
7
- import { Colors, erase, findCommandKitJSON, panic, write } from './common.mjs';
8
-
9
- export async function bootstrapDevelopmentServer(config) {
10
- const { src, main = 'index.mjs', nodeOptions = ['--watch'] } = findCommandKitJSON(config);
11
-
12
- if (!src) {
13
- panic('Could not find src in commandkit.json');
14
- }
15
-
16
- const status = ora(Colors.green('Starting a development server...\n')).start();
17
- const start = performance.now();
18
-
19
- erase('.commandkit');
20
-
21
- try {
22
- await build({
23
- clean: true,
24
- format: ['esm'],
25
- dts: false,
26
- skipNodeModulesBundle: true,
27
- minify: false,
28
- shims: true,
29
- sourcemap: false,
30
- keepNames: true,
31
- outDir: '.commandkit',
32
- silent: true,
33
- entry: [src, '!dist', '!.commandkit'],
34
- watch: nodeOptions.includes('--watch'),
35
- });
36
-
37
- status.succeed(
38
- Colors.green(`Server started in ${(performance.now() - start).toFixed(2)}ms!\n`),
39
- );
40
-
41
- const processEnv = {};
42
-
43
- const env = dotenv({
44
- path: join(process.cwd(), '.env'),
45
- // @ts-expect-error
46
- processEnv,
47
- });
48
-
49
- if (env.error) {
50
- write(Colors.yellow(`[DOTENV] Warning: ${env.error.message}`));
51
- }
52
-
53
- if (env.parsed) {
54
- write(Colors.blue('[DOTENV] Loaded .env file!'));
55
- }
56
-
57
- const ps = child_process.spawn(
58
- 'node',
59
- [...nodeOptions, join(process.cwd(), '.commandkit', main)],
60
- {
61
- env: {
62
- ...process.env,
63
- ...processEnv,
64
- NODE_ENV: 'development',
65
- COMMANDKIT_DEV: 'true',
66
- },
67
- cwd: process.cwd(),
68
- },
69
- );
70
-
71
- ps.stdout.on('data', (data) => {
72
- write(data.toString());
73
- });
74
-
75
- ps.stderr.on('data', (data) => {
76
- write(Colors.red(data.toString()));
77
- });
78
-
79
- ps.on('close', (code) => {
80
- write('\n');
81
- process.exit(code ?? 0);
82
- });
83
-
84
- ps.on('error', (err) => {
85
- panic(err);
86
- });
87
- } catch (e) {
88
- status.fail(`Error occurred after ${(performance.now() - start).toFixed(2)}ms!\n`);
89
- panic(e);
90
- }
91
- }