Implement function to find configuration in the current path.
This commit is contained in:
parent
67a2035365
commit
0b4af2757b
@ -119,6 +119,7 @@ function create(options, done) {
|
|||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
function(next) {
|
function(next) {
|
||||||
|
// Setup the the bitcore-node directory and configuration
|
||||||
if (!fs.existsSync(absConfigDir)) {
|
if (!fs.existsSync(absConfigDir)) {
|
||||||
createConfigDirectory(absConfigDir, name, isGlobal, next);
|
createConfigDirectory(absConfigDir, name, isGlobal, next);
|
||||||
} else {
|
} else {
|
||||||
@ -126,6 +127,7 @@ function create(options, done) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
|
// Setup the bitcoin directory and configuration
|
||||||
if (!fs.existsSync(absDataDir)) {
|
if (!fs.existsSync(absDataDir)) {
|
||||||
createBitcoinDirectory(absDataDir, next);
|
createBitcoinDirectory(absDataDir, next);
|
||||||
} else {
|
} else {
|
||||||
@ -133,6 +135,7 @@ function create(options, done) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
|
// Install all of the necessary dependencies
|
||||||
if (!isGlobal) {
|
if (!isGlobal) {
|
||||||
var npm = spawn('npm', ['install'], {cwd: absConfigDir});
|
var npm = spawn('npm', ['install'], {cwd: absConfigDir});
|
||||||
|
|
||||||
|
|||||||
@ -1,2 +1,29 @@
|
|||||||
'use strict';
|
'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;
|
||||||
|
|||||||
@ -1,36 +1,79 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
var should = require('chai').should();
|
var should = require('chai').should();
|
||||||
var sinon = require('sinon');
|
var sinon = require('sinon');
|
||||||
|
var mkdirp = require('mkdirp');
|
||||||
|
var rimraf = require('rimraf');
|
||||||
|
|
||||||
|
var findConfig = require('../../lib/scaffold/find-config');
|
||||||
|
|
||||||
describe('#findConfig', function() {
|
describe('#findConfig', function() {
|
||||||
|
|
||||||
before(function() {
|
var testDir = path.resolve(__dirname, '../temporary-test-data');
|
||||||
|
var expectedConfig = {
|
||||||
|
name: 'My Node'
|
||||||
|
};
|
||||||
|
|
||||||
|
before(function(done) {
|
||||||
// setup testing directories
|
// 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
|
// cleanup testing directories
|
||||||
|
rimraf(testDir, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('will find a configuration file', function() {
|
describe('will find a configuration file', function() {
|
||||||
|
|
||||||
it('in the current directory', 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() {
|
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() {
|
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user