feat(config): detect and move outdated config, replace with new default
This commit is contained in:
parent
e3b96a2d32
commit
33d6559305
@ -9,7 +9,7 @@ var path = require('path');
|
|||||||
var packageFile = require('../../package.json');
|
var packageFile = require('../../package.json');
|
||||||
var mkdirp = require('mkdirp');
|
var mkdirp = require('mkdirp');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var defaultBaseConfig = require('./default-base-config');
|
var defaultConfig = require('./default-config');
|
||||||
|
|
||||||
var version = '^' + packageFile.version;
|
var version = '^' + packageFile.version;
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ function createConfigDirectory(options, configDir, isGlobal, done) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
var configInfo = defaultBaseConfig(options);
|
var configInfo = defaultConfig(options);
|
||||||
var config = configInfo.config;
|
var config = configInfo.config;
|
||||||
|
|
||||||
var configJSON = JSON.stringify(config, null, 2);
|
var configJSON = JSON.stringify(config, null, 2);
|
||||||
|
|||||||
@ -1,34 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var path = require('path');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Will return the path and default bitcore-node configuration on environment variables
|
|
||||||
* or default locations.
|
|
||||||
* @param {Object} options
|
|
||||||
* @param {String} options.network - "testnet" or "livenet"
|
|
||||||
* @param {String} options.datadir - Absolute path to bitcoin database directory
|
|
||||||
*/
|
|
||||||
function getDefaultBaseConfig(options) {
|
|
||||||
if (!options) {
|
|
||||||
options = {};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
path: process.cwd(),
|
|
||||||
config: {
|
|
||||||
network: options.network || 'livenet',
|
|
||||||
port: 3001,
|
|
||||||
services: ['bitcoind', 'web'],
|
|
||||||
servicesConfig: {
|
|
||||||
bitcoind: {
|
|
||||||
spawn: {
|
|
||||||
datadir: options.datadir || path.resolve(process.env.HOME, '.bitcoin'),
|
|
||||||
exec: path.resolve(__dirname, '../../bin/bitcoind')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = getDefaultBaseConfig;
|
|
||||||
@ -1,8 +1,7 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var mkdirp = require('mkdirp');
|
var mkdirp = require('mkdirp');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
var package = require('../../package');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will return the path and default bitcore-node configuration. It will search for the
|
* Will return the path and default bitcore-node configuration. It will search for the
|
||||||
@ -24,7 +23,34 @@ function getDefaultConfig(options) {
|
|||||||
mkdirp.sync(defaultPath);
|
mkdirp.sync(defaultPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultServices = [
|
if (fs.existsSync(defaultConfigFile)) {
|
||||||
|
const currentConfig = require(defaultConfigFile);
|
||||||
|
|
||||||
|
function getMajorVersion(versionString) {
|
||||||
|
return parseInt(versionString.split('.')[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// config must have a `version` field with major equal to package major version
|
||||||
|
if(currentConfig.version && getMajorVersion(package.version) === getMajorVersion(currentConfig.version)) {
|
||||||
|
return {
|
||||||
|
path: defaultPath,
|
||||||
|
config: config
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`The configuration file at '${defaultConfigFile}' is incompatible with this version of Bitcore.`);
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
// bitcore-node.YYYY-MM-DD.UnixTimestamp.json
|
||||||
|
const backupFileName = `bitcore-node.${now.getUTCFullYear() + '-' + now.getUTCMonth() + '-' + now.getUTCDate() + '.' + now.getTime()}.json`;
|
||||||
|
const backupFile = path.resolve(defaultPath, backupFileName);
|
||||||
|
fs.renameSync(defaultConfigFile, backupFile);
|
||||||
|
console.log(`The previous configuration file has been moved to: ${backupFile}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Creating a new configuration file at: ${defaultConfigFile}.`);
|
||||||
|
|
||||||
|
const defaultServices = [
|
||||||
'address',
|
'address',
|
||||||
'block',
|
'block',
|
||||||
'db',
|
'db',
|
||||||
@ -37,33 +63,28 @@ function getDefaultConfig(options) {
|
|||||||
'web'
|
'web'
|
||||||
];
|
];
|
||||||
|
|
||||||
if (options.additionalServices) {
|
|
||||||
defaultServices = defaultServices.concat(options.additionalServices);
|
|
||||||
}
|
|
||||||
|
|
||||||
var defaultDataDir = path.resolve(defaultPath, './data');
|
var defaultDataDir = path.resolve(defaultPath, './data');
|
||||||
|
|
||||||
if (!fs.existsSync(defaultDataDir)) {
|
if (!fs.existsSync(defaultDataDir)) {
|
||||||
mkdirp.sync(defaultDataDir);
|
mkdirp.sync(defaultDataDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(defaultConfigFile)) {
|
var defaultConfig = {
|
||||||
var defaultConfig = {
|
version: package.version,
|
||||||
network: 'testnet',
|
network: 'livenet',
|
||||||
port: 3001,
|
port: 3001,
|
||||||
services: defaultServices,
|
services: options.additionalServices ? defaultServices.concat(options.additionalServices) : defaultServices,
|
||||||
datadir: defaultDataDir,
|
datadir: defaultDataDir,
|
||||||
servicesConfig: {
|
servicesConfig: {
|
||||||
'insight-api': {
|
'insight-api': {
|
||||||
cwdRequirePath: 'node_modules/insight-api'
|
cwdRequirePath: 'node_modules/insight-api'
|
||||||
},
|
},
|
||||||
'insight-ui': {
|
'insight-ui': {
|
||||||
cwdRequirePath: 'node_modules/insight-ui'
|
cwdRequirePath: 'node_modules/insight-ui'
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
fs.writeFileSync(defaultConfigFile, JSON.stringify(defaultConfig, null, 2));
|
};
|
||||||
}
|
fs.writeFileSync(defaultConfigFile, JSON.stringify(defaultConfig, null, 2));
|
||||||
|
|
||||||
var config = JSON.parse(fs.readFileSync(defaultConfigFile, 'utf-8'));
|
var config = JSON.parse(fs.readFileSync(defaultConfigFile, 'utf-8'));
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
"node": ">=8.2.0"
|
"node": ">=8.2.0"
|
||||||
},
|
},
|
||||||
"author": "BitPay <dev@bitpay.com>",
|
"author": "BitPay <dev@bitpay.com>",
|
||||||
"version": "4.0.0",
|
"version": "5.0.0",
|
||||||
"main": "./index.js",
|
"main": "./index.js",
|
||||||
"repository": "git://github.com/bitpay/bitcore-node.git",
|
"repository": "git://github.com/bitpay/bitcore-node.git",
|
||||||
"homepage": "https://github.com/bitpay/bitcore-node",
|
"homepage": "https://github.com/bitpay/bitcore-node",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user