-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
executable file
·242 lines (213 loc) · 7.06 KB
/
main.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
var os = require('os');
const electron = require('electron')
const {app, Menu, MenuItem, Tray} = require('electron')
const spawn = require('child_process').spawn;
const storage = require('electron-json-storage');
const {BrowserWindow} = require('electron')
const {ipcMain} = require('electron')
let tray = null
let contextMenu = new Menu();
let notifWin = null;
let managerWin = null;
let orgs = null
app.on('ready', () => {
// Our hidden notification window.
notifWin = new BrowserWindow({
show: false,
height:0,
width:0,
useContentSize: true,
frame: false,
skipTaskbar: true
})
notifWin.loadURL(`file://${__dirname}/notificationWindow.html`)
//MacOS tray icons, slightly different size (22x22 > 176x176)
if(os.platform() == 'darwin') {
tray = new Tray(app.getAppPath() + '/img/tray-icon-mac-Template.png')
}
//other icons, available in 32x32 > 256X256
else {
tray = new Tray(app.getAppPath() + '/img/tray-icon.png')
}
tray.setToolTip('Clicks not passwords.')
refreshMenu()
ipcMain.on("aliases",function(err,cmd, args){
// console.log("Received message: ", cmd, args);
switch (cmd){
case 'fetch':
ipc_aliases_fetch();
break;
case 'update':
ipc_aliases_update(args);
break;
case 'add':
ipc_aliases_add(args);
break;
default:
managerWin.webContents.send("aliases","unknown command: " + cmd);
}
})
}) // end app.on('ready')
function showMenu(orgs){
orgs.forEach(org => {
// console.log(org);
if (org.visible) contextMenu.append(new MenuItem({label: org.alias + " : " + org.value, click(){ openOrg(org.alias); }}));
});
contextMenu.append(new MenuItem({type: 'separator'}));
contextMenu.append(new MenuItem({label: 'Refresh', click() { refreshMenu() }}));
contextMenu.append(new MenuItem({type: 'separator'}));
contextMenu.append(new MenuItem({label: 'Manage', click() { launchManager() }}));
contextMenu.append(new MenuItem({type: 'separator'}));
contextMenu.append(new MenuItem({label: 'Quit', click() { app.quit() }}));
tray.setContextMenu(contextMenu)
}
function launchManager() {
if (managerWin && !managerWin.isDestroyed()) {
managerWin.show();
} else {
managerWin = new BrowserWindow({
width: 700,
height: 600
})
// Note: ionicMode=wp is to make it look not like Material design - maybe
// we want this, maybe we don't.
// managerWin.loadURL(`file://${__dirname}/www/index.html?ionicMode=wp`)
managerWin.loadURL(`file://${__dirname}/www/index.html`, {show:false});
// managerWin.webContents.openDevTools();
managerWin.once('ready-to-show', () => {
managerWin.show()
})
}
}
function refreshMenu() {
contextMenu = new Menu();
getOrgAliases().then(orgs => {
showMenu(orgs);
// Show our notification via the hidden window
notifWin.webContents.send('notification', 'Aliases refreshed')
}).catch(e => {
console.log("e", e);
contextMenu.append(new MenuItem({label: 'Quit', click() { app.quit() }}));
tray.setContextMenu(contextMenu)
// Show our notification via the hidden window
notifWin.webContents.send('notification', 'Oh dear, something went wrong :(\n' + e)
});
}
function getOrgAliases(){
return new Promise((resolve, reject) => {
// get Aliases from SFDX
var ret = spawn('sfdx', ['force:alias:list', '--json']);
ret.stdout.on('data', (data) => {
// console.log("SFDX ouput", JSON.parse(data));
// Update our version of config and enrich the result from SFDX
updateAndEnrichOrgs(JSON.parse(data).result).then(res => {
orgs = res;
resolve(res);
}).catch(e => {
console.log("e", e);
});
});
});
}
function updateAndEnrichOrgs(orgs){
return new Promise((resolve, reject) => {
storage.get('aliases', function(error, data) {
if (error) throw error;
// console.log("data", data);
let tmpAliases = (data.aliases) ? data.aliases : {};
let newAliases = []
let newAliasesForFile = {}
orgs.forEach(org => {
org.visible = (tmpAliases[org.alias]) ? tmpAliases[org.alias].visible : true;
org.order = (tmpAliases[org.alias]) ? tmpAliases[org.alias].order : 9999;
newAliasesForFile[org.alias] = org;
newAliases.push(org);
});
newAliases.sort(function (a, b) {
return a.order - b.order;
});
storage.set('aliases', {aliases: newAliasesForFile}, function(error) {
if (error) throw error;
});
resolve(newAliases);
});
});
}
function openOrg(orgAlias){
notifWin.webContents.send('notification', 'Your org will open in a new tab shortly')
var ret = spawn('sfdx', ['force:org:open', '-u', orgAlias]);
ret.stdout.on('data', (data) => {
});
ret.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
notifWin.webContents.send('notification', 'Oh dear, something went wrong :(\n' + data)
});
}
function ipc_aliases_fetch(){
return new Promise((resolve, reject) => {
if (orgs) {
managerWin.webContents.send("aliases",orgs);
} else {
getOrgAliases().then(x => {
managerWin.webContents.send("aliases",x);
}).catch(e => {
console.log("e", e);
managerWin.webContents.send("aliases","Error:" +JSON.stringify(e));
});
}
resolve();
});
}
function ipc_aliases_update(orgs){
storage.get('aliases', function(error, data) {
if (error) throw error;
orgs.forEach(org => {
data.aliases[org.alias].visible = org.visible;
if (typeof(org.order) !== "undefined") data.aliases[org.alias].order = org.order;
});
let updatedOrgs = Object.values(data.aliases);
updatedOrgs.sort(function (a, b) {
return a.order - b.order;
});
// Update the Tray Icon menu
contextMenu = new Menu();
showMenu(updatedOrgs);
// Write back to our conf file
storage.set('aliases', data, function(error) {
if (error) throw error;
});
});
}
function ipc_aliases_add(alias){
// Setup the alias itself
const exec = require('child_process').exec;
exec('sfdx force:alias:set ' + alias.alias + '=' + alias.value
, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stderr: ${stderr}`);
exec('sfdx force:auth:web:login -a ' + alias.alias + ' -r ' + alias.endpoint
, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stderr: ${stderr}`);
// All good output is something like this for stdout, so we use this
// ----
// Successfully authorized [email protected] with org id 00Db0000000dRGREA2
// You may now close the browser
// ----
if (stdout.includes("Successfully")) {
getOrgAliases().then(x => {
refreshMenu();
managerWin.webContents.send("aliases","add_ok");
});
} else {
managerWin.webContents.send("aliases","add_fail");
}
});
});
}