diff --git a/bin/start.js b/bin/start.js index 72a59a0c..f03b672f 100644 --- a/bin/start.js +++ b/bin/start.js @@ -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()); diff --git a/cli/bitcore-node.js b/cli/bitcore-node.js index d5c3d76e..168cac74 100755 --- a/cli/bitcore-node.js +++ b/cli/bitcore-node.js @@ -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 [name]') .description('Create a new node') - .action(function(dirname, name){ - var options = { + .option('-d, --datadir ', '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 ', '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); diff --git a/lib/scaffold/create.js b/lib/scaffold/create.js index c57fb71e..d2d14594 100644 --- a/lib/scaffold/create.js +++ b/lib/scaffold/create.js @@ -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.')); } diff --git a/lib/scaffold/default-config.js b/lib/scaffold/default-config.js new file mode 100644 index 00000000..b35437b1 --- /dev/null +++ b/lib/scaffold/default-config.js @@ -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; diff --git a/lib/scaffold/start.js b/lib/scaffold/start.js index ff624006..b827aedc 100644 --- a/lib/scaffold/start.js +++ b/lib/scaffold/start.js @@ -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;