diff --git a/install/generator-cyphernode/generators/app/features.json b/install/generator-cyphernode/generators/app/features.json new file mode 100644 index 0000000..0195ce5 --- /dev/null +++ b/install/generator-cyphernode/generators/app/features.json @@ -0,0 +1,19 @@ +[ + { + "name": "Bitcoin full node", + "value": "bitcoin" + }, + { + "name": "Lightning node", + "value": "lightning" + + }, + { + "name": "Open timestamps client", + "value": "opentimestamps" + }, + { + "name": "Electrum server", + "value": "electrum" + } +] \ No newline at end of file diff --git a/install/generator-cyphernode/generators/app/features/bitcoin.js b/install/generator-cyphernode/generators/app/features/bitcoin.js new file mode 100644 index 0000000..9ea5e84 --- /dev/null +++ b/install/generator-cyphernode/generators/app/features/bitcoin.js @@ -0,0 +1,21 @@ +const featureCondition = function(props) { + return props.features && props.features.indexOf( 'bitcoin' ) != -1; +}; + +module.exports = function( utils ) { + return [{ + when: featureCondition, + type: 'confirm', + name: 'bitcoin_prune', + default: utils._getDefault( 'bitcoin_prune' ), + message: 'Run bitcoin node in prune mode?'+'\n', + }, + { + when: featureCondition, + type: 'input', + name: 'bitcoin_external_ip', + default: utils._getDefault( 'bitcoin_external_ip' ), + validate: utils._ipValidator, + message: 'What external ip does your bitcoin full node have?'+'\n', + }]; +}; \ No newline at end of file diff --git a/install/generator-cyphernode/generators/app/features/electrum.js b/install/generator-cyphernode/generators/app/features/electrum.js new file mode 100644 index 0000000..b8d73f7 --- /dev/null +++ b/install/generator-cyphernode/generators/app/features/electrum.js @@ -0,0 +1,23 @@ +const featureCondition = function(props) { + return props.features && props.features.indexOf( 'electrum' ) != -1; +} + +module.exports = function( utils ) { + return [{ + when: featureCondition, + type: 'list', + name: 'electrum_implementation', + default: utils._getDefault( 'electrum_implementation' ), + message: 'What electrum implementation do you want to use?'+'\n', + choices: [ + { + name: 'Electrum personal server', + value: 'eps' + }, + { + name: 'Electrumx server', + value: 'elx' + } + ] + }]; +}; \ No newline at end of file diff --git a/install/generator-cyphernode/generators/app/features/lightning.js b/install/generator-cyphernode/generators/app/features/lightning.js new file mode 100644 index 0000000..b6622f2 --- /dev/null +++ b/install/generator-cyphernode/generators/app/features/lightning.js @@ -0,0 +1,23 @@ +const featureCondition = function(props) { + return props.features && props.features.indexOf( 'lightning' ) != -1; +} + +module.exports = function( utils ) { + return [{ + when: featureCondition, + type: 'list', + name: 'lightning_implementation', + default: utils._getDefault( 'lightning_implementation' ), + message: 'What lightning implementation do you want to use?'+'\n', + choices: [ + { + name: 'C-lightning', + value: 'c-lightning' + }, + { + name: 'LND', + value: 'lnd' + } + ] + }]; +}; \ No newline at end of file diff --git a/install/generator-cyphernode/generators/app/features/opentimestamps.js b/install/generator-cyphernode/generators/app/features/opentimestamps.js new file mode 100644 index 0000000..6c0e7e8 --- /dev/null +++ b/install/generator-cyphernode/generators/app/features/opentimestamps.js @@ -0,0 +1,7 @@ +const featureCondition = function(props) { + return props.features && props.features.indexOf( 'opentimestamps' ) != -1; +} + +module.exports = function( utils ) { + return []; +}; \ No newline at end of file diff --git a/install/generator-cyphernode/generators/app/index.js b/install/generator-cyphernode/generators/app/index.js index d182959..ff6d82e 100644 --- a/install/generator-cyphernode/generators/app/index.js +++ b/install/generator-cyphernode/generators/app/index.js @@ -4,164 +4,42 @@ const chalk = require('chalk'); const fs = require('fs'); const wrap = require('wordwrap')(86); const validator = require('validator'); +const path = require("path"); +const featureChoices = require(path.join(__dirname, "features.json")); + +let featurePromptModules = []; +const normalizedPath = path.join(__dirname, "features"); +fs.readdirSync(normalizedPath).forEach(function(file) { + featurePromptModules.push(require(path.join(normalizedPath,file))); +}); module.exports = class extends Generator { constructor(args, opts) { super(args, opts); + if( fs.existsSync('/data/props.json') ) { this.props = require('/data/props.json'); } else { this.props = {}; } - console.log( this.props ); - } + this.featureChoices = featureChoices; + for( let c of this.featureChoices ) { + c.checked = this._isChecked( 'features', c.value ); + } - /* values */ - _isChecked( name, value ) { - return this.props && this.props[name].indexOf(value) != -1 ; - } - - _getDefault( name ) { - return this.props && this.props[name]; - } - - /* validators */ - _ipValidator( ip ) { - return validator.isIP((ip+"").trim()); - } - - /* filters */ - - _trimFilter( input ) { - return (input+"").trim(); - } - - /* prompts */ - _configureFeatures() { - return [{ - // https://github.com/SBoudrias/Inquirer.js#question - // input, confirm, list, rawlist, expand, checkbox, password, editor - type: 'checkbox', - name: 'features', - message: wrap('What features do you want to add to your cyphernode?')+'\n', - choices: [ - { - name: 'Bitcoin full node', - value: 'bitcoin', - checked: this._isChecked( 'features', 'bitcoin' ) - }, - { - name: 'Lightning node', - value: 'lightning', - checked: this._isChecked( 'features', 'lightning' ) - - }, - { - name: 'Open timestamps client', - value: 'ots', - checked: this._isChecked( 'features', 'ots' ) - }, - { - name: 'Electrum server', - value: 'electrum', - checked: this._isChecked( 'features', 'electrum' ) - } - - ] - }]; - } - - _configureBitcoinFullNode() { - return [{ - when: function(answers) { - return answers.features && - answers.features.indexOf( 'bitcoin' ) != -1; - }, - type: 'confirm', - name: 'bitcoin_prune', - default: this._getDefault( 'bitcoin_prune' ), - message: wrap('Run bitcoin node in prune mode?')+'\n', - }, - { - when: function(answers) { - return answers.features && - answers.features.indexOf( 'bitcoin' ) != -1; - }, - type: 'input', - name: 'bitcoin_external_ip', - default: this._getDefault( 'bitcoin_external_ip' ), - validate: this._ipValidator, - message: wrap('What external ip does your bitcoin full node have?')+'\n', - }]; - } - - _configureLightningImplementation() { - return [{ - when: function(answers) { - return answers.features && - answers.features.indexOf( 'lightning' ) != -1; - }, - type: 'list', - name: 'lightning_implementation', - default: this._getDefault( 'lightning_implementation' ), - message: wrap('What lightning implementation do you want to use?')+'\n', - choices: [ - { - name: 'C-lightning', - value: 'c-lightning' - }, - { - name: 'LND', - value: 'lnd' - } - ] - }]; - } - - _configureElectrumImplementation() { - return [{ - when: function(answers) { - return answers.features && - answers.features.indexOf( 'electrum' ) != -1; - }, - type: 'list', - name: 'electrum_implementation', - default: this._getDefault( 'electrum_implementation' ), - message: wrap('What electrum implementation do you want to use?')+'\n', - choices: [ - { - name: 'Electrum personal server', - value: 'eps' - }, - { - name: 'Electrumx server', - value: 'elx' - } - ] - }]; - } - - _configureCLightning() { - return [{}]; - } - - _configureLND() { - return [{}]; } prompting() { const splash = fs.readFileSync(this.templatePath('splash.txt')); this.log(splash.toString()); - var prompts = - this._configureFeatures() - .concat(this._configureBitcoinFullNode()) - .concat(this._configureLightningImplementation()) - .concat(this._configureElectrumImplementation()) - //.concat(this._configureCLightning()) - //.concat(this._configureLND()) + var prompts = this._configureFeatures() + + for( let m of featurePromptModules ) { + prompts = prompts.concat(m(this)); + } return this.prompt(prompts).then(props => { this.props = Object.assign(this.props, props); @@ -180,4 +58,38 @@ module.exports = class extends Generator { install() { } + + /* some utils */ + + _configureFeatures() { + return [{ + // https://github.com/SBoudrias/Inquirer.js#question + // input, confirm, list, rawlist, expand, checkbox, password, editor + type: 'checkbox', + name: 'features', + message: wrap('What features do you want to add to your cyphernode?')+'\n', + choices: this.featureChoices + }]; + } + + _isChecked( name, value ) { + return this.props && this.props[name] && this.props[name].indexOf(value) != -1 ; + } + + _getDefault( name ) { + return this.props && this.props[name]; + } + + _ipValidator( ip ) { + return validator.isIP((ip+"").trim()); + } + + _trimFilter( input ) { + return (input+"").trim(); + } + + _wrap(text) { + return wrap(text); + } + };