Skip to content

Commit f9812c0

Browse files
committed
feat: move cli code to commandkit
1 parent 6bf4288 commit f9812c0

File tree

14 files changed

+200
-192
lines changed

14 files changed

+200
-192
lines changed

packages/commandkit/bin/index.mjs

+2-50
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,5 @@
11
#!/usr/bin/env node
22

3-
// @ts-check
3+
import { bootstrapCommandkitCLI } from '../dist/index.mjs'
44

5-
import { Command } from 'commander';
6-
import { bootstrapDevelopmentServer } from './development.mjs';
7-
import { bootstrapProductionServer } from './production.mjs';
8-
import { bootstrapProductionBuild } from './build.mjs';
9-
10-
const program = new Command('commandkit');
11-
12-
program
13-
.command('dev')
14-
.description('Start your bot in development mode.')
15-
.option(
16-
'-c, --config <path>',
17-
'Path to your commandkit config file.',
18-
'./commandkit.js',
19-
)
20-
.action(() => {
21-
const options = program.opts();
22-
bootstrapDevelopmentServer(options);
23-
});
24-
25-
program
26-
.command('start')
27-
.description(
28-
'Start your bot in production mode after running the build command.',
29-
)
30-
.option(
31-
'-c, --config <path>',
32-
'Path to your commandkit.json file.',
33-
'./commandkit.js',
34-
)
35-
.action(() => {
36-
const options = program.opts();
37-
bootstrapProductionServer(options.config);
38-
});
39-
40-
program
41-
.command('build')
42-
.description('Build your project for production usage.')
43-
.option(
44-
'-c, --config <path>',
45-
'Path to your commandkit.json file.',
46-
'./commandkit.json',
47-
)
48-
.action(() => {
49-
const options = program.opts();
50-
bootstrapProductionBuild(options.config);
51-
});
52-
53-
program.parse(process.argv);
5+
await bootstrapCommandkitCLI(process.argv);

packages/commandkit/package.json

+3-13
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@
77
"module": "./dist/index.mjs",
88
"types": "./dist/index.d.ts",
99
"bin": "./bin/index.mjs",
10-
"exports": {
11-
".": {
12-
"require": "./dist/index.js",
13-
"import": "./dist/index.mjs",
14-
"types": "./dist/index.d.ts"
15-
},
16-
"./cli": {
17-
"require": "./dist/environment/cli.js",
18-
"import": "./dist/environment/cli.mjs",
19-
"types": "./dist/environment/cli.d.ts"
20-
}
21-
},
2210
"files": [
2311
"dist",
2412
"bin"
@@ -55,6 +43,8 @@
5543
"tsup": "^8.3.5"
5644
},
5745
"devDependencies": {
46+
"@types/babel__generator": "^7.6.8",
47+
"@types/babel__traverse": "^7.20.6",
5848
"@types/ms": "^0.7.34",
5949
"@types/node": "^22.10.2",
6050
"@types/yargs": "^17.0.32",
@@ -71,4 +61,4 @@
7161
"engines": {
7262
"node": ">=22"
7363
}
74-
}
64+
}

packages/commandkit/bin/build.mjs renamed to packages/commandkit/src/cli/build.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
import { readFile, writeFile } from 'node:fs/promises';
44
import { join } from 'node:path';
55
import { build } from 'tsup';
6-
import {
7-
Colors,
8-
erase,
9-
findCommandKitConfig,
10-
panic,
11-
write,
12-
} from './common.mjs';
6+
import { Colors, erase, findCommandKitConfig, panic, write } from './common.js';
137
import ora from 'ora';
14-
import { commandkitPlugin } from './esbuild-plugins/plugin.mjs';
8+
import { commandkitPlugin } from './esbuild-plugins/plugin';
159

