mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Initial release v0.2.5
This commit is contained in:
commit
bbf66c259b
158 files changed
+37115
No files matched your search
@@ -0,0 +1,35 @@
|
||||
type LogLevel = 'info' | 'warn' | 'error' | 'debug'
|
||||
|
||||
class Logger {
|
||||
private prefix: string
|
||||
|
||||
constructor(prefix: string = '') {
|
||||
this.prefix = prefix
|
||||
}
|
||||
|
||||
private format(level: LogLevel, message: string, ...args: unknown[]): string {
|
||||
const timestamp = new Date().toISOString()
|
||||
const prefixStr = this.prefix ? `[${this.prefix}] ` : ''
|
||||
return `[${timestamp}] [${level.toUpperCase()}] ${prefixStr}${message}`
|
||||
}
|
||||
|
||||
info(message: string, ...args: unknown[]): void {
|
||||
console.log(this.format('info', message), ...args)
|
||||
}
|
||||
|
||||
warn(message: string, ...args: unknown[]): void {
|
||||
console.warn(this.format('warn', message), ...args)
|
||||
}
|
||||
|
||||
error(message: string, ...args: unknown[]): void {
|
||||
console.error(this.format('error', message), ...args)
|
||||
}
|
||||
|
||||
debug(message: string, ...args: unknown[]): void {
|
||||
if (process.env.DEBUG) {
|
||||
console.debug(this.format('debug', message), ...args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const logger = new Logger()
|
||||
@@ -0,0 +1,42 @@
|
||||
import { spawn, type ChildProcess } from 'child_process'
|
||||
import { logger } from './logger'
|
||||
|
||||
export async function executeCommand(
|
||||
args: string[],
|
||||
cwd?: string
|
||||
): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const [command, ...cmdArgs] = args
|
||||
|
||||
const proc: ChildProcess = spawn(command || '', cmdArgs, {
|
||||
cwd,
|
||||
shell: false
|
||||
})
|
||||
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
|
||||
proc.stdout?.on('data', (data: Buffer) => {
|
||||
stdout += data.toString()
|
||||
})
|
||||
|
||||
proc.stderr?.on('data', (data: Buffer) => {
|
||||
stderr += data.toString()
|
||||
})
|
||||
|
||||
proc.on('error', (error: Error) => {
|
||||
logger.error(`Command failed: ${args.join(' ')}`, error)
|
||||
reject(error)
|
||||
})
|
||||
|
||||
proc.on('close', (code: number | null) => {
|
||||
if (code === 0) {
|
||||
resolve(stdout)
|
||||
} else {
|
||||
const error = new Error(`Command failed with code ${code}: ${stderr || stdout}`)
|
||||
logger.error(`Command failed: ${args.join(' ')}`, error)
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user