commandkit 0.1.6-dev.20231113071041 → 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 +42 -8
- package/bin/common.mjs +33 -5
- package/bin/development.mjs +163 -0
- package/bin/index.mjs +13 -3
- package/bin/parse-env.mjs +52 -0
- package/bin/production.mjs +83 -0
- package/dist/index.d.mts +224 -1
- package/dist/index.d.ts +224 -1
- package/dist/index.js +42 -19
- package/dist/index.mjs +39 -16
- package/package.json +4 -3
- package/bin/dev-server.mjs +0 -91
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,
|
|
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 {
|
|
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
|
|
33
|
+
js: '/* Optimized production build generated by CommandKit */',
|
|
25
34
|
},
|
|
26
|
-
sourcemap
|
|
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(`
|
|
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
|
-
|
|
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
|
|
74
|
+
const locations = src ? [join(cwd, src)] : possibleFileNames.map((name) => join(cwd, name));
|
|
64
75
|
|
|
65
|
-
|
|
66
|
-
|
|
76
|
+
for (const location of locations) {
|
|
77
|
+
try {
|
|
78
|
+
return await loadConfigInner(location);
|
|
79
|
+
} catch (e) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
67
82
|
}
|
|
68
83
|
|
|
69
|
-
|
|
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 './
|
|
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
|
|
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
|
-
|
|
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
|
+
}
|