Skip to content

Commit

Permalink
fix(cli): watch mode not executing with commonjs projects
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Dec 14, 2023
1 parent 7c74fd2 commit fa4b9f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
61 changes: 31 additions & 30 deletions packages/commandkit/bin/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function bootstrapProductionBuild(config) {
keepNames: true,
outDir,
silent: true,
watch: false,
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
});

Expand All @@ -63,40 +64,40 @@ export async function injectShims(outDir, main, antiCrash, polyfillRequire) {
const tail = '\n})();';
const requireScript = polyfillRequire
? [
'// --- CommandKit require() polyfill ---',
' if (typeof require === "undefined") {',
' const { createRequire } = await import("node:module");',
' const __require = createRequire(import.meta.url);',
' Object.defineProperty(globalThis, "require", {',
' value: (id) => {',
' return __require(id);',
' },',
' configurable: true,',
' enumerable: false,',
' writable: true,',
' });',
' }',
'// --- CommandKit require() polyfill ---',
].join('\n')
'// --- CommandKit require() polyfill ---',
' if (typeof require === "undefined") {',
' const { createRequire } = await import("node:module");',
' const __require = createRequire(import.meta.url);',
' Object.defineProperty(globalThis, "require", {',
' value: (id) => {',
' return __require(id);',
' },',
' configurable: true,',
' enumerable: false,',
' writable: true,',
' });',
' }',
'// --- CommandKit require() polyfill ---',
].join('\n')
: '';

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

const contents = await readFile(path, 'utf-8');
Expand Down
6 changes: 4 additions & 2 deletions packages/commandkit/bin/development.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ export async function bootstrapDevelopmentServer(opts) {
silent: true,
entry: [src, '!dist', '!.commandkit', `!${outDir}`].filter(Boolean),
watch: watchMode,
async onSuccess() {
console.log('watch triggered')
return await injectShims('.commandkit', main, false, requirePolyfill);
},
});

await injectShims('.commandkit', main, false, requirePolyfill);

status.succeed(
Colors.green(`Dev server started in ${(performance.now() - start).toFixed(2)}ms!\n`),
);
Expand Down
1 change: 1 addition & 0 deletions packages/commandkit/tests/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommandKit } from '../../src/index';
import { Client } from 'discord.js';
require('dotenv').config();

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

0 comments on commit fa4b9f4

Please sign in to comment.