sftp-client-promise
Advanced tools
Comparing version 1.0.3 to 1.0.4
95
index.js
@@ -94,9 +94,94 @@ let fs = require('fs'); | ||
case 'mkdir': | ||
sftp.mkdir(params.path, params.attrs, (err) => { | ||
if(err) reject(err); | ||
resolve(); | ||
}); | ||
sftp.mkdir(params.path, params.attrs, (err) => { | ||
if(err){ | ||
switch(err.code){ | ||
// no such file | ||
case 2: | ||
let tokens = params.path.split(/\//).filter((path) => path.trim()); | ||
let path = '/'; | ||
let mkdir = () => { | ||
if(!tokens.length){ | ||
return resolve(); | ||
} | ||
path += tokens.shift() + '/'; | ||
sftp.exists(path, (isExists) => { | ||
if(isExists){ | ||
mkdir(); | ||
}else{ | ||
sftp.mkdir(path, (err) => { | ||
if(err){ | ||
if(err.code === 4){ | ||
reject(new Error("cannot create directory: File exists")); | ||
}else{ | ||
reject(err); | ||
} | ||
}else{ | ||
mkdir(); | ||
} | ||
}) | ||
} | ||
}); | ||
} | ||
mkdir(); | ||
break; | ||
case 4: | ||
reject(new Error("cannot create directory: File exists")); | ||
break; | ||
default: reject(err); | ||
break; | ||
} | ||
}else{ | ||
resolve(); | ||
} | ||
}); | ||
break; | ||
case 'rmdir': | ||
sftp.rmdir(params.path, resolve); | ||
sftp.rmdir(params.path, (err) => { | ||
if(err){ | ||
switch(err.code){ | ||
case 2: | ||
reject(new Error("No such file or directory")); | ||
break; | ||
case 4: | ||
let path = [params.path]; | ||
let rmdir = () => { | ||
let _path = path.pop(); | ||
if(!_path) return resolve(); | ||
sftp.readdir(_path, (err, list) => { | ||
if(err) return reject(err); | ||
if(!list.length) { | ||
return sftp.rmdir(_path, (err) => err ? reject(err) : rmdir()); | ||
} | ||
// staging dir not deleted before the subdirectory or file is processed | ||
path.push(_path); | ||
let files = []; | ||
list.map((item) => ({ | ||
path: _path.endsWith('/') ? _path + item.filename | ||
: _path + "/" + item.filename, | ||
type: item.longname.substr(0,1) | ||
})).map((item) => { | ||
if(item.type === 'd'){ | ||
path.push(item.path); | ||
}else{ | ||
files.push(item.path); | ||
} | ||
}); | ||
if(!files.length) return rmdir(); | ||
let promise = files.map((file) => this.sftp('unlink', {path: file})); | ||
Promise.all(promise).then(rmdir).catch(reject); | ||
}); | ||
}; | ||
rmdir(); | ||
break; | ||
default: | ||
reject(err); | ||
break; | ||
} | ||
}else{ | ||
resolve(); | ||
} | ||
}); | ||
break; | ||
@@ -103,0 +188,0 @@ case 'rename': |
{ | ||
"name": "sftp-client-promise", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "a SFTP client for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -125,3 +125,3 @@ ## SSH2 SFTP Client | ||
create a new directory. | ||
create new directory recursively. | ||
``` | ||
@@ -133,3 +133,3 @@ conn.sftp('mkdir', {path: remoteFilePath}); | ||
remove the directory. | ||
remove the directory recursively. | ||
``` | ||
@@ -136,0 +136,0 @@ conn.sftp('rmdir', {path: localPath}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16267
244