This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 229
/
index-shell.js
275 lines (259 loc) · 7.97 KB
/
index-shell.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var atom = require('app');
var path = require('path');
var spawn = require('child_process').spawn;
var BrowserWindow = require('browser-window');
var Menu = require('menu');
var shell = require('shell');
var versionCheck = require('./lib/version-check');
var log = require('./lib/log');
var node = path.resolve(path.join(__dirname, 'vendor', 'node'));
var script = path.resolve(path.join(__dirname, 'index-server.js'));
var logger = require('fastlog')('', 'debug', '<${timestamp}>');
var server = null
var serverPort = null;
var mainWindow = null;
if (process.platform === 'win32') {
// HOME is undefined on windows
process.env.HOME = process.env.USERPROFILE;
}
atom.on('window-all-closed', exit);
atom.on('ready', makeWindow);
process.on('exit', function(code) {
logger.debug('Mapbox Studio exited ' + (undefined === code ? 'normally' : 'with ' + code));
});
process.on('uncaughtException', function(err) {
logger.debug('Hit unexpected JS Error in server, please report this entire log to https://github.com/mapbox/mapbox-studio-classic/issues');
if (err) {
logger.debug(err);
} else {
logger.debug('no error reported');
}
});
function exit() {
if (server){ server.kill() };
//atom.quit(); //is the way to do it: https://github.com/atom/atom-shell/blob/master/docs/api/app.md#app
if (atom.listeners('window-all-closed').length == 1){ atom.quit(); }
};
var shellLog = path.join(process.env.HOME, '.mapbox-studio', 'shell.log');
// set up shell.log and log rotation
log(shellLog, 10e6, shellsetup);
function shellsetup(err) {
if(err) {
console.log('shellsetup:', err);
exit();
return;
}
// Start the server child process.
server = spawn(node, [script, '--shell=true'])
.on('error', function(error) {
process.stdout.write('error spawning server process: ' + error + '\n');
})
.on('exit', exit);
if(!server.pid){
process.stdout.write('server process has no pid\n');
} else {
process.stdout.write('server process pid: ' + server.pid + '\n');
server.stdout.once('data', function(data) {
var matches = data.toString().match(/Mapbox Studio @ http:\/\/localhost:([0-9]+)\//);
if (!matches) {
console.warn('Server port not found');
process.exit(1);//?? atom.quit();
}
serverPort = matches[1];
logger.debug('Mapbox Studio @ http://localhost:'+serverPort+'/');
loadURL();
});
// Report crashes to our server.
require('crash-reporter').start();
}
};
function makeWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 960,
height: 600,
'min-width': 720,
'min-height': 480,
title: 'Mapbox Studio Classic',
'node-integration': 'all',
'web-preferences': {
webgl: true,
java: false,
webaudio: false
}
});
mainWindow.loadUrl('file://' + path.join(__dirname, 'app', 'loading.html'));
// Restore OS X fullscreen state.
restoreFullScreen();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
// Prevent page changes from updating the window title (typically to 'Untitled').
mainWindow.on('page-title-updated', function(e) {
e.preventDefault();
});
mainWindow.on('enter-full-screen', function(e) {
persistFullScreen();
});
mainWindow.on('leave-full-screen', function(e) {
persistFullScreen();
});
createMenu();
loadURL();
// Restore OS X fullscreen state.
function restoreFullScreen() {
if (process.platform !== 'darwin') return;
var cp = require("child_process");
if (cp.execSync("defaults read com.mapbox.mapbox-studio FullScreen 2>/dev/null || echo 0") == 1) {
setTimeout(function(){
mainWindow.setFullScreen(true);
}, 100);
}
}
// Persist OS X fullscreen state.
function persistFullScreen() {
if (process.platform !== 'darwin') return;
var cp = require("child_process");
cp.execSync("defaults write com.mapbox.mapbox-studio FullScreen -bool " + mainWindow.isFullScreen());
}
}
function loadURL() {
if (!mainWindow) return;
if (!serverPort) return;
versionCheck({
host: 'mapbox.s3.amazonaws.com',
path: '/mapbox-studio/latest',
pckge: require('./package.json')
}, function(update, current, latest){
update = update ? '/update?current='+current+'&latest='+latest : '';
mainWindow.loadUrl('http://localhost:'+serverPort + update);
});
}
function createMenu() {
var template;
if (process.platform == 'darwin') {
template = [
{
label: 'Mapbox Studio',
submenu: [
{
label: 'About Mapbox Studio',
selector: 'orderFrontStandardAboutPanel:'
},
{
type: 'separator'
},
{
label: 'Hide Mapbox Studio',
accelerator: 'Command+H',
selector: 'hide:'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
selector: 'hideOtherApplications:'
},
{
label: 'Show All',
selector: 'unhideAllApplications:'
},
{
type: 'separator'
},
{
label: 'Quit Mapbox Studio',
accelerator: 'Command+Q',
selector: 'performClose:'
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Cut',
accelerator: 'Command+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'Command+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'Command+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'Command+A',
selector: 'selectAll:'
}
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'Command+R',
click: function() { mainWindow.restart(); }
},
{
label: 'Toggle Developer Tools',
accelerator: 'Alt+Command+I',
click: function() { mainWindow.toggleDevTools(); }
},
{
type: 'separator'
},
{
label: 'Toggle Full Screen',
accelerator: 'Ctrl+Command+F',
click: function() { mainWindow.setFullScreen(!mainWindow.isFullScreen()); }
}
]
},
{
label: 'Window',
submenu: [
{
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:'
}
]
},
{
label: 'Help',
submenu: [
{
label: 'Online Resources',
click: function() { shell.openExternal('https://www.mapbox.com/mapbox-studio/'); }
},
{
label: 'Application Log',
click: function() {
var cp = require("child_process");
cp.exec("open -a /Applications/Utilities/Console.app ~/.mapbox-studio/app.log");
}
},
{
label: 'Shell Log',
click: function() {
var cp = require("child_process");
cp.exec("open -a /Applications/Utilities/Console.app ~/.mapbox-studio/shell.log");
}
}
]
}
];
menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
}