-
Notifications
You must be signed in to change notification settings - Fork 55
/
installed-software.js
104 lines (89 loc) · 2.67 KB
/
installed-software.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var commandExistsSync = require('command-exists').sync;
var app = require('electron').app;
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');
const { ipcMain } = require('electron');
var pipeline = util.promisify(stream.pipeline);
exports.getInstalledSoftware = (softwareList) => {
const installed = {};
for(var software of softwareList) {
var name = software.name;
var locations = software.locations;
installed[name] = [];
for(var location of locations) {
// var exists = commandExistsSync(command);
var exists = fs.existsSync(location);
if(exists) {
installed[name].push(location);
}
}
}
return installed;
}
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)
);
}
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);
}
}
ipcMain.on('installed-software-install', async function (event, software) {
const softwarePath = await getSoftwareInstallationPath(software);
const responseChannel = `installed-software-installed-${software.name}`;
if (software.type == 'web') {
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) {
event.sender.send(responseChannel, {
success: false,
message: error.message
});
}
}
}
let child;
if (software.sudo) {
child = spawn('powershell.exe', ['Start-Process', '-FilePath', `"${softwarePath}"`]);
}
else {
child = spawn(softwarePath, software.installation_arguments);
}
child.on('exit', () => {
event.sender.send(responseChannel, {
success: true
});
});
child.on('error', (err) => {
event.sender.send(responseChannel, {
success: false,
message: err.message
});
});
child.stdin.end();
});