flocore-node/lib/scaffold/default-config.js
Braydon Fuller e046f7294e Changes to be able to run with only a configuration file.
- Adds parameter to cli methods to be able to specify the location of services modules. This is useful for packages that wrap bitcore-node to be able to pass along a node_modules directory with services.
- Adds another parameter for including additional services in the default settings.
- Will use the `process.env.HOME + '/.bitcore` as the default configuration location.
- There are now two `getDefaultConfig`, one that will instatiate a `~/.bitcore` directory with a default if it doesn't exist, and `getBaseDefaultConfig` that will return a basic configuration without additional services enabled.
- Changes logic to use the global install if a local node_modules version is not available, this would previously assume that it was a local install because of the existence of a configuration file.
2015-10-20 12:33:53 -04:00

58 lines
1.5 KiB
JavaScript

'use strict';
var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('fs');
/**
* Will return the path and default bitcore-node configuration. It will search for the
* configuration file in the "~/.bitcore" directory, and if it doesn't exist, it will create one
* based on default settings.
* @param {Object} [options]
* @param {Array} [options.additionalServices] - An optional array of services.
*/
function getDefaultConfig(options) {
/* jshint maxstatements: 40 */
if (!options) {
options = {};
}
var defaultPath = path.resolve(process.env.HOME, './.bitcore');
var defaultConfigFile = path.resolve(defaultPath, './bitcore-node.json');
if (!fs.existsSync(defaultPath)) {
mkdirp.sync(defaultPath);
}
var defaultServices = ['bitcoind', 'db', 'address', 'web'];
if (options.additionalServices) {
defaultServices = defaultServices.concat(options.additionalServices);
}
if (!fs.existsSync(defaultConfigFile)) {
var defaultConfig = {
datadir: path.resolve(defaultPath, './data'),
network: 'livenet',
port: 3001,
services: defaultServices
};
fs.writeFileSync(defaultConfigFile, JSON.stringify(defaultConfig, null, 2));
}
var defaultDataDir = path.resolve(defaultPath, './data');
if (!fs.existsSync(defaultDataDir)) {
mkdirp.sync(defaultDataDir);
}
var config = JSON.parse(fs.readFileSync(defaultConfigFile, 'utf-8'));
return {
path: defaultPath,
config: config
};
}
module.exports = getDefaultConfig;