Skip to content

Commit

Permalink
Merge pull request #11 from davidfig/master
Browse files Browse the repository at this point in the history
added utils.joinRemote, unlink, emptyDir, rmDir
  • Loading branch information
maitrungduc1410 authored Feb 1, 2021
2 parents f692770 + ef32594 commit 9fc8bc3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
66 changes: 61 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface IScpOptions {
username?: string
password?: string
privateKey?: Buffer | string
passphrase? : string
passphrase?: string
forceIPv4?: boolean
forceIPv6?: boolean
readyTimeout?: number
Expand Down Expand Up @@ -94,6 +94,23 @@ export class ScpClient extends EventEmitter {
})
}

public async emptyDir(dir: string): Promise<void> {
utils.haveConnection(this, 'uploadDir')
try {
const isExist = await this.exists(dir)

if (!isExist) {
await this.mkdir(dir)
} else if (isExist === 'd') {
await this.rmdir(dir)
await this.mkdir(dir)
}
}
catch (error) {
throw error
}
}

public async uploadDir(src: string, dest: string): Promise<void> {
utils.haveConnection(this, 'uploadDir')
try {
Expand All @@ -111,11 +128,11 @@ export class ScpClient extends EventEmitter {
for (const e of dirEntries) {
if (e.isDirectory()) {
const newSrc = join(src, e.name)
const newDst = join(dest, e.name)
const newDst = utils.joinRemote(this, dest, e.name)
await this.uploadDir(newSrc, newDst)
} else if (e.isFile()) {
const newSrc = join(src, e.name)
const newDst = join(dest, e.name)
const newDst = utils.joinRemote(this, dest, e.name)
await this.uploadFile(newSrc, newDst)

// this.client.emit('upload', {source: src, destination: dst})
Expand Down Expand Up @@ -145,7 +162,7 @@ export class ScpClient extends EventEmitter {
const localInfo = await utils.checkLocalPath(localPath, targetType.writeDir)

if (localInfo.valid && !localInfo.type) {
mkdirSync(localInfo.path, {recursive: true})
mkdirSync(localInfo.path, { recursive: true })
}

if (!localInfo.valid) {
Expand All @@ -161,7 +178,7 @@ export class ScpClient extends EventEmitter {
const src = remoteInfo.path + this.remotePathSep + f.name
const dst = join(localInfo.path, f.name)
await this.downloadFile(src, dst)
this.sshClient!.emit('download', {source: src, destination: dst})
this.sshClient!.emit('download', { source: src, destination: dst })
} else {
console.log(`downloadDir: File ignored: ${f.name} not regular file`)
}
Expand All @@ -185,6 +202,45 @@ export class ScpClient extends EventEmitter {
})
}

public async unlink(remotePath: string): Promise<void> {
utils.haveConnection(this, 'unlink')
return new Promise((resolve, reject) => {
this.sftpWrapper!.unlink(remotePath, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}

// _rmdir - only works with an empty directory
async _rmdir(remotePath: string): Promise<void> {
return new Promise(async (resolve, reject) => {
this.sftpWrapper!.rmdir(remotePath, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}

public async rmdir(remotePath: string): Promise<void> {
const files = await this.list(remotePath)
for (const file of files) {
const fullFilename = utils.joinRemote(this, remotePath, file.name)
if (file.type === 'd') {
await this.rmdir(fullFilename)
} else {
await this.unlink(fullFilename)
}
}
await this._rmdir(remotePath)
}

public async mkdir(remotePath: string): Promise<void> {
utils.haveConnection(this, 'mkdir')
return new Promise((resolve, reject) => {
Expand Down
14 changes: 11 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export function localAccess(localPath: string, mode: number): Promise<CheckResul
return new Promise((resolve) => {
fs.access(localPath, mode, (err) => {
if (err) {
const {msg, code} = classifyError(err, localPath)
const { msg, code } = classifyError(err, localPath)
resolve({
path: localPath,
valid: false,
Expand Down Expand Up @@ -482,7 +482,7 @@ export async function checkWriteFile(client: ScpClient, aPath: string, type: str
code: errorCode.badPath
}
} else if (!type) {
const {root, dir} = path.parse(aPath)
const { root, dir } = path.parse(aPath)
// let parentDir = path.parse(aPath).dir;
if (!dir) {
return {
Expand Down Expand Up @@ -541,7 +541,7 @@ export async function checkWriteDir(client: ScpClient, aPath: string, type: stri
code: errorCode.badPath
}
} else if (!type) {
const {root, dir} = path.parse(aPath)
const { root, dir } = path.parse(aPath)
if (root === dir) {
return {
path: aPath,
Expand Down Expand Up @@ -659,4 +659,12 @@ export function hasListener(emitter: EventEmitter, eventName: string, listenerNa
const listeners = emitter.listeners(eventName)
const matches = listeners.filter((l) => l.name === listenerName)
return matches.length === 0 ? false : true
}

export function joinRemote(client: ScpClient, ...args: string[]) {
debugger
if (client.remotePathSep === '/') {
return path.posix.join(...args)
}
return path.win32.join(...args)
}

0 comments on commit 9fc8bc3

Please sign in to comment.