diff --git a/CHANGELOG.md b/CHANGELOG.md index 85fd639..8a8b52e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +#### 4.2.0 + +- Fix a typo in README +- Add support for passing direct options to `ssh2.exec` + #### 4.1.0 - Add sftp `opts` support in `getFile`, `putFile`, `putFiles` and `putDirectory` diff --git a/README.md b/README.md index 26fef79..034899b 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ ssh.connect({ console.log('STDERR: ' + result.stderr) }) // Command with escaped params - ssh.exec('hh_client', ['--json'], { cwd: '/var/www', stream: 'stdout' }).then(function(result) { + ssh.exec('hh_client', ['--json'], { cwd: '/var/www', stream: 'stdout', options: { pty: true } }).then(function(result) { console.log('STDOUT: ' + result) }) }) @@ -93,8 +93,8 @@ class SSH{ requestSFTP(): Promise requestShell(): Promise mkdir(path: string): Promise - exec(command: string, parameters: Array, options: { cwd?: string, stdin?: string, stream?: 'stdout' | 'stderr', 'both' } = {}): Promise - execCommand(command: string, options: { cwd: string, stdin: string } = {}): Promise<{ stdout: string, stderr: string, signal: ?string, code: number }> + exec(command: string, parameters: Array, options: { cwd?: string, options?: Object, stdin?: string, stream?: 'stdout' | 'stderr', 'both' } = {}): Promise + execCommand(command: string, options: { cwd: string, stdin: string } = {}): Promise<{ stdout: string, options?: Object, stderr: string, signal: ?string, code: number }> putFile(localFile: string, remoteFile: string, sftp: ?Object = null, opts: ?Object = null): Promise getFile(localFile: string, remoteFile: string, sftp: ?Object = null, opts: ?Object = null): Promise putFiles(files: Array<{ local: string, remote: string }>, sftp: ?Object = null, maxAtOnce: number = 5, opts: ?Object = null): Promise diff --git a/src/index.js b/src/index.js index 36b3f86..731523e 100644 --- a/src/index.js +++ b/src/index.js @@ -75,12 +75,14 @@ class SSH { } } } - async exec(command: string, parameters: Array = [], options: { cwd?: string, stdin?: string, stream?: string } = {}): Promise { + async exec(command: string, parameters: Array = [], options: { cwd?: string, stdin?: string, stream?: string, options?: Object } = {}): Promise { invariant(this.connection, 'Not connected to server') invariant(typeof options === 'object' && options, 'options must be an Object') invariant(!options.cwd || typeof options.cwd === 'string', 'options.cwd must be a string') invariant(!options.stdin || typeof options.stdin === 'string', 'options.stdin must be a string') invariant(!options.stream || ['stdout', 'stderr', 'both'].indexOf(options.stream) !== -1, 'options.stream must be among "stdout", "stderr" and "both"') + invariant(!options.options || typeof options.options === 'object', 'options.options must be an object') + const output = await this.execCommand([command].concat(shellEscape(parameters)).join(' '), options) if (!options.stream || options.stream === 'stdout') { if (output.stderr) { @@ -93,13 +95,14 @@ class SSH { } return output } - async execCommand(givenCommand: string, options: { cwd?: string, stdin?: string } = {}): Promise<{ stdout: string, stderr: string, code: number, signal: ?string }> { + async execCommand(givenCommand: string, options: { cwd?: string, stdin?: string, options?: Object } = {}): Promise<{ stdout: string, stderr: string, code: number, signal: ?string }> { let command = givenCommand const connection = this.connection invariant(connection, 'Not connected to server') invariant(typeof options === 'object' && options, 'options must be an Object') invariant(!options.cwd || typeof options.cwd === 'string', 'options.cwd must be a string') invariant(!options.stdin || typeof options.stdin === 'string', 'options.stdin must be a string') + invariant(!options.options || typeof options.options === 'object', 'options.options must be an object') if (options.cwd) { // NOTE: Output piping cd command to hide directory non-existent errors @@ -121,7 +124,7 @@ class SSH { stream.on('close', function(code, signal) { resolve({ code, signal, stdout: output.stdout.join('').trim(), stderr: output.stderr.join('').trim() }) }) - }, reject)) + }, reject), options.options || {}) }) } async getFile(localFile: string, remoteFile: string, givenSftp: ?Object = null, givenOpts: ?Object = null): Promise {