|
|
@ -1,37 +1,19 @@ |
|
|
|
var fs = require('fs') |
|
|
|
var fs = require('fs') |
|
|
|
, crypto = require('crypto') |
|
|
|
, crypto = require('crypto') |
|
|
|
, path = require('path') |
|
|
|
, path = require('path') |
|
|
|
|
|
|
|
, childProcess = require('child_process') |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Check if a directory exists and create it on the fly if it is not the case |
|
|
|
* Check if a directory exists and create it on the fly if it is not the case |
|
|
|
* Does it recursively |
|
|
|
|
|
|
|
* cb is optional, signature: err |
|
|
|
* cb is optional, signature: err |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function ensureDirectoryExists (dir, cb) { |
|
|
|
function ensureDirectoryExists (dir, cb) { |
|
|
|
var callback = cb || function () {} |
|
|
|
var callback = cb || function () {} |
|
|
|
, parts, currentPart |
|
|
|
|
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
if (typeof dir === 'string') { return ensureDirectoryExists({ toTreat: dir, treated: '' }, callback); } |
|
|
|
childProcess.exec('mkdir -p ' + dir, function (err) { return callback(err); }); |
|
|
|
if (dir.toTreat.length === 0) { return callback(); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parts = dir.toTreat.split(path.sep); |
|
|
|
|
|
|
|
currentPart = path.join(dir.treated, parts[0]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parts = parts.slice(1).join(path.sep); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.exists(currentPart, function (exists) { |
|
|
|
|
|
|
|
if (exists) { |
|
|
|
|
|
|
|
return ensureDirectoryExists({ toTreat: parts, treated: currentPart }, callback); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return fs.mkdir(currentPart, '0777', function (err) { |
|
|
|
|
|
|
|
if (err) { return callback(err); } |
|
|
|
|
|
|
|
return ensureDirectoryExists({ toTreat: parts, treated: currentPart }, callback); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|