different cyphernode modules can now be configured in seperate files. This makes adding modules easier

This commit is contained in:
jash
2018-10-06 18:25:02 +02:00
committed by kexkey
parent d1945b6f37
commit 574766095f
6 changed files with 145 additions and 140 deletions

View File

@@ -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"
}
]

View File

@@ -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',
}];
};

View File

@@ -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'
}
]
}];
};

View File

@@ -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'
}
]
}];
};

View File

@@ -0,0 +1,7 @@
const featureCondition = function(props) {
return props.features && props.features.indexOf( 'opentimestamps' ) != -1;
}
module.exports = function( utils ) {
return [];
};

View File

@@ -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);
}
};