Created
August 31, 2010 21:18
-
-
Save c4milo/559769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var sys = require('sys'); | |
var EventEmitter = require('events').EventEmitter; | |
var mkpath = function mkpath(_path, callback) { | |
var self = this; | |
var dirs = _path.split('/'); | |
var d = './'; | |
var entry = -1; | |
var length = dirs.length - 1; | |
self.on('nextdir', function() { | |
entry++; | |
if(entry > length) { | |
callback(); | |
return; | |
} | |
d += dirs[entry] + '/'; | |
self.emit('mkpath', d); | |
}); | |
self.on('mkpath', function(d) { | |
path.exists(d, function(exists) { | |
if(!exists) { | |
fs.mkdir(d, 0755, function() { | |
self.emit('nextdir'); | |
}); | |
} | |
}); | |
}); | |
self.emit('nextdir'); | |
}; | |
sys.inherits(mkpath, EventEmitter); | |
fs.mkpath = function(_path, callback) { | |
return new mkpath(_path, callback); | |
}; | |
fs.mkpathSync = function(_path) { | |
var dirs = _path.split('/'); | |
var d = './'; | |
for(dir in dirs) { | |
d += dirs[dir] + '/'; | |
if(!path.existsSync(d)) { | |
fs.mkdirSync(d, 0755); | |
} | |
} | |
}; | |
var dir = 'tmp/cosa/eso/prebuilt'; | |
fs.mkpath(dir, function() { | |
console.log('path created'); | |
}); | |
//fs.mkpathSync(dir, 0755); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment