diff --git a/lib/scaffold/create.js b/lib/scaffold/create.js index ba3714d2..b5c01a75 100644 --- a/lib/scaffold/create.js +++ b/lib/scaffold/create.js @@ -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}); diff --git a/lib/scaffold/find-config.js b/lib/scaffold/find-config.js index eb109abb..6a947ad7 100644 --- a/lib/scaffold/find-config.js +++ b/lib/scaffold/find-config.js @@ -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; diff --git a/test/scaffold/find-config.integration.js b/test/scaffold/find-config.integration.js index 17cb4621..c1032736 100644 --- a/test/scaffold/find-config.integration.js +++ b/test/scaffold/find-config.integration.js @@ -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); }); });