Skip to content

Commit

Permalink
fix(config): fixed saving configuration on develop builds
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Feb 5, 2018
1 parent 245cefa commit b640bd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
26 changes: 15 additions & 11 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import createWindow from "./helpers/window";
// Special module holding environment variables which you declared
// in config/env_xxx.json file.
import env from "env";
import spiderCall from './spider'

const { spawn, exec } = require('child_process')
const fs = require('fs')
Expand All @@ -35,6 +34,9 @@ if (env.name !== "production") {
app.setPath("userData", `${userDataPath} (${env.name})`);
}

const spiderCall = require('./spider')
const appConfig = require('./config')

let sphinx = undefined
let spider = undefined

Expand Down Expand Up @@ -88,12 +90,12 @@ const getSphinxPath = () => {
return 'searchd'
}

const writeSphinxConfig = (path) => {
const writeSphinxConfig = (path, dbPath) => {
const config = `
index torrents
{
type = rt
path = ${path}/database/torrents
path = ${dbPath}/database/torrents
rt_attr_string = hash
rt_attr_string = name
Expand All @@ -117,7 +119,7 @@ const writeSphinxConfig = (path) => {
index files
{
type = rt
path = ${path}/database/files
path = ${dbPath}/database/files
rt_attr_string = path
rt_field = pathIndex
Expand All @@ -128,7 +130,7 @@ const writeSphinxConfig = (path) => {
index statistic
{
type = rt
path = ${path}/database/statistic
path = ${dbPath}/database/statistic
rt_attr_bigint = size
rt_attr_bigint = files
Expand All @@ -155,9 +157,9 @@ const writeSphinxConfig = (path) => {
// clear dir in test env
if(env.name === 'test')
{
if (fs.existsSync(`${path}/database`)) {
fs.readdirSync(`${path}/database`).forEach(function(file, index){
const curPath = `${path}/database` + "/" + file;
if (fs.existsSync(`${dbPath}/database`)) {
fs.readdirSync(`${dbPath}/database`).forEach(function(file, index){
const curPath = `${dbPath}/database` + "/" + file;
if (!fs.lstatSync(curPath).isDirectory()) {
fs.unlinkSync(curPath);
}
Expand All @@ -174,20 +176,22 @@ const writeSphinxConfig = (path) => {
}
}

if (!fs.existsSync(`${path}/database`)){
fs.mkdirSync(`${path}/database`);
if (!fs.existsSync(`${dbPath}/database`)){
fs.mkdirSync(`${dbPath}/database`);
}

fs.writeFileSync(`${path}/sphinx.conf`, config)
console.log(`writed sphinx config to ${path}`)
console.log('db path:', dbPath)
}

const sphinxPath = path.resolve(getSphinxPath())
console.log('Sphinx Path:', sphinxPath)

const startSphinx = (callback) => {
const sphinxConfigDirectory = app.getPath("userData")
writeSphinxConfig(sphinxConfigDirectory)
appConfig['dbPath'] = appConfig.dbPath && appConfig.dbPath.length > 0 ? appConfig.dbPath : sphinxConfigDirectory;
writeSphinxConfig(sphinxConfigDirectory, appConfig.dbPath)

const config = `${sphinxConfigDirectory}/sphinx.conf`
const options = ['--config', config]
Expand Down
9 changes: 6 additions & 3 deletions src/background/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ let config = {
trafficInterface: 'enp2s0',
trafficMax: 0,
trafficUpdateTime: 3, //secs
trafficIgnoreDHT: false
trafficIgnoreDHT: false,

dbPath: '',
}

const fs = require('fs');
Expand All @@ -59,7 +61,8 @@ const configProxy = new Proxy(config, {
let obj = JSON.parse(data)
obj[prop] = value;
fs.writeFileSync(configPath, JSON.stringify(obj, null, 4), 'utf8');
debug('saving config.json:', prop, '=', value)
debug('saving rats.json:', prop, '=', value)
return true
}
})

Expand All @@ -73,7 +76,7 @@ config.load = () => {
for(let prop in obj)
{
config[prop] = obj[prop]
debug('config.json:', prop, '=', obj[prop])
debug('rats.json:', prop, '=', obj[prop])
}
}
return configProxy
Expand Down

0 comments on commit b640bd4

Please sign in to comment.