2019-01-09 12:45:37 +00:00
|
|
|
var commandExistsSync = require('command-exists').sync;
|
2019-01-09 14:32:19 +00:00
|
|
|
var app = require('electron').app;
|
2019-01-11 12:51:23 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var util = require('util');
|
|
|
|
var fetch = require('node-fetch')
|
|
|
|
var stream = require('stream');
|
|
|
|
var path = require('path');
|
|
|
|
const { spawn } = require('child_process');
|
2019-02-01 08:58:31 +00:00
|
|
|
const { ipcMain } = require('electron');
|
2019-01-11 12:51:23 +00:00
|
|
|
|
|
|
|
var pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2019-01-09 12:45:37 +00:00
|
|
|
exports.getInstalledSoftware = (softwareList) => {
|
|
|
|
const installed = {};
|
|
|
|
for(var software of softwareList) {
|
|
|
|
var name = software.name;
|
2019-01-14 11:47:09 +00:00
|
|
|
var locations = software.locations;
|
2019-01-09 12:45:37 +00:00
|
|
|
|
|
|
|
installed[name] = [];
|
|
|
|
|
2019-01-14 11:47:09 +00:00
|
|
|
for(var location of locations) {
|
|
|
|
// var exists = commandExistsSync(command);
|
|
|
|
var exists = fs.existsSync(location);
|
2019-01-09 12:45:37 +00:00
|
|
|
if(exists) {
|
2019-01-14 11:47:09 +00:00
|
|
|
installed[name].push(location);
|
2019-01-09 12:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return installed;
|
|
|
|
}
|
|
|
|
|
2019-01-14 11:47:09 +00:00
|
|
|
async function downloadFile(resource, softwarePath) {
|
|
|
|
var response = await fetch(resource);
|
|
|
|
if (response.status != 200) {
|
|
|
|
throw new Error(`Cannot download file ${resource}, response status = ${response.status}`);
|
|
|
|
}
|
|
|
|
await pipeline(
|
|
|
|
response.body,
|
|
|
|
fs.createWriteStream(softwarePath)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:29:39 +00:00
|
|
|
async function getSoftwareInstallationPath(software) {
|
|
|
|
if (software.installer) {
|
|
|
|
return path.join(app.getPath('temp'), software.binary);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const externalPath = path.join(app.getAppPath(), 'external');
|
|
|
|
const exists = fs.existsSync(externalPath);
|
|
|
|
if (!exists) {
|
|
|
|
fs.mkdirSync(externalPath);
|
|
|
|
}
|
|
|
|
return path.join(externalPath, software.binary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-11 12:51:23 +00:00
|
|
|
ipcMain.on('installed-software-install', async function (event, software) {
|
2019-02-04 10:29:39 +00:00
|
|
|
const softwarePath = await getSoftwareInstallationPath(software);
|
|
|
|
|
2019-01-14 12:52:31 +00:00
|
|
|
const responseChannel = `installed-software-installed-${software.name}`;
|
2019-01-09 14:32:19 +00:00
|
|
|
|
2019-01-11 12:51:23 +00:00
|
|
|
if (software.type == 'web') {
|
2019-01-14 11:47:09 +00:00
|
|
|
const exists = fs.existsSync(softwarePath);
|
|
|
|
if (exists) {
|
|
|
|
console.log(`Skipping downloading file due to '${softwarePath}' path exists`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(`File '${softwarePath}' doesn't exist. Downloading file.`);
|
|
|
|
try {
|
|
|
|
await downloadFile(software.resource, softwarePath);
|
|
|
|
} catch(error) {
|
2019-01-14 12:52:31 +00:00
|
|
|
event.sender.send(responseChannel, {
|
2019-01-14 11:47:09 +00:00
|
|
|
success: false,
|
|
|
|
message: error.message
|
|
|
|
});
|
2019-01-14 08:20:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-09 14:32:19 +00:00
|
|
|
}
|
2019-01-14 11:47:09 +00:00
|
|
|
|
|
|
|
let child;
|
2019-01-09 14:32:19 +00:00
|
|
|
|
2019-01-14 11:47:09 +00:00
|
|
|
if (software.sudo) {
|
|
|
|
child = spawn('powershell.exe', ['Start-Process', '-FilePath', `"${softwarePath}"`]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
child = spawn(softwarePath, software.installation_arguments);
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:51:23 +00:00
|
|
|
child.on('exit', () => {
|
2019-01-14 13:07:42 +00:00
|
|
|
event.sender.send(responseChannel, {
|
|
|
|
success: true
|
|
|
|
});
|
2019-01-11 12:51:23 +00:00
|
|
|
});
|
2019-01-14 08:20:07 +00:00
|
|
|
|
2019-01-14 11:47:09 +00:00
|
|
|
child.on('error', (err) => {
|
2019-01-14 13:07:42 +00:00
|
|
|
event.sender.send(responseChannel, {
|
|
|
|
success: false,
|
|
|
|
message: err.message
|
|
|
|
});
|
2019-01-14 11:47:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
child.stdin.end();
|
2019-01-11 12:51:23 +00:00
|
|
|
});
|