16-
export async function bootstrapProductionBuild(config) {
10+
export async function bootstrapProductionBuild(config: any) {
1711
const {
1812
sourcemap = false,
1913
minify = false,
@@ -72,7 +66,12 @@ export async function bootstrapProductionBuild(config) {
7266
}
7367
}
7468

75-
export async function injectShims(outDir, main, antiCrash, polyfillRequire) {
69+
export async function injectShims(
70+
outDir: string,
71+
main: string,
72+
antiCrash: boolean,
73+
polyfillRequire: boolean,
74+
) {
7675
const path = join(process.cwd(), outDir, main);
7776

7877
const head = ['\n\n;await (async()=>{', " 'use strict';"].join('\n');

packages/commandkit/bin/common.mjs renamed to packages/commandkit/src/cli/common.ts

+33-32
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@ import fs from 'node:fs';
77
const resetColor = '\x1b[0m';
88

99
export const Colors = {
10-
reset: (text) => `${text}${resetColor}`,
11-
bright: (text) => `\x1b[1m${text}${resetColor}`,
12-
dim: (text) => `\x1b[2m${text}${resetColor}`,
13-
underscore: (text) => `\x1b[4m${text}${resetColor}`,
14-
blink: (text) => `\x1b[5m${text}${resetColor}`,
15-
reverse: (text) => `\x1b[7m${text}${resetColor}`,
16-
hidden: (text) => `\x1b[8m${text}${resetColor}`,
17-
18-
black: (text) => `\x1b[30m${text}${resetColor}`,
19-
red: (text) => `\x1b[31m${text}${resetColor}`,
20-
green: (text) => `\x1b[32m${text}${resetColor}`,
21-
yellow: (text) => `\x1b[33m${text}${resetColor}`,
22-
blue: (text) => `\x1b[34m${text}${resetColor}`,
23-
magenta: (text) => `\x1b[35m${text}${resetColor}`,
24-
cyan: (text) => `\x1b[36m${text}${resetColor}`,
25-
white: (text) => `\x1b[37m${text}${resetColor}`,
26-
27-
bgBlack: (text) => `\x1b[40m${text}${resetColor}`,
28-
bgRed: (text) => `\x1b[41m${text}${resetColor}`,
29-
bgGreen: (text) => `\x1b[42m${text}${resetColor}`,
30-
bgYellow: (text) => `\x1b[43m${text}${resetColor}`,
31-
bgBlue: (text) => `\x1b[44m${text}${resetColor}`,
32-
bgMagenta: (text) => `\x1b[45m${text}${resetColor}`,
33-
bgCyan: (text) => `\x1b[46m${text}${resetColor}`,
34-
bgWhite: (text) => `\x1b[47m${text}${resetColor}`,
10+
reset: (text: string) => `${text}${resetColor}`,
11+
bright: (text: string) => `\x1b[1m${text}${resetColor}`,
12+
dim: (text: string) => `\x1b[2m${text}${resetColor}`,
13+
underscore: (text: string) => `\x1b[4m${text}${resetColor}`,
14+
blink: (text: string) => `\x1b[5m${text}${resetColor}`,
15+
reverse: (text: string) => `\x1b[7m${text}${resetColor}`,
16+
hidden: (text: string) => `\x1b[8m${text}${resetColor}`,
17+
18+
black: (text: string) => `\x1b[30m${text}${resetColor}`,
19+
red: (text: string) => `\x1b[31m${text}${resetColor}`,
20+
green: (text: string) => `\x1b[32m${text}${resetColor}`,
21+
yellow: (text: string) => `\x1b[33m${text}${resetColor}`,
22+
blue: (text: string) => `\x1b[34m${text}${resetColor}`,
23+
magenta: (text: string) => `\x1b[35m${text}${resetColor}`,
24+
cyan: (text: string) => `\x1b[36m${text}${resetColor}`,
25+
white: (text: string) => `\x1b[37m${text}${resetColor}`,
26+
27+
bgBlack: (text: string) => `\x1b[40m${text}${resetColor}`,
28+
bgRed: (text: string) => `\x1b[41m${text}${resetColor}`,
29+
bgGreen: (text: string) => `\x1b[42m${text}${resetColor}`,
30+
bgYellow: (text: string) => `\x1b[43m${text}${resetColor}`,
31+
bgBlue: (text: string) => `\x1b[44m${text}${resetColor}`,
32+
bgMagenta: (text: string) => `\x1b[45m${text}${resetColor}`,
33+
bgCyan: (text: string) => `\x1b[46m${text}${resetColor}`,
34+
bgWhite: (text: string) => `\x1b[47m${text}${resetColor}`,
3535
};
3636

37-
export function write(message) {
37+
export function write(message: any) {
3838
process.stdout.write(message);
3939
process.stdout.write('\n');
4040
}
4141

4242
/**
4343
* @returns {never}
4444
*/
45-
export function panic(message) {
45+
export function panic(message: any) {
4646
write(Colors.red(`Error: ${message}`));
4747
process.exit(1);
4848
}
@@ -72,7 +72,7 @@ const possibleFileNames = [
7272
'commandkit.cts',
7373
];
7474

75-
export async function findCommandKitConfig(src) {
75+
export async function findCommandKitConfig(src: string) {
7676
const cwd = process.cwd();
7777
const locations = src
7878
? [join(cwd, src)]
@@ -89,7 +89,7 @@ export async function findCommandKitConfig(src) {
8989
panic(`Could not locate commandkit config from ${cwd}`);
9090
}
9191

92-
function ensureTypeScript(target) {
92+
function ensureTypeScript(target: string) {
9393
const isTypeScript = /\.(c|m)tsx?$/.test(target);
9494

9595
if (isTypeScript && !process.features.typescript) {
@@ -99,7 +99,7 @@ function ensureTypeScript(target) {
9999
}
100100
}
101101

102-
async function loadConfigInner(target) {
102+
async function loadConfigInner(target: string) {
103103
const isJSON = target.endsWith('.json');
104104

105105
await ensureExists(target);
@@ -109,17 +109,18 @@ async function loadConfigInner(target) {
109109
/**
110110
* @type {import('..').CommandKitConfig}
111111
*/
112+
// @ts-ignore
112113
const config = await import(`file://${target}`, {
113114
assert: isJSON ? { type: 'json' } : undefined,
114115
}).then((conf) => conf.default || conf);
115116

116117
return config;
117118
}
118119

119-
async function ensureExists(loc) {
120+
async function ensureExists(loc: string) {
120121
await fs.promises.access(loc, fs.constants.F_OK);
121122
}
122123

123-
export function erase(dir) {
124+
export function erase(dir: string) {
124125
rimrafSync(dir);
125126
}

packages/commandkit/bin/development.mjs renamed to packages/commandkit/src/cli/development.ts

+22-31
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22
import { config as dotenv } from 'dotenv';
33
import { join } from 'node:path';
44
import { build } from 'tsup';
5-
import {
6-
Colors,
7-
erase,
8-
findCommandKitConfig,
9-
panic,
10-
write,
11-
} from './common.mjs';
12-
import { parseEnv } from './parse-env.mjs';
5+
import { Colors, erase, findCommandKitConfig, panic, write } from './common';
6+
import { parseEnv } from './parse-env';
137
import child_process from 'node:child_process';
148
import ora from 'ora';
15-
import { injectShims } from './build.mjs';
16-
import { commandkitPlugin } from './esbuild-plugins/plugin.mjs';
9+
import { injectShims } from './build';
10+
import { commandkitPlugin } from './esbuild-plugins/plugin';
1711

1812
const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
1913
const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;
2014

21-
export async function bootstrapDevelopmentServer(opts) {
15+
export async function bootstrapDevelopmentServer(opts: any) {
2216
const {
2317
src,
2418
main,
@@ -93,7 +87,6 @@ export async function bootstrapDevelopmentServer(opts) {
9387

9488
const env = dotenv({
9589
path: join(process.cwd(), '.env'),
96-
// @ts-expect-error
9790
processEnv,
9891
});
9992

@@ -109,25 +102,23 @@ export async function bootstrapDevelopmentServer(opts) {
109102
write(Colors.blue('[DOTENV] Loaded .env file!'));
110103
}
111104

112-
/**
113-
* @type {child_process.ChildProcessWithoutNullStreams}
114-
*/
115-
const ps = child_process.spawn(
116-
'node',
117-
[...nodeOptions, join(process.cwd(), '.commandkit', main)],
118-
{
119-
env: {
120-
...process.env,
121-
...processEnv,
122-
NODE_ENV: 'development',
123-
// @ts-expect-error
124-
COMMANDKIT_DEV: true,
125-
// @ts-expect-error
126-
COMMANDKIT_PRODUCTION: false,
105+
const ps: child_process.ChildProcessWithoutNullStreams =
106+
child_process.spawn(
107+
'node',
108+
[...nodeOptions, join(process.cwd(), '.commandkit', main)],
109+
{
110+
env: {
111+
...process.env,
112+
...processEnv,
113+
NODE_ENV: 'development',
114+
// @ts-expect-error
115+
COMMANDKIT_DEV: true,
116+
// @ts-expect-error
117+
COMMANDKIT_PRODUCTION: false,
118+
},
119+
cwd: process.cwd(),
127120
},
128-
cwd: process.cwd(),
129-
},
130-
);
121+
);
131122

132123
let isLastLogRestarting = false,
133124
hasStarted = false;
@@ -182,6 +173,6 @@ export async function bootstrapDevelopmentServer(opts) {
182173
status.fail(
183174
`Error occurred after ${(performance.now() - start).toFixed(2)}ms!\n`,
184175
);
185-
panic(e.stack ?? e);
176+
panic(e instanceof Error ? e.stack : e);
186177
}
187178
}

0 commit comments

Comments
 (0)