1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #2684 from node-red/skip-html

Avoid loading node html if disableEditor set
This commit is contained in:
Nick O'Leary 2020-09-03 15:02:09 +01:00 committed by GitHub
commit 2f2a6367c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 189 additions and 113 deletions

View File

@ -25,6 +25,11 @@ module.exports = function(grunt) {
if (flowFile) { if (flowFile) {
nodemonArgs.push(flowFile); nodemonArgs.push(flowFile);
} }
var userDir = grunt.option('userDir');
if (userDir) {
nodemonArgs.push("-u");
nodemonArgs.push(userDir);
}
var browserstack = grunt.option('browserstack'); var browserstack = grunt.option('browserstack');
if (browserstack) { if (browserstack) {

View File

@ -15,7 +15,7 @@
**/ **/
var when = require("when"); var when = require("when");
var fs = require("fs"); var fs = require("fs-extra");
var path = require("path"); var path = require("path");
var semver = require("semver"); var semver = require("semver");
@ -113,52 +113,8 @@ function loadNodeFiles(nodeFiles) {
}); });
} }
function loadNodeConfig(fileInfo) { async function loadNodeTemplate(node) {
return new Promise(function(resolve) { return fs.readFile(node.template,'utf8').then(content => {
var file = fileInfo.file;
var module = fileInfo.module;
var name = fileInfo.name;
var version = fileInfo.version;
var id = module + "/" + name;
var info = registry.getNodeInfo(id);
var isEnabled = true;
if (info) {
if (info.hasOwnProperty("loaded")) {
throw new Error(file+" already loaded");
}
isEnabled = info.enabled;
}
var node = {
id: id,
module: module,
name: name,
file: file,
template: file.replace(/\.js$/,".html"),
enabled: isEnabled,
loaded:false,
version: version,
local: fileInfo.local
};
if (fileInfo.hasOwnProperty("types")) {
node.types = fileInfo.types;
}
fs.readFile(node.template,'utf8', function(err,content) {
if (err) {
node.types = [];
if (err.code === 'ENOENT') {
if (!node.types) {
node.types = [];
}
node.err = "Error: "+node.template+" does not exist";
} else {
node.types = [];
node.err = err.toString();
}
resolve(node);
} else {
var types = []; var types = [];
var regExp = /<script (?:[^>]*)data-template-name\s*=\s*['"]([^'"]*)['"]/gi; var regExp = /<script (?:[^>]*)data-template-name\s*=\s*['"]([^'"]*)['"]/gi;
@ -202,29 +158,78 @@ function loadNodeConfig(fileInfo) {
break; break;
} }
} }
return node
}).catch(err => {
node.types = [];
if (err.code === 'ENOENT') {
if (!node.types) {
node.types = [];
}
node.err = "Error: "+node.template+" does not exist";
} else {
node.types = [];
node.err = err.toString();
}
return node;
});
}
async function loadNodeLocales(node) {
if (node.module === 'node-red') { if (node.module === 'node-red') {
// do not look up locales directory for core nodes // do not look up locales directory for core nodes
node.namespace = node.module; node.namespace = node.module;
resolve(node); return node
return;
} }
fs.stat(path.join(path.dirname(file),"locales"),function(err,stat) { return fs.stat(path.join(path.dirname(node.file),"locales")).then(stat => {
if (!err) {
node.namespace = node.id; node.namespace = node.id;
i18n.registerMessageCatalog(node.id, return i18n.registerMessageCatalog(node.id,
path.join(path.dirname(file),"locales"), path.join(path.dirname(node.file),"locales"),
path.basename(file,".js")+".json") path.basename(node.file,".js")+".json")
.then(function() { .then(() => node);
resolve(node); }).catch(err => {
});
} else {
node.namespace = node.module; node.namespace = node.module;
resolve(node); return node;
}
}); });
} }
});
}); async function loadNodeConfig(fileInfo) {
var file = fileInfo.file;
var module = fileInfo.module;
var name = fileInfo.name;
var version = fileInfo.version;
var id = module + "/" + name;
var info = registry.getNodeInfo(id);
var isEnabled = true;
if (info) {
if (info.hasOwnProperty("loaded")) {
throw new Error(file+" already loaded");
}
isEnabled = info.enabled;
}
var node = {
id: id,
module: module,
name: name,
file: file,
template: file.replace(/\.js$/,".html"),
enabled: isEnabled,
loaded:false,
version: version,
local: fileInfo.local,
types: [],
config: "",
help: {}
};
if (fileInfo.hasOwnProperty("types")) {
node.types = fileInfo.types;
}
await loadNodeLocales(node)
if (!settings.disableEditor) {
return loadNodeTemplate(node);
}
return node;
} }
/** /**

View File

@ -18,7 +18,7 @@ var should = require("should");
var when = require("when"); var when = require("when");
var sinon = require("sinon"); var sinon = require("sinon");
var path = require("path"); var path = require("path");
var fs = require("fs"); var fs = require("fs-extra");
var NR_TEST_UTILS = require("nr-test-utils"); var NR_TEST_UTILS = require("nr-test-utils");
@ -111,7 +111,73 @@ describe("red/nodes/registry/loader",function() {
module.nodes.TestNode1.types.should.have.a.length(1); module.nodes.TestNode1.types.should.have.a.length(1);
module.nodes.TestNode1.types[0].should.eql('test-node-1'); module.nodes.TestNode1.types[0].should.eql('test-node-1');
module.nodes.TestNode1.should.have.property("config"); module.nodes.TestNode1.should.have.property("config");
module.nodes.TestNode1.config.should.not.eql("");
module.nodes.TestNode1.should.have.property("help"); module.nodes.TestNode1.should.have.property("help");
module.nodes.TestNode1.help.should.have.property("en-US");
module.nodes.TestNode1.should.have.property("namespace","node-red");
nodes.registerType.calledOnce.should.be.true();
nodes.registerType.lastCall.args[0].should.eql('node-red/TestNode1');
nodes.registerType.lastCall.args[1].should.eql('test-node-1');
done();
}).catch(function(err) {
done(err);
});
});
it("load core node files scanned by lfs - ignore html if disableEditor true", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
var result = {};
result["node-red"] = {
"name": "node-red",
"version": "1.2.3",
"nodes": {
"TestNode1": {
"file": path.join(resourcesDir,"TestNode1","TestNode1.js"),
"module": "node-red",
"name": "TestNode1"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{disableEditor: true, available:function(){return true;}}});
loader.load().then(function(result) {
registry.addModule.called.should.be.true();
var module = registry.addModule.lastCall.args[0];
module.should.have.property("name","node-red");
module.should.have.property("version","1.2.3");
module.should.have.property("nodes");
module.nodes.should.have.property("TestNode1");
module.nodes.TestNode1.should.have.property("id","node-red/TestNode1");
module.nodes.TestNode1.should.have.property("module","node-red");
module.nodes.TestNode1.should.have.property("name","TestNode1");
module.nodes.TestNode1.should.have.property("file");
module.nodes.TestNode1.should.have.property("template");
module.nodes.TestNode1.should.have.property("enabled",true);
module.nodes.TestNode1.should.have.property("loaded",true);
// With disableEditor true, the types property is not populated by the
// html file - but instead is populated as nodes register themselves.
// But for this test, we have stubbed out registerType, so we won't get any types
// module.nodes.TestNode1.should.have.property("types");
// module.nodes.TestNode1.types.should.have.a.length(1);
// module.nodes.TestNode1.types[0].should.eql('test-node-1');
// With disableEditor set, config should be blank
module.nodes.TestNode1.should.have.property("config");
module.nodes.TestNode1.config.should.eql("");
// help should be an empty object
module.nodes.TestNode1.should.have.property("help");
module.nodes.TestNode1.help.should.eql({})
module.nodes.TestNode1.should.have.property("namespace","node-red"); module.nodes.TestNode1.should.have.property("namespace","node-red");
nodes.registerType.calledOnce.should.be.true(); nodes.registerType.calledOnce.should.be.true();
@ -336,9 +402,9 @@ describe("red/nodes/registry/loader",function() {
module.nodes.DoesNotExist.should.have.property("loaded",false); module.nodes.DoesNotExist.should.have.property("loaded",false);
module.nodes.DoesNotExist.should.have.property("types"); module.nodes.DoesNotExist.should.have.property("types");
module.nodes.DoesNotExist.types.should.have.a.length(0); module.nodes.DoesNotExist.types.should.have.a.length(0);
module.nodes.DoesNotExist.should.not.have.property("config"); module.nodes.DoesNotExist.should.have.property("config","");
module.nodes.DoesNotExist.should.not.have.property("help"); module.nodes.DoesNotExist.should.have.property("help",{});
module.nodes.DoesNotExist.should.not.have.property("namespace","node-red"); module.nodes.DoesNotExist.should.have.property("namespace","node-red");
module.nodes.DoesNotExist.should.have.property('err'); module.nodes.DoesNotExist.should.have.property('err');
nodes.registerType.called.should.be.false(); nodes.registerType.called.should.be.false();
@ -390,9 +456,9 @@ describe("red/nodes/registry/loader",function() {
module.nodes.DuffNode.should.have.property("loaded",false); module.nodes.DuffNode.should.have.property("loaded",false);
module.nodes.DuffNode.should.have.property("types"); module.nodes.DuffNode.should.have.property("types");
module.nodes.DuffNode.types.should.have.a.length(0); module.nodes.DuffNode.types.should.have.a.length(0);
module.nodes.DuffNode.should.not.have.property("config"); module.nodes.DuffNode.should.have.property("config","");
module.nodes.DuffNode.should.not.have.property("help"); module.nodes.DuffNode.should.have.property("help",{});
module.nodes.DuffNode.should.not.have.property("namespace","node-red"); module.nodes.DuffNode.should.have.property("namespace","node-red");
module.nodes.DuffNode.should.have.property('err'); module.nodes.DuffNode.should.have.property('err');
module.nodes.DuffNode.err.should.endWith("DuffNode.html does not exist"); module.nodes.DuffNode.err.should.endWith("DuffNode.html does not exist");