From 33d6559305a44e0db0655cd8815b0d3cfa37474c Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Tue, 8 Aug 2017 17:16:25 -0400 Subject: [PATCH 1/3] feat(config): detect and move outdated config, replace with new default --- lib/scaffold/create.js | 4 +- lib/scaffold/default-base-config.js | 34 --------------- lib/scaffold/default-config.js | 67 +++++++++++++++++++---------- package.json | 2 +- 4 files changed, 47 insertions(+), 60 deletions(-) delete mode 100644 lib/scaffold/default-base-config.js diff --git a/lib/scaffold/create.js b/lib/scaffold/create.js index c521602e..49cee1e6 100644 --- a/lib/scaffold/create.js +++ b/lib/scaffold/create.js @@ -9,7 +9,7 @@ var path = require('path'); var packageFile = require('../../package.json'); var mkdirp = require('mkdirp'); var fs = require('fs'); -var defaultBaseConfig = require('./default-base-config'); +var defaultConfig = require('./default-config'); var version = '^' + packageFile.version; @@ -55,7 +55,7 @@ function createConfigDirectory(options, configDir, isGlobal, done) { if (err) { throw err; } - var configInfo = defaultBaseConfig(options); + var configInfo = defaultConfig(options); var config = configInfo.config; var configJSON = JSON.stringify(config, null, 2); diff --git a/lib/scaffold/default-base-config.js b/lib/scaffold/default-base-config.js deleted file mode 100644 index 1f584b55..00000000 --- a/lib/scaffold/default-base-config.js +++ /dev/null @@ -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; diff --git a/lib/scaffold/default-config.js b/lib/scaffold/default-config.js index 304df232..0621aa06 100644 --- a/lib/scaffold/default-config.js +++ b/lib/scaffold/default-config.js @@ -1,8 +1,7 @@ -'use strict'; - var path = require('path'); var mkdirp = require('mkdirp'); var fs = require('fs'); +var package = require('../../package'); /** * Will return the path and default bitcore-node configuration. It will search for the @@ -24,7 +23,34 @@ function getDefaultConfig(options) { 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', 'block', 'db', @@ -37,33 +63,28 @@ function getDefaultConfig(options) { 'web' ]; - if (options.additionalServices) { - defaultServices = defaultServices.concat(options.additionalServices); - } - var defaultDataDir = path.resolve(defaultPath, './data'); if (!fs.existsSync(defaultDataDir)) { mkdirp.sync(defaultDataDir); } - if (!fs.existsSync(defaultConfigFile)) { - var defaultConfig = { - network: 'testnet', - port: 3001, - services: defaultServices, - datadir: defaultDataDir, - servicesConfig: { - 'insight-api': { - cwdRequirePath: 'node_modules/insight-api' - }, - 'insight-ui': { - cwdRequirePath: 'node_modules/insight-ui' - } + var defaultConfig = { + version: package.version, + network: 'livenet', + port: 3001, + services: options.additionalServices ? defaultServices.concat(options.additionalServices) : defaultServices, + datadir: defaultDataDir, + servicesConfig: { + 'insight-api': { + cwdRequirePath: 'node_modules/insight-api' + }, + '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')); diff --git a/package.json b/package.json index 099e6902..a2e52efc 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "node": ">=8.2.0" }, "author": "BitPay ", - "version": "4.0.0", + "version": "5.0.0", "main": "./index.js", "repository": "git://github.com/bitpay/bitcore-node.git", "homepage": "https://github.com/bitpay/bitcore-node", From 16c7708dd772107e4ccfcaf0dc28935b990b5d69 Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Tue, 8 Aug 2017 17:26:38 -0400 Subject: [PATCH 2/3] refactor: pacify jshint --- .jshintrc | 4 ++-- lib/scaffold/default-config.js | 12 ++++++------ lib/scaffold/start.js | 12 +++++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.jshintrc b/.jshintrc index c3c58cd8..7cf0813b 100644 --- a/.jshintrc +++ b/.jshintrc @@ -18,7 +18,7 @@ "quotmark": "single", "regexp": true, "smarttabs": false, - "strict": true, + "strict": false, "trailing": true, "undef": true, "unused": true, @@ -26,7 +26,7 @@ "maxstatements": 25, "maxcomplexity": 10, "maxdepth": 4, - "maxlen": 120, + "maxlen": 140, "multistr": true, "predef": [ "after", diff --git a/lib/scaffold/default-config.js b/lib/scaffold/default-config.js index 0621aa06..17d3a68b 100644 --- a/lib/scaffold/default-config.js +++ b/lib/scaffold/default-config.js @@ -3,6 +3,10 @@ var mkdirp = require('mkdirp'); var fs = require('fs'); var package = require('../../package'); +function getMajorVersion(versionString) { + return parseInt(versionString.split('.')[0]); +} + /** * 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 @@ -25,16 +29,12 @@ function getDefaultConfig(options) { 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 + config: currentConfig }; } @@ -42,7 +42,7 @@ function getDefaultConfig(options) { 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 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}.`); diff --git a/lib/scaffold/start.js b/lib/scaffold/start.js index dca22f52..f3384283 100644 --- a/lib/scaffold/start.js +++ b/lib/scaffold/start.js @@ -1,5 +1,3 @@ -'use strict'; - var path = require('path'); var BitcoreNode = require('../node'); var index = require('../'); @@ -86,11 +84,13 @@ function lookInRequirePathConfig(req, service) { } function lookInCwd(req, service) { - var location = service.config.cwdRequirePath ? service.config.cwdRequirePath : service.name + const location = service.config.cwdRequirePath ? service.config.cwdRequirePath : service.name; try { return req(process.cwd() + '/' + location); } catch(e) { - if(e.code !== 'MODULE_NOT_FOUND') log.error(e); + if(e.code !== 'MODULE_NOT_FOUND') { + log.error(e); + } log.info('Checked the current working directory for service: ' + location); } } @@ -100,7 +100,9 @@ function lookInBuiltInPath(req, service) { var serviceFile = path.resolve(__dirname, '../services/' + service.name); return req(serviceFile); } catch(e) { - if(e.code !== 'MODULE_NOT_FOUND') log.error(e); + if(e.code !== 'MODULE_NOT_FOUND') { + log.error(e); + } log.info('Checked the built-in path: lib/services, for service: ' + service.name); } } From 30bcddd34deafcd8b62d74a17e1d632b39022121 Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Wed, 9 Aug 2017 12:49:15 -0400 Subject: [PATCH 3/3] refactor: downlevel, use strict --- .jshintrc | 2 +- lib/scaffold/default-config.js | 18 ++++++++++-------- lib/scaffold/start.js | 4 +++- lib/services/p2p/index.js | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.jshintrc b/.jshintrc index 7cf0813b..d735c345 100644 --- a/.jshintrc +++ b/.jshintrc @@ -18,7 +18,7 @@ "quotmark": "single", "regexp": true, "smarttabs": false, - "strict": false, + "strict": true, "trailing": true, "undef": true, "unused": true, diff --git a/lib/scaffold/default-config.js b/lib/scaffold/default-config.js index 17d3a68b..c6b833aa 100644 --- a/lib/scaffold/default-config.js +++ b/lib/scaffold/default-config.js @@ -1,7 +1,9 @@ +'use strict'; + var path = require('path'); var mkdirp = require('mkdirp'); var fs = require('fs'); -var package = require('../../package'); +var packageJson = require('../../package'); function getMajorVersion(versionString) { return parseInt(versionString.split('.')[0]); @@ -28,10 +30,10 @@ function getDefaultConfig(options) { } if (fs.existsSync(defaultConfigFile)) { - const currentConfig = require(defaultConfigFile); + var currentConfig = require(defaultConfigFile); // config must have a `version` field with major equal to package major version - if(currentConfig.version && getMajorVersion(package.version) === getMajorVersion(currentConfig.version)) { + if(currentConfig.version && getMajorVersion(packageJson.version) === getMajorVersion(currentConfig.version)) { return { path: defaultPath, config: currentConfig @@ -40,17 +42,17 @@ function getDefaultConfig(options) { console.log(`The configuration file at '${defaultConfigFile}' is incompatible with this version of Bitcore.`); - const now = new Date(); + var 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); + var backupFileName = `bitcore-node.${now.getUTCFullYear()}-${now.getUTCMonth()}-${now.getUTCDate()}.${now.getTime()}.json`; + var 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 = [ + var defaultServices = [ 'address', 'block', 'db', @@ -70,7 +72,7 @@ function getDefaultConfig(options) { } var defaultConfig = { - version: package.version, + version: packageJson.version, network: 'livenet', port: 3001, services: options.additionalServices ? defaultServices.concat(options.additionalServices) : defaultServices, diff --git a/lib/scaffold/start.js b/lib/scaffold/start.js index f3384283..419608a7 100644 --- a/lib/scaffold/start.js +++ b/lib/scaffold/start.js @@ -1,3 +1,5 @@ +'use strict'; + var path = require('path'); var BitcoreNode = require('../node'); var index = require('../'); @@ -84,7 +86,7 @@ function lookInRequirePathConfig(req, service) { } function lookInCwd(req, service) { - const location = service.config.cwdRequirePath ? service.config.cwdRequirePath : service.name; + var location = service.config.cwdRequirePath ? service.config.cwdRequirePath : service.name; try { return req(process.cwd() + '/' + location); } catch(e) { diff --git a/lib/services/p2p/index.js b/lib/services/p2p/index.js index 68a50bcf..183ffb94 100644 --- a/lib/services/p2p/index.js +++ b/lib/services/p2p/index.js @@ -332,7 +332,7 @@ P2P.prototype._setResourceFilter = function(filter, resource) { }; P2P.prototype._startBcoin = function() { - const network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet'; + var network = ['livenet', 'live', 'main', 'mainnet'].indexOf(this.node.network) !== -1? 'main' : 'testnet'; this._bcoin = new Bcoin({ network: network, prefix: this.node.datadir