diff --git a/bin/bitcore-node b/bin/bitcore-node new file mode 100755 index 00000000..9d968099 --- /dev/null +++ b/bin/bitcore-node @@ -0,0 +1,5 @@ +#!/usr/bin/env node +// vi: ft=javascript -*- mode: JavaScript; -*- + +var node = require('..'); +node.cli.bitcore(); diff --git a/cli/bitcore-node.js b/cli/bitcore-node.js deleted file mode 100755 index a99704ed..00000000 --- a/cli/bitcore-node.js +++ /dev/null @@ -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(); - -}); diff --git a/docs/index.md b/docs/index.md index 2a5363d6..7d87ebbe 100644 --- a/docs/index.md +++ b/docs/index.md @@ -26,8 +26,8 @@ Bitcore includes a Command Line Interface (CLI) for managing, configuring and in ```bash bitcore create -d mynode cd mynode -bitcore add -bitcore add https://github.com/yourname/helloworld +bitcore install +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). diff --git a/docs/scaffold.md b/docs/scaffold.md index e38c0b3a..d44b74e0 100644 --- a/docs/scaffold.md +++ b/docs/scaffold.md @@ -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. -## Create - -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 +## Install 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. -## Remove +## Uninstall This function will remove a service from a node by uninstalling the necessary dependencies and modifying the `bitcore-node.json` configuration. diff --git a/index.js b/index.js index cda89377..da3b0539 100644 --- a/index.js +++ b/index.js @@ -22,4 +22,7 @@ module.exports.scaffold.findConfig = require('./lib/scaffold/find-config'); module.exports.scaffold.defaultConfig = require('./lib/scaffold/default-config'); 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'); diff --git a/lib/cli/bitcore.js b/lib/cli/bitcore.js new file mode 100644 index 00000000..df3ea894 --- /dev/null +++ b/lib/cli/bitcore.js @@ -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; diff --git a/lib/cli/bitcored.js b/lib/cli/bitcored.js new file mode 100644 index 00000000..277e4f0a --- /dev/null +++ b/lib/cli/bitcored.js @@ -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; diff --git a/lib/cli/daemon.js b/lib/cli/daemon.js new file mode 100644 index 00000000..cd12c547 --- /dev/null +++ b/lib/cli/daemon.js @@ -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 ', '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; diff --git a/cli/main.js b/lib/cli/main.js similarity index 93% rename from cli/main.js rename to lib/cli/main.js index 8eb36763..66be97d4 100644 --- a/cli/main.js +++ b/lib/cli/main.js @@ -20,17 +20,18 @@ function main() { .version(version); program - .command('create [name]') + .command('create ') .description('Create a new node') .option('-d, --datadir ', '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) { cmd.datadir = path.resolve(process.cwd(), cmd.datadir); } var opts = { cwd: process.cwd(), dirname: dirname, - name: name, datadir: cmd.datadir || './data', isGlobal: false }; @@ -62,8 +63,8 @@ function main() { }); program - .command('add ') - .alias('install') + .command('install ') + .alias('add') .description('Install a service for the current node') .action(function(services){ var configInfo = findConfig(process.cwd()); @@ -89,8 +90,8 @@ function main() { }); program - .command('remove ') - .alias('uninstall') + .command('uninstall ') + .alias('remove') .description('Uninstall a service for the current node') .action(function(services){ var configInfo = findConfig(process.cwd()); diff --git a/lib/scaffold/create.js b/lib/scaffold/create.js index 35f41a41..12491b92 100644 --- a/lib/scaffold/create.js +++ b/lib/scaffold/create.js @@ -1,7 +1,7 @@ 'use strict'; var spawn = require('child_process').spawn; -var bitcore = require('bitcore-lib'); +var bitcore = require('bitcore'); var async = require('async'); var $ = bitcore.util.preconditions; var _ = bitcore.deps._; @@ -20,7 +20,7 @@ if (packageFile.version.match('-dev')) { var BASE_PACKAGE = { dependencies: { - 'bitcore-lib': '^' + bitcore.version, + 'bitcore': '^' + bitcore.version, 'bitcore-node': version } }; diff --git a/package.json b/package.json index 427891c8..0fe2f081 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "version": "0.2.0-dev", "lastBuild": "0.2.0-beta.11", "main": "./index.js", - "bin": "./cli/bitcore-node.js", "repository": "git://github.com/bitpay/bitcore-node.git", "homepage": "https://github.com/bitpay/bitcore-node.js", "bugs": { @@ -28,6 +27,9 @@ "email": "patrick@bitpay.com" } ], + "bin": { + "bitcore-node": "./bin/bitcore-node" + }, "scripts": { "install": "./bin/install", "build": "./bin/build", @@ -54,7 +56,6 @@ "express": "^4.13.3", "leveldown": "^1.4.1", "levelup": "^1.2.1", - "liftoff": "^2.1.0", "memdown": "^1.0.0", "mkdirp": "0.5.0", "nan": "^2.0.9",