1+ import type { RunBunCommandOptions , RunBunCommandResult } from '@onlook/models' ;
12import { exec } from 'child_process' ;
23import { app } from 'electron' ;
34import path from 'path' ;
5+ import { promisify } from 'util' ;
46import { __dirname } from '../index' ;
57import { replaceCommand } from './parse' ;
68
9+ const execAsync = promisify ( exec ) ;
10+
711export const getBunExecutablePath = ( ) : string => {
812 const arch = process . arch === 'arm64' ? 'aarch64' : process . arch ;
913 const isProduction = app . isPackaged ;
@@ -16,60 +20,29 @@ export const getBunExecutablePath = (): string => {
1620 return bunPath ;
1721} ;
1822
19- export interface RunBunCommandOptions {
20- cwd : string ;
21- env ?: NodeJS . ProcessEnv ;
22- callbacks ?: {
23- onStdout ?: ( data : string ) => void ;
24- onStderr ?: ( data : string ) => void ;
25- onClose ?: ( code : number | null , signal : string | null ) => void ;
26- onError ?: ( err : Error ) => void ;
27- } ;
28- }
29-
30- export const runBunCommand = (
23+ export async function runBunCommand (
3124 command : string ,
3225 options : RunBunCommandOptions ,
33- ) : Promise < { stdout : string ; stderr : string } > => {
34- const commandToExecute = getBunCommand ( command ) ;
35- const shell = process . platform === 'win32' ? 'powershell.exe' : 'bash' ;
36-
37- return new Promise ( ( resolve , reject ) => {
38- exec (
39- commandToExecute ,
40- {
41- cwd : options . cwd ,
42- env : {
43- ...options . env ,
44- ...process . env ,
45- } ,
46- maxBuffer : 1024 * 1024 * 10 ,
47- shell,
48- } ,
49- ( error : Error | null , stdout : string , stderr : string ) => {
50- // Call the callbacks with the complete output
51- if ( stdout && options . callbacks ?. onStdout ) {
52- options . callbacks . onStdout ( stdout ) ;
53- }
54-
55- if ( stderr && options . callbacks ?. onStderr ) {
56- options . callbacks . onStderr ( stderr ) ;
57- }
58-
59- if ( error ) {
60- options . callbacks ?. onError ?.( error ) ;
61- reject (
62- new Error ( `Process exited with error: ${ error . message } \nStderr: ${ stderr } ` ) ,
63- ) ;
64- return ;
65- }
66-
67- options . callbacks ?. onClose ?.( 0 , null ) ;
68- resolve ( { stdout, stderr } ) ;
69- } ,
70- ) ;
71- } ) ;
72- } ;
26+ ) : Promise < RunBunCommandResult > {
27+ try {
28+ const commandToExecute = getBunCommand ( command ) ;
29+ const shell = process . platform === 'win32' ? 'powershell.exe' : '/bin/sh' ;
30+
31+ console . log ( 'Executing command: ' , commandToExecute , options . cwd ) ;
32+ const { stdout, stderr } = await execAsync ( commandToExecute , {
33+ cwd : options . cwd ,
34+ maxBuffer : 1024 * 1024 * 10 ,
35+ env : options . env ,
36+ shell,
37+ } ) ;
38+
39+ console . log ( 'Command executed with output: ' , stdout ) ;
40+ return { success : true , output : stdout . toString ( ) , error : stderr . toString ( ) } ;
41+ } catch ( error ) {
42+ console . error ( error ) ;
43+ return { success : false , error : error instanceof Error ? error . message : 'Unknown error' } ;
44+ }
45+ }
7346
7447export const getBunCommand = ( command : string ) : string => {
7548 const bunExecutable = getBunExecutablePath ( ) ;
0 commit comments