Skip to content

Commit

Permalink
Merge pull request #28 from twlite/cli-update
Browse files Browse the repository at this point in the history
refactor: improve cli
  • Loading branch information
notunderctrl authored Nov 24, 2023
2 parents 21f078a + 60ad575 commit 3084bbd
Show file tree
Hide file tree
Showing 22 changed files with 556 additions and 192 deletions.
7 changes: 5 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
node_modules

**/docs/.next/
dist
.astro
dist
.commandkit

.DS_Store
.vscode

README.md
pnpm-lock.yaml

package.json

npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

.env
.env.production
.env.production
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "commandkit",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"dev": "turbo dev",
"lint": "turbo lint && prettier ./ --check --ignore-path=.prettierignore",
"build": "turbo lint && turbo build",
"build:package": "turbo lint --filter='commandkit' && turbo build --filter='commandkit'",
"deploy:package": "turbo lint --filter='commandkit' && turbo build --filter='commandkit' && turbo deploy --filter='commandkit'",
"deploy:package-dev": "turbo lint --filter='commandkit' && turbo build --filter='commandkit' && turbo deploy-dev --filter='commandkit'"
},
"devDependencies": {
"prettier": "^3.0.3",
"turbo": "^1.10.13"
}
"name": "commandkit",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"dev": "turbo dev",
"lint": "turbo lint && prettier ./ --check --ignore-path=.prettierignore",
"build": "turbo lint && turbo build",
"build:package": "turbo lint --filter='commandkit' && turbo build --filter='commandkit'",
"deploy:package": "turbo lint --filter='commandkit' && turbo build --filter='commandkit' && turbo deploy --filter='commandkit'",
"deploy:package-dev": "turbo lint --filter='commandkit' && turbo build --filter='commandkit' && turbo deploy-dev --filter='commandkit'",
"format": "prettier --write \"./{packages,apps}/**/*.{js,ts,jsx,tsx,mjs,mts,cjs,cts,css,md,mdx}\""
},
"devDependencies": {
"prettier": "^3.0.3",
"turbo": "^1.10.13"
}
}
50 changes: 42 additions & 8 deletions packages/commandkit/bin/build.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
// @ts-check

import { build } from 'tsup';
import { Colors, erase, findCommandKitJSON, panic, write } from './common.mjs';
import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
import ora from 'ora';
import { appendFile } from 'node:fs/promises';
import { join } from 'node:path';

export async function bootstrapProductionBuild(config) {
const { minify = false, outDir = 'dist', main, src } = findCommandKitJSON(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 All @@ -21,27 +30,52 @@ export async function bootstrapProductionBuild(config) {
minify,
shims: true,
banner: {
js: '/* Optimized production build of your project, generated by CommandKit */',
js: '/* Optimized production build generated by CommandKit */',
},
sourcemap: false,
sourcemap,
keepNames: true,
outDir,
silent: true,
entry: [src, '!dist', '!.commandkit'],
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
});

if (antiCrash) await injectAntiCrash(outDir, main);

status.succeed(
Colors.green(`Build completed in ${(performance.now() - start).toFixed(2)}ms!`),
);
write(
Colors.green(
`\nRun ${Colors.magenta(`node ${outDir}/${main}`)} ${Colors.green(
'to start your bot.',
)}`,
`\nRun ${Colors.magenta(`commandkit start`)} ${Colors.green('to start your bot.')}`,
),
);
} catch (e) {
status.fail(`Build failed after ${(performance.now() - start).toFixed(2)}ms!`);
panic(e);
}
}

function injectAntiCrash(outDir, main) {
const path = join(process.cwd(), outDir, main);
const snippet = [
'\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.',
" 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',
].join('\n');

return appendFile(path, snippet);
}
38 changes: 33 additions & 5 deletions packages/commandkit/bin/common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,43 @@ export function findPackageJSON() {
return JSON.parse(fs.readFileSync(target, 'utf8'));
}

export function findCommandKitJSON(src) {
const possibleFileNames = [
'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 target = src || join(cwd, 'commandkit.json');
const locations = src ? [join(cwd, src)] : possibleFileNames.map((name) => join(cwd, name));

if (!fs.existsSync(target)) {
panic('Could not find commandkit.json in current directory.');
for (const location of locations) {
try {
return await loadConfigInner(location);
} catch (e) {
continue;
}
}

return JSON.parse(fs.readFileSync(target, 'utf8'));
panic('Could not locate commandkit config.');
}

async function loadConfigInner(target) {
const isJSON = target.endsWith('.json');

/**
* @type {import('..').CommandKitConfig}
*/
const config = await import(`file://${target}`, {
assert: isJSON ? { type: 'json' } : undefined,
}).then((conf) => conf.default || conf);

return config;
}

export function erase(dir) {
Expand Down
91 changes: 0 additions & 91 deletions packages/commandkit/bin/dev-server.mjs

This file was deleted.

Loading

0 comments on commit 3084bbd

Please sign in to comment.