Implement function to find configuration in the current path.

This commit is contained in:
Braydon Fuller 2015-08-20 15:43:13 -04:00
parent 67a2035365
commit 0b4af2757b
3 changed files with 81 additions and 8 deletions

View File

@ -119,6 +119,7 @@ function create(options, done) {
async.series([
function(next) {
// Setup the the bitcore-node directory and configuration
if (!fs.existsSync(absConfigDir)) {
createConfigDirectory(absConfigDir, name, isGlobal, next);
} else {
@ -126,6 +127,7 @@ function create(options, done) {
}
},
function(next) {
// Setup the bitcoin directory and configuration
if (!fs.existsSync(absDataDir)) {
createBitcoinDirectory(absDataDir, next);
} else {
@ -133,6 +135,7 @@ function create(options, done) {
}
},
function(next) {
// Install all of the necessary dependencies
if (!isGlobal) {
var npm = spawn('npm', ['install'], {cwd: absConfigDir});

View File

@ -1,2 +1,29 @@
'use strict';
var bitcore = require('bitcore');
var $ = bitcore.util.preconditions;
var _ = bitcore.deps._;
var path = require('path');
var fs = require('fs');
/**
* Will return the path and bitcore-node configuration
* @param {String} cwd - The absolute path to the current working directory
*/
function findConfig(cwd) {
$.checkArgument(_.isString(cwd), 'Argument should be a string');
$.checkArgument(path.isAbsolute(cwd), 'Argument should be an absolute path');
var directory = String(cwd);
while (!fs.existsSync(path.resolve(directory, 'bitcore-node.json'))) {
directory = path.resolve(directory, '../');
if (directory === '/') {
return false;
}
}
return {
path: directory,
config: require(path.resolve(directory, 'bitcore-node.json'))
};
}
module.exports = findConfig;

View File

@ -1,36 +1,79 @@
'use strict';
var fs = require('fs');
var path = require('path');
var should = require('chai').should();
var sinon = require('sinon');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var findConfig = require('../../lib/scaffold/find-config');
describe('#findConfig', function() {
before(function() {
var testDir = path.resolve(__dirname, '../temporary-test-data');
var expectedConfig = {
name: 'My Node'
};
before(function(done) {
// setup testing directories
mkdirp(testDir + '/p2/p1/p0', function(err) {
if (err) {
throw err;
}
fs.writeFile(
testDir + '/p2/bitcore-node.json',
JSON.stringify(expectedConfig),
function() {
mkdirp(testDir + '/e0', function(err) {
if (err) {
throw err;
}
done();
});
}
);
});
});
after(function() {
after(function(done) {
// cleanup testing directories
rimraf(testDir, function(err) {
if (err) {
throw err;
}
done();
});
});
describe('will find a configuration file', function() {
it('in the current directory', function() {
var config = findConfig(path.resolve(testDir, 'p2'));
config.path.should.equal(path.resolve(testDir, 'p2'));
config.config.should.deep.equal(expectedConfig);
});
it('in a parent directory', function() {
var config = findConfig(path.resolve(testDir, 'p2/p1'));
config.path.should.equal(path.resolve(testDir, 'p2'));
config.config.should.deep.equal(expectedConfig);
});
it('recursively find in parent directories', function() {
var config = findConfig(path.resolve(testDir, 'p2/p1/p0'));
config.path.should.equal(path.resolve(testDir, 'p2'));
config.config.should.deep.equal(expectedConfig);
});
});
it('will fallback to a default location in the home directory', function() {
it('will return false if missing a configuration', function() {
var config = findConfig(path.resolve(testDir, 'e0'));
config.should.equal(false);
});
});