forked from sindresorhus/jsftp-mkdirp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (44 loc) · 930 Bytes
/
index.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
56
'use strict';
var parents = require('parents');
function mkdirp(dir, cb) {
if (typeof dir !== 'string') {
throw new Error('`path` is required.');
}
cb = cb || function () {};
var dirs = parents(dir);
// skip root as it's always there
if (dirs[dirs.length - 1] === '/') {
dirs.pop();
}
if (dirs.length === 0) {
return cb();
}
var mkdir = function (dir) {
dir = dir.replace(/\\/g, '/');
this.raw.mkd(dir, function (err) {
if (err && (err.code === 550|| err.code === 521) ) {
if (dirs.length > 0) {
return mkdir(dirs.pop());
}
return cb();
}
if (err) {
err.message += ' - mkd: ' + dir;
// return cb(err);
}
if (dirs.length > 0) {
return mkdir(dirs.pop());
}
cb();
});
}.bind(this);
mkdir(dirs.pop());
}
module.exports = function (JSFtp) {
JSFtp.prototype = Object.create(JSFtp.prototype, {
mkdirp: {
value: mkdirp
}
});
return JSFtp;
};