fix(parse-zcash-conf): remove side-effects from zcashParse method

This commit is contained in:
George Lima 2019-03-07 16:13:05 -03:00
parent 5fc34f611b
commit c22fbb4eb3
1 changed files with 12 additions and 8 deletions

View File

@ -4,7 +4,6 @@ import fs from 'fs';
import { locateZcashConf } from './locate-zcash-conf';
import { filterObjectNullKeys } from '../../app/utils/filter-object-null-keys';
import store from '../electron-store';
type ZcashConfFile = {
testnet: ?string,
@ -32,14 +31,15 @@ type ZcashConfFile = {
paytxfee: ?string,
};
export const parseZcashConf = (): Promise<Array<string>> => new Promise((resolve, reject) => {
export const parseZcashConf = (): Promise<ZcashConfFile> => new Promise((resolve, reject) => {
fs.readFile(locateZcashConf(), (err, file) => {
if (err) return reject(err);
const fileString = file.toString();
/* eslint-disable no-unused-vars */
// $FlowFixMe
const { rpcuser, rpcpassword, ...payload }: ZcashConfFile = filterObjectNullKeys(
const payload: ZcashConfFile = filterObjectNullKeys(
fileString.split('\n').reduce((acc, cur) => {
if (!cur) return acc;
@ -52,14 +52,18 @@ export const parseZcashConf = (): Promise<Array<string>> => new Promise((resolve
}, {}),
);
store.set('rpcuser', rpcuser || '');
store.set('rpcpassword', rpcpassword || '');
// $FlowFixMe
resolve(Object.keys(payload).reduce((acc, key) => acc.concat(`-${key}=${payload[key]}`), []));
resolve(payload);
});
});
/* eslint-disable-next-line max-len */
export const generateArgsFromConf = (obj: ZcashConfFile): Array<string> => Object.keys(obj).reduce((acc, key) => {
// We can omit the credentials for the command line
if (key === 'rpcuser' || key === 'rpcpassword') return acc;
return acc.concat(`-${key}=${String(obj[key])}`);
}, []);
export const parseCmdArgs = (cmd: string): { user: string, password: string } => {
const rpcUserInArgs = cmd.split(' ').find(x => x.startsWith('-rpcuser'));
const rpcPasswordInArgs = cmd.split(' ').find(x => x.startsWith('-rpcpassword'));