forked from OpenBazaar/OpenBazaar-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
122 lines (102 loc) · 3.59 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
"use strict";
// Check that the deps in node_modules match what's in package.json.
var safestart = require('safestart');
safestart(__dirname);
var fs = require('fs');
var path = require('path');
var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
var request = require('request');
var launched_from_installer = false;
// Check if we need to kick off the python server-daemon (Desktop app)
if(fs.existsSync(__dirname + path.sep + "OpenBazaar-Server")) {
launched_from_installer = true;
console.log('Starting OpenBazaar Server');
var platform = process.platform;
if(platform == "darwin" || platform == "linux") {
var subpy = require('child_process').spawn('/usr/local/bin/python', ['bootstrap.py', 'testnet', '--loglevel', 'debug'], {
detach: true,
cwd: __dirname + '/OpenBazaar-Server'
});
var stdout = '';
var stderr = '';
subpy.stdout.on('data', function (buf) {
console.log('[STR] stdout "%s"', String(buf));
stdout += buf;
});
subpy.stderr.on('data', function (buf) {
console.log('[STR] stderr "%s"', String(buf));
stderr += buf;
});
subpy.on('error', function(err) {
console.log('Python error %s', String(err));
});
subpy.on('close', function (code) {
console.log('exited with ' + code);
console.log('[END] stdout "%s"', stdout);
console.log('[END] stderr "%s"', stderr);
});
subpy.unref();
}
}
// Report crashes to our server.
require('crash-reporter').start();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform != 'darwin') {
app.quit();
}
});
// You can use 'before-quit' instead of (or with) the close event
app.on('before-quit', function (e) {
// Handle menu-item or keyboard shortcut quit here
console.log('Closing Application');
if(launched_from_installer) {
console.log('Shutting down server daemon');
request('http://localhost:18469/api/v1/shutdown', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('Shutting down server');
} else {
console.log('Server does not seem to be running.');
}
});
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({
"width": 1200,
"height": 720,
"min-width": 1024,
"min-height": 700,
"center": true,
"title": "OpenBazaar",
"frame": false,
"icon": "imgs/openbazaar-icon.png",
"title-bar-style": "hidden"
});
// and load the index.html of the app.
mainWindow.loadUrl('file://' + __dirname + '/index.html');
// Open the devtools.
mainWindow.openDevTools({detach: true});
// 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;
if(subpy) {
subpy.kill('SIGHUP');
}
});
app.on('activate-with-no-open-windows', function() {
mainWindow.show();
});
});