Skip to content

Commit

Permalink
chore: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Nov 24, 2023
1 parent ce1a499 commit 60ad575
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
43 changes: 24 additions & 19 deletions packages/commandkit/bin/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import { appendFile } from 'node:fs/promises';
import { join } from 'node:path';

export async function bootstrapProductionBuild(config) {
const { sourcemap = false, minify = false, outDir = 'dist', antiCrash = true, src, main } = await findCommandKitConfig(config);
const {
sourcemap = false,
minify = false,
outDir = 'dist',
antiCrash = true,
src,
main,
} = await findCommandKitConfig(config);

const status = ora('Creating optimized production build...\n').start();
const start = performance.now();
Expand Down Expand Up @@ -39,9 +46,7 @@ export async function bootstrapProductionBuild(config) {
);
write(
Colors.green(
`\nRun ${Colors.magenta(`commandkit start`)} ${Colors.green(
'to start your bot.',
)}`,
`\nRun ${Colors.magenta(`commandkit start`)} ${Colors.green('to start your bot.')}`,
),
);
} catch (e) {
Expand All @@ -53,24 +58,24 @@ export async function bootstrapProductionBuild(config) {
function injectAntiCrash(outDir, main) {
const path = join(process.cwd(), outDir, main);
const snippet = [
"\n\n// --- CommandKit Anti-Crash Monitor ---",
";(()=>{",
'\n\n// --- CommandKit Anti-Crash Monitor ---',
';(()=>{',
" 'use strict';",
" // '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.",
' // 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, o) => {",
" 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 ---\n",
' if (!process.eventNames().includes(e1)) // skip if it is already handled',
' process.on(e1, (e, o) => {',
' 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 ---\n',
].join('\n');

return appendFile(path, snippet);
}
}
16 changes: 10 additions & 6 deletions packages/commandkit/bin/common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ export function findPackageJSON() {
}

const possibleFileNames = [
'commandkit.json', 'commandkit.config.json',
'commandkit.js', 'commandkit.config.js',
'commandkit.mjs', 'commandkit.config.mjs',
'commandkit.cjs', 'commandkit.config.cjs'
'commandkit.json',
'commandkit.config.json',
'commandkit.js',
'commandkit.config.js',
'commandkit.mjs',
'commandkit.config.mjs',
'commandkit.cjs',
'commandkit.config.cjs',
];

export async function findCommandKitConfig(src) {
const cwd = process.cwd();
const locations = src ? [join(cwd, src)] : possibleFileNames.map(name => join(cwd, name));
const locations = src ? [join(cwd, src)] : possibleFileNames.map((name) => join(cwd, name));

for (const location of locations) {
try {
Expand All @@ -88,7 +92,7 @@ async function loadConfigInner(target) {
*/
const config = await import(`file://${target}`, {
assert: isJSON ? { type: 'json' } : undefined,
}).then(conf => conf.default || conf);
}).then((conf) => conf.default || conf);

return config;
}
Expand Down
12 changes: 9 additions & 3 deletions packages/commandkit/bin/development.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function bootstrapDevelopmentServer(opts) {
nodeOptions = [],
envExtra = true,
clearRestartLogs = true,
outDir
outDir,
} = await findCommandKitConfig(opts.config);

if (!src) {
Expand Down Expand Up @@ -107,7 +107,8 @@ export async function bootstrapDevelopmentServer(opts) {
},
);

let isLastLogRestarting = false, hasStarted = false;
let isLastLogRestarting = false,
hasStarted = false;

ps.stdout.on('data', (data) => {
const message = data.toString();
Expand Down Expand Up @@ -137,7 +138,12 @@ export async function bootstrapDevelopmentServer(opts) {
ps.stderr.on('data', (data) => {
const message = data.toString();

if (message.includes('ExperimentalWarning: Watch mode is an experimental feature and might change at any time')) return;
if (
message.includes(
'ExperimentalWarning: Watch mode is an experimental feature and might change at any time',
)
)
return;

write(Colors.red(message));
});
Expand Down
35 changes: 23 additions & 12 deletions packages/commandkit/bin/production.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { existsSync } from 'node:fs';
import { parseEnv } from './parse-env.mjs';

export async function bootstrapProductionServer(config) {
const { main, outDir = 'dist', envExtra = true, sourcemap } = await findCommandKitConfig(config);
const {
main,
outDir = 'dist',
envExtra = true,
sourcemap,
} = await findCommandKitConfig(config);

if (!existsSync(join(process.cwd(), outDir, main))) {
panic('Could not find production build, maybe run `commandkit build` first?');
Expand Down Expand Up @@ -37,18 +42,24 @@ export async function bootstrapProductionServer(config) {
/**
* @type {child_process.ChildProcessWithoutNullStreams}
*/
const ps = child_process.spawn('node', [sourcemap ? '--enable-source-maps' : '', join(process.cwd(), outDir, main)].filter(Boolean), {
env: {
...process.env,
...processEnv,
NODE_ENV: 'production',
// @ts-expect-error
COMMANDKIT_DEV: false,
// @ts-expect-error
COMMANDKIT_PROD: true,
const ps = child_process.spawn(
'node',
[sourcemap ? '--enable-source-maps' : '', join(process.cwd(), outDir, main)].filter(
Boolean,
),
{
env: {
...process.env,
...processEnv,
NODE_ENV: 'production',
// @ts-expect-error
COMMANDKIT_DEV: false,
// @ts-expect-error
COMMANDKIT_PROD: true,
},
cwd: process.cwd(),
},
cwd: process.cwd(),
});
);

ps.stdout.on('data', (data) => {
write(data.toString());
Expand Down
2 changes: 1 addition & 1 deletion packages/commandkit/tests/commandkit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import { defineConfig } from '../dist/index.mjs';
export default defineConfig({
main: 'index.mjs',
src: 'src',
});
});

0 comments on commit 60ad575

Please sign in to comment.