deployCommands.js

1const fs = require('fs')
2const path = require('path')
3const { spawn } = require('child_process')
4
5const BUILD_PATH = path.join(__dirname, '..', 'dist')
6const SCRIPTS_PATH = path.join(BUILD_PATH, '__scripts__')
7const DEPLOY_TS = path.join('src', '__scripts__', 'deployDiscordCommands.ts')
8const DEPLOY_JS = path.join(SCRIPTS_PATH, 'deployDiscordCommands.js')
9
10// Execute command and handle output
11const executeCommand = (command, args) => {
12	// Use local node_modules for ts-node
13	const tsNodePath = path.join(__dirname, '..', 'node_modules', '.bin', command)
14	const finalCommand = command === 'ts-node' ? tsNodePath : command
15
16	const proc = spawn(finalCommand, args, {
17		stdio: 'inherit',
18		shell: process.platform === 'win32', // Use shell on Windows
19	})
20
21	proc.on('error', (error) => {
22		console.error(`Execution error: ${error}`)
23		process.exit(1)
24	})
25
26	proc.on('exit', (code) => {
27		if (code !== 0) {
28			console.error(`Process exited with code ${code}`)
29			process.exit(code)
30		}
31	})
32}
33
34// Use ts-node for development, node for production
35const isProduction =
36	fs.existsSync(BUILD_PATH) &&
37	fs.existsSync(SCRIPTS_PATH) &&
38	fs.existsSync(DEPLOY_JS)
39
40if (isProduction) {
41	executeCommand('node', [DEPLOY_JS])
42} else {
43	executeCommand('ts-node', [DEPLOY_TS])
44}
45