Skip to content

Commit

Permalink
refactor remotePathSep to use posix or win32 separator
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrungduc1410 committed Feb 28, 2021
1 parent 9fc8bc3 commit 66a22dc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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

Expand Down Expand Up @@ -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<void> {
Expand Down Expand Up @@ -345,7 +350,7 @@ export class ScpClient extends EventEmitter {
}
}

public realPath(remotePath: string) {
public realPath(remotePath: string): Promise<string> {
return new Promise((resolve, reject) => {
const closeListener = utils.makeCloseListener(this, reject, 'realPath')
this.sshClient!.prependListener('close', closeListener)
Expand Down
7 changes: 3 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 66a22dc

Please sign in to comment.