From 4bb41d2e6c06f5268d384fa4469f7a67809b458d Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 19 Nov 2019 15:21:09 +0000 Subject: [PATCH] tidy docs --- README.md | 38 +++++++++++++++++++++++++++----------- merger.js | 22 +++++++++++++++++----- package-template.json | 18 +++++++++--------- package.json | 18 +++++++++--------- 4 files changed, 62 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 35ff356..35a640c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![GitHub version](https://badge.fury.io/gh/dceejay%2Felectron-node-red.svg)](https://badge.fury.io/gh/dceejay%2Felectron-node-red) [![GitHub license](https://img.shields.io/github/license/dceejay/electron-node-red.svg)](https://github.com/dceejay/electron-node-red/blob/master/LICENSE) -This is an Electron template to embed [Node-RED](https://nodered.org) with a Dashboard generated by node-red-dashboard to create a native application. +This is an Electron template to embed [Node-RED](https://nodered.org) with an existing Node-RED project to create a native application. This is not intended to be a useful tool as-is, but as a base for you to create your own versions. You will need to edit the `main.js` to suit your application and update the `package.json` file to include your own required nodes and dependencies. @@ -11,6 +11,28 @@ There are several simple switches that can be set in the `NRelectron` section of the `package.json` file. More significant modifications will require modification of the `main.js` file. Have fun. +## Wrapping an existing Node-RED project + +The `merger.js` utility should be run from within this projects directory and can be pointed at +an existing Node-RED project directory. It will try to copy over and package up the +relevant files into this project ready to install and build - so the simple flow would be + +```bash +./merger.js {path to my Node-RED project directory} +yarn +yarn start +``` + +This uses the `package-template.json` file as the main electron setup - so you should edit any +build parameters, product name, whether you want the app version to be editable, run in kiosk mode, etc in the `NRelectron` section before running. + +The app name, version and description are picked up from the package.json file of the original project. + +When running using yarn start - the flow file is picked from the current directory. When running as an app (but only if editable), then the flow file is copied into the users `.node-red` directory and is read and written from there. The deafult name can be changed in the package.json file if required. + +**NOTE**: Currently the settings are set around line 109 of the `main.js` file. If you do use any +custom settings then currently you will need to modify this manually. + ## Configuring the project for building This project uses the **electron-builder** project to help build native versions @@ -40,17 +62,11 @@ yarn && yarn dist to create a runtime for your local platform. However - there may be some errors. If so they are usually fairly self explanatory, -and may just require installation of another npm or brew or apt package, +and may just require installation of another **npm** or **brew** or **apt** package, then retry the command. Runtimes are created in the `dist` directory under the `electron-node-red` project. -The `merger.js` utility can be pointed at an existing Node-RED projects directory and it will try to copy over and package up the relevant files into this project ready to install and build - so the simple flow would be - -``` -./merger.js {path to my Node-RED project directory} && yarn && yarn start -``` - ### Building for other platforms Generally you can just add the required parameter to the command @@ -61,12 +77,12 @@ yarn && yarn dist -l // for linux yarn && yarn dist -m // for mac ``` -These will generally fail the first time through and you will need to install some extra library in order to make it +These will generally fail the first time through, and you will need to install some extra library in order to make it complete successfully. The defaults are to build a `.msi` for Windows, a `.dmg` for Mac, and both a `.deb` and `.rpm` for Linux. These can be changed by editing the build section of the `package.json` file, see the -[electron-builder config docs](https://www.electron.build/configuration/configuration) for more info. +[electron-builder config docs](https://www.electron.build/configuration/configuration) for more information. ## Building multi platform using Docker @@ -89,6 +105,6 @@ The initial flow file is named `electronflow.json` along with it's credentials f `electronflow_cred.json`. Just copy your existing flow in instead. The default is to start on the dashboard page - as this is intended to be just an application - without the Node-RED editor exposed to the end user, but there are some simple flags to -to configure this at the top of `main.js`. +to configure this within the package.json or at the top of main.js. --- diff --git a/merger.js b/merger.js index f7b881a..9ff06b9 100755 --- a/merger.js +++ b/merger.js @@ -8,6 +8,7 @@ let arg = "./example"; let flowfile = null; let dn = arg; let app; +// If extra param specified then is it a directory or a package or flow file ? if (process.argv.length === 3 ) { arg = process.argv[2]; @@ -30,31 +31,38 @@ else { app = require(arg+"/package.json"); } +// Merge electron settings over project settings (project has priority) const merge = { ...app.dependencies, ...pkg.dependencies }; pkg.dependencies = merge; -// Try to get flow file name +// Try to get flow file name from package.json setiings if (app.hasOwnProperty("node-red") && app["node-red"].hasOwnProperty("settings") && app["node-red"].settings.hasOwnProperty("flowFile") ) { pkg.NRelectron.flowFile = app["node-red"].settings.flowFile; -} +} // or the npm scripts if there is a run command else if (app.hasOwnProperty("scripts") && app.scripts.hasOwnProperty("start")) { pkg.NRelectron.flowFile = app.scripts.start.split(' ').pop(); -} -else { +} // or the command line if the user gave us a name - or just guess flow.json. +else { pkg.NRelectron.flowFile = flowfile || "flow.json"; } +// If dashboard is in package.json assume start with dashboard. if (merge.hasOwnProperty("node-red-dashboard")) { pkg.NRelectron.start = "dashboard"; } +// If map is not in package.json then force it to be hidden +if (!merge.hasOwnProperty("node-red-contrib-web-worldmap")) { + pkg.NRelectron.showmap = false; +} pkg.name = app.name; pkg.version = app.version; pkg.description = app.description; // console.log(pkg); +// Copy over existing flow file and creds file fs.copyFile(path.join(arg, pkg.NRelectron.flowFile), path.join("./", pkg.NRelectron.flowFile), (err) => { if (err) { console.log("Failed to copy flows file - "+path.join(arg, pkg.NRelectron.flowFile)); } else { console.log('Copied flows file - '+pkg.NRelectron.flowFile); } @@ -65,9 +73,13 @@ fs.copyFile(path.join(arg, creds), path.join("./", creds), (err) => { else { console.log('Copied creds file - '+creds); } }); +// Finally re-write th new package.json fs.writeFile("./package.json", JSON.stringify(pkg, null, 4), 'utf8', function (err) { if (err) { console.log("Failed to re-write package.json file."); } else { - console.log("Merged package.json."); + console.log("Merged package.json"); + console.log("OK - you can now run yarn"); + console.log("and then yarn start to run"); + console.log(" or yarn dist to build"); } }); diff --git a/package-template.json b/package-template.json index cd7746e..3e9cc18 100644 --- a/package-template.json +++ b/package-template.json @@ -3,6 +3,15 @@ "version": "1.0.2", "description": "Electron Node-RED application starter kit for development", "main": "main.js", + "NRelectron": { + "productName": "Node-RED Electron", + "editable": true, + "allowLoadSave": false, + "showMap": false, + "kioskMode": false, + "flowFile": "electronflow.json", + "start": "editor" + }, "scripts": { "pack": "electron-builder --dir", "dist": "electron-builder", @@ -38,15 +47,6 @@ "electron": "^7.1.1", "electron-builder": "^22.1.0" }, - "NRelectron": { - "productName": "Node-RED Electron", - "editable": true, - "allowLoadSave": false, - "showMap": false, - "kioskMode": false, - "flowFile": "electronflow.json", - "start": "editor" - }, "build": { "appId": "com.electron.node-red", "productName": "Node-RED Electron", diff --git a/package.json b/package.json index 82f8f38..8ee0d54 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,15 @@ "version": "1.0.2", "description": "Electron Node-RED application starter kit for development", "main": "main.js", + "NRelectron": { + "productName": "Node-RED Electron", + "editable": true, + "allowLoadSave": false, + "showMap": false, + "kioskMode": false, + "flowFile": "electronflow.json", + "start": "dashboard" + }, "scripts": { "pack": "electron-builder --dir", "dist": "electron-builder", @@ -45,15 +54,6 @@ "electron": "^7.1.1", "electron-builder": "^22.1.0" }, - "NRelectron": { - "productName": "Node-RED Electron", - "editable": true, - "allowLoadSave": false, - "showMap": false, - "kioskMode": false, - "flowFile": "electronflow.json", - "start": "dashboard" - }, "build": { "appId": "com.electron.node-red", "productName": "Node-RED Electron",