import {Host} from './Host'
import {ShellCommand} from './types'
import {Application, Awaitable, Filesystem, LocalFilesystem} from '@extollo/lib'
import {ExecutionResult} from './ExecutionResult'
import * as childProcess from 'child_process'

export class LocalHost extends Host {
    async execute(command: ShellCommand): Promise<ExecutionResult> {
        const result = new ExecutionResult()
        return new Promise(res => {
            childProcess.exec(command, (error, stdout, stderr) => {
                result.exit(error?.code || 0)
                result.out(stdout)
                result.error(stderr)
                res(result)
            })
        })
    }

    getFilesystem(): Awaitable<Filesystem> {
        return Application.getContainer().makeNew<LocalFilesystem>(LocalFilesystem, {
            baseDir: '/'
        })
    }
}
