Skip to content

Commit fa4b9f4

Browse files
committed
fix(cli): watch mode not executing with commonjs projects
1 parent 7c74fd2 commit fa4b9f4

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

packages/commandkit/bin/build.mjs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export async function bootstrapProductionBuild(config) {
3737
keepNames: true,
3838
outDir,
3939
silent: true,
40+
watch: false,
4041
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
4142
});
4243

@@ -63,40 +64,40 @@ export async function injectShims(outDir, main, antiCrash, polyfillRequire) {
6364
const tail = '\n})();';
6465
const requireScript = polyfillRequire
6566
? [
66-
'// --- CommandKit require() polyfill ---',
67-
' if (typeof require === "undefined") {',
68-
' const { createRequire } = await import("node:module");',
69-
' const __require = createRequire(import.meta.url);',
70-
' Object.defineProperty(globalThis, "require", {',
71-
' value: (id) => {',
72-
' return __require(id);',
73-
' },',
74-
' configurable: true,',
75-
' enumerable: false,',
76-
' writable: true,',
77-
' });',
78-
' }',
79-
'// --- CommandKit require() polyfill ---',
80-
].join('\n')
67+
'// --- CommandKit require() polyfill ---',
68+
' if (typeof require === "undefined") {',
69+
' const { createRequire } = await import("node:module");',
70+
' const __require = createRequire(import.meta.url);',
71+
' Object.defineProperty(globalThis, "require", {',
72+
' value: (id) => {',
73+
' return __require(id);',
74+
' },',
75+
' configurable: true,',
76+
' enumerable: false,',
77+
' writable: true,',
78+
' });',
79+
' }',
80+
'// --- CommandKit require() polyfill ---',
81+
].join('\n')
8182
: '';
8283

8384
const antiCrashScript = antiCrash
8485
? [
85-
'// --- CommandKit Anti-Crash Monitor ---',
86-
" // 'uncaughtException' event is supposed to be used to perform synchronous cleanup before shutting down the process",
87-
' // instead of using it as a means to resume operation.',
88-
' // But it exists here due to compatibility reasons with discord bot ecosystem.',
89-
" const p = (t) => `\\x1b[33m${t}\\x1b[0m`, b = '[CommandKit Anti-Crash Monitor]', l = console.log, e1 = 'uncaughtException', e2 = 'unhandledRejection';",
90-
' if (!process.eventNames().includes(e1)) // skip if it is already handled',
91-
' process.on(e1, (e) => {',
92-
' l(p(`${b} Uncaught Exception`)); l(p(b), p(e.stack || e));',
93-
' })',
94-
' if (!process.eventNames().includes(e2)) // skip if it is already handled',
95-
' process.on(e2, (r) => {',
96-
' l(p(`${b} Unhandled promise rejection`)); l(p(`${b} ${r.stack || r}`));',
97-
' });',
98-
'// --- CommandKit Anti-Crash Monitor ---',
99-
].join('\n')
86+
'// --- CommandKit Anti-Crash Monitor ---',
87+
" // 'uncaughtException' event is supposed to be used to perform synchronous cleanup before shutting down the process",
88+
' // instead of using it as a means to resume operation.',
89+
' // But it exists here due to compatibility reasons with discord bot ecosystem.',
90+
" const p = (t) => `\\x1b[33m${t}\\x1b[0m`, b = '[CommandKit Anti-Crash Monitor]', l = console.log, e1 = 'uncaughtException', e2 = 'unhandledRejection';",
91+
' if (!process.eventNames().includes(e1)) // skip if it is already handled',
92+
' process.on(e1, (e) => {',
93+
' l(p(`${b} Uncaught Exception`)); l(p(b), p(e.stack || e));',
94+
' })',
95+
' if (!process.eventNames().includes(e2)) // skip if it is already handled',
96+
' process.on(e2, (r) => {',
97+
' l(p(`${b} Unhandled promise rejection`)); l(p(`${b} ${r.stack || r}`));',
98+
' });',
99+
'// --- CommandKit Anti-Crash Monitor ---',
100+
].join('\n')
100101
: '';
101102

102103
const contents = await readFile(path, 'utf-8');

packages/commandkit/bin/development.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ export async function bootstrapDevelopmentServer(opts) {
6161
silent: true,
6262
entry: [src, '!dist', '!.commandkit', `!${outDir}`].filter(Boolean),
6363
watch: watchMode,
64+
async onSuccess() {
65+
console.log('watch triggered')
66+
return await injectShims('.commandkit', main, false, requirePolyfill);
67+
},
6468
});
6569

66-
await injectShims('.commandkit', main, false, requirePolyfill);
67-
6870
status.succeed(
6971
Colors.green(`Dev server started in ${(performance.now() - start).toFixed(2)}ms!\n`),
7072
);

packages/commandkit/tests/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CommandKit } from '../../src/index';
22
import { Client } from 'discord.js';
3+
require('dotenv').config();
34

45
const client = new Client({
56
intents: ['Guilds', 'GuildMembers', 'GuildMessages', 'MessageContent'],

0 commit comments

Comments
 (0)