forked from COVAIL/electron-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
172 lines (138 loc) · 7.21 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
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
const port = "9191"
const child = require('child_process');
const MACOS = "darwin"
const WINDOWS = "win32"
//const cmdStr = ".\\R-Portable\\bin\\RScript.exe -e \"shiny::runApp('shinyApp.R', port="+port+")\"";
//ShinyProjects\MarketSizing\Market_sizing_Cardinal-master-b823f3aa918475f5d56f01aec9763ed860715158\mrktsiz_no_crosstalk
var killStr = ""
var appPath = path.join(app.getAppPath(), "app.R" )
var execPath = "RScript"
if(process.platform == WINDOWS){
//killStr = "taskkill /im Rscript.exe /f"
appPath = appPath.replace(/\\/g, "\\\\");
execPath = path.join(app.getAppPath(), "R-Portable-Win", "bin", "RScript.exe" )
} else if(process.platform == MACOS){
//killStr = 'pkill -9 "R"'
//execPath = "export PATH=\""+path.join(app.getAppPath(), "R-Portable-Win")+":$PATH\"
var macAbsolutePath = path.join(app.getAppPath(), "R-Portable-Mac")
var env_path = macAbsolutePath+((process.env.PATH)?":"+process.env.PATH:"");
var env_libs_site = macAbsolutePath+"/library"+((process.env.R_LIBS_SITE)?":"+process.env.R_LIBS_SITE:"");
process.env.PATH = env_path
process.env.R_LIBS_SITE = env_libs_site
process.env.NODE_R_HOME = macAbsolutePath
//process.env.R_HOME = macAbsolutePath
execPath = path.join(app.getAppPath(), "R-Portable-Mac", "bin", "R" )
} else {
console.log("not on windows or macos?")
throw new Error("not on windows or macos?")
}
console.log(process.env)
const childProcess = child.spawn(execPath, ["-e", "shiny::runApp(file.path('"+appPath+"'), port="+port+")"])
childProcess.stdout.on('data', (data) => {
console.log(`stdout:${data}`)
})
childProcess.stderr.on('data', (data) => {
console.log(`stderr:${data}`)
})
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
// mainWindow = new BrowserWindow({webPreferences:{nodeIntegration:false},width: 800, height: 600})
// console.log(process.cwd())
console.log('create-window')
let loading = new BrowserWindow({show: false, frame: false})
//let loading = new BrowserWindow()
console.log(new Date().toISOString()+'::showing loading');
loading.loadURL("data:text/html;charset=utf-8;base64,PGh0bWw+DQo8c3R5bGU+DQpib2R5ew0KICBwYWRkaW5nOiAxZW07DQogIGNvbG9yOiAjNzc3Ow0KICB0ZXh0LWFsaWduOiBjZW50ZXI7DQogIGZvbnQtZmFtaWx5OiAiR2lsbCBzYW5zIiwgc2Fucy1zZXJpZjsNCiAgd2lkdGg6IDgwJTsNCiAgbWFyZ2luOiAwIGF1dG87DQp9DQpoMXsNCiAgbWFyZ2luOiAxZW0gMDsNCiAgYm9yZGVyLWJvdHRvbTogMXB4IGRhc2hlZDsNCiAgcGFkZGluZy1ib3R0b206IDFlbTsNCiAgZm9udC13ZWlnaHQ6IGxpZ2h0ZXI7DQp9DQpwew0KICBmb250LXN0eWxlOiBpdGFsaWM7DQp9DQoubG9hZGVyew0KICBtYXJnaW46IDAgMCAyZW07DQogIGhlaWdodDogMTAwcHg7DQogIHdpZHRoOiAyMCU7DQogIHRleHQtYWxpZ246IGNlbnRlcjsNCiAgcGFkZGluZzogMWVtOw0KICBtYXJnaW46IDAgYXV0byAxZW07DQogIGRpc3BsYXk6IGlubGlu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0Q291bnQ9ImluZGVmaW5pdGUiLz4NCiAgICA8L3BhdGg+DQogIDwvc3ZnPg0KPC9kaXY+DQo8L2JvZHk+DQo8L2h0bWw+");
//loading.loadURL("http://www.google.com")
///loading.toggleDevTools()
loading.once('show', () => {
console.log(new Date().toISOString()+'::show loading')
mainWindow = new BrowserWindow({webPreferences:{nodeIntegration:false}, show:false, width: 800, height: 600, title:""})
/*
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
*/
mainWindow.webContents.once('dom-ready', () => {
console.log(new Date().toISOString()+'::mainWindow loaded')
setTimeout( () => {
mainWindow.show()
if(process.platform==MACOS){
mainWindow.reload()
}
loading.hide()
loading.close()
}, 2000)
})
console.log(port)
// long loading html
mainWindow.loadURL('http://127.0.0.1:'+port)
/**
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
**/
mainWindow.webContents.on('did-finish-load', function() {
console.log(new Date().toISOString()+'::did-finish-load')
});
mainWindow.webContents.on('did-start-load', function() {
console.log(new Date().toISOString()+'::did-start-load')
});
mainWindow.webContents.on('did-stop-load', function() {
console.log(new Date().toISOString()+'::did-stop-load')
});
mainWindow.webContents.on('dom-ready', function() {
console.log(new Date().toISOString()+'::dom-ready')
});
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
console.log(new Date().toISOString()+'::mainWindow.closed()')
cleanUpApplication()
})
})
loading.show()
}
function cleanUpApplication(){
app.quit()
if(childProcess){
childProcess.kill();
if(killStr != "")
child.execSync(killStr)
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
console.log('EVENT::window-all-closed')
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
cleanUpApplication()
// On macOS 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()
})
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.