Include more options and fallback to default config with start command

This commit is contained in:
Braydon Fuller 2015-08-20 20:05:23 -04:00
parent be525b055d
commit 348598747b
5 changed files with 82 additions and 56 deletions

View File

@ -1,45 +1,6 @@
'use strict';
var start = require('../lib/scaffold/start');
var path = require('path');
var defaultConfig = require('../lib/scaffold/default-config');
start({
path: process.cwd(),
config: {
datadir: process.env.BITCORENODE_DIR || path.resolve(process.env.HOME, '.bitcoin'),
network: process.env.BITCORENODE_NETWORK || 'livenet',
port: process.env.BITCORENODE_PORT || 3001
}
});
node.on('stopping', function() {
clearInterval(interval);
});
function exitHandler(options, err) {
if (err) {
log.error('uncaught exception:', err);
if(err.stack) {
console.log(err.stack);
}
process.exit(-1);
}
if (options.sigint) {
node.stop(function(err) {
if(err) {
log.error('Failed to stop services: ' + err);
return process.exit(1);
}
log.info('Halted');
process.exit(0);
});
}
}
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {sigint:true}));
start(defaultConfig());

View File

@ -4,28 +4,34 @@
var program = require('commander');
var version = require(__dirname + '/../package.json').version;
var bitcore = require('bitcore');
var $ = bitcore.util.preconditions;
var path = require('path');
var create = require('../lib/scaffold/create');
var add = require('../lib/scaffold/add');
var start = require('../lib/scaffold/start');
var findConfig = require('../lib/scaffold/find-config');
var defaultConfig = require('../lib/scaffold/default-config');
program
.version(version)
.option('-d, --datadir', 'Database and configuration directory')
.option('-t, --testnet', 'Enable testnet network');
.version(version);
program
.command('create <directory> [name]')
.description('Create a new node')
.action(function(dirname, name){
var options = {
.option('-d, --datadir <dir>', 'Specify the bitcoin database directory')
.action(function(dirname, name, cmd){
if (cmd.datadir) {
cmd.datadir = path.resolve(process.cwd(), cmd.datadir);
}
var opts = {
cwd: process.cwd(),
dirname: dirname,
name: name,
datadir: './data',
datadir: cmd.datadir || './data',
isGlobal: false
};
create(options, function(err) {
create(opts, function(err) {
if (err) {
throw err;
}
@ -36,13 +42,16 @@ program
program
.command('start')
.description('Start the current node')
.action(function(){
var configInfo = findConfig(process.cwd());
if (configInfo) {
start(configInfo);
} else {
throw new Error('Can not find bitcore-node.json in current path');
.option('-c, --config <dir>', 'Specify the directory with Bitcore Node configuration')
.action(function(cmd){
if (cmd.config) {
cmd.config = path.resolve(process.cwd(), cmd.config);
}
var configInfo = findConfig(cmd.config || process.cwd());
if (!configInfo) {
configInfo = defaultConfig();
}
start(configInfo);
});
program
@ -51,6 +60,9 @@ program
.description('Install a module for the current node')
.action(function(module){
var config = findConfig();
if (!config) {
throw new Error('Could not find configuration, see `bitcore-node create --help`');
}
add(config, module);
console.log('Successfully added module: ', module);
console.log(module);

View File

@ -60,10 +60,11 @@ function createBitcoinDirectory(datadir, done) {
* Will create a base Bitcore Node configuration directory and files.
* @param {String} configDir - The absolute path
* @param {String} name - The name of the node
* @param {String} datadir - The bitcoin database directory
* @param {Boolean} isGlobal - If the configuration depends on globally installed node modules.
* @param {Function} done - The callback function called when finished
*/
function createConfigDirectory(configDir, name, isGlobal, done) {
function createConfigDirectory(configDir, name, datadir, isGlobal, done) {
mkdirp(configDir, function(err) {
if (err) {
throw err;
@ -71,6 +72,7 @@ function createConfigDirectory(configDir, name, isGlobal, done) {
var config = BASE_CONFIG;
config.name = name || 'Bitcore Node';
config.datadir = datadir;
var configJSON = JSON.stringify(config, null, 2);
var packageJSON = JSON.stringify(BASE_PACKAGE, null, 2);
try {
@ -121,7 +123,7 @@ function create(options, done) {
function(next) {
// Setup the the bitcore-node directory and configuration
if (!fs.existsSync(absConfigDir)) {
createConfigDirectory(absConfigDir, name, isGlobal, next);
createConfigDirectory(absConfigDir, name, datadir, isGlobal, next);
} else {
next(new Error('Directory "' + absConfigDir+ '" already exists.'));
}

View File

@ -0,0 +1,20 @@
'use strict';
var path = require('path');
/**
* Will return the path and default bitcore-node configuration on environment variables
* or default locations.
*/
function getDefaultConfig() {
return {
path: process.cwd(),
config: {
datadir: process.env.BITCORENODE_DIR || path.resolve(process.env.HOME, '.bitcoin'),
network: process.env.BITCORENODE_NETWORK || 'livenet',
port: process.env.BITCORENODE_PORT || 3001
}
};
}
module.exports = getDefaultConfig;

View File

@ -177,6 +177,37 @@ function start(options) {
}
});
node.on('stopping', function() {
clearInterval(interval);
});
function exitHandler(options, err) {
if (err) {
log.error('uncaught exception:', err);
if(err.stack) {
console.log(err.stack);
}
process.exit(-1);
}
if (options.sigint) {
node.stop(function(err) {
if(err) {
log.error('Failed to stop services: ' + err);
return process.exit(1);
}
log.info('Halted');
process.exit(0);
});
}
}
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {sigint:true}));
}
module.exports = start;