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-10-18 13:28:24 -07:00
|
|
|
var bitcore = require('bitcore-lib');
|
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');
|
2015-10-19 10:41:08 -07:00
|
|
|
var defaultBaseConfig = require('./default-base-config');
|
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 = {
|
2015-10-20 13:48:51 -07:00
|
|
|
description: 'A full Bitcoin node build with Bitcore',
|
|
|
|
repository: 'https://github.com/user/project',
|
|
|
|
license: 'MIT',
|
|
|
|
readme: 'README.md',
|
2015-08-19 13:14:44 -07:00
|
|
|
dependencies: {
|
2015-10-19 05:43:51 -07:00
|
|
|
'bitcore-lib': '^' + bitcore.version,
|
2015-08-20 11:53:07 -07:00
|
|
|
'bitcore-node': version
|
2015-08-19 13:14:44 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
done();
|
2015-09-10 08:06:37 -07:00
|
|
|
|
|
|
|
// Don't create the configuration yet
|
2015-08-20 09:34:26 -07:00
|
|
|
});
|
|
|
|
}
|
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.
|
2015-10-21 12:28:28 -07:00
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.network - "testnet" or "livenet"
|
|
|
|
* @param {String} options.datadir - The bitcoin database directory
|
2015-08-20 09:34:26 -07:00
|
|
|
* @param {String} configDir - The absolute path
|
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-10-21 12:28:28 -07:00
|
|
|
function createConfigDirectory(options, configDir, 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;
|
|
|
|
}
|
2015-10-21 12:28:28 -07:00
|
|
|
var configInfo = defaultBaseConfig(options);
|
2015-09-03 13:07:35 -07:00
|
|
|
var config = configInfo.config;
|
|
|
|
|
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.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));
|
|
|
|
$.checkArgument(_.isBoolean(options.isGlobal));
|
|
|
|
$.checkArgument(_.isString(options.datadir));
|
|
|
|
|
|
|
|
var cwd = options.cwd;
|
|
|
|
var dirname = options.dirname;
|
|
|
|
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-10-21 12:28:28 -07:00
|
|
|
var createOptions = {
|
|
|
|
network: options.network,
|
|
|
|
datadir: datadir
|
|
|
|
};
|
|
|
|
createConfigDirectory(createOptions, absConfigDir, 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;
|