-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkdirs.js
39 lines (36 loc) · 1018 Bytes
/
mkdirs.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
/**
* @Author: Created By McChen
* @Date: 2016/7/11
* @Mail: [email protected]
* @Version: V1.0.0
*/
(function(){
var fs = require('fs');
var path = require('path');
var mkdirs = {
// 异步创建,无法保证先后执行回调的顺序
async: function (dirname, callback) {
fs.exists(dirname, function (exists){
if(exists){
callback();
}else{
mkdirs.async(path.dirname(dirname), function (){
fs.mkdir(dirname, callback);
});
}
});
},
// 同步创建,可保证顺序
sync: function (dirname) {
if(fs.existsSync(dirname)){
return true;
}else{
if(mkdirs.sync(path.dirname(dirname))){
fs.mkdirSync(dirname);
return true;
}
}
}
};
module.exports = mkdirs;
})();