From 66a22dc3b29c2048965ba7f433321af01ac089b6 Mon Sep 17 00:00:00 2001 From: Mai Trung Duc Date: Sun, 28 Feb 2021 11:06:46 +0800 Subject: [PATCH] refactor remotePathSep to use posix or win32 separator --- package-lock.json | 2 +- package.json | 2 +- src/index.ts | 11 ++++++++--- src/utils.ts | 7 +++---- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 092d6c5..a0f1d5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-scp", - "version": "0.0.11", + "version": "0.0.14", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index bf11016..3846256 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "node-scp", "title": "SCP module for NodeJS", "description": "Lightweight, fast and secure SCP function for NodeJS", - "version": "0.0.11", + "version": "0.0.14", "main": "lib/index.js", "types": "lib/index.d.ts", "license": "MIT", diff --git a/src/index.ts b/src/index.ts index def490b..0003da8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events' import { mkdirSync, readdirSync, existsSync } from 'fs' -import { join } from 'path' +import { join, win32, posix } from 'path' import { Client as SSHClient, SFTPWrapper } from 'ssh2' import { Stats } from 'ssh2-streams' import { targetType } from './constant' @@ -18,12 +18,13 @@ export interface IScpOptions { readyTimeout?: number keepaliveInterval?: number keepaliveCountMax?: number + remoteOsType?: 'posix' | 'win32' } export class ScpClient extends EventEmitter { sftpWrapper: SFTPWrapper | null = null private sshClient: SSHClient | null = null - remotePathSep = '/' + remotePathSep = posix.sep endCalled = false errorHandled = false @@ -66,6 +67,10 @@ export class ScpClient extends EventEmitter { ssh.connect(options) this.sshClient = ssh + + if (options.remoteOsType === 'win32') { + this.remotePathSep = win32.sep + } } public async uploadFile(localPath: string, remotePath: string): Promise { @@ -345,7 +350,7 @@ export class ScpClient extends EventEmitter { } } - public realPath(remotePath: string) { + public realPath(remotePath: string): Promise { return new Promise((resolve, reject) => { const closeListener = utils.makeCloseListener(this, reject, 'realPath') this.sshClient!.prependListener('close', closeListener) diff --git a/src/utils.ts b/src/utils.ts index 149bcc0..41a0590 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -662,9 +662,8 @@ export function hasListener(emitter: EventEmitter, eventName: string, listenerNa } export function joinRemote(client: ScpClient, ...args: string[]) { - debugger - if (client.remotePathSep === '/') { - return path.posix.join(...args) + if (client.remotePathSep === path.win32.sep) { + return path.win32.join(...args) } - return path.win32.join(...args) + return path.posix.join(...args) } \ No newline at end of file