22import consola from "consola" ;
33import { build as esbuild } from "esbuild" ;
44import { nodeExternalsPlugin } from "esbuild-node-externals" ;
5+ import { promises as fs } from "fs" ;
56import path from "path" ;
67
78import {
@@ -18,6 +19,63 @@ interface BuildOptions {
1819 builder ?: Builder ;
1920}
2021
22+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
23+ async function compileAndRunRegisterScript ( isTS : boolean ) {
24+ const registerTsPath = path . join ( process . cwd ( ) , "scripts" , "register.ts" ) ;
25+ const registerJsPath = path . join ( process . cwd ( ) , "scripts" , "register.js" ) ;
26+ const outPath = path . join (
27+ process . cwd ( ) ,
28+ ".discraft" ,
29+ "commands" ,
30+ "register.js" ,
31+ ) ;
32+
33+ let registerFile : string | null = null ;
34+ try {
35+ await fs . access ( registerTsPath ) ;
36+ registerFile = registerTsPath ;
37+ } catch {
38+ try {
39+ await fs . access ( registerJsPath ) ;
40+ registerFile = registerJsPath ;
41+ } catch {
42+ consola . warn (
43+ "register.ts or register.js not found in scripts folder. Skipping compilation." ,
44+ ) ;
45+ return ;
46+ }
47+ }
48+
49+ consola . info ( "Compiling and running register script..." ) ;
50+
51+ try {
52+ if ( registerFile ?. endsWith ( ".ts" ) ) {
53+ // Compile register.ts to register.js
54+ await esbuild ( {
55+ entryPoints : [ registerFile ] ,
56+ outfile : outPath ,
57+ platform : "node" ,
58+ format : "esm" ,
59+ bundle : true ,
60+ plugins : [ nodeExternalsPlugin ( ) ] ,
61+ } ) ;
62+ consola . verbose ( "register.ts compiled to .discraft/commands/register.js" ) ;
63+ } else {
64+ // Copy register.js to .discraft/commands/register.js
65+ await fs . copyFile ( registerFile ! , outPath ) ;
66+ consola . verbose ( "register.js copied to .discraft/commands/register.js" ) ;
67+ }
68+
69+ // Run register.js
70+ await runSubprocess ( "node" , [ outPath ] ) ;
71+ consola . success ( "Command registration script ran successfully." ) ;
72+ } catch ( error : any ) {
73+ consola . error ( `Error during register script execution.` ) ;
74+ consola . verbose ( error ) ;
75+ consola . warn ( "Continuing build process." ) ;
76+ }
77+ }
78+
2179async function startBuild ( options ?: BuildOptions ) {
2280 consola . info ( "Starting Vercel build..." ) ;
2381 const currentWorkingDirectory = process . cwd ( ) ;
@@ -36,6 +94,9 @@ async function startBuild(options?: BuildOptions) {
3694 await generateVercelCommandsIndex ( isTS ? "ts" : "js" ) ;
3795 consola . success ( "Command index file generated." ) ;
3896
97+ // Compile & Run register.ts or register.js if it exists.
98+ await compileAndRunRegisterScript ( isTS ) ;
99+
39100 let runner : Builder = options ?. builder || "esbuild" ;
40101
41102 if ( ! options ?. builder ) {
0 commit comments