spee.ch/cli/configure.js

251 lines
8.3 KiB
JavaScript
Raw Normal View History

2018-08-01 01:01:16 +02:00
const inquirer = require('inquirer');
const fs = require('fs');
const Path = require('path');
const axios = require('axios');
2018-08-15 22:53:09 +02:00
const ip = require('ip');
const pwGenerator = require('generate-password');
2018-08-01 01:01:16 +02:00
const mysqlQuestions = require(Path.resolve(__dirname, 'questions/mysqlQuestions.js'));
const siteQuestions = require(Path.resolve(__dirname, 'questions/siteQuestions.js'));
let primaryClaimAddress = '';
let thumbnailChannelDefault = '@thumbnails';
let thumbnailChannel = '';
let thumbnailChannelId = '';
const createConfigFile = (fileName, configObject, topSecret) => { // siteConfig.json , siteConfig
const fileLocation = topSecret
? Path.resolve(__dirname, `../site/private/${fileName}`)
: Path.resolve(__dirname, `../site/config/${fileName}`);
2018-08-15 23:10:00 +02:00
const fileContents = JSON.stringify(configObject, null, 2);
fs.writeFileSync(fileLocation, fileContents, 'utf-8');
console.log(`Successfully created ${fileLocation}\n`);
2018-08-15 23:10:00 +02:00
};
// import existing configs or import the defaults
2018-08-01 01:01:16 +02:00
let mysqlConfig;
try {
2018-11-07 23:03:17 +01:00
mysqlConfig = require('../site/config/mysqlConfig.json');
2018-08-01 01:01:16 +02:00
} catch (error) {
mysqlConfig = require('./defaults/mysqlConfig.json');
}
const { database: mysqlDatabase, username: mysqlUsername, password: mysqlPassword } = mysqlConfig;
let siteConfig;
try {
2018-11-07 23:03:17 +01:00
siteConfig = require('../site/config/siteConfig.json');
2018-08-01 01:01:16 +02:00
} catch (error) {
siteConfig = require('./defaults/siteConfig.json');
}
const {
details: {
port,
title,
host,
},
publishing: {
uploadDirectory,
2018-11-08 20:58:41 +01:00
channelClaimBidAmount: channelBid,
2018-08-01 01:01:16 +02:00
},
} = siteConfig;
2018-08-15 23:10:00 +02:00
let lbryConfig;
try {
2018-11-07 23:03:17 +01:00
lbryConfig = require('../site/config/lbryConfig.json');
2018-08-15 23:10:00 +02:00
} catch (error) {
lbryConfig = require('./defaults/lbryConfig.json');
}
let loggerConfig;
try {
2018-11-07 23:03:17 +01:00
loggerConfig = require('../site/config/loggerConfig.json');
2018-08-15 23:10:00 +02:00
} catch (error) {
loggerConfig = require('./defaults/loggerConfig.json');
}
let slackConfig;
try {
2018-11-07 23:03:17 +01:00
slackConfig = require('../site/config/slackConfig.json');
2018-08-15 23:10:00 +02:00
} catch (error) {
slackConfig = require('./defaults/slackConfig.json');
}
2018-11-12 15:26:40 +01:00
let chainqueryConfig;
try {
chainqueryConfig = require('../site/config/chainqueryConfig.json');
} catch (error) {
chainqueryConfig = require('./defaults/chainqueryConfig.json');
}
// authConfig
let randSessionKey = pwGenerator.generate({
length : 20,
numbers: true,
});
let randMasterPass = pwGenerator.generate({
length : 20,
numbers: true,
});
let authConfig;
try {
authConfig = require('../site/private/authConfig.json');
} catch (error) {
authConfig = {
sessionKey : randSessionKey,
masterPassword: randMasterPass,
};
}
2018-08-15 23:10:00 +02:00
// ask user questions and create config files
2018-08-01 01:01:16 +02:00
inquirer
.prompt(mysqlQuestions(mysqlDatabase, mysqlUsername, mysqlPassword))
.then(results => {
console.log('\nCreating mysql config file...');
2018-08-15 23:10:00 +02:00
createConfigFile('mysqlConfig.json', results);
2018-08-01 01:01:16 +02:00
})
.then(() => {
// check for lbrynet connection & retrieve a default address
console.log('\nRetrieving your primary claim address from LBRY...');
return axios
.post('http://localhost:5279', {
method: 'wallet_list',
})
.then(response => {
if (response.data) {
if (response.data.error) {
throw new Error(response.data.error.message);
}
primaryClaimAddress = response.data.result[0];
console.log('Primary claim address:', primaryClaimAddress, '!\n');
siteConfig['publishing']['primaryClaimAddress'] = primaryClaimAddress;
return;
}
throw new Error('No data received from lbrynet');
}).catch(error => {
throw error;
});
})
.then(() => {
console.log('\nChecking to see if a LBRY channel exists for thumbnails...');
// see if a channel name already exists in the configs
const { publishing } = siteConfig;
thumbnailChannel = publishing.thumbnailChannel;
thumbnailChannelId = publishing.thumbnailChannelId;
console.log(`Thumbnail channel in configs: ${thumbnailChannel}#${thumbnailChannelId}.`);
// see if channel exists in the wallet
return axios
.post('http://localhost:5279', {
method: 'channel_list',
})
.then(response => {
if (response.data) {
if (response.data.error) {
throw new Error(response.data.error.message);
}
const channelList = response.data.result || [];
console.log('channels in this wallet:', channelList.length);
for (let i = 0; i < channelList.length; i++) {
if (channelList[i].name === thumbnailChannelDefault) {
const foundChannel = channelList[i];
console.log(`Found a thumbnail channel in wallet.`);
if (foundChannel.is_mine === false) {
console.log('Channel was not mine');
continue;
}
console.log('name:', foundChannel.name);
console.log('claim_id:', foundChannel.claim_id);
if (foundChannel.name === thumbnailChannel && foundChannel.claim_id === thumbnailChannelId) {
console.log('No update to existing thumbnail config required\n');
} else {
console.log(`Replacing thumbnail channel in config...`);
siteConfig['publishing']['thumbnailChannel'] = foundChannel.name;
siteConfig['publishing']['thumbnailChannelId'] = foundChannel.claim_id;
console.log(`Successfully replaced channel in config.\n`);
}
return true;
}
}
console.log(`Did not find a thumbnail channel that is mine in wallet.\n`);
return false;
}
throw new Error('No data received from lbrynet');
}).catch(error => {
throw error;
});
})
.then((thumbnailChannelAlreadyExists) => {
// exit if a channel already exists, skip this step
if (thumbnailChannelAlreadyExists) {
return;
}
// create thumbnail address
console.log('\nCreating a LBRY channel to publish your thumbnails to...');
return axios
.post('http://localhost:5279', {
method: 'channel_new',
params: {
channel_name: thumbnailChannelDefault,
2018-11-01 04:59:01 +01:00
amount : channelBid,
2018-08-01 01:01:16 +02:00
},
})
.then(response => {
if (response.data) {
if (response.data.error) {
throw new Error(response.data.error.message);
}
thumbnailChannel = thumbnailChannelDefault;
thumbnailChannelId = response.data.result.claim_id;
siteConfig['publishing']['thumbnailChannel'] = thumbnailChannel;
siteConfig['publishing']['thumbnailChannelId'] = thumbnailChannelId;
console.log(`Created channel: ${thumbnailChannel}#${thumbnailChannelId}\n`);
return;
}
throw new Error('No data received from lbrynet');
}).catch(error => {
throw error;
});
})
.then(() => {
return inquirer.prompt(siteQuestions(port, title, host, uploadDirectory));
})
.then(results => {
console.log('\nCreating site config file...');
siteConfig['details']['port'] = results.port;
siteConfig['details']['title'] = results.title;
siteConfig['details']['host'] = results.host;
2018-08-15 22:53:09 +02:00
siteConfig['details']['ipAddress'] = ip.address();
2018-08-01 01:01:16 +02:00
siteConfig['publishing']['uploadDirectory'] = results.uploadDirectory;
2018-08-15 22:53:09 +02:00
})
.then(() => {
2018-08-15 23:10:00 +02:00
// create the config files
createConfigFile('siteConfig.json', siteConfig);
createConfigFile('lbryConfig.json', lbryConfig);
createConfigFile('loggerConfig.json', loggerConfig);
createConfigFile('slackConfig.json', slackConfig);
2018-11-12 15:26:40 +01:00
createConfigFile('chainqueryConfig.json', chainqueryConfig);
createConfigFile('authConfig.json', authConfig, true);
2018-08-01 01:01:16 +02:00
})
.then(() => {
console.log('\nYou\'re all done!');
console.log('\nIt\'s a good idea to BACK UP YOUR MASTER PASSWORD \nin "/site/private/authConfig.json" so that you don\'t lose \ncontrol of your channel.');
console.log('\nNext step: run "npm run start" to build and start your server!');
console.log('If you want to change any settings, you can edit the files in the "/site" folder.');
2018-08-01 01:01:16 +02:00
process.exit(0);
})
.catch(error => {
if (error.code === 'ECONNREFUSED') {
console.log('Error: could not connect to LBRY. Please make sure lbrynet daemon is running.');
process.exit(1);
} else {
console.log(error);
process.exit(1);
}
});