Skip to content

Commit

Permalink
### __WORK IN PROGRESS__
Browse files Browse the repository at this point in the history
* (withstu) Fixed node-red modules installation
  • Loading branch information
GermanBluefox committed Jan 20, 2021
1 parent 782ca58 commit 744f401
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ This adapter uses the node-red server from https://github.com/node-red/node-red
-->

## Changelog
### __WORK IN PROGRESS__
* (withstu) Fixed node-red modules installation

### 2.0.3 (2020-12-27)
* (Apollon77) make sure empty state values do not crash list node
Expand Down Expand Up @@ -255,7 +257,7 @@ This adapter uses the node-red server from https://github.com/node-red/node-red

## License

Copyright 2014-2020 bluefox <[email protected]>.
Copyright 2014-2021 bluefox <[email protected]>.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
35 changes: 17 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* ioBroker node-red Adapter
*
* (c) 2014-2020 bluefox<[email protected]>
* (c) 2014-2021 bluefox<[email protected]>
*
* Apache 2.0 License
*
Expand Down Expand Up @@ -48,7 +48,7 @@ function installNpm(npmLib, callback) {
npmLib = undefined;
}

const cmd = 'npm install ' + npmLib + ' --production --prefix "' + userDataDir + '" --save';
const cmd = `npm install ${npmLib} --production --prefix "${userDataDir}" --save`;
adapter.log.info(cmd + ' (System call)');
// Install node modules as system call

Expand All @@ -60,15 +60,15 @@ function installNpm(npmLib, callback) {
child.stderr.on('data', buf => adapter.log.error(buf.toString('utf8')));

child.on('exit', (code, signal) => {
code && adapter.log.error('Cannot install ' + npmLib + ': ' + code);
code && adapter.log.error(`Cannot install ${npmLib}: ${code}`);
// command succeeded
callback && callback(npmLib);
});
}

function installLibraries(callback) {
let allInstalled = true;

if (typeof adapter.common.npmLibs === 'string') {
adapter.common.npmLibs = adapter.common.npmLibs.split(/[,;\s]+/);
}
Expand All @@ -77,8 +77,7 @@ function installLibraries(callback) {

// normally /opt/iobroker/node_modules/iobroker.js-controller
// but can be /example/ioBroker.js-controller
const controllerDir = utils.controllerDir;
const parts = controllerDir.split('/');
const parts = utils.controllerDir.split('/');
if (parts.length > 1 && parts[parts.length - 2] === 'node_modules') {
parts.splice(parts.length - 2, 2);
userDataDir = parts.join('/');
Expand All @@ -90,7 +89,7 @@ function installLibraries(callback) {
for (let lib = 0; lib < adapter.common.npmLibs.length; lib++) {
if (adapter.common.npmLibs[lib] && adapter.common.npmLibs[lib].trim()) {
adapter.common.npmLibs[lib] = adapter.common.npmLibs[lib].trim();
if (!fs.existsSync(userDataDir + '/node_modules/' + adapter.common.npmLibs[lib] + '/package.json')) {
if (!fs.existsSync(`${userDataDir}/node_modules/${adapter.common.npmLibs[lib]}/package.json`)) {

if (!attempts[adapter.common.npmLibs[lib]]) {
attempts[adapter.common.npmLibs[lib]] = 1;
Expand Down Expand Up @@ -121,7 +120,7 @@ function installLibraries(callback) {
// is called if a subscribed state changes
//adapter.on('stateChange', function (id, state) {
//});
function unloadRed (callback) {
function unloadRed(callback) {
// Stop node-red
stopping = true;
if (redProcess) {
Expand All @@ -138,7 +137,7 @@ function unloadRed (callback) {
function processMessage(obj) {
switch (obj.command) {
case 'update':
writeStateList(error =>
writeStateList(error =>
obj.callback && adapter.sendTo(obj.from, obj.command, error, obj.callback));
break;

Expand Down Expand Up @@ -253,7 +252,7 @@ function startNodeRed() {
}

function setOption(line, option, value) {
const toFind = "'%%" + option + "%%'";
const toFind = `'%%${option}%%'`;
const pos = line.indexOf(toFind);
if (pos !== -1) {
return line.substring(0, pos) + ((value !== undefined) ? value : (adapter.config[option] === null || adapter.config[option] === undefined) ? '' : adapter.config[option]) + line.substring(pos + toFind.length);
Expand All @@ -270,11 +269,11 @@ function writeSettings() {
const nodesDir = '"' + __dirname.replace(/\\/g, '/') + '/nodes/"';

const bind = '"' + (adapter.config.bind || '0.0.0.0') + '"';
const auth = adapter.config.user && adapter.config.pass ?
JSON.stringify({type: 'credentials', users: [{username: adapter.config.user, password: adapter.config.pass, permissions: '*'}]}) :

const auth = adapter.config.user && adapter.config.pass ?
JSON.stringify({type: 'credentials', users: [{username: adapter.config.user, password: adapter.config.pass, permissions: '*'}]}) :
JSON.stringify({type: 'credentials', users: [], default: {permissions: '*'}});

const pass = '"' + adapter.config.pass + '"';
const secure = adapter.config.secure ? '' : '// ';
const certFile = adapter.config.certPublic ? userDataDir + adapter.config.certPublic + '.crt' : '';
Expand All @@ -284,7 +283,7 @@ function writeSettings() {
if (additional[a].startsWith('node-red-')) {
continue;
}
npms += ' "' + additional[a] + '": require("' + dir + additional[a] + '")';
npms += ` "${additional[a]}": require("${dir}${additional[a]}")`;
if (a !== additional.length - 1) {
npms += ', \r\n';
}
Expand Down Expand Up @@ -312,7 +311,7 @@ function writeSettings() {
fs.writeFileSync(keyFile, certificates.key);
});
}

for (let i = 0; i < lines.length; i++) {
lines[i] = setOption(lines[i], 'port');
lines[i] = setOption(lines[i], 'auth', auth);
Expand Down Expand Up @@ -368,7 +367,7 @@ function saveObjects() {
cred = JSON.parse(fs.readFileSync(userDataDir + 'flows_cred.json'));
}
} catch(e) {
adapter.log.error('Cannot save ' + userDataDir + 'flows_cred.json');
adapter.log.error(`Cannot save ${userDataDir}flows_cred.json`);
}
try {
if (fs.existsSync(userDataDir + 'flows.json')) {
Expand All @@ -389,7 +388,7 @@ function saveObjects() {
},
type: 'config'
},
() => adapter.log.info('Save ' + userDataDir + 'flows.json')
() => adapter.log.info(`Save ${userDataDir}flows.json`)
);
}

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
"mongodb": "^3.6.3"
},
"dependencies": {
"node-red": "^1.2.6",
"node-red-contrib-os": "^0.1.7",
"node-red-dashboard": "^2.26.1",
"node-red": "^1.2.7",
"node-red-contrib-os": "^0.2.0",
"node-red-dashboard": "^2.27.0",
"node-red-contrib-aggregator": "^1.5.0",
"node-red-contrib-polymer": "^0.0.22",
"node-red-node-email": "^1.8.2",
"node-red-node-email": "^1.8.3",
"node-red-node-feedparser": "^0.1.16",
"node-red-node-sentiment": "^0.1.6",
"node-red-node-twitter": "^1.1.7",
Expand Down

0 comments on commit 744f401

Please sign in to comment.