-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
214 lines (197 loc) · 4.52 KB
/
index.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
'use strict';
const {app, Menu, Tray, BrowserWindow} = require('electron')
const command = require('shelljs/global')
const jquery = require('jquery')
const fs = require('fs')
const path = require('path')
const openLink = require('electron').shell
const trayActive = 'assets/logo/trayIcon.png'
const trayWait = 'assets/logo/trayIconWait.png'
let tray = null
let aboutUs = null
app.on('ready', () =>
{
tray = new Tray(path.join(__dirname, trayActive))
aboutUs = new BrowserWindow({
width : 400,
height : 600,
resizable : false,
fullscreen : false,
title : 'About | Vagrant Manager',
icon : __dirname+'/assets/logo/windowIcon.png',
show : false,
})
aboutUs.setMenu(null)
aboutUs.loadURL('file:\/\/'+__dirname+'/about.html')
// aboutUs.webContents.openDevTools()
aboutUs.webContents.on('new-window', function(e, url) {
e.preventDefault()
openLink.openExternal(url)
})
function boxDetails(callback)
{
var getFile = exec('cd ~ && pwd', {silent:true,async:true})
getFile.stdout.on('data', function(data)
{
var box = []
fs.readFile(data.trim()+'/.vagrant.d/data/machine-index/index', 'utf8', function (err, data)
{
if (err) throw err
var jsonData = JSON.parse(data)
for(var index in jsonData.machines) {
box.push({
'path' : jsonData.machines[index]['vagrantfile_path'],
'state' : jsonData.machines[index]['state'],
'name' : jsonData.machines[index]['extra_data']['box']['name'],
'provider' : jsonData.machines[index]['extra_data']['box']['provider'],
})
}
return callback(box)
})
})
}
var vagrantManager = function(event)
{
tray.setImage(path.join(__dirname, trayActive))
boxDetails( function(box)
{
var menu = [
{
label: "Refresh",
click: function(menuItem)
{
vagrantManager()
}
},
{
type: "separator"
}]
for(var index in box) {
menu.push(
{
label: box[index]['name'],
icon: __dirname+"/assets/logo/"+box[index]['state']+".png",
submenu: [
{
label: "Vagrant Up",
submenu: [
{
label: "Up",
sublabel: index,
id: box[index]['path'],
click: function(menuItem)
{
runShell(contextMenu, menuItem, "vagrant up")
}
}]
},
{
label: "Vagrant Suspend",
submenu: [
{
label: "Suspend",
sublabel: index,
id: box[index]['path'],
click: function(menuItem)
{
runShell(contextMenu, menuItem, "vagrant suspend")
}
}]
},
{
label: "Vagrant Resume",
submenu: [
{
label: "Resume",
sublabel: index,
id: box[index]['path'],
click: function(menuItem)
{
runShell(contextMenu, menuItem, "vagrant resume")
}
}]
},
{
label: "Vagrant Halt",
submenu: [
{
label: "Halt",
sublabel: index,
id: box[index]['path'],
click: function(menuItem)
{
runShell(contextMenu, menuItem, "vagrant halt")
}
}]
},
{
label: "Vagrant Destroy",
submenu: [
{
label: "Destroy",
sublabel: index,
id: box[index]['path'],
click: function(menuItem)
{
runShell(contextMenu, menuItem, "vagrant destroy")
}
}]
},
{
type: "separator"
},
{
label : "Provider: "+box[index]['provider'],
enabled: false
},
{
label: "Status: "+box[index]['state'],
enabled: false
}
]
})
}
menu.push(
{
type: "separator"
},
{
label: 'About',
click: function (menuItem)
{
aboutUs.show()
aboutUs.on('close', function (e)
{
e.preventDefault()
aboutUs.hide()
aboutUs.removeAllListeners('close')
})
}
},
{
label: "Quit",
role: 'quit'
})
var contextMenu = Menu.buildFromTemplate(menu)
tray.setToolTip('Vagrant Manager')
tray.setContextMenu(contextMenu)
})
}
let runShell = function(contextMenu, menuItem, command)
{
tray.setImage(path.join(__dirname, trayWait))
contextMenu.items[0].enabled = false
var parentID = +menuItem.sublabel + 2
contextMenu.items[parentID].enabled = false
tray.setContextMenu(contextMenu)
let shellCommand = new exec('cd ' + menuItem.id + ' && '+ command, function(code, stdout, stderr)
{
console.log('Exit code:', code)
console.log('Program output:', stdout)
console.log('Program stderr:', stderr)
vagrantManager()
})
}
// Run
vagrantManager()
})