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 1 commit
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
}