Include more options and fallback to default config with start command
This commit is contained in:
parent
be525b055d
commit
348598747b
43
bin/start.js
43
bin/start.js
@ -1,45 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var start = require('../lib/scaffold/start');
|
var start = require('../lib/scaffold/start');
|
||||||
var path = require('path');
|
var defaultConfig = require('../lib/scaffold/default-config');
|
||||||
|
|
||||||
start({
|
start(defaultConfig());
|
||||||
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}));
|
|
||||||
|
|||||||
@ -4,28 +4,34 @@
|
|||||||
|
|
||||||
var program = require('commander');
|
var program = require('commander');
|
||||||
var version = require(__dirname + '/../package.json').version;
|
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 create = require('../lib/scaffold/create');
|
||||||
var add = require('../lib/scaffold/add');
|
var add = require('../lib/scaffold/add');
|
||||||
var start = require('../lib/scaffold/start');
|
var start = require('../lib/scaffold/start');
|
||||||
var findConfig = require('../lib/scaffold/find-config');
|
var findConfig = require('../lib/scaffold/find-config');
|
||||||
|
var defaultConfig = require('../lib/scaffold/default-config');
|
||||||
|
|
||||||
program
|
program
|
||||||
.version(version)
|
.version(version);
|
||||||
.option('-d, --datadir', 'Database and configuration directory')
|
|
||||||
.option('-t, --testnet', 'Enable testnet network');
|
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('create <directory> [name]')
|
.command('create <directory> [name]')
|
||||||
.description('Create a new node')
|
.description('Create a new node')
|
||||||
.action(function(dirname, name){
|
.option('-d, --datadir <dir>', 'Specify the bitcoin database directory')
|
||||||
var options = {
|
.action(function(dirname, name, cmd){
|
||||||
|
if (cmd.datadir) {
|
||||||
|
cmd.datadir = path.resolve(process.cwd(), cmd.datadir);
|
||||||
|
}
|
||||||
|
var opts = {
|
||||||
cwd: process.cwd(),
|
cwd: process.cwd(),
|
||||||
dirname: dirname,
|
dirname: dirname,
|
||||||
name: name,
|
name: name,
|
||||||
datadir: './data',
|
datadir: cmd.datadir || './data',
|
||||||
isGlobal: false
|
isGlobal: false
|
||||||
};
|
};
|
||||||
create(options, function(err) {
|
create(opts, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
@ -36,13 +42,16 @@ program
|
|||||||
program
|
program
|
||||||
.command('start')
|
.command('start')
|
||||||
.description('Start the current node')
|
.description('Start the current node')
|
||||||
.action(function(){
|
.option('-c, --config <dir>', 'Specify the directory with Bitcore Node configuration')
|
||||||
var configInfo = findConfig(process.cwd());
|
.action(function(cmd){
|
||||||
if (configInfo) {
|
if (cmd.config) {
|
||||||
start(configInfo);
|
cmd.config = path.resolve(process.cwd(), cmd.config);
|
||||||
} else {
|
|
||||||
throw new Error('Can not find bitcore-node.json in current path');
|
|
||||||
}
|
}
|
||||||
|
var configInfo = findConfig(cmd.config || process.cwd());
|
||||||
|
if (!configInfo) {
|
||||||
|
configInfo = defaultConfig();
|
||||||
|
}
|
||||||
|
start(configInfo);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
@ -51,6 +60,9 @@ program
|
|||||||
.description('Install a module for the current node')
|
.description('Install a module for the current node')
|
||||||
.action(function(module){
|
.action(function(module){
|
||||||
var config = findConfig();
|
var config = findConfig();
|
||||||
|
if (!config) {
|
||||||
|
throw new Error('Could not find configuration, see `bitcore-node create --help`');
|
||||||
|
}
|
||||||
add(config, module);
|
add(config, module);
|
||||||
console.log('Successfully added module: ', module);
|
console.log('Successfully added module: ', module);
|
||||||
console.log(module);
|
console.log(module);
|
||||||
|
|||||||
@ -60,10 +60,11 @@ function createBitcoinDirectory(datadir, done) {
|
|||||||
* Will create a base Bitcore Node configuration directory and files.
|
* Will create a base Bitcore Node configuration directory and files.
|
||||||
* @param {String} configDir - The absolute path
|
* @param {String} configDir - The absolute path
|
||||||
* @param {String} name - The name of the node
|
* @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 {Boolean} isGlobal - If the configuration depends on globally installed node modules.
|
||||||
* @param {Function} done - The callback function called when finished
|
* @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) {
|
mkdirp(configDir, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
@ -71,6 +72,7 @@ function createConfigDirectory(configDir, name, isGlobal, done) {
|
|||||||
|
|
||||||
var config = BASE_CONFIG;
|
var config = BASE_CONFIG;
|
||||||
config.name = name || 'Bitcore Node';
|
config.name = name || 'Bitcore Node';
|
||||||
|
config.datadir = datadir;
|
||||||
var configJSON = JSON.stringify(config, null, 2);
|
var configJSON = JSON.stringify(config, null, 2);
|
||||||
var packageJSON = JSON.stringify(BASE_PACKAGE, null, 2);
|
var packageJSON = JSON.stringify(BASE_PACKAGE, null, 2);
|
||||||
try {
|
try {
|
||||||
@ -121,7 +123,7 @@ function create(options, done) {
|
|||||||
function(next) {
|
function(next) {
|
||||||
// Setup the the bitcore-node directory and configuration
|
// Setup the the bitcore-node directory and configuration
|
||||||
if (!fs.existsSync(absConfigDir)) {
|
if (!fs.existsSync(absConfigDir)) {
|
||||||
createConfigDirectory(absConfigDir, name, isGlobal, next);
|
createConfigDirectory(absConfigDir, name, datadir, isGlobal, next);
|
||||||
} else {
|
} else {
|
||||||
next(new Error('Directory "' + absConfigDir+ '" already exists.'));
|
next(new Error('Directory "' + absConfigDir+ '" already exists.'));
|
||||||
}
|
}
|
||||||
|
|||||||
20
lib/scaffold/default-config.js
Normal file
20
lib/scaffold/default-config.js
Normal 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;
|
||||||
@ -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;
|
module.exports = start;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user