commandkit 0.1.11-dev.20240202064607 → 0.1.11-dev.20241214134338
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 +126 -0
- package/bin/common.mjs +125 -0
- package/bin/development.mjs +180 -0
- package/bin/index.mjs +53 -0
- package/bin/parse-env.mjs +65 -0
- package/bin/production.mjs +86 -0
- package/dist/CommandKit-Co5QNlps.d.ts +451 -0
- package/dist/CommandKit-D84HlsL0.d.mts +451 -0
- package/dist/CommandKit.d.mts +5 -0
- package/dist/CommandKit.d.ts +5 -0
- package/dist/CommandKit.js +11 -0
- package/dist/CommandKit.js.map +1 -0
- package/dist/CommandKit.mjs +2 -0
- package/dist/CommandKit.mjs.map +1 -0
- package/dist/chunk-6B4TPFI4.mjs +7 -0
- package/dist/chunk-6B4TPFI4.mjs.map +1 -0
- package/dist/chunk-6J4XVVL4.mjs +2 -0
- package/dist/chunk-6J4XVVL4.mjs.map +1 -0
- package/dist/chunk-6TIBWNPX.mjs +2 -0
- package/dist/chunk-6TIBWNPX.mjs.map +1 -0
- package/dist/chunk-7YWJADOI.mjs +2 -0
- package/dist/chunk-7YWJADOI.mjs.map +1 -0
- package/dist/chunk-CATZUF3K.mjs +2 -0
- package/dist/chunk-CATZUF3K.mjs.map +1 -0
- package/dist/chunk-EQ62HPWY.mjs +2 -0
- package/dist/chunk-EQ62HPWY.mjs.map +1 -0
- package/dist/chunk-FBYSGXIA.mjs +2 -0
- package/dist/chunk-FBYSGXIA.mjs.map +1 -0
- package/dist/chunk-IHDVSF4H.mjs +2 -0
- package/dist/chunk-IHDVSF4H.mjs.map +1 -0
- package/dist/chunk-JWT7PUZJ.mjs +4 -0
- package/dist/chunk-JWT7PUZJ.mjs.map +1 -0
- package/dist/chunk-KR3OAQKX.mjs +2 -0
- package/dist/chunk-KR3OAQKX.mjs.map +1 -0
- package/dist/chunk-M5FMJETI.mjs +1 -0
- package/dist/chunk-M5FMJETI.mjs.map +1 -0
- package/dist/chunk-MTG5LNGD.mjs +2 -0
- package/dist/chunk-MTG5LNGD.mjs.map +1 -0
- package/dist/chunk-PFU3PH6M.mjs +1 -0
- package/dist/chunk-PFU3PH6M.mjs.map +1 -0
- package/dist/chunk-PXNT5WYC.mjs +2 -0
- package/dist/chunk-PXNT5WYC.mjs.map +1 -0
- package/dist/chunk-Q7JC46G6.mjs +2 -0
- package/dist/chunk-Q7JC46G6.mjs.map +1 -0
- package/dist/chunk-VHU3SDTR.mjs +2 -0
- package/dist/chunk-VHU3SDTR.mjs.map +1 -0
- package/dist/chunk-VURGPMLB.mjs +2 -0
- package/dist/chunk-VURGPMLB.mjs.map +1 -0
- package/dist/chunk-XVIYQWRZ.mjs +2 -0
- package/dist/chunk-XVIYQWRZ.mjs.map +1 -0
- package/dist/chunk-Y7QBWZVQ.mjs +4 -0
- package/dist/chunk-Y7QBWZVQ.mjs.map +1 -0
- package/dist/components/ButtonKit.d.mts +55 -0
- package/dist/components/ButtonKit.d.ts +55 -0
- package/dist/components/ButtonKit.js +2 -0
- package/dist/components/ButtonKit.js.map +1 -0
- package/dist/components/ButtonKit.mjs +2 -0
- package/dist/components/ButtonKit.mjs.map +1 -0
- package/dist/components/index.d.mts +2 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.js +2 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/index.mjs +2 -0
- package/dist/components/index.mjs.map +1 -0
- package/dist/config.d.mts +53 -0
- package/dist/config.d.ts +53 -0
- package/dist/config.js +2 -0
- package/dist/config.js.map +1 -0
- package/dist/config.mjs +2 -0
- package/dist/config.mjs.map +1 -0
- package/dist/handlers/command-handler/CommandHandler.d.mts +5 -0
- package/dist/handlers/command-handler/CommandHandler.d.ts +5 -0
- package/dist/handlers/command-handler/CommandHandler.js +11 -0
- package/dist/handlers/command-handler/CommandHandler.js.map +1 -0
- package/dist/handlers/command-handler/CommandHandler.mjs +2 -0
- package/dist/handlers/command-handler/CommandHandler.mjs.map +1 -0
- package/dist/handlers/command-handler/functions/loadCommandsWithRest.d.mts +35 -0
- package/dist/handlers/command-handler/functions/loadCommandsWithRest.d.ts +35 -0
- package/dist/handlers/command-handler/functions/loadCommandsWithRest.js +4 -0
- package/dist/handlers/command-handler/functions/loadCommandsWithRest.js.map +1 -0
- package/dist/handlers/command-handler/functions/loadCommandsWithRest.mjs +2 -0
- package/dist/handlers/command-handler/functions/loadCommandsWithRest.mjs.map +1 -0
- package/dist/handlers/command-handler/functions/registerCommands.d.mts +20 -0
- package/dist/handlers/command-handler/functions/registerCommands.d.ts +20 -0
- package/dist/handlers/command-handler/functions/registerCommands.js +7 -0
- package/dist/handlers/command-handler/functions/registerCommands.js.map +1 -0
- package/dist/handlers/command-handler/functions/registerCommands.mjs +2 -0
- package/dist/handlers/command-handler/functions/registerCommands.mjs.map +1 -0
- package/dist/handlers/command-handler/typings.d.mts +5 -0
- package/dist/handlers/command-handler/typings.d.ts +5 -0
- package/dist/handlers/command-handler/typings.js +2 -0
- package/dist/handlers/command-handler/typings.js.map +1 -0
- package/dist/handlers/command-handler/typings.mjs +1 -0
- package/dist/handlers/command-handler/typings.mjs.map +1 -0
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.d.mts +9 -0
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.d.ts +9 -0
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.js +2 -0
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.js.map +1 -0
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.mjs +2 -0
- package/dist/handlers/command-handler/utils/areSlashCommandsDifferent.mjs.map +1 -0
- package/dist/handlers/command-handler/validations/devOnly.d.mts +9 -0
- package/dist/handlers/command-handler/validations/devOnly.d.ts +9 -0
- package/dist/handlers/command-handler/validations/devOnly.js +2 -0
- package/dist/handlers/command-handler/validations/devOnly.js.map +1 -0
- package/dist/handlers/command-handler/validations/devOnly.mjs +2 -0
- package/dist/handlers/command-handler/validations/devOnly.mjs.map +1 -0
- package/dist/handlers/command-handler/validations/index.d.mts +10 -0
- package/dist/handlers/command-handler/validations/index.d.ts +10 -0
- package/dist/handlers/command-handler/validations/index.js +4 -0
- package/dist/handlers/command-handler/validations/index.js.map +1 -0
- package/dist/handlers/command-handler/validations/index.mjs +2 -0
- package/dist/handlers/command-handler/validations/index.mjs.map +1 -0
- package/dist/handlers/command-handler/validations/permissions.d.mts +9 -0
- package/dist/handlers/command-handler/validations/permissions.d.ts +9 -0
- package/dist/handlers/command-handler/validations/permissions.js +4 -0
- package/dist/handlers/command-handler/validations/permissions.js.map +1 -0
- package/dist/handlers/command-handler/validations/permissions.mjs +2 -0
- package/dist/handlers/command-handler/validations/permissions.mjs.map +1 -0
- package/dist/handlers/event-handler/EventHandler.d.mts +5 -0
- package/dist/handlers/event-handler/EventHandler.d.ts +5 -0
- package/dist/handlers/event-handler/EventHandler.js +2 -0
- package/dist/handlers/event-handler/EventHandler.js.map +1 -0
- package/dist/handlers/event-handler/EventHandler.mjs +2 -0
- package/dist/handlers/event-handler/EventHandler.mjs.map +1 -0
- package/dist/handlers/event-handler/typings.d.mts +5 -0
- package/dist/handlers/event-handler/typings.d.ts +5 -0
- package/dist/handlers/event-handler/typings.js +2 -0
- package/dist/handlers/event-handler/typings.js.map +1 -0
- package/dist/handlers/event-handler/typings.mjs +1 -0
- package/dist/handlers/event-handler/typings.mjs.map +1 -0
- package/dist/handlers/index.d.mts +5 -0
- package/dist/handlers/index.d.ts +5 -0
- package/dist/handlers/index.js +11 -0
- package/dist/handlers/index.js.map +1 -0
- package/dist/handlers/index.mjs +2 -0
- package/dist/handlers/index.mjs.map +1 -0
- package/dist/handlers/validation-handler/ValidationHandler.d.mts +14 -0
- package/dist/handlers/validation-handler/ValidationHandler.d.ts +14 -0
- package/dist/handlers/validation-handler/ValidationHandler.js +2 -0
- package/dist/handlers/validation-handler/ValidationHandler.js.map +1 -0
- package/dist/handlers/validation-handler/ValidationHandler.mjs +2 -0
- package/dist/handlers/validation-handler/ValidationHandler.mjs.map +1 -0
- package/dist/handlers/validation-handler/typings.d.mts +14 -0
- package/dist/handlers/validation-handler/typings.d.ts +14 -0
- package/dist/handlers/validation-handler/typings.js +2 -0
- package/dist/handlers/validation-handler/typings.js.map +1 -0
- package/dist/handlers/validation-handler/typings.mjs +1 -0
- package/dist/handlers/validation-handler/typings.mjs.map +1 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.d.mts +5 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils/clone.d.mts +3 -0
- package/dist/utils/clone.d.ts +3 -0
- package/dist/utils/clone.js +2 -0
- package/dist/utils/clone.js.map +1 -0
- package/dist/utils/clone.mjs +2 -0
- package/dist/utils/clone.mjs.map +1 -0
- package/dist/utils/colors.d.mts +27 -0
- package/dist/utils/colors.d.ts +27 -0
- package/dist/utils/colors.js +2 -0
- package/dist/utils/colors.js.map +1 -0
- package/dist/utils/colors.mjs +2 -0
- package/dist/utils/colors.mjs.map +1 -0
- package/dist/utils/get-paths.d.mts +4 -0
- package/dist/utils/get-paths.d.ts +4 -0
- package/dist/utils/get-paths.js +2 -0
- package/dist/utils/get-paths.js.map +1 -0
- package/dist/utils/get-paths.mjs +2 -0
- package/dist/utils/get-paths.mjs.map +1 -0
- package/dist/utils/resolve-file-url.d.mts +8 -0
- package/dist/utils/resolve-file-url.d.ts +8 -0
- package/dist/utils/resolve-file-url.js +2 -0
- package/dist/utils/resolve-file-url.js.map +1 -0
- package/dist/utils/resolve-file-url.mjs +2 -0
- package/dist/utils/resolve-file-url.mjs.map +1 -0
- package/package.json +8 -9
- package/bin/index.cjs +0 -3
package/bin/build.mjs
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { build } from 'tsup';
|
|
6
|
+
import {
|
|
7
|
+
Colors,
|
|
8
|
+
erase,
|
|
9
|
+
findCommandKitConfig,
|
|
10
|
+
panic,
|
|
11
|
+
write,
|
|
12
|
+
} from './common.mjs';
|
|
13
|
+
import ora from 'ora';
|
|
14
|
+
|
|
15
|
+
export async function bootstrapProductionBuild(config) {
|
|
16
|
+
const {
|
|
17
|
+
sourcemap = false,
|
|
18
|
+
minify = false,
|
|
19
|
+
outDir = 'dist',
|
|
20
|
+
antiCrash = true,
|
|
21
|
+
src,
|
|
22
|
+
main,
|
|
23
|
+
requirePolyfill: polyfillRequire,
|
|
24
|
+
} = await findCommandKitConfig(config);
|
|
25
|
+
|
|
26
|
+
const status = ora('Creating optimized production build...\n').start();
|
|
27
|
+
const start = performance.now();
|
|
28
|
+
|
|
29
|
+
erase(outDir);
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
await build({
|
|
33
|
+
clean: true,
|
|
34
|
+
format: ['esm'],
|
|
35
|
+
dts: false,
|
|
36
|
+
skipNodeModulesBundle: true,
|
|
37
|
+
minify,
|
|
38
|
+
shims: true,
|
|
39
|
+
banner: {
|
|
40
|
+
js: '/* Optimized production build generated by CommandKit */',
|
|
41
|
+
},
|
|
42
|
+
sourcemap,
|
|
43
|
+
keepNames: true,
|
|
44
|
+
outDir,
|
|
45
|
+
silent: true,
|
|
46
|
+
watch: false,
|
|
47
|
+
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
await injectShims(outDir, main, antiCrash, polyfillRequire);
|
|
51
|
+
|
|
52
|
+
status.succeed(
|
|
53
|
+
Colors.green(
|
|
54
|
+
`Build completed in ${(performance.now() - start).toFixed(2)}ms!`,
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
write(
|
|
58
|
+
Colors.green(
|
|
59
|
+
`\nRun ${Colors.magenta(`commandkit start`)} ${Colors.green(
|
|
60
|
+
'to start your bot.',
|
|
61
|
+
)}`,
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
} catch (e) {
|
|
65
|
+
status.fail(
|
|
66
|
+
`Build failed after ${(performance.now() - start).toFixed(2)}ms!`,
|
|
67
|
+
);
|
|
68
|
+
panic(e);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function injectShims(outDir, main, antiCrash, polyfillRequire) {
|
|
73
|
+
const path = join(process.cwd(), outDir, main);
|
|
74
|
+
|
|
75
|
+
const head = ['\n\n;await (async()=>{', " 'use strict';"].join('\n');
|
|
76
|
+
const tail = '\n})();';
|
|
77
|
+
const requireScript = polyfillRequire
|
|
78
|
+
? [
|
|
79
|
+
'// --- CommandKit require() polyfill ---',
|
|
80
|
+
' if (typeof require === "undefined") {',
|
|
81
|
+
' const { createRequire } = await import("node:module");',
|
|
82
|
+
' const __require = createRequire(import.meta.url);',
|
|
83
|
+
' Object.defineProperty(globalThis, "require", {',
|
|
84
|
+
' value: (id) => {',
|
|
85
|
+
' return __require(id);',
|
|
86
|
+
' },',
|
|
87
|
+
' configurable: true,',
|
|
88
|
+
' enumerable: false,',
|
|
89
|
+
' writable: true,',
|
|
90
|
+
' });',
|
|
91
|
+
' }',
|
|
92
|
+
'// --- CommandKit require() polyfill ---',
|
|
93
|
+
].join('\n')
|
|
94
|
+
: '';
|
|
95
|
+
|
|
96
|
+
const antiCrashScript = antiCrash
|
|
97
|
+
? [
|
|
98
|
+
'// --- CommandKit Anti-Crash Monitor ---',
|
|
99
|
+
" // 'uncaughtException' event is supposed to be used to perform synchronous cleanup before shutting down the process",
|
|
100
|
+
' // instead of using it as a means to resume operation.',
|
|
101
|
+
' // But it exists here due to compatibility reasons with discord bot ecosystem.',
|
|
102
|
+
" const p = (t) => `\\x1b[33m${t}\\x1b[0m`, b = '[CommandKit Anti-Crash Monitor]', l = console.log, e1 = 'uncaughtException', e2 = 'unhandledRejection';",
|
|
103
|
+
' if (!process.eventNames().includes(e1)) // skip if it is already handled',
|
|
104
|
+
' process.on(e1, (e) => {',
|
|
105
|
+
' l(p(`${b} Uncaught Exception`)); l(p(b), p(e.stack || e));',
|
|
106
|
+
' })',
|
|
107
|
+
' if (!process.eventNames().includes(e2)) // skip if it is already handled',
|
|
108
|
+
' process.on(e2, (r) => {',
|
|
109
|
+
' l(p(`${b} Unhandled promise rejection`)); l(p(`${b} ${r.stack || r}`));',
|
|
110
|
+
' });',
|
|
111
|
+
'// --- CommandKit Anti-Crash Monitor ---',
|
|
112
|
+
].join('\n')
|
|
113
|
+
: '';
|
|
114
|
+
|
|
115
|
+
const contents = await readFile(path, 'utf-8');
|
|
116
|
+
const finalScript = [
|
|
117
|
+
head,
|
|
118
|
+
requireScript,
|
|
119
|
+
antiCrashScript,
|
|
120
|
+
tail,
|
|
121
|
+
'\n\n',
|
|
122
|
+
contents,
|
|
123
|
+
].join('\n');
|
|
124
|
+
|
|
125
|
+
return writeFile(path, finalScript);
|
|
126
|
+
}
|
package/bin/common.mjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { rimrafSync } from 'rimraf';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
|
|
7
|
+
const resetColor = '\x1b[0m';
|
|
8
|
+
|
|
9
|
+
export const Colors = {
|
|
10
|
+
reset: (text) => `${text}${resetColor}`,
|
|
11
|
+
bright: (text) => `\x1b[1m${text}${resetColor}`,
|
|
12
|
+
dim: (text) => `\x1b[2m${text}${resetColor}`,
|
|
13
|
+
underscore: (text) => `\x1b[4m${text}${resetColor}`,
|
|
14
|
+
blink: (text) => `\x1b[5m${text}${resetColor}`,
|
|
15
|
+
reverse: (text) => `\x1b[7m${text}${resetColor}`,
|
|
16
|
+
hidden: (text) => `\x1b[8m${text}${resetColor}`,
|
|
17
|
+
|
|
18
|
+
black: (text) => `\x1b[30m${text}${resetColor}`,
|
|
19
|
+
red: (text) => `\x1b[31m${text}${resetColor}`,
|
|
20
|
+
green: (text) => `\x1b[32m${text}${resetColor}`,
|
|
21
|
+
yellow: (text) => `\x1b[33m${text}${resetColor}`,
|
|
22
|
+
blue: (text) => `\x1b[34m${text}${resetColor}`,
|
|
23
|
+
magenta: (text) => `\x1b[35m${text}${resetColor}`,
|
|
24
|
+
cyan: (text) => `\x1b[36m${text}${resetColor}`,
|
|
25
|
+
white: (text) => `\x1b[37m${text}${resetColor}`,
|
|
26
|
+
|
|
27
|
+
bgBlack: (text) => `\x1b[40m${text}${resetColor}`,
|
|
28
|
+
bgRed: (text) => `\x1b[41m${text}${resetColor}`,
|
|
29
|
+
bgGreen: (text) => `\x1b[42m${text}${resetColor}`,
|
|
30
|
+
bgYellow: (text) => `\x1b[43m${text}${resetColor}`,
|
|
31
|
+
bgBlue: (text) => `\x1b[44m${text}${resetColor}`,
|
|
32
|
+
bgMagenta: (text) => `\x1b[45m${text}${resetColor}`,
|
|
33
|
+
bgCyan: (text) => `\x1b[46m${text}${resetColor}`,
|
|
34
|
+
bgWhite: (text) => `\x1b[47m${text}${resetColor}`,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function write(message) {
|
|
38
|
+
process.stdout.write(message);
|
|
39
|
+
process.stdout.write('\n');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @returns {never}
|
|
44
|
+
*/
|
|
45
|
+
export function panic(message) {
|
|
46
|
+
write(Colors.red(`Error: ${message}`));
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function findPackageJSON() {
|
|
51
|
+
const cwd = process.cwd();
|
|
52
|
+
const target = join(cwd, 'package.json');
|
|
53
|
+
|
|
54
|
+
if (!fs.existsSync(target)) {
|
|
55
|
+
panic('Could not find package.json in current directory.');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return JSON.parse(fs.readFileSync(target, 'utf8'));
|
|
59
|
+
}
|
|
60
|
+
|
|
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
|
+
'commandkit.ts',
|
|
71
|
+
'commandkit.mts',
|
|
72
|
+
'commandkit.cts',
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
export async function findCommandKitConfig(src) {
|
|
76
|
+
const cwd = process.cwd();
|
|
77
|
+
const locations = src
|
|
78
|
+
? [join(cwd, src)]
|
|
79
|
+
: possibleFileNames.map((name) => join(cwd, name));
|
|
80
|
+
|
|
81
|
+
for (const location of locations) {
|
|
82
|
+
try {
|
|
83
|
+
return await loadConfigInner(location);
|
|
84
|
+
} catch (e) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
panic(`Could not locate commandkit config from ${cwd}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function ensureTypeScript(target) {
|
|
93
|
+
const isTypeScript = /\.(c|m)tsx?$/.test(target);
|
|
94
|
+
|
|
95
|
+
if (isTypeScript && !process.features.typescript) {
|
|
96
|
+
panic(
|
|
97
|
+
'You are trying to load commandkit config file that is written in typescript. The current Node.js version does not have TypeScript feature enabled.',
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function loadConfigInner(target) {
|
|
103
|
+
const isJSON = target.endsWith('.json');
|
|
104
|
+
|
|
105
|
+
await ensureExists(target);
|
|
106
|
+
|
|
107
|
+
ensureTypeScript(target);
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @type {import('..').CommandKitConfig}
|
|
111
|
+
*/
|
|
112
|
+
const config = await import(`file://${target}`, {
|
|
113
|
+
assert: isJSON ? { type: 'json' } : undefined,
|
|
114
|
+
}).then((conf) => conf.default || conf);
|
|
115
|
+
|
|
116
|
+
return config;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function ensureExists(loc) {
|
|
120
|
+
await fs.promises.access(loc, fs.constants.F_OK);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function erase(dir) {
|
|
124
|
+
rimrafSync(dir);
|
|
125
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { config as dotenv } from 'dotenv';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { build } from 'tsup';
|
|
5
|
+
import {
|
|
6
|
+
Colors,
|
|
7
|
+
erase,
|
|
8
|
+
findCommandKitConfig,
|
|
9
|
+
panic,
|
|
10
|
+
write,
|
|
11
|
+
} from './common.mjs';
|
|
12
|
+
import { parseEnv } from './parse-env.mjs';
|
|
13
|
+
import child_process from 'node:child_process';
|
|
14
|
+
import ora from 'ora';
|
|
15
|
+
import { injectShims } from './build.mjs';
|
|
16
|
+
|
|
17
|
+
const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
|
|
18
|
+
const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;
|
|
19
|
+
|
|
20
|
+
export async function bootstrapDevelopmentServer(opts) {
|
|
21
|
+
const {
|
|
22
|
+
src,
|
|
23
|
+
main,
|
|
24
|
+
watch = Boolean(opts.noWatch),
|
|
25
|
+
nodeOptions = [],
|
|
26
|
+
envExtra = true,
|
|
27
|
+
clearRestartLogs = true,
|
|
28
|
+
outDir,
|
|
29
|
+
requirePolyfill,
|
|
30
|
+
} = await findCommandKitConfig(opts.config);
|
|
31
|
+
|
|
32
|
+
if (!src) {
|
|
33
|
+
panic('Could not find src in commandkit.json');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!main) {
|
|
37
|
+
panic('Could not find main in commandkit.json');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const watchMode = watch;
|
|
41
|
+
const status = ora(
|
|
42
|
+
Colors.green('Starting a development server...\n'),
|
|
43
|
+
).start();
|
|
44
|
+
const start = performance.now();
|
|
45
|
+
|
|
46
|
+
if (watchMode && !nodeOptions.includes('--watch')) {
|
|
47
|
+
nodeOptions.push('--watch');
|
|
48
|
+
} else if (!watchMode && nodeOptions.includes('--watch')) {
|
|
49
|
+
nodeOptions.splice(nodeOptions.indexOf('--watch'), 1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!nodeOptions.includes('--enable-source-maps')) {
|
|
53
|
+
nodeOptions.push('--enable-source-maps');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
erase('.commandkit');
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
await build({
|
|
60
|
+
clean: true,
|
|
61
|
+
format: ['esm'],
|
|
62
|
+
dts: false,
|
|
63
|
+
skipNodeModulesBundle: true,
|
|
64
|
+
minify: false,
|
|
65
|
+
shims: true,
|
|
66
|
+
sourcemap: 'inline',
|
|
67
|
+
keepNames: true,
|
|
68
|
+
outDir: '.commandkit',
|
|
69
|
+
silent: true,
|
|
70
|
+
entry: [src, '!dist', '!.commandkit', `!${outDir}`].filter(Boolean),
|
|
71
|
+
watch: watchMode,
|
|
72
|
+
async onSuccess() {
|
|
73
|
+
return await injectShims('.commandkit', main, false, requirePolyfill);
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
status.succeed(
|
|
78
|
+
Colors.green(
|
|
79
|
+
`Dev server started in ${(performance.now() - start).toFixed(2)}ms!\n`,
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
if (watchMode) write(Colors.cyan('Watching for file changes...\n'));
|
|
84
|
+
|
|
85
|
+
const processEnv = {};
|
|
86
|
+
|
|
87
|
+
const env = dotenv({
|
|
88
|
+
path: join(process.cwd(), '.env'),
|
|
89
|
+
// @ts-expect-error
|
|
90
|
+
processEnv,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (envExtra) {
|
|
94
|
+
parseEnv(processEnv);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (env.error) {
|
|
98
|
+
write(Colors.yellow(`[DOTENV] Warning: ${env.error.message}`));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (env.parsed) {
|
|
102
|
+
write(Colors.blue('[DOTENV] Loaded .env file!'));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @type {child_process.ChildProcessWithoutNullStreams}
|
|
107
|
+
*/
|
|
108
|
+
const ps = child_process.spawn(
|
|
109
|
+
'node',
|
|
110
|
+
[...nodeOptions, join(process.cwd(), '.commandkit', main)],
|
|
111
|
+
{
|
|
112
|
+
env: {
|
|
113
|
+
...process.env,
|
|
114
|
+
...processEnv,
|
|
115
|
+
NODE_ENV: 'development',
|
|
116
|
+
// @ts-expect-error
|
|
117
|
+
COMMANDKIT_DEV: true,
|
|
118
|
+
// @ts-expect-error
|
|
119
|
+
COMMANDKIT_PRODUCTION: false,
|
|
120
|
+
},
|
|
121
|
+
cwd: process.cwd(),
|
|
122
|
+
},
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
let isLastLogRestarting = false,
|
|
126
|
+
hasStarted = false;
|
|
127
|
+
|
|
128
|
+
ps.stdout.on('data', (data) => {
|
|
129
|
+
const message = data.toString();
|
|
130
|
+
|
|
131
|
+
if (FAILED_RUNNING_PATTERN.test(message)) {
|
|
132
|
+
write(Colors.cyan('Failed running the bot, waiting for changes...'));
|
|
133
|
+
isLastLogRestarting = false;
|
|
134
|
+
if (!hasStarted) hasStarted = true;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (clearRestartLogs && !RESTARTING_MSG_PATTERN.test(message)) {
|
|
139
|
+
write(message);
|
|
140
|
+
isLastLogRestarting = false;
|
|
141
|
+
} else {
|
|
142
|
+
if (isLastLogRestarting || !hasStarted) {
|
|
143
|
+
if (!hasStarted) hasStarted = true;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
write(Colors.cyan('⌀ Restarting the bot...'));
|
|
147
|
+
isLastLogRestarting = true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!hasStarted) hasStarted = true;
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
ps.stderr.on('data', (data) => {
|
|
154
|
+
const message = data.toString();
|
|
155
|
+
|
|
156
|
+
if (
|
|
157
|
+
message.includes(
|
|
158
|
+
'ExperimentalWarning: Watch mode is an experimental feature and might change at any time',
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
return;
|
|
162
|
+
|
|
163
|
+
write(Colors.red(message));
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
ps.on('close', (code) => {
|
|
167
|
+
write('\n');
|
|
168
|
+
process.exit(code ?? 0);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
ps.on('error', (err) => {
|
|
172
|
+
panic(err);
|
|
173
|
+
});
|
|
174
|
+
} catch (e) {
|
|
175
|
+
status.fail(
|
|
176
|
+
`Error occurred after ${(performance.now() - start).toFixed(2)}ms!\n`,
|
|
177
|
+
);
|
|
178
|
+
panic(e.stack ?? e);
|
|
179
|
+
}
|
|
180
|
+
}
|
package/bin/index.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// @ts-check
|
|
4
|
+
|
|
5
|
+
import { Command } from 'commander';
|
|
6
|
+
import { bootstrapDevelopmentServer } from './development.mjs';
|
|
7
|
+
import { bootstrapProductionServer } from './production.mjs';
|
|
8
|
+
import { bootstrapProductionBuild } from './build.mjs';
|
|
9
|
+
|
|
10
|
+
const program = new Command('commandkit');
|
|
11
|
+
|
|
12
|
+
program
|
|
13
|
+
.command('dev')
|
|
14
|
+
.description('Start your bot in development mode.')
|
|
15
|
+
.option(
|
|
16
|
+
'-c, --config <path>',
|
|
17
|
+
'Path to your commandkit config file.',
|
|
18
|
+
'./commandkit.js',
|
|
19
|
+
)
|
|
20
|
+
.action(() => {
|
|
21
|
+
const options = program.opts();
|
|
22
|
+
bootstrapDevelopmentServer(options);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
program
|
|
26
|
+
.command('start')
|
|
27
|
+
.description(
|
|
28
|
+
'Start your bot in production mode after running the build command.',
|
|
29
|
+
)
|
|
30
|
+
.option(
|
|
31
|
+
'-c, --config <path>',
|
|
32
|
+
'Path to your commandkit.json file.',
|
|
33
|
+
'./commandkit.js',
|
|
34
|
+
)
|
|
35
|
+
.action(() => {
|
|
36
|
+
const options = program.opts();
|
|
37
|
+
bootstrapProductionServer(options.config);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
program
|
|
41
|
+
.command('build')
|
|
42
|
+
.description('Build your project for production usage.')
|
|
43
|
+
.option(
|
|
44
|
+
'-c, --config <path>',
|
|
45
|
+
'Path to your commandkit.json file.',
|
|
46
|
+
'./commandkit.json',
|
|
47
|
+
)
|
|
48
|
+
.action(() => {
|
|
49
|
+
const options = program.opts();
|
|
50
|
+
bootstrapProductionBuild(options.config);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
function catcher(fn) {
|
|
19
|
+
try {
|
|
20
|
+
fn();
|
|
21
|
+
return true;
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseEnv(src) {
|
|
28
|
+
for (const key in src) {
|
|
29
|
+
const value = src[key];
|
|
30
|
+
|
|
31
|
+
if (typeof value !== 'string') continue;
|
|
32
|
+
|
|
33
|
+
if (value.startsWith(VALUE_PREFIXES.JSON)) {
|
|
34
|
+
catcher(
|
|
35
|
+
() => (src[key] = JSON.parse(value.replace(VALUE_PREFIXES.JSON, ''))),
|
|
36
|
+
);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (value.startsWith(VALUE_PREFIXES.DATE)) {
|
|
41
|
+
src[key] = new Date(value.replace(VALUE_PREFIXES.DATE, ''));
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (value.includes(',')) {
|
|
46
|
+
src[key] = value.split(',').map((v) => v.trim());
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (/^[0-9]+n$/.test(value)) {
|
|
51
|
+
src[key] = BigInt(value);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (value in valuesMap) {
|
|
56
|
+
src[key] =
|
|
57
|
+
typeof valuesMap[value] === 'function'
|
|
58
|
+
? valuesMap[value]()
|
|
59
|
+
: valuesMap[value];
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return src;
|
|
65
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { config as dotenv } from 'dotenv';
|
|
3
|
+
import { existsSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { Colors, findCommandKitConfig, panic, write } from './common.mjs';
|
|
6
|
+
import { parseEnv } from './parse-env.mjs';
|
|
7
|
+
import child_process from 'node:child_process';
|
|
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(
|
|
19
|
+
'Could not find production build, maybe run `commandkit build` first?',
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const processEnv = {};
|
|
25
|
+
|
|
26
|
+
const env = dotenv({
|
|
27
|
+
path: join(process.cwd(), '.env'),
|
|
28
|
+
// @ts-expect-error
|
|
29
|
+
processEnv,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (envExtra) {
|
|
33
|
+
parseEnv(processEnv);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (env.error) {
|
|
37
|
+
write(Colors.yellow(`[DOTENV] Warning: ${env.error.message}`));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (env.parsed) {
|
|
41
|
+
write(Colors.blue('[DOTENV] Loaded .env file!'));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @type {child_process.ChildProcessWithoutNullStreams}
|
|
46
|
+
*/
|
|
47
|
+
const ps = child_process.spawn(
|
|
48
|
+
'node',
|
|
49
|
+
[
|
|
50
|
+
sourcemap ? '--enable-source-maps' : '',
|
|
51
|
+
join(process.cwd(), outDir, main),
|
|
52
|
+
].filter(Boolean),
|
|
53
|
+
{
|
|
54
|
+
env: {
|
|
55
|
+
...process.env,
|
|
56
|
+
...processEnv,
|
|
57
|
+
NODE_ENV: 'production',
|
|
58
|
+
// @ts-expect-error
|
|
59
|
+
COMMANDKIT_DEV: false,
|
|
60
|
+
// @ts-expect-error
|
|
61
|
+
COMMANDKIT_PROD: true,
|
|
62
|
+
},
|
|
63
|
+
cwd: process.cwd(),
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
ps.stdout.on('data', (data) => {
|
|
68
|
+
write(data.toString());
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
ps.stderr.on('data', (data) => {
|
|
72
|
+
write(Colors.red(data.toString()));
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
ps.on('close', (code) => {
|
|
76
|
+
write('\n');
|
|
77
|
+
process.exit(code ?? 0);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
ps.on('error', (err) => {
|
|
81
|
+
panic(err);
|
|
82
|
+
});
|
|
83
|
+
} catch (e) {
|
|
84
|
+
panic(e);
|
|
85
|
+
}
|
|
86
|
+
}
|