directory structure.

This commit is contained in:
Christopher Jeffrey 2016-05-23 20:30:09 -07:00
parent 1c2e1b8000
commit 1011d1f824
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 49 additions and 21 deletions

View File

@ -208,14 +208,14 @@ Environment.prototype.set = function set(options) {
options = utils.merge({}, options);
options.prefix = options.prefix
|| process.env.BCOIN_PREFIX
|| utils.HOME + '/.bcoin';
options.network = options.network
|| process.env.BCOIN_NETWORK
|| 'main';
options.prefix = options.prefix
|| process.env.BCOIN_PREFIX
|| utils.HOME + '/.bcoin/' + options.network;
if (!options.db)
options.db = process.env.BCOIN_DB;
@ -244,7 +244,7 @@ Environment.prototype.set = function set(options) {
options.workerTimeout = +process.env.BCOIN_WORKER_TIMEOUT;
if (options.debugFile && typeof options.debugFile !== 'string')
options.debugFile = options.prefix + '/debug-' + options.network + '.log';
options.debugFile = options.prefix + '/debug.log';
this.prefix = options.prefix;
this.networkType = options.network;
@ -284,14 +284,7 @@ Environment.prototype.ensurePrefix = function ensurePrefix() {
this._ensured = true;
try {
fs.statSync(this.prefix);
} catch (e) {
if (e.code === 'ENOENT')
fs.mkdirSync(this.prefix, 488 /* 0750 */);
else
throw e;
}
mkdirp(this.prefix);
};
/**
@ -376,7 +369,7 @@ Environment.prototype.write = function write(msg) {
this._debug = fs.createWriteStream(this.debugFile, { flags: 'a' });
}
this._debug.write(process.pid + ': ' + msg + '\n');
this._debug.write(process.pid + ' (' + utils.date() + '): ' + msg + '\n');
};
/**
@ -388,6 +381,47 @@ Environment.prototype.now = function now() {
return this.time.now();
};
/**
* Create a full directory structure.
* @param {String} dir
*/
function mkdirp(dir) {
var path = require('path');
var i, parts, name;
if (!fs)
return;
dir = path.normalize(dir);
dir = dir.replace(/\\/g, '/');
dir = dir.replace(/\/$/, '');
parts = dir.split('/');
for (i = 0; i < parts.length; i++) {
if (i === 0) {
if (process.platform === 'win32') {
if (parts[0].indexOf(':') !== -1)
continue;
} else {
if (parts[0].length === 0)
continue;
}
}
name = parts.slice(0, i + 1).join('/');
try {
fs.statSync(name);
} catch (e) {
if (e.code === 'ENOENT')
fs.mkdirSync(name, 488 /* 0750 */);
else
throw e;
}
}
}
/*
* Expose by converting `exports` to an
* Environment.

View File

@ -117,13 +117,7 @@ ldb.getLocation = function getLocation(options) {
if (options.location)
return options.location;
return bcoin.prefix
+ '/'
+ options.name
+ '-'
+ bcoin.network.get(options.network).type
+ '.'
+ backend.ext;
return bcoin.prefix + '/' + options.name + '.' + backend.ext;
};
/**