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