Expose cli methods.

This commit is contained in:
Braydon Fuller 2015-10-17 08:15:25 -04:00
parent 1dec93edd4
commit 58894bc4b2
11 changed files with 142 additions and 64 deletions

5
bin/bitcore-node Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env node
// vi: ft=javascript -*- mode: JavaScript; -*-
var node = require('..');
node.cli.bitcore();

View File

@ -1,44 +0,0 @@
#!/usr/bin/env node
'use strict';
var semver = require('semver');
var Liftoff = require('liftoff');
var cliPackage = require('../package.json');
var liftoff = new Liftoff({
name: 'bitcore-node',
moduleName: 'bitcore-node',
configName: 'bitcore-node',
processTitle: 'bitcore-node'
}).on('require', function (name, module) {
console.log('Loading:', name);
}).on('requireFail', function (name, err) {
console.log('Unable to load:', name, err);
}).on('respawn', function (flags, child) {
console.log('Detected node flags:', flags);
console.log('Respawned to PID:', child.pid);
});
liftoff.launch({
cwd: process.cwd()
}, function(env){
var bitcorenode;
if (env.modulePackage && env.configPath) {
// use the local version
if (semver.gt(cliPackage.version, env.modulePackage.version)) {
throw new Error(
'Version mismatch, global bitcore-node is ' + cliPackage.version +
' and local bitcore-node is ' + env.modulePackage.version
);
}
bitcorenode = require(env.modulePath);
} else {
// use the global version
bitcorenode = require('..');
}
bitcorenode.cli.main();
});

View File

@ -26,8 +26,8 @@ Bitcore includes a Command Line Interface (CLI) for managing, configuring and in
```bash ```bash
bitcore create -d <bitcoin-data-dir> mynode bitcore create -d <bitcoin-data-dir> mynode
cd mynode cd mynode
bitcore add <service> bitcore install <service>
bitcore add https://github.com/yourname/helloworld bitcore install https://github.com/yourname/helloworld
``` ```
This will create a directory with configuration files for your node and install the necessary dependencies. For more information about (and developing) services, please see the [Service Documentation](services.md). This will create a directory with configuration files for your node and install the necessary dependencies. For more information about (and developing) services, please see the [Service Documentation](services.md).

View File

@ -6,11 +6,7 @@ description: Description of functions for Bitcore Node configuration
A collection of functions for creating, managing, starting, stopping and interacting with a Bitcore Node. A collection of functions for creating, managing, starting, stopping and interacting with a Bitcore Node.
## Create ## Install
This function will create a new directory and the initial configuration files/directories, including 'bitcore-node.json', 'package.json', 'bitcoin.conf', install the necessary Node.js modules, and create a data directory.
## Add
This function will add a service to a node by installing the necessary dependencies and modifying the `bitcore-node.json` configuration. This function will add a service to a node by installing the necessary dependencies and modifying the `bitcore-node.json` configuration.
@ -26,7 +22,7 @@ This function will recursively find a configuration `bitcore-node.json` file in
This function will return a default configuration with the default services based on environment variables, and will default to using the standard `~/.bitcoin` data directory. This function will return a default configuration with the default services based on environment variables, and will default to using the standard `~/.bitcoin` data directory.
## Remove ## Uninstall
This function will remove a service from a node by uninstalling the necessary dependencies and modifying the `bitcore-node.json` configuration. This function will remove a service from a node by uninstalling the necessary dependencies and modifying the `bitcore-node.json` configuration.

View File

@ -22,4 +22,7 @@ module.exports.scaffold.findConfig = require('./lib/scaffold/find-config');
module.exports.scaffold.defaultConfig = require('./lib/scaffold/default-config'); module.exports.scaffold.defaultConfig = require('./lib/scaffold/default-config');
module.exports.cli = {}; module.exports.cli = {};
module.exports.cli.main = require('./cli/main'); module.exports.cli.main = require('./lib/cli/main');
module.exports.cli.daemon = require('./lib/cli/daemon');
module.exports.cli.bitcore = require('./lib/cli/bitcore');
module.exports.cli.bitcored = require('./lib/cli/bitcored');

40
lib/cli/bitcore.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
var Liftoff = require('liftoff');
function main() {
var liftoff = new Liftoff({
name: 'bitcore',
moduleName: 'bitcore-node',
configName: 'bitcore-node',
processTitle: 'bitcore'
}).on('require', function (name, module) {
console.log('Loading:', name);
}).on('requireFail', function (name, err) {
console.log('Unable to load:', name, err);
}).on('respawn', function (flags, child) {
console.log('Detected node flags:', flags);
console.log('Respawned to PID:', child.pid);
});
liftoff.launch({
cwd: process.cwd()
}, function(env){
var node;
if (env.modulePackage && env.configPath) {
// use the configured version
node = require(env.modulePath);
} else {
// use this version
node = require('..');
}
node.cli.main();
});
}
module.exports = main;

