Refactoring
This commit is contained in:
114
src/helpers.js
114
src/helpers.js
@@ -8,10 +8,6 @@ const spawn = require('child_process').spawn;
|
|||||||
const Constants = require('./constants');
|
const Constants = require('./constants');
|
||||||
const RandomString = require('randomstring');
|
const RandomString = require('randomstring');
|
||||||
|
|
||||||
const _getDataDirectory = () => {
|
|
||||||
return _resolvePath(Constants.DATA_LOCATIONS[os.platform()]);
|
|
||||||
};
|
|
||||||
|
|
||||||
const _executeProcess = (command, args=[]) => {
|
const _executeProcess = (command, args=[]) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const processOptions = {
|
const processOptions = {
|
||||||
@@ -61,6 +57,26 @@ const _execProcessGetOutput = (cmd, args) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const _getDataDirectory = () => {
|
||||||
|
return _resolvePath(Constants.DATA_LOCATIONS[os.platform()]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const _getDefaultRepertoryArgs = (provider, remote) => {
|
||||||
|
const providerLower = provider.toLowerCase();
|
||||||
|
const args = [];
|
||||||
|
if (remote) {
|
||||||
|
args.push('-rm');
|
||||||
|
args.push(provider.substr(6));
|
||||||
|
} else if (Constants.PROVIDER_ARG[providerLower] && (Constants.PROVIDER_ARG[providerLower].length > 0)) {
|
||||||
|
args.push(Constants.PROVIDER_ARG[providerLower]);
|
||||||
|
}
|
||||||
|
return args;
|
||||||
|
};
|
||||||
|
|
||||||
|
const _getRepertoryExec = version => {
|
||||||
|
return path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
||||||
|
};
|
||||||
|
|
||||||
const _resolvePath = str => {
|
const _resolvePath = str => {
|
||||||
if (os.platform() === 'win32') {
|
if (os.platform() === 'win32') {
|
||||||
return str.replace(/%([^%]+)%/g, (_, n) => {
|
return str.replace(/%([^%]+)%/g, (_, n) => {
|
||||||
@@ -71,7 +87,7 @@ const _resolvePath = str => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const tryParse = (j, def) => {
|
const _tryParse = (j, def) => {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(j);
|
return JSON.parse(j);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -87,12 +103,9 @@ module.exports.checkDaemonVersion = (version, provider) => {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = [];
|
const args = _getDefaultRepertoryArgs(provider, false);
|
||||||
args.push('-cv');
|
args.push('-cv');
|
||||||
if (Constants.PROVIDER_ARG[provider.toLowerCase()].length > 0) {
|
|
||||||
args.push(Constants.PROVIDER_ARG[provider.toLowerCase()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const process = new spawn(command, args, processOptions);
|
const process = new spawn(command, args, processOptions);
|
||||||
|
|
||||||
@@ -152,17 +165,10 @@ module.exports.detectRepertoryMounts = (version, providerList) => {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = [];
|
const args = _getDefaultRepertoryArgs(provider, !Constants.PROVIDER_LIST.includes(provider));
|
||||||
args.push('-status');
|
args.push('-status');
|
||||||
if (Constants.PROVIDER_LIST.includes(provider)) {
|
|
||||||
if (provider !== 'Sia') {
|
|
||||||
args.push('-sp');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
args.push('-rm');
|
|
||||||
args.push(provider.substr(6));
|
|
||||||
}
|
|
||||||
const process = new spawn(command, args, processOptions);
|
const process = new spawn(command, args, processOptions);
|
||||||
let result = '';
|
let result = '';
|
||||||
|
|
||||||
@@ -175,7 +181,7 @@ module.exports.detectRepertoryMounts = (version, providerList) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
process.on('exit', () => {
|
process.on('exit', () => {
|
||||||
mountState[provider] = tryParse(result, defaultData)[provider];
|
mountState[provider] = _tryParse(result, defaultData)[provider];
|
||||||
grabStatus(++index);
|
grabStatus(++index);
|
||||||
});
|
});
|
||||||
process.unref();
|
process.unref();
|
||||||
@@ -345,14 +351,8 @@ module.exports.executeMount = (version, provider, remote, location, noConsoleSup
|
|||||||
stdio: 'ignore',
|
stdio: 'ignore',
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = [];
|
const args = _getDefaultRepertoryArgs(provider, remote);
|
||||||
if (remote) {
|
|
||||||
args.push('-rm');
|
|
||||||
args.push(provider.substr(6));
|
|
||||||
} else if (Constants.PROVIDER_ARG[provider.toLowerCase()].length > 0) {
|
|
||||||
args.push(Constants.PROVIDER_ARG[provider.toLowerCase()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((os.platform() === 'linux') || (os.platform() === 'darwin')) {
|
if ((os.platform() === 'linux') || (os.platform() === 'darwin')) {
|
||||||
args.push('-o');
|
args.push('-o');
|
||||||
@@ -393,15 +393,9 @@ module.exports.getConfig = (version, provider, remote) => {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = [];
|
const args = _getDefaultRepertoryArgs(provider, remote);
|
||||||
args.push('-dc');
|
args.push('-dc');
|
||||||
if (remote) {
|
|
||||||
args.push('-rm');
|
|
||||||
args.push(provider.substr(6));
|
|
||||||
} else if (Constants.PROVIDER_ARG[provider.toLowerCase()].length > 0) {
|
|
||||||
args.push(Constants.PROVIDER_ARG[provider.toLowerCase()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const process = new spawn(command, args, processOptions);
|
const process = new spawn(command, args, processOptions);
|
||||||
let result = '';
|
let result = '';
|
||||||
@@ -442,15 +436,9 @@ module.exports.getConfigTemplate = (version, provider, remote) => {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = [];
|
const args = _getDefaultRepertoryArgs(provider, remote);
|
||||||
args.push('-gt');
|
args.push('-gt');
|
||||||
if (remote) {
|
|
||||||
args.push('-rm');
|
|
||||||
args.push(provider.substr(6));
|
|
||||||
} else if (Constants.PROVIDER_ARG[provider.toLowerCase()].length > 0) {
|
|
||||||
args.push(Constants.PROVIDER_ARG[provider.toLowerCase()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const process = new spawn(command, args, processOptions);
|
const process = new spawn(command, args, processOptions);
|
||||||
let result = '';
|
let result = '';
|
||||||
@@ -624,7 +612,7 @@ module.exports.performWindowsUninstall = names => {
|
|||||||
parseLine(++index);
|
parseLine(++index);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err=> {
|
.catch(() => {
|
||||||
parseLine(++index);
|
parseLine(++index);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -669,17 +657,11 @@ module.exports.setConfigValue = (name, value, provider, remote, version) => {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = [];
|
const args = _getDefaultRepertoryArgs(provider, remote);
|
||||||
args.push('-set');
|
args.push('-set');
|
||||||
args.push(name);
|
args.push(name);
|
||||||
args.push(value);
|
args.push(value);
|
||||||
if (remote) {
|
|
||||||
args.push('-rm');
|
|
||||||
args.push(provider.substr(6));
|
|
||||||
} else if (Constants.PROVIDER_ARG[provider.toLowerCase()].length > 0) {
|
|
||||||
args.push(Constants.PROVIDER_ARG[provider.toLowerCase()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const process = new spawn(command, args, processOptions);
|
const process = new spawn(command, args, processOptions);
|
||||||
|
|
||||||
@@ -703,14 +685,9 @@ module.exports.stopMountProcess = (version, provider, remote) => {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = ['-unmount'];
|
const args = _getDefaultRepertoryArgs(provider, remote);
|
||||||
if (remote) {
|
args.push('-unmount');
|
||||||
args.push('-rm');
|
|
||||||
args.push(provider.substr(6));
|
|
||||||
} else if (Constants.PROVIDER_ARG[provider.toLowerCase()].length > 0) {
|
|
||||||
args.push(Constants.PROVIDER_ARG[provider.toLowerCase()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const process = new spawn(command, args, processOptions);
|
const process = new spawn(command, args, processOptions);
|
||||||
const pid = process.pid;
|
const pid = process.pid;
|
||||||
@@ -737,14 +714,9 @@ module.exports.stopMountProcessSync = (version, provider, remote) => {
|
|||||||
windowsHide: true,
|
windowsHide: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
const args = ['-unmount'];
|
const args = _getDefaultRepertoryArgs(provider, remote);
|
||||||
if (remote) {
|
args.push('-unmount');
|
||||||
args.push('-rm');
|
|
||||||
args.push(provider.substr(6));
|
|
||||||
} else if (Constants.PROVIDER_ARG[provider.toLowerCase()].length > 0) {
|
|
||||||
args.push(Constants.PROVIDER_ARG[provider.toLowerCase()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const process = new spawn(command, args, processOptions);
|
const process = new spawn(command, args, processOptions);
|
||||||
process.unref();
|
process.unref();
|
||||||
@@ -752,7 +724,7 @@ module.exports.stopMountProcessSync = (version, provider, remote) => {
|
|||||||
|
|
||||||
module.exports.testRepertoryBinary = version => {
|
module.exports.testRepertoryBinary = version => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const command = path.join(_getDataDirectory(), version, (os.platform() === 'win32') ? 'repertory.exe' : 'repertory');
|
const command = _getRepertoryExec(version);
|
||||||
_executeProcess(command, ['-dc'])
|
_executeProcess(command, ['-dc'])
|
||||||
.then(code => {
|
.then(code => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user