Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading 3rd party widgets #1286

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions nodes/config/ui_base.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const fs = require('fs')
const path = require('path')

const v = require('../../package.json').version
const datastore = require('../store/data.js')
const statestore = require('../store/state.js')
const { appendTopic, addConnectionCredentials } = require('../utils/index.js')
const { appendTopic, addConnectionCredentials, getThirdPartyWidgets } = require('../utils/index.js')

// from: https://stackoverflow.com/a/28592528/3016654
function join (...paths) {
Expand Down Expand Up @@ -90,36 +89,8 @@ module.exports = function (RED) {
/**
* Load in third party widgets
*/
let packagePath, packageJson
if (RED.settings?.userDir) {
packagePath = path.join(RED.settings.userDir, 'package.json')
packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
} else {
node.log('Cannot import third party widgets. No access to Node-RED package.json')
}

if (packageJson && packageJson.dependencies) {
Object.entries(packageJson.dependencies)?.filter(([packageName, _packageVersion]) => {
return packageName.includes('node-red-dashboard-2-')
}).map(([packageName, _packageVersion]) => {
const modulePath = path.join(RED.settings.userDir, 'node_modules', packageName)
const packagePath = path.join(modulePath, 'package.json')
// get third party package.json
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
if (packageJson?.['node-red-dashboard-2']) {
// loop over object of widgets
Object.entries(packageJson['node-red-dashboard-2'].widgets).forEach(([widgetName, widgetConfig]) => {
uiShared.contribs[widgetName] = {
package: packageName,
name: widgetName,
src: widgetConfig.output,
component: widgetConfig.component
}
})
}
return packageJson
})
}
uiShared.contribs = loadContribs(node)

/**
* Configure Web Server to handle UI traffic
Expand Down Expand Up @@ -217,6 +188,43 @@ module.exports = function (RED) {
}
}

function loadContribs (node) {
// from nodesDir
let contribs = { ...uiShared.contribs }
if (RED.settings?.nodesDir) {
const nodesDir = Array.isArray(RED.settings.nodesDir) ? RED.settings.nodesDir : [RED.settings.nodesDir]
for (const dir of nodesDir) {
try {
if (!dir || typeof dir !== 'string') { continue }
const _contribs = getThirdPartyWidgets(dir)
contribs = { ...contribs, ..._contribs }
} catch (error) {
node.log(`Cannot import third party widgets from nodes directory '${dir}}' package.json`)
}
}
}

// from user directory package.json
if (RED.settings?.userDir) {
try {
const _contribs = getThirdPartyWidgets(RED.settings.userDir)
contribs = { ...contribs, ..._contribs }
} catch (error) {
node.log('Cannot import third party widgets from user directory package.json')
}
}

// from main Node-RED package.json
try {
const appRoot = path.join(require.main.paths?.[0]?.split('node_modules')[0], '..')
const _contribs = getThirdPartyWidgets(appRoot)
contribs = { ...contribs, ..._contribs }
} catch (error) {
node.log('Cannot import third party widgets from main application root package.json')
}
return contribs
}

/**
* Close the SocketIO Server
*/
Expand Down
47 changes: 46 additions & 1 deletion nodes/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('fs')
const path = require('path')

function asyncEvaluateNodeProperty (RED, value, type, node, msg) {
return new Promise(function (resolve, reject) {
RED.util.evaluateNodeProperty(value, type, node, msg, function (e, r) {
Expand Down Expand Up @@ -54,8 +57,50 @@ function addConnectionCredentials (RED, msg, conn, config) {
return msg
}

function getThirdPartyWidgets (directory) {
const contribs = {}
const packagePath = path.join(directory, 'package.json')
if (!fs.existsSync(packagePath)) {
return contribs
}
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
const getWidgets = (packageJson) => {
if (packageJson?.['node-red-dashboard-2']) {
// loop over object of widgets & add to contribs object
Object.entries(packageJson['node-red-dashboard-2'].widgets).forEach(([widgetName, widgetConfig]) => {
contribs[widgetName] = {
package: packageJson.name,
name: widgetName,
src: widgetConfig.output,
path: path.resolve(path.join(directory)),
Steve-Mcl marked this conversation as resolved.
Show resolved Hide resolved
component: widgetConfig.component
}
})
}
}
if (packageJson?.['node-red-dashboard-2']) {
// this _is_ a dashboard node! get its widgets.
getWidgets(packageJson)
} else if (packageJson && packageJson.dependencies) {
// get widgets from dependencies of this package
Object.entries(packageJson.dependencies)?.filter(([packageName, _packageVersion]) => {
return packageName.includes('node-red-dashboard-2-')
}).forEach(([packageName, _packageVersion]) => {
const modulePath = path.join(directory, 'node_modules', packageName)
Steve-Mcl marked this conversation as resolved.
Show resolved Hide resolved
const packagePath = path.join(modulePath, 'package.json')
// get third party package.json
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
if (packageJson?.['node-red-dashboard-2']) {
getWidgets(packageJson)
}
})
}
return contribs
}

module.exports = {
asyncEvaluateNodeProperty,
appendTopic,
addConnectionCredentials
addConnectionCredentials,
getThirdPartyWidgets
}
20 changes: 20 additions & 0 deletions test/nodes/fixtures/contrib-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@me/node-red-dashboard-2-two-widgets",
"version": "1.0.0",
"description": "My dashboard 2 test node package",
"node-red-dashboard-2": {
"version": "1.0.0",
"widgets": {
"ui-widget-1": {
"output": "ui-widget-1.js",
"component": "ui-widget-1"
},
"ui-widget-2": {
"output": "ui-widget-2.js",
"component": "ui-widget-2"
}
}
},
"author": "Your Name",
"license": "Apache-2.0"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/nodes/fixtures/contrib-nodes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@me/my-2-widgets",
"version": "1.0.0",
"description": "My dashboard 2 test node",
"dependencies": {
"@me/node-red-dashboard-2-widget-a": "1.0.0",
"@me/node-red-dashboard-2-widget-b": "1.0.0"
},
"author": "Your Name",
"license": "Apache-2.0"
}
42 changes: 42 additions & 0 deletions test/nodes/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const should = require('should') // eslint-disable-line no-unused-vars

const utils = require('../../nodes/utils/index.js')

describe('utils', function () {
describe('getThirdPartyWidgets', function () {
it('should load single node package', function () {
// this covers loading from a nodesDir source
const widgets = utils.getThirdPartyWidgets('test/nodes/fixtures/contrib-node')
widgets.should.be.an.Object()
widgets.should.have.properties(['ui-widget-1', 'ui-widget-2'])
widgets['ui-widget-1'].should.have.properties(['component', 'name', 'package', 'path', 'src'])
widgets['ui-widget-1'].component.should.equal('ui-widget-1')
widgets['ui-widget-1'].name.should.equal('ui-widget-1')
widgets['ui-widget-1'].package.should.equal('@me/node-red-dashboard-2-two-widgets')
widgets['ui-widget-1'].src.should.equal('ui-widget-1.js')

widgets['ui-widget-2'].should.have.properties(['component', 'name', 'package', 'path', 'src'])
widgets['ui-widget-2'].component.should.equal('ui-widget-2')
widgets['ui-widget-2'].name.should.equal('ui-widget-2')
widgets['ui-widget-2'].package.should.equal('@me/node-red-dashboard-2-two-widgets')
widgets['ui-widget-2'].src.should.equal('ui-widget-2.js')
})
it('should load nodes from a package dependencies', function () {
// this covers loading from node-red src package and from userDir package
const widgets = utils.getThirdPartyWidgets('test/nodes/fixtures/contrib-nodes')
widgets.should.be.an.Object()
widgets.should.have.properties(['widget-a', 'widget-b'])
widgets['widget-a'].should.have.properties(['component', 'name', 'package', 'path', 'src'])
widgets['widget-a'].component.should.equal('ui-widget-a')
widgets['widget-a'].name.should.equal('widget-a')
widgets['widget-a'].package.should.equal('@me/node-red-dashboard-2-widget-a')
widgets['widget-a'].src.should.equal('ui-widget.js')

widgets['widget-b'].should.have.properties(['component', 'name', 'package', 'path', 'src'])
widgets['widget-b'].component.should.equal('ui-widget-b')
widgets['widget-b'].name.should.equal('widget-b')
widgets['widget-b'].package.should.equal('@me/node-red-dashboard-2-widget-b')
widgets['widget-b'].src.should.equal('ui-widget.js')
})
})
})
Loading