-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(executors): add react native (#4)
- Loading branch information
1 parent
986dc6f
commit 2f601d4
Showing
4 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import SSHClient from 'react-native-ssh-sftp'; | ||
import { executor as Executor } from '../instance'; | ||
|
||
export type SSHConfig = { | ||
host: string; | ||
port: number; | ||
username: string; | ||
password: string; | ||
}; | ||
|
||
export class ReactNativeExecutor extends Executor.Executor<SSHConfig> { | ||
private connection: SSHClient; | ||
|
||
private ready = false; | ||
|
||
connect(): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this.connection = new SSHClient( | ||
this.config.host, | ||
this.config.port, | ||
this.config.username, | ||
this.config.password, | ||
(error) => { | ||
if (error) reject(error); | ||
this.ready = true; | ||
resolve(); | ||
} | ||
); | ||
}); | ||
} | ||
|
||
disconnect(): void { | ||
this.connection.disconnect(); | ||
} | ||
|
||
execute(command: Executor.IExecuteSimple): Promise<Executor.IExecuteResult>; | ||
|
||
execute({ command }: Executor.IExecute): Promise<Executor.IExecuteResult>; | ||
|
||
execute(command: Executor.IExecuteSimple | Executor.IExecute): Promise<Executor.IExecuteResult>; | ||
|
||
async execute(command: Executor.IExecuteSimple | Executor.IExecute): Promise<Executor.IExecuteResult> { | ||
if (!this.ready) await this.connect(); | ||
return new Promise((resolve, reject) => { | ||
if (typeof command === 'object') command = command.command; | ||
this.connection.execute(command, (error, output) => { | ||
if (error) reject(error); | ||
const response = { | ||
stderr: [], | ||
stdout: output.split('\n'), | ||
}; | ||
resolve({ | ||
...response, | ||
code: 0, | ||
signal: 0, | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters