forked from dceejay/electron-node-red
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·446 lines (402 loc) · 15.4 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
'use strict';
const pkg = require('./package.json');
let options;
if (pkg.hasOwnProperty("NRelectron")) { options = pkg["NRelectron"] }
// Some settings you can edit if you don't set them in package.json
//console.log(options)
const editable = options.editable || true; // set this to false to create a run only application - no editor/no console
const allowLoadSave = options.allowLoadSave || false; // set to true to allow import and export of flow file
const showMap = options.showMap || false; // set to true to add Worldmap to the menu
const kioskMode = options.kioskMode || false; // set to true to start in kiosk mode
const addNodes = options.addNodes || true; // set to false to block installing extra nodes
let flowfile = options.flowFile || 'electronflow.json'; // default Flows file name - loaded at start
const urldash = "/ui/#/0"; // url for the dashboard page
const urledit = "/red"; // url for the editor page
const urlconsole = "/console.htm"; // url for the console page
const urlmap = "/worldmap"; // url for the worldmap
const nrIcon = "nodered.png" // Icon for the app in root dir (usually 256x256)
let urlStart; // Start on this page
if (options.start.toLowerCase() === "editor") { urlStart = urledit; }
else if (options.start.toLowerCase() === "map") { urlStart = urlmap; }
else { urlStart = urldash; }
// TCP port to use
//const listenPort = "18880"; // fix it if you like
const listenPort = parseInt(Math.random()*16383+49152) // or random ephemeral port
const os = require('os');
const fs = require('fs');
const url = require('url');
const path = require('path');
const http = require('http');
const express = require("express");
const electron = require('electron');
const isDev = require('electron-is-dev');
const {app, Menu} = electron;
const ipc = electron.ipcMain;
const dialog = electron.dialog;
const BrowserWindow = electron.BrowserWindow;
const Tray = electron.Tray;
var RED = require("node-red");
var red_app = express();
// Add a simple route for static content served from 'public'
red_app.use("/",express.static("web"));
//red_app.use(express.static(__dirname +"/public"));
// Create a server
var server = http.createServer(red_app);
// Setup user directory and flowfile (if editable)
var userdir = __dirname;
if (editable) {
// if running as raw electron use the current directory (mainly for dev)
if (process.argv[1] && (process.argv[1] === "main.js")) {
userdir = __dirname;
if ((process.argv.length > 2) && (process.argv[process.argv.length-1].indexOf(".json") > -1)) {
if (path.isAbsolute(process.argv[process.argv.length-1])) {
flowfile = process.argv[process.argv.length-1];
}
else {
flowfile = path.join(process.cwd(),process.argv[process.argv.length-1]);
}
}
}
else { // We set the user directory to be in the users home directory...
userdir = os.homedir() + '/.node-red';
if (!fs.existsSync(userdir)) {
fs.mkdirSync(userdir);
}
if ((process.argv.length > 1) && (process.argv[process.argv.length-1].indexOf(".json") > -1)) {
if (path.isAbsolute(process.argv[process.argv.length-1])) {
flowfile = process.argv[process.argv.length-1];
}
else {
flowfile = path.join(process.cwd(),process.argv[process.argv.length-1]);
}
}
else {
if (!fs.existsSync(userdir+"/"+flowfile)) {
fs.writeFileSync(userdir+"/"+flowfile, fs.readFileSync(__dirname+"/"+flowfile));
}
let credFile = flowfile.replace(".json","_cred.json");
if (fs.existsSync(__dirname+"/"+credFile) && !fs.existsSync(userdir+"/"+credFile)) {
fs.writeFileSync(userdir+"/"+credFile, fs.readFileSync(__dirname+"/"+credFile));
}
}
}
}
// console.log("CWD",process.cwd());
// console.log("DIR",__dirname);
// console.log("UserDir :",userdir);
// console.log("FlowFile :",flowfile);
// console.log("PORT",listenPort);
// Keep a global reference of the window objects, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let conWindow;
let tray;
let logBuffer = [];
let logLength = 250; // No. of lines of console log to keep.
const levels = [ "", "fatal", "error", "warn", "info", "debug", "trace" ];
ipc.on('clearLogBuffer', function() { logBuffer = []; });
// Create the settings object - see default settings.js file for other options
var settings = {
uiHost: "localhost", // only allow local connections, remove if you want to allow external access
httpAdminRoot: "/red", // set to false to disable editor and deploy
httpNodeRoot: "/",
userDir: userdir,
flowFile: flowfile,
editorTheme: { projects:{ enabled:false }, palette: { editable:addNodes } }, // enable projects feature
functionGlobalContext: { }, // enables global context - add extras ehre if you need them
logging: {
websock: {
level: 'info',
metrics: false,
handler: function() {
return function(msg) {
if (editable) { // No logging if not editable
var ts = (new Date(msg.timestamp)).toISOString();
ts = ts.replace("Z"," ").replace("T"," ");
var line = "";
if (msg.type && msg.id) {
line = ts+" : ["+levels[msg.level/10]+"] ["+msg.type+":"+msg.id+"] "+msg.msg;
}
else {
line = ts+" : ["+levels[msg.level/10]+"] "+msg.msg;
}
logBuffer.push(line);
if (conWindow) { conWindow.webContents.send('debugMsg', line); }
if (logBuffer.length > logLength) { logBuffer.shift(); }
}
}
}
}
}
};
if (!editable) {
settings.httpAdminRoot = false;
settings.readOnly = true;
}
// Initialise the runtime with a server and settings
RED.init(server,settings);
// Serve the editor UI from /red (if editable)
if (settings.httpAdminRoot !== false) {
red_app.use(settings.httpAdminRoot,RED.httpAdmin);
}
// Serve the http nodes UI from /
red_app.use(settings.httpNodeRoot,RED.httpNode);
// Create the Application's main menu
var template = [{
label: "View",
submenu: [
{ label: 'Import Flow',
accelerator: "Shift+CmdOrCtrl+O",
click() { openFlow(); }
},
{ label: 'Save Flow As',
accelerator: "Shift+CmdOrCtrl+S",
click() { saveFlow(); }
},
{ type: 'separator' },
{ label: 'Console',
accelerator: "Shift+CmdOrCtrl+C",
click() { createConsole(); }
},
{ label: 'Dashboard',
accelerator: "Shift+CmdOrCtrl+D",
click() { mainWindow.loadURL("http://localhost:"+listenPort+urldash); }
},
{ label: 'Editor',
accelerator: "Shift+CmdOrCtrl+E",
click() { mainWindow.loadURL("http://localhost:"+listenPort+urledit); }
},
{ label: 'Worldmap',
accelerator: "Shift+CmdOrCtrl+M",
click() { mainWindow.loadURL("http://localhost:"+listenPort+urlmap); }
},
{ type: 'separator' },
{ type: 'separator' },
{ label: 'Documentation',
click() { electron.shell.openExternal('https://nodered.org/docs') }
},
{ label: 'Flows and Nodes',
click() { electron.shell.openExternal('https://flows.nodered.org') }
},
{ label: 'Discourse Forum',
click() { electron.shell.openExternal('https://discourse.nodered.org/') }
},
{ type: 'separator' },
{ role: 'togglefullscreen' },
{ role: 'quit' }
]
}];
if (!showMap) { template[0].submenu.splice(6,1); }
if (!editable) {
template[0].submenu.splice(3,1);
template[0].submenu.splice(4,1);
}
if (!allowLoadSave) { template[0].submenu.splice(0,2); }
// Top and tail menu on Mac
if (process.platform === 'darwin') {
template[0].submenu.unshift({ type: 'separator' });
template[0].submenu.unshift({ label: "About "+options.productName||"Node-RED Electron", selector: "orderFrontStandardAboutPanel:" });
template[0].submenu.unshift({ type: 'separator' });
template[0].submenu.unshift({ type: 'separator' });
}
// Add Dev menu if in dev mode
if (isDev) {
template.push({
label: 'Development',
submenu: [
{ label: 'Reload', accelerator: 'CmdOrCtrl+R',
click (item, focusedWindow) {
if (focusedWindow) focusedWindow.reload()
}
},
{ label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click (item, focusedWindow) {
if (focusedWindow) focusedWindow.webContents.toggleDevTools()
}
}
]
})
}
let fileName = "";
function saveFlow() {
dialog.showSaveDialog({
filters:[{ name:'JSON', extensions:['json'] }],
defaultPath: fileName
}, function(file_path) {
if (file_path) {
var flo = JSON.stringify(RED.nodes.getFlows().flows);
fs.writeFile(file_path, flo, function(err) {
if (err) { dialog.showErrorBox('Error', err); }
else {
dialog.showMessageBox({
icon: nrIcon,
message:"Flow file saved as\n\n"+file_path,
buttons: ["OK"]
});
}
});
}
});
}
function openFlow() {
dialog.showOpenDialog({ filters:[{ name:'JSON', extensions:['json']} ]},
function (fileNames) {
if (fileNames && fileNames.length > 0) {
fs.readFile(fileNames[0], 'utf-8', function (err, data) {
try {
var flo = JSON.parse(data);
if (Array.isArray(flo) && (flo.length > 0)) {
RED.nodes.setFlows(flo,"full");
fileName = fileNames[0];
}
else {
dialog.showErrorBox("Error", "Failed to parse flow file.\n\n "+fileNames[0]+".\n\nAre you sure it's a flow file ?");
}
}
catch(e) {
dialog.showErrorBox("Error", "Failed to load flow file.\n\n "+fileNames[0]);
}
});
}
}
)
}
// Create the console log window
function createConsole() {
if (conWindow) { conWindow.show(); return; }
// Create the hidden console window
conWindow = new BrowserWindow({
title: "Node-RED Console",
width: 800,
height: 600,
icon: path.join(__dirname, nrIcon),
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true
}
});
conWindow.loadURL(url.format({
pathname: path.join(__dirname, urlconsole),
protocol: 'file:',
slashes: true
}))
conWindow.webContents.on('did-finish-load', () => {
conWindow.webContents.send('logBuff', logBuffer);
});
conWindow.on('closed', () => {
conWindow = null;
});
//conWindow.webContents.openDevTools();
}
// Create the main browser window
function createWindow() {
mainWindow = new BrowserWindow({
title: "Node-RED",
width: 1024,
height: 768,
icon: path.join(__dirname, nrIcon),
fullscreenable: true,
autoHideMenuBar: false,
kiosk: kioskMode,
webPreferences: {
nodeIntegration: false
}
});
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
if (process.platform !== 'darwin') { mainWindow.setAutoHideMenuBar(true); }
mainWindow.loadURL(`file://${__dirname}/load.html`);
mainWindow.webContents.on('did-get-response-details', function(event, status, newURL, originalURL, httpResponseCode) {
if ((httpResponseCode == 404) && (newURL == ("http://localhost:"+listenPort+urlStart))) {
setTimeout(mainWindow.webContents.reload, 250);
}
});
// mainWindow.webContents.on('did-finish-load', (a) => {
// console.log("FINISHED LOAD",a);
// });
mainWindow.webContents.on("new-window", function(e, url, frameName, disposition, option) {
// if a child window opens... modify any other options such as width/height, etc
// in this case make the child overlap the parent exactly...
//console.log("NEW WINDOW",url);
var w = mainWindow.getBounds();
option.x = w.x;
option.y = w.y;
option.width = w.width;
option.height = w.height;
})
mainWindow.on('close', function(e) {
const choice = require('electron').dialog.showMessageBoxSync(this, {
type: 'question',
icon: nrIcon,
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?'
});
if (choice === 1) {
e.preventDefault();
}
});
mainWindow.on('closed', () => {
mainWindow = null;
});
// Start the app full screen
//mainWindow.setFullScreen(true)
// Open the DevTools at start
//mainWindow.webContents.openDevTools();
}
// Create the tray icon and context menu
function createTray() {
tray = new Tray(path.join(__dirname, "nrtray.png"));
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: function() {
mainWindow.show();
}
},
{
label: 'Quit',
click: function() {
app.quit();
}
}
]);
tray.setToolTip('Node-RED Electron application.')
tray.setContextMenu(contextMenu);
}
// Called when Electron has finished initialization and is ready to create browser windows.
app.on('ready', () => {
createTray()
createWindow()
})
// 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(); }
});
app.on('activate', function() {
// On OS X 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();
mainWindow.loadURL("http://localhost:"+listenPort+urlStart);
}
});
if (process.platform === 'darwin') {
app.setAboutPanelOptions({
applicationVersion: pkg.version,
version: pkg.dependencies["node-red"],
copyright: "Copyright © 2019, "+pkg.author.name,
credits: "Node-RED and other components are copyright the JS Foundation and other contributors."
});
// Don't show in the dock bar if you like
//app.dock.hide();
}
// Start the Node-RED runtime, then load the inital dashboard page
RED.start().then(function() {
server.listen(listenPort,"localhost",function() {
mainWindow.loadURL("http://localhost:"+listenPort+urlStart);
});
});