Skip to content

Commit 8dbb0d2

Browse files
committed
Add mkdir and mkdirSync methods to create paths
1 parent 79756ce commit 8dbb0d2

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/Util.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,65 @@ var Util = (function () {
516516
}
517517
};
518518

519+
/**
520+
* Create a new directory synchronously
521+
* @param {string} path The path of the new directory
522+
* @throws {Error}
523+
* @return {boolean}
524+
*/
525+
Util.prototype.mkdirSync = function (path) {
526+
var _f = require('fs');
527+
var _p = require('path');
528+
529+
path = _p.normalize(path);
530+
531+
if (!this.existsSync(_p.dirname(path))) {
532+
this.mkdirSync(_p.dirname(path));
533+
}
534+
535+
if (_f.mkdirSync(path, 0x1ff) === false) {
536+
throw new Error('Cannot create directory "' + path + '"');
537+
}
538+
else {
539+
_f.chmodSync(path, 0x1ff);
540+
return true;
541+
}
542+
};
543+
544+
/**
545+
* Create a new directory asynchronously
546+
* @param {string} path The path of the new directory
547+
* @param {function} callback The callback
548+
* @throws {Error}
549+
* @return {boolean}
550+
*/
551+
Util.prototype.mkdir = function (path, callback) {
552+
var _f = require('fs');
553+
var _p = require('path');
554+
555+
path = _p.normalize(path);
556+
callback = callback || function() {};
557+
558+
this.exists(path, function(exists) {
559+
if (!exists) {
560+
this.mkdir(_p.dirname(path));
561+
}
562+
563+
_f.mkdir(path, 0x1ff, function(error) {
564+
if (error) {
565+
callback(error);
566+
}
567+
_f.chmod(path, 0x1ff, function(error) {
568+
if (error) {
569+
callback(error);
570+
}
571+
callback(null);
572+
});
573+
})
574+
}.bind(this));
575+
576+
};
577+
519578
return Util;
520579
})();
521580

0 commit comments

Comments
 (0)