2015-08-19 13:14:44 -07:00
|
|
|
'use strict';
|
|
|
|
|
2015-08-20 11:53:07 -07:00
|
|
|
var spawn = require('child_process').spawn;
|
2015-08-19 13:14:44 -07:00
|
|
|
var bitcore = require('bitcore');
|
2015-08-20 11:53:07 -07:00
|
|
|
var async = require('async');
|
2015-08-20 09:34:26 -07:00
|
|
|
var $ = bitcore.util.preconditions;
|
|
|
|
var _ = bitcore.deps._;
|
|
|
|
var path = require('path');
|
2015-08-20 11:53:07 -07:00
|
|
|
var packageFile = require('../../package.json');
|
2015-08-19 13:14:44 -07:00
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var BASE_CONFIG = {
|
|
|
|
name: 'My Node',
|
2015-08-31 06:00:00 -07:00
|
|
|
services: [
|
2015-08-19 13:14:44 -07:00
|
|
|
'address'
|
|
|
|
],
|
|
|
|
datadir: './data',
|
2015-08-20 14:32:25 -07:00
|
|
|
network: 'livenet',
|
|
|
|
port: 3001
|
2015-08-19 13:14:44 -07:00
|
|
|
};
|
|
|
|
|
2015-08-20 11:53:07 -07:00
|
|
|
var version;
|
|
|
|
if (packageFile.version.match('-dev')) {
|
2015-08-20 15:20:50 -07:00
|
|
|
version = '^' + packageFile.lastBuild;
|
2015-08-20 11:53:07 -07:00
|
|
|
} else {
|
|
|
|
version = '^' + packageFile.version;
|
|
|
|
}
|
|
|
|
|
2015-08-19 13:14:44 -07:00
|
|
|
var BASE_PACKAGE = {
|
|
|
|
dependencies: {
|
|
|
|
'bitcore': '^' + bitcore.version,
|
2015-08-20 11:53:07 -07:00
|
|
|
'bitcore-node': version
|
2015-08-19 13:14:44 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var BASE_BITCOIN_CONFIG = 'whitelist=127.0.0.1\n' + 'txindex=1\n';
|
|
|
|
|
2015-08-20 09:34:26 -07:00
|
|
|
/**
|
|
|
|
* Will create a directory and bitcoin.conf file for Bitcoin.
|
|
|
|
* @param {String} dataDir - The absolute path
|
|
|
|
* @param {Function} done - The callback function called when finished
|
|
|
|
*/
|
|
|
|
function createBitcoinDirectory(datadir, done) {
|
|
|
|
mkdirp(datadir, function(err) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-08-19 13:14:44 -07:00
|
|
|
|
2015-08-20 09:34:26 -07:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(datadir + '/bitcoin.conf', BASE_BITCOIN_CONFIG);
|
|
|
|
} catch(e) {
|
|
|
|
done(e);
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2015-08-19 13:14:44 -07:00
|
|
|
|
2015-08-20 09:34:26 -07:00
|
|
|
/**
|
|
|
|
* Will create a base Bitcore Node configuration directory and files.
|
|
|
|
* @param {String} configDir - The absolute path
|
|
|
|
* @param {String} name - The name of the node
|
2015-08-20 17:05:23 -07:00
|
|
|
* @param {String} datadir - The bitcoin database directory
|
2015-08-31 06:00:00 -07:00
|
|
|
* @param {Boolean} isGlobal - If the configuration depends on globally installed node services.
|
2015-08-20 09:34:26 -07:00
|
|
|
* @param {Function} done - The callback function called when finished
|
|
|
|
*/
|
2015-08-20 17:05:23 -07:00
|
|
|
function createConfigDirectory(configDir, name, datadir, isGlobal, done) {
|
2015-08-20 09:34:26 -07:00
|
|
|
mkdirp(configDir, function(err) {
|
2015-08-19 13:14:44 -07:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = BASE_CONFIG;
|
2015-08-20 11:53:07 -07:00
|
|
|
config.name = name || 'Bitcore Node';
|
2015-08-20 17:05:23 -07:00
|
|
|
config.datadir = datadir;
|
2015-08-19 13:14:44 -07:00
|
|
|
var configJSON = JSON.stringify(config, null, 2);
|
|
|
|
var packageJSON = JSON.stringify(BASE_PACKAGE, null, 2);
|
|
|
|
try {
|
2015-08-20 09:34:26 -07:00
|
|
|
fs.writeFileSync(configDir + '/bitcore-node.json', configJSON);
|
|
|
|
if (!isGlobal) {
|
|
|
|
fs.writeFileSync(configDir + '/package.json', packageJSON);
|
|
|
|
}
|
2015-08-19 13:14:44 -07:00
|
|
|
} catch(e) {
|
|
|
|
done(e);
|
|
|
|
}
|
2015-08-20 09:34:26 -07:00
|
|
|
done();
|
2015-08-19 13:14:44 -07:00
|
|
|
|
2015-08-20 09:34:26 -07:00
|
|
|
});
|
|
|
|
}
|
2015-08-19 13:14:44 -07:00
|
|
|
|
2015-08-20 09:34:26 -07:00
|
|
|
/**
|
2015-08-20 11:53:07 -07:00
|
|
|
* Will setup a directory with a Bitcore Node directory, configuration file,
|
|
|
|
* bitcoin configuration, and will install all necessary dependencies.
|
|
|
|
*
|
2015-08-20 09:34:26 -07:00
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.cwd - The current working directory
|
|
|
|
* @param {String} options.dirname - The name of the bitcore node configuration directory
|
|
|
|
* @param {String} options.name - The name of the bitcore node
|
|
|
|
* @param {String} options.datadir - The path to the bitcoin datadir
|
|
|
|
* @param {Function} done - A callback function called when finished
|
|
|
|
*/
|
|
|
|
function create(options, done) {
|
|
|
|
/* jshint maxstatements:20 */
|
|
|
|
|
|
|
|
$.checkArgument(_.isObject(options));
|
|
|
|
$.checkArgument(_.isFunction(done));
|
|
|
|
$.checkArgument(_.isString(options.cwd));
|
|
|
|
$.checkArgument(_.isString(options.dirname));
|
2015-08-20 11:53:07 -07:00
|
|
|
$.checkArgument(_.isString(options.name) || _.isUndefined(options.name));
|
2015-08-20 09:34:26 -07:00
|
|
|
$.checkArgument(_.isBoolean(options.isGlobal));
|
|
|
|
$.checkArgument(_.isString(options.datadir));
|
|
|
|
|
|
|
|
var cwd = options.cwd;
|
|
|
|
var dirname = options.dirname;
|
|
|
|
var name = options.name;
|
|
|
|
var datadir = options.datadir;
|
|
|
|
var isGlobal = options.isGlobal;
|
|
|
|
|
|
|
|
var absConfigDir = path.resolve(cwd, dirname);
|
|
|
|
var absDataDir = path.resolve(absConfigDir, datadir);
|
2015-08-19 13:14:44 -07:00
|
|
|
|
2015-08-20 11:53:07 -07:00
|
|
|
async.series([
|
|
|
|
function(next) {
|
2015-08-20 12:43:13 -07:00
|
|
|
// Setup the the bitcore-node directory and configuration
|
2015-08-20 11:53:07 -07:00
|
|
|
if (!fs.existsSync(absConfigDir)) {
|
2015-08-20 17:05:23 -07:00
|
|
|
createConfigDirectory(absConfigDir, name, datadir, isGlobal, next);
|
2015-08-20 11:53:07 -07:00
|
|
|
} else {
|
|
|
|
next(new Error('Directory "' + absConfigDir+ '" already exists.'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(next) {
|
2015-08-20 12:43:13 -07:00
|
|
|
// Setup the bitcoin directory and configuration
|
2015-08-20 09:34:26 -07:00
|
|
|
if (!fs.existsSync(absDataDir)) {
|
2015-08-20 11:53:07 -07:00
|
|
|
createBitcoinDirectory(absDataDir, next);
|
2015-08-20 09:34:26 -07:00
|
|
|
} else {
|
2015-08-20 11:53:07 -07:00
|
|
|
next();
|
2015-08-20 09:34:26 -07:00
|
|
|
}
|
2015-08-20 11:53:07 -07:00
|
|
|
},
|
|
|
|
function(next) {
|
2015-08-20 12:43:13 -07:00
|
|
|
// Install all of the necessary dependencies
|
2015-08-20 11:53:07 -07:00
|
|
|
if (!isGlobal) {
|
|
|
|
var npm = spawn('npm', ['install'], {cwd: absConfigDir});
|
|
|
|
|
|
|
|
npm.stdout.on('data', function (data) {
|
|
|
|
process.stdout.write(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
npm.stderr.on('data', function (data) {
|
|
|
|
process.stderr.write(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
npm.on('close', function (code) {
|
2015-08-25 10:15:30 -07:00
|
|
|
if (code !== 0) {
|
|
|
|
return next(new Error('There was an error installing dependencies.'));
|
|
|
|
} else {
|
|
|
|
return next();
|
|
|
|
}
|
2015-08-20 11:53:07 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], done);
|
2015-08-19 13:14:44 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = create;
|