-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
254 lines (221 loc) · 5.93 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'use strict';
/**
* @file mkdir-recursive main
* @module mkdir-recursive
* @version 0.3.0
* @author hex7c0 <[email protected]>
* @copyright hex7c0 2015
* @license GPLv3
*/
/*
* initialize module
*/
var fs = require('fs');
var path = require('path');
/*
* exports
*/
/**
* make main. Check README.md
*
* @exports mkdir
* @function mkdir
* @param {String} root - pathname
* @param {Number} mode - directories mode, see Node documentation
* @param {Function} callback - next callback
*/
function mkdir(root, mode, callback) {
if (typeof mode === 'function') {
var callback = mode;
var mode = null;
}
if (typeof root !== 'string') {
throw new Error('missing root');
} else if (typeof callback !== 'function') {
throw new Error('missing callback');
}
var chunks = root.split(path.sep); // split in chunks
var chunk;
if (path.isAbsolute(root) === true) { // build from absolute path
chunk = chunks.shift(); // remove "/" or C:/
if (!chunk) { // add "/"
chunk = path.sep;
}
} else {
chunk = path.resolve(); // build with relative path
}
return mkdirRecursive(chunk, chunks, mode, callback);
}
module.exports.mkdir = mkdir;
/**
* makeSync main. Check README.md
*
* @exports mkdirSync
* @function mkdirSync
* @param {String} root - pathname
* @param {Number} mode - directories mode, see Node documentation
* @return [{Object}]
*/
function mkdirSync(root, mode) {
if (typeof root !== 'string') {
throw new Error('missing root');
}
var chunks = root.split(path.sep); // split in chunks
var chunk;
if (path.isAbsolute(root) === true) { // build from absolute path
chunk = chunks.shift(); // remove "/" or C:/
if (!chunk) { // add "/"
chunk = path.sep;
}
} else {
chunk = path.resolve(); // build with relative path
}
return mkdirSyncRecursive(chunk, chunks, mode);
}
module.exports.mkdirSync = mkdirSync;
/**
* remove main. Check README.md
*
* @exports rmdir
* @function rmdir
* @param {String} root - pathname
* @param {Function} callback - next callback
*/
function rmdir(root, callback) {
if (typeof root !== 'string') {
throw new Error('missing root');
} else if (typeof callback !== 'function') {
throw new Error('missing callback');
}
var chunks = root.split(path.sep); // split in chunks
var chunk = path.resolve(root); // build absolute path
// remove "/" from head and tail
if (chunks[0] === '') {
chunks.shift();
}
if (chunks[chunks.length - 1] === '') {
chunks.pop();
}
return rmdirRecursive(chunk, chunks, callback);
}
module.exports.rmdir = rmdir;
/**
* removeSync main. Check README.md
*
* @exports rmdirSync
* @function rmdirSync
* @param {String} root - pathname
* @return [{Object}]
*/
function rmdirSync(root) {
if (typeof root !== 'string') {
throw new Error('missing root');
}
var chunks = root.split(path.sep); // split in chunks
var chunk = path.resolve(root); // build absolute path
// remove "/" from head and tail
if (chunks[0] === '') {
chunks.shift();
}
if (chunks[chunks.length - 1] === '') {
chunks.pop();
}
return rmdirSyncRecursive(chunk, chunks);
}
module.exports.rmdirSync = rmdirSync;
/*
* functions
*/
/**
* make directory recursively
*
* @function mkdirRecursive
* @param {String} root - absolute root where append chunks
* @param {Array} chunks - directories chunks
* @param {Number} mode - directories mode, see Node documentation
* @param {Function} callback - next callback
*/
function mkdirRecursive(root, chunks, mode, callback) {
var chunk = chunks.shift();
if (!chunk) {
return callback(null);
}
var root = path.join(root, chunk);
return fs.exists(root, function(exists) {
if (exists === true) { // already done
return mkdirRecursive(root, chunks, mode, callback);
}
return fs.mkdir(root, mode, function(err) {
if (err && err.code !== 'EEXIST')
return callback(err);
return mkdirRecursive(root, chunks, mode, callback); // let's magic
});
});
}
/**
* make directory recursively. Sync version
*
* @function mkdirSyncRecursive
* @param {String} root - absolute root where append chunks
* @param {Array} chunks - directories chunks
* @param {Number} mode - directories mode, see Node documentation
* @return [{Object}]
*/
function mkdirSyncRecursive(root, chunks, mode) {
var chunk = chunks.shift();
if (!chunk) {
return;
}
var root = path.join(root, chunk);
if (fs.existsSync(root) === true) { // already done
return mkdirSyncRecursive(root, chunks, mode);
}
var err = fs.mkdirSync(root, mode);
return err ? err : mkdirSyncRecursive(root, chunks, mode); // let's magic
}
/**
* remove directory recursively
*
* @function rmdirRecursive
* @param {String} root - absolute root where take chunks
* @param {Array} chunks - directories chunks
* @param {Function} callback - next callback
*/
function rmdirRecursive(root, chunks, callback) {
var chunk = chunks.pop();
if (!chunk) {
return callback(null);
}
var pathname = path.join(root, '..'); // backtrack
return fs.exists(root, function(exists) {
if (exists === false) { // already done
return rmdirRecursive(root, chunks, callback);
}
return fs.rmdir(root, function(err) {
if (err) {
return callback(err);
}
return rmdirRecursive(pathname, chunks, callback); // let's magic
});
});
}
/**
* remove directory recursively. Sync version
*
* @function rmdirRecursive
* @param {String} root - absolute root where take chunks
* @param {Array} chunks - directories chunks
* @return [{Object}]
*/
function rmdirSyncRecursive(root, chunks) {
var chunk = chunks.pop();
if (!chunk) {
return;
}
var pathname = path.join(root, '..'); // backtrack
if (fs.existsSync(root) === false) { // already done
return rmdirSyncRecursive(root, chunks);
}
var err = fs.rmdirSync(root);
return err ? err : rmdirSyncRecursive(pathname, chunks); // let's magic
}