40
lib/cli/bitcored.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
var Liftoff = require('liftoff');
function main() {
var liftoff = new Liftoff({
name: 'bitcored',
moduleName: 'bitcore-node',
configName: 'bitcore-node',
processTitle: 'bitcored'
}).on('require', function (name, module) {
console.log('Loading:', name);
}).on('requireFail', function (name, err) {
console.log('Unable to load:', name, err);
}).on('respawn', function (flags, child) {
console.log('Detected node flags:', flags);
console.log('Respawned to PID:', child.pid);
});
liftoff.launch({
cwd: process.cwd()
}, function(env){
var node;
if (env.modulePackage && env.configPath) {
// use the configured version
node = require(env.modulePath);
} else {
// use this version
node = require('..');
}
node.cli.daemon();
});
}
module.exports = main;

36
lib/cli/daemon.js Normal file
View File

@ -0,0 +1,36 @@
'use strict';
var program = require('commander');
var path = require('path');
var bitcore = require('..');
function main() {
/* jshint maxstatements: 100 */
var version = bitcore.version;
var start = bitcore.scaffold.start;
var findConfig = bitcore.scaffold.findConfig;
var defaultConfig = bitcore.scaffold.defaultConfig;
program
.version(version)
.description('Start the current node')
.option('-c, --config <dir>', 'Specify the directory with Bitcore Node configuration')
.option('-d, --daemon', 'Make bitcore a daemon (running in the background)');
program.parse(process.argv);
if (program.config) {
program.config = path.resolve(process.cwd(), program.config);
}
var configInfo = findConfig(program.config || process.cwd());
if (!configInfo) {
configInfo = defaultConfig();
}
if(program.daemon) {
configInfo.config.daemon = true;
}
start(configInfo);
}
module.exports = main;

View File

@ -20,17 +20,18 @@ function main() {
.version(version); .version(version);
program program
.command('create <directory> [name]') .command('create <directory>')
.description('Create a new node') .description('Create a new node')
.option('-d, --datadir <dir>', 'Specify the bitcoin database directory') .option('-d, --datadir <dir>', 'Specify the bitcoin database directory')
.action(function(dirname, name, cmd){ .action(function(dirname, cmd){
console.log('dirname', dirname);
console.log('cmd', cmd);
if (cmd.datadir) { if (cmd.datadir) {
cmd.datadir = path.resolve(process.cwd(), cmd.datadir); cmd.datadir = path.resolve(process.cwd(), cmd.datadir);
} }
var opts = { var opts = {
cwd: process.cwd(), cwd: process.cwd(),
dirname: dirname, dirname: dirname,
name: name,
datadir: cmd.datadir || './data', datadir: cmd.datadir || './data',
isGlobal: false isGlobal: false
}; };
@ -62,8 +63,8 @@ function main() {
}); });
program program
.command('add <services...>') .command('install <services...>')
.alias('install') .alias('add')
.description('Install a service for the current node') .description('Install a service for the current node')
.action(function(services){ .action(function(services){
var configInfo = findConfig(process.cwd()); var configInfo = findConfig(process.cwd());
@ -89,8 +90,8 @@ function main() {
}); });
program program
.command('remove <services...>') .command('uninstall <services...>')
.alias('uninstall') .alias('remove')
.description('Uninstall a service for the current node') .description('Uninstall a service for the current node')
.action(function(services){ .action(function(services){
var configInfo = findConfig(process.cwd()); var configInfo = findConfig(process.cwd());

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
var bitcore = require('bitcore-lib'); var bitcore = require('bitcore');
var async = require('async'); var async = require('async');
var $ = bitcore.util.preconditions; var $ = bitcore.util.preconditions;
var _ = bitcore.deps._; var _ = bitcore.deps._;
@ -20,7 +20,7 @@ if (packageFile.version.match('-dev')) {
var BASE_PACKAGE = { var BASE_PACKAGE = {
dependencies: { dependencies: {
'bitcore-lib': '^' + bitcore.version, 'bitcore': '^' + bitcore.version,
'bitcore-node': version 'bitcore-node': version
} }
}; };

View File

@ -5,7 +5,6 @@
"version": "0.2.0-dev", "version": "0.2.0-dev",
"lastBuild": "0.2.0-beta.11", "lastBuild": "0.2.0-beta.11",
"main": "./index.js", "main": "./index.js",
"bin": "./cli/bitcore-node.js",
"repository": "git://github.com/bitpay/bitcore-node.git", "repository": "git://github.com/bitpay/bitcore-node.git",
"homepage": "https://github.com/bitpay/bitcore-node.js", "homepage": "https://github.com/bitpay/bitcore-node.js",
"bugs": { "bugs": {
@ -28,6 +27,9 @@
"email": "patrick@bitpay.com" "email": "patrick@bitpay.com"
} }
], ],
"bin": {
"bitcore-node": "./bin/bitcore-node"
},
"scripts": { "scripts": {
"install": "./bin/install", "install": "./bin/install",
"build": "./bin/build", "build": "./bin/build",
@ -54,7 +56,6 @@
"express": "^4.13.3", "express": "^4.13.3",
"leveldown": "^1.4.1", "leveldown": "^1.4.1",
"levelup": "^1.2.1", "levelup": "^1.2.1",
"liftoff": "^2.1.0",
"memdown": "^1.0.0", "memdown": "^1.0.0",
"mkdirp": "0.5.0", "mkdirp": "0.5.0",
"nan": "^2.0.9", "nan": "^2.0.9",