2015-08-19 13:14:44 -07:00
|
|
|
'use strict';
|
|
|
|
|
2015-08-25 10:01:52 -07:00
|
|
|
var async = require('async');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var $ = bitcore.util.preconditions;
|
|
|
|
var _ = bitcore.deps._;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} configFilePath - The absolute path to the configuration file
|
2015-08-31 06:00:00 -07:00
|
|
|
* @param {String} service - The name of the service
|
2015-08-25 10:01:52 -07:00
|
|
|
* @param {Function} done
|
|
|
|
*/
|
2015-08-31 06:00:00 -07:00
|
|
|
function addConfig(configFilePath, service, done) {
|
2015-08-25 10:01:52 -07:00
|
|
|
$.checkState(path.isAbsolute(configFilePath), 'An absolute path is expected');
|
|
|
|
fs.readFile(configFilePath, function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
var config = JSON.parse(data);
|
|
|
|
$.checkState(
|
2015-08-31 06:00:00 -07:00
|
|
|
Array.isArray(config.services),
|
|
|
|
'Configuration file is expected to have a services array.'
|
2015-08-25 10:01:52 -07:00
|
|
|
);
|
2015-08-31 06:00:00 -07:00
|
|
|
config.services.push(service);
|
|
|
|
config.services = _.unique(config.services);
|
|
|
|
config.services.sort(function(a, b) {
|
2015-08-25 10:01:52 -07:00
|
|
|
return a > b;
|
|
|
|
});
|
|
|
|
fs.writeFile(configFilePath, JSON.stringify(config, null, 2), done);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} configDir - The absolute configuration directory path
|
2015-08-31 06:00:00 -07:00
|
|
|
* @param {String} service - The name of the service
|
2015-08-25 10:01:52 -07:00
|
|
|
* @param {Function} done
|
|
|
|
*/
|
2015-08-31 06:00:00 -07:00
|
|
|
function addService(configDir, service, done) {
|
2015-08-25 10:01:52 -07:00
|
|
|
$.checkState(path.isAbsolute(configDir), 'An absolute path is expected');
|
2015-08-31 06:00:00 -07:00
|
|
|
var npm = spawn('npm', ['install', service, '--save'], {cwd: configDir});
|
2015-08-25 10:01:52 -07:00
|
|
|
|
|
|
|
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) {
|
|
|
|
if (code !== 0) {
|
2015-08-31 06:00:00 -07:00
|
|
|
return done(new Error('There was an error installing service: ' + service));
|
2015-08-25 10:01:52 -07:00
|
|
|
} else {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} options.cwd - The current working directory
|
|
|
|
* @param {String} options.dirname - The bitcore-node configuration directory
|
2015-08-31 06:00:00 -07:00
|
|
|
* @param {Array} options.services - An array of strings of service names
|
2015-08-25 10:01:52 -07:00
|
|
|
* @param {Function} done - A callback function called when finished
|
|
|
|
*/
|
|
|
|
function add(options, done) {
|
|
|
|
$.checkArgument(_.isObject(options));
|
|
|
|
$.checkArgument(_.isFunction(done));
|
|
|
|
$.checkArgument(
|
|
|
|
_.isString(options.path) && path.isAbsolute(options.path),
|
|
|
|
'An absolute path is expected'
|
|
|
|
);
|
2015-08-31 06:00:00 -07:00
|
|
|
$.checkArgument(Array.isArray(options.services));
|
2015-08-25 10:01:52 -07:00
|
|
|
|
|
|
|
var configPath = options.path;
|
2015-08-31 06:00:00 -07:00
|
|
|
var services = options.services;
|
2015-08-25 10:01:52 -07:00
|
|
|
|
|
|
|
var bitcoreConfigPath = path.resolve(configPath, 'bitcore-node.json');
|
|
|
|
var packagePath = path.resolve(configPath, 'package.json');
|
|
|
|
|
|
|
|
if (!fs.existsSync(bitcoreConfigPath) || !fs.existsSync(packagePath)) {
|
|
|
|
return done(
|
|
|
|
new Error('Directory does not have a bitcore-node.json and/or package.json file.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-15 08:07:17 -07:00
|
|
|
var oldPackage = JSON.parse(fs.readFileSync(packagePath));
|
|
|
|
|
2015-08-25 10:01:52 -07:00
|
|
|
async.eachSeries(
|
2015-08-31 06:00:00 -07:00
|
|
|
services,
|
|
|
|
function(service, next) {
|
|
|
|
// npm install <service_name> --save
|
|
|
|
addService(configPath, service, function(err) {
|
2015-08-25 10:01:52 -07:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2015-09-02 15:30:35 -07:00
|
|
|
|
2015-09-15 08:07:17 -07:00
|
|
|
// get the name of the service from package.json
|
|
|
|
var updatedPackage = JSON.parse(fs.readFileSync(packagePath));
|
|
|
|
var newDependencies = _.difference(
|
|
|
|
Object.keys(updatedPackage.dependencies),
|
|
|
|
Object.keys(oldPackage.dependencies)
|
|
|
|
);
|
|
|
|
$.checkState(newDependencies.length === 1);
|
|
|
|
oldPackage = updatedPackage;
|
|
|
|
var serviceName = newDependencies[0];
|
2015-09-02 15:30:35 -07:00
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
// add service to bitcore-node.json
|
2015-09-15 08:07:17 -07:00
|
|
|
addConfig(bitcoreConfigPath, serviceName, next);
|
2015-08-25 10:01:52 -07:00
|
|
|
});
|
|
|
|
}, done
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = add;
|