forked from krysalead/gulp-sftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear.js
55 lines (49 loc) · 1.7 KB
/
clear.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const async = require('async');
const path = require('path');
const gutil = require('gulp-util');
function log(...msg) {
msg.unshift('gulp-sftp:');
gutil.log.apply(console, msg);
}
function isDirectory(entry) {
// Save one network call for 'stat' by checking the 'd' flag in longname:
// drwxr--r-- 1 bar bar 718 Dec 8 2009 foo
return entry.longname.startsWith('d');
}
/*
* Delete contents of a directory on an SFTP server
* @param {SFTPStream} sftp
* @param {string} dirPath Remote directory
* @param {(error) -> ()} callback Callback
*/
function clearDirectory(sftp, dirPath, callback) {
function deleteEntry(entry, callback) {
var entryPath = path.posix.join(dirPath, entry.filename);
if (isDirectory(entry)) {
clearDirectory(sftp, entryPath, () => {
log(gutil.colors.green('Deleting remote directory'), entryPath);
sftp.rmdir(entryPath, callback);
});
} else {
log(gutil.colors.green('Deleting remote file'), entryPath);
sftp.unlink(entryPath, callback);
}
}
sftp.readdir(dirPath, (err, entries) => {
async.map(entries, deleteEntry, callback);
});
}
/**
* Clean the top level directory once all subdirectory and files are removed
* @param {SFTPStream} sftp
* @param {string} dirPath remote directory
* @param {(error)=>{}} callback Callback
*/
function clearAllDirectory(sftp,dirPath,callback){
function deleteTopLevelDirectory(){
//Remove the top level directory and call the caller callback
sftp.rmdir(dirPath, callback);
}
clearDirectory(sftp,dirPath,deleteTopLevelDirectory)
}
module.exports.clearDirectory = clearAllDirectory;