-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderer.js
260 lines (238 loc) · 6.91 KB
/
renderer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Require all electron and nodeJS dependencies.
var app = require('electron').remote;
var dialog = app.dialog;
var fs = require('fs');
const exec = require('child_process').exec;
// Set up settings variable.
var settings = "";
main();
/**
* Adds behavior to all elements.
*
* Delegates to readSettings to fill in the settings variable if a settings file
* exists and tries to bind events and parses the composer file.
*/
function main() {
bindEvents();
// Try to read settings from the settings.json file if that's found on disk.
settings = readSettings();
// Settings are found, so parse the json file, bind events, and show the
// "remove" actions box.
if (settings != false) {
document.getElementById('actions_remove').classList.remove('visually-hidden')
document.getElementById('actions_select').classList.add('visually-hidden')
parseComposerJson();
}
// Add action to select button.
document.getElementById('action__select').addEventListener('click', openFile);
}
/**
* Parses the composer.json file to see if it's valid.
*
* Also checks to see if the composer-file has dependencies on drupal or is the
* drupal core's composer.json.
*/
function parseComposerJson() {
var composerfile = settings.fileName;
var content = fs.readFileSync(composerfile, "utf8");
var composer = JSON.parse(content);
if (composer.name == '') {
logMessage('No name in comopser.json, exiting.');
removeSettings();
return;
}
document.getElementById('actions__composer').classList.remove('visually-hidden')
logMessage('parsed composer.json');
if (composer.name == 'drupal/drupal') {
document.getElementById('actions__drupal').classList.remove('visually-hidden')
logMessage('It\'s a drupal.');
}
for (var k in composer.require) {
if (k == 'drupal/core') {
document.getElementById('actions__drupal').classList.remove('visually-hidden')
logMessage('It\'s a drupal.');
}
}
}
/**
* Binds events to buttons.
*/
function bindEvents() {
document.getElementById('action__install')
.addEventListener('click', runComposerInstall);
document.getElementById('action__update')
.addEventListener('click', runComposerUpdate);
document.getElementById('action__update_drupal')
.addEventListener('click', runDrupalUpdate);
document.getElementById('action__remove_settings')
.addEventListener('click', removeSettings);
}
/**
* Updates the drupal core package.
*/
function runDrupalUpdate() {
showThrobber();
var dir = settings.dir;
if (settings.verbose) {
var command = 'composer update --with-dependencies --verbose drupal/core';
}
else {
var command = 'composer update --with-dependencies drupal/core';
}
logMessage('Updating drupal');
logCommand(command, dir);
exec('cd ' + dir + '; ' + command, (error, stdout, stderr) => {
if (error) {
logMessage(error);
return;
}
logMessage(`stdout: ${stdout}`);
logMessage(`stderr: ${stderr}`);
logMessage('Drupal install done.');
hideThrobber();
});
}
/**
* Installs all composer dependencies.
*/
function runComposerInstall() {
showThrobber();
var dir = settings.dir;
if (settings.verbose) {
var command = 'composer install --verbose'
}
else {
var command = 'composer install';
}
logMessage('Installing dependencies.');
logCommand(command, dir);
exec('cd ' + dir + '; ' + command, (error, stdout, stderr) => {
if (error) {
logMessage(error);
return;
}
logMessage(`stdout: ${stdout}`);
logMessage(`stderr: ${stderr}`);
logMessage('Composer install done.');
hideThrobber();
});
}
/**
* Updates all composer dependencies.
*/
function runComposerUpdate() {
showThrobber();
var dir = settings.dir;
if (settings.verbose) {
var command = 'composer update --with-dependencies --verbose';
}
else {
var command = 'composer update --with-dependencies';
}
logMessage('Updating dependencies.');
logCommand(command, dir);
exec('cd ' + dir + '; ' + command, (error, stdout, stderr) => {
if (error) {
logMessage(error);
return;
}
logMessage(`stdout: ${stdout}`);
logMessage(`stderr: ${stderr}`);
logMessage('Composer update done.');
hideThrobber();
});
}
/**
* Removes the settings.json file.
*/
function removeSettings() {
document.getElementById('actions__composer').classList.add('visually-hidden');
document.getElementById('actions__drupal').classList.add('visually-hidden');
document.getElementById('actions_remove').classList.add('visually-hidden');
document.getElementById('actions_select').classList.remove('visually-hidden');
fs.unlink(__dirname + '/settings.json');
logMessage('Settings file removed, starting over will allow you to select a new directory.');
}
/**
* Opens a file using the dialog method.
*/
function openFile() {
dialog.showOpenDialog(function (fileNames) {
// fileNames is an array that contains all the selected files.
if (fileNames === undefined) {
logMessage("No file selected");
}
else {
var fileName = fileNames[0];
if (fileName.indexOf('composer.json') == -1) {
logMessage('not a composer file.');
return false;
}
// remove 'composer.json' from the filename to get
// the directory.
settings = {
dir: fileName.replace(/composer\.json/g, ''),
fileName: fileName,
verbose: false
}
writeSettings(settings);
}
});
}
/**
* Writes the settings json blob into settings.json.
*/
function writeSettings(settings) {
fs.writeFile(__dirname + '/settings.json', JSON.stringify( settings ), "utf8");
setTimeout(function() { main(); }, 100);
}
/**
* Try reading from the settings.json.
*/
function readSettings() {
try {
var content = fs.readFileSync(__dirname + '/settings.json', "utf8");
logMessage('Read and parsed settings.json.');
return JSON.parse(content);
} catch (err) {
logMessage('Failed to read settings.json.');
return false;
}
}
/**
* Logs a message to the 'log' div.
*
* @param {string} message
* The message to be added to the log container.
*/
function logMessage(message) {
var div = document.getElementById('log');
div.innerHTML = div.innerHTML + `${message} <br />`;
}
/**
* Logs a message about the command we're executing.
*
* This delegates to logMessage to do the actual writing but adds extra classes
* for styling options.
*
* @param {string} command
* The command.
* @param {string} dir
* The directory we're running this from.
*/
function logCommand(command, dir) {
var string = `<span class="log-command">Running command: <span class="command">${command}</span> from <span class="directory">${dir}</span></span>`;
logMessage(string);
}
/**
* Hides the throbber.
*/
function hideThrobber() {
document.getElementById('throbber').classList.add('visually-hidden');
}
/**
* Shows the throbber.
*/
function showThrobber() {
document.getElementById('throbber').classList.remove('visually-hidden');
}