Skip to content

Commit

Permalink
writeFileSync
Browse files Browse the repository at this point in the history
  • Loading branch information
rikaaa0928 committed Feb 16, 2019
1 parent 98ee787 commit 0d729bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "v2ray_electron",
"productName": "v2ray electron client",
"version": "0.2.0",
"version": "0.2.1",
"main": "main.js",
"devDependencies": {
"electron": "4.0.1",
Expand Down
22 changes: 12 additions & 10 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const BrowserWindow = require('electron').remote.BrowserWindow;
const parseConfigFile = require('./tools').parseConfigFile;
const saveToFile = require('./tools').saveToFile;
const path = require('path');
let PORTABLE_EXECUTABLE_DIR = '';
let REAL_DIR = '';
if (remote.process.env.PORTABLE_EXECUTABLE_DIR == undefined) {
PORTABLE_EXECUTABLE_DIR = __dirname
REAL_DIR = __dirname
} else {
PORTABLE_EXECUTABLE_DIR = remote.process.env.PORTABLE_EXECUTABLE_DIR
REAL_DIR = remote.process.env.PORTABLE_EXECUTABLE_DIR
}
let guiConfigFilePath = path.join(PORTABLE_EXECUTABLE_DIR, 'guiConfig.json');
let guiConfig = parseConfigFile(guiConfigFilePath);
let guiConfigFilePath = path.join(REAL_DIR, 'guiConfig.json');
let guiConfig = null;
const tbody = $("tbody");
let runningProcess = [];
const tmp = tbody.html();
Expand Down Expand Up @@ -87,6 +87,9 @@ function getPorts(conf) {
if (str.length > 0) {
str = str.substring(0, str.length - 1);
}
if (str.length <= 0) {
str = "undefined";
}
return str;
}
return "undefined";
Expand All @@ -101,7 +104,7 @@ function updateList(obj) {
c.find("th").eq(0)[0].innerText = i + 1;
let tds = c.find("td");
tds.eq(0)[0].innerText = obj.servers[i].name;
let conf = parseConfigFile(path.join(PORTABLE_EXECUTABLE_DIR, obj.servers[i].file));
let conf = parseConfigFile(path.join(REAL_DIR, obj.servers[i].file));
try {
tds.eq(1)[0].innerText = getPorts(conf);
} catch {
Expand All @@ -128,7 +131,6 @@ function updateList(obj) {
if ($("#delete" + i).html() != "Really?") {
penddinDelete.push("#delete" + i);
$("#delete" + i).html("Really?");
console.log(penddinDelete);
} else {
deleteConfig(i);
}
Expand All @@ -137,15 +139,15 @@ function updateList(obj) {
}

function deleteConfig(i) {
fs.unlinkSync(path.join(PORTABLE_EXECUTABLE_DIR, guiConfig.servers[i].file));
fs.unlinkSync(path.join(REAL_DIR, guiConfig.servers[i].file));
guiConfig.servers.splice(i, 1);
saveToFile(guiConfigFilePath, JSON.stringify(guiConfig, null, '\t'));
init();
}

function startServer(fileName, i) {
let exePath = path.join(PORTABLE_EXECUTABLE_DIR, 'core/v2ray');
let confPath = path.join(PORTABLE_EXECUTABLE_DIR, fileName);
let exePath = path.join(REAL_DIR, 'core/v2ray');
let confPath = path.join(REAL_DIR, fileName);
$("#status" + i).html("Running");
runningProcess.push(i);
childProcess[fileName] = spawn(exePath, ['-config', confPath]);
Expand Down
16 changes: 7 additions & 9 deletions tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
parseConfigFile: function (name) {
let err, data = fs.readFileSync(name, 'utf8');
if (err) throw err;
return JSON.parse(data)
return JSON.parse(data);
},
parseValue: function (data) {
if (data == "" || data == " ") {
Expand All @@ -25,13 +25,11 @@ module.exports = {
return String(data)
},
saveToFile: function (file, data) {
let state = true;
fs.writeFile(file, data, function (err) {
if (err) {
state = false;
return console.error(err);
}
});
return state;
try {
fs.writeFileSync(file, data);
return true;
} catch {
return false;
}
}
};

0 comments on commit 0d729bb

Please sign in to comment.