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

Use absolute flow file path in project settings

This ensures the diff logic can recognise the project flow file
and apply merge resolution to paths that git knows
This commit is contained in:
Nick O'Leary 2019-02-17 22:18:40 +00:00
parent e05ff01d57
commit 9d673a213e
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 47 additions and 38 deletions

View File

@ -766,8 +766,7 @@ RED.projects.settings = (function() {
row = $('<div class="user-settings-row"></div>').appendTo(filesContainer); row = $('<div class="user-settings-row"></div>').appendTo(filesContainer);
$('<label for=""></label>').text(RED._("sidebar.project.projectSettings.package")).appendTo(row); $('<label for=""></label>').text(RED._("sidebar.project.projectSettings.package")).appendTo(row);
var packageFileLabel = $('<div class="uneditable-input" style="padding:0">').appendTo(row); var packageFileLabel = $('<div class="uneditable-input" style="padding:0">').appendTo(row);
var packageFileLabelPrefixText = $('<span style="display:inline-block; padding: 6px 0 6px 6px">').text("/").appendTo(packageFileLabel); var packageFileLabelText = $('<span style="display:inline-block; padding: 6px">').text(activeProject.files.package||"package.json").appendTo(packageFileLabel);
var packageFileLabelText = $('<span style="display:inline-block; padding: 6px 6px 6px 0">').text(activeProject.files.package||"package.json").appendTo(packageFileLabel);
var packageFileInput = $('<input type="hidden">').val(activeProject.files.package||"package.json").appendTo(packageFileLabel); var packageFileInput = $('<input type="hidden">').val(activeProject.files.package||"package.json").appendTo(packageFileLabel);
var packageFileInputSearch = $('<button type="button" class="editor-button toggle single" style="border-top-right-radius: 4px; border-bottom-right-radius: 4px; width: 36px; height: 34px; position: absolute; top: -1px; right: -1px;"><i class="fa fa-folder-open-o"></i></button>') var packageFileInputSearch = $('<button type="button" class="editor-button toggle single" style="border-top-right-radius: 4px; border-bottom-right-radius: 4px; width: 36px; height: 34px; position: absolute; top: -1px; right: -1px;"><i class="fa fa-folder-open-o"></i></button>')
@ -791,8 +790,8 @@ RED.projects.settings = (function() {
packageFileInput.val(result); packageFileInput.val(result);
packageFileLabelText.text(result); packageFileLabelText.text(result);
var rootDir = result.substring(0,result.length - 12); var rootDir = result.substring(0,result.length - 12);
flowFileLabelPrefixText.text("/"+rootDir); flowFileLabelPrefixText.text(rootDir);
credFileLabelPrefixText.text("/"+rootDir); credFileLabelPrefixText.text(rootDir);
flowFileInputResize(); flowFileInputResize();
packageFileSubLabel.hide(); packageFileSubLabel.hide();
} }
@ -816,7 +815,7 @@ RED.projects.settings = (function() {
} }
var projectPackage = "/"+(activeProject.files.package||"package.json"); var projectPackage = activeProject.files.package||"package.json";
var projectRoot = projectPackage.substring(0,projectPackage.length - 12); var projectRoot = projectPackage.substring(0,projectPackage.length - 12);
// Flow files // Flow files
@ -824,13 +823,21 @@ RED.projects.settings = (function() {
$('<label for=""></label>').text(RED._("sidebar.project.projectSettings.flow")).appendTo(row); $('<label for=""></label>').text(RED._("sidebar.project.projectSettings.flow")).appendTo(row);
var flowFileLabel = $('<div class="uneditable-input" style="padding:0">').appendTo(row); var flowFileLabel = $('<div class="uneditable-input" style="padding:0">').appendTo(row);
var flowFileLabelPrefixText = $('<span style="display:inline-block; padding: 6px 0 6px 6px">').text(projectRoot).appendTo(flowFileLabel); var flowFileLabelPrefixText = $('<span style="display:inline-block; padding: 6px 0 6px 6px">').text(projectRoot).appendTo(flowFileLabel);
var flowFileLabelText = $('<span style="display:inline-block; padding: 6px 6px 6px 0">').text(activeProject.files.flow||"flows.json").appendTo(flowFileLabel); var flowFileName = "flows.json";
if (activeProject.files.flow) {
if (activeProject.files.flow.indexOf(projectRoot) === 0) {
flowFileName = activeProject.files.flow.substring(projectRoot.length);
} else {
flowFileName = activeProject.files.flow;
}
}
var flowFileLabelText = $('<span style="display:inline-block; padding: 6px 6px 6px 0">').text(flowFileName).appendTo(flowFileLabel);
var flowFileInputResize = function() { var flowFileInputResize = function() {
flowFileInput.css({ flowFileInput.css({
"width": "calc(100% - "+(flowFileInputSearch.width() + flowFileLabelPrefixText.width())+"px)" "width": "calc(100% - "+(flowFileInputSearch.width() + flowFileLabelPrefixText.width())+"px)"
}); });
} }
var flowFileInput = $('<input type="text" style="padding-left:1px; margin-top: -2px; margin-bottom: 0;border: none;">').val(activeProject.files.flow||"flows.json").hide().appendTo(flowFileLabel); var flowFileInput = $('<input type="text" style="padding-left:1px; margin-top: -2px; margin-bottom: 0;border: none;">').val(flowFileName).hide().appendTo(flowFileLabel);
var flowFileInputSearch = $('<button type="button" class="editor-button toggle single" style="border-top-right-radius: 4px; border-bottom-right-radius: 4px; width: 36px; height: 34px; position: absolute; top: -1px; right: -1px;"><i class="fa fa-folder-open-o"></i></button>') var flowFileInputSearch = $('<button type="button" class="editor-button toggle single" style="border-top-right-radius: 4px; border-bottom-right-radius: 4px; width: 36px; height: 34px; position: absolute; top: -1px; right: -1px;"><i class="fa fa-folder-open-o"></i></button>')
.hide() .hide()
.appendTo(flowFileLabel) .appendTo(flowFileLabel)
@ -875,11 +882,20 @@ RED.projects.settings = (function() {
row = $('<div class="user-settings-row"></div>').appendTo(filesContainer); row = $('<div class="user-settings-row"></div>').appendTo(filesContainer);
$('<label for=""></label>').text(RED._("sidebar.project.projectSettings.credentials")).appendTo(row); $('<label for=""></label>').text(RED._("sidebar.project.projectSettings.credentials")).appendTo(row);
var credFileName = "flows_cred.json";
if (activeProject.files.credentials) {
if (activeProject.files.flow.indexOf(projectRoot) === 0) {
credFileName = activeProject.files.credentials.substring(projectRoot.length);
} else {
credFileName = activeProject.files.credentials;
}
}
var credFileLabel = $('<div class="uneditable-input" style="padding:0">').appendTo(row); var credFileLabel = $('<div class="uneditable-input" style="padding:0">').appendTo(row);
var credFileLabelPrefixText = $('<span style="display:inline-block;padding: 6px 0 6px 6px">').text(projectRoot).appendTo(credFileLabel); var credFileLabelPrefixText = $('<span style="display:inline-block;padding: 6px 0 6px 6px">').text(projectRoot).appendTo(credFileLabel);
var credFileLabelText = $('<span style="display:inline-block; padding: 6px 6px 6px 0">').text(activeProject.files.credentials||"flows_cred.json").appendTo(credFileLabel); var credFileLabelText = $('<span style="display:inline-block; padding: 6px 6px 6px 0">').text(credFileName).appendTo(credFileLabel);
var credFileInput = $('<input type="hidden">').val(activeProject.files.credentials||"flows_cred.json").insertAfter(credFileLabel); var credFileInput = $('<input type="hidden">').val(credFileName).insertAfter(credFileLabel);
var checkFiles = function() { var checkFiles = function() {
var saveDisabled; var saveDisabled;
@ -1053,7 +1069,7 @@ RED.projects.settings = (function() {
.appendTo(formButtons) .appendTo(formButtons)
.click(function(evt) { .click(function(evt) {
evt.preventDefault(); evt.preventDefault();
var projectPackage = "/"+(activeProject.files.package||"package.json"); var projectPackage = activeProject.files.package||"package.json";
var projectRoot = projectPackage.substring(0,projectPackage.length - 12); var projectRoot = projectPackage.substring(0,projectPackage.length - 12);
flowFileLabelPrefixText.text(projectRoot); flowFileLabelPrefixText.text(projectRoot);
credFileLabelPrefixText.text(projectRoot); credFileLabelPrefixText.text(projectRoot);
@ -1065,7 +1081,7 @@ RED.projects.settings = (function() {
packageFileSubLabel.hide(); packageFileSubLabel.hide();
} }
flowFileInput.val(flowFileLabelText.text()); flowFileInput.val(flowFileLabelText.text());
credFileLabelText.text(activeProject.files.credentials||"flows_cred.json"); credFileLabelText.text(credFileName);
hideEditForm(); hideEditForm();
}); });
var saveButton = $('<button type="button" class="editor-button">' + RED._("common.label.save") + '</button>') var saveButton = $('<button type="button" class="editor-button">' + RED._("common.label.save") + '</button>')
@ -1084,11 +1100,13 @@ RED.projects.settings = (function() {
packageFileSubLabel.hide(); packageFileSubLabel.hide();
hideEditForm(); hideEditForm();
} }
var rootPath = packageFileInput.val();
rootPath = rootPath.substring(0,rootPath.length-12);
var payload = { var payload = {
files: { files: {
flow: flowFileInput.val(), package: packageFileInput.val(),
credentials: credFileInput.val(), flow: rootPath+flowFileInput.val(),
package: packageFileInput.val() credentials: rootPath+credFileInput.val()
} }
} }
@ -1101,9 +1119,6 @@ RED.projects.settings = (function() {
payload.currentCredentialSecret = credentialSecretExistingInput.val(); payload.currentCredentialSecret = credentialSecretExistingInput.val();
} }
} }
console.log(payload);
return;
RED.deploy.setDeployInflight(true); RED.deploy.setDeployInflight(true);
utils.sendRequest({ utils.sendRequest({
url: "projects/"+activeProject.name, url: "projects/"+activeProject.name,

View File

@ -84,14 +84,15 @@ Project.prototype.load = function () {
return checkProjectFiles(project).then(function(missingFiles) { return checkProjectFiles(project).then(function(missingFiles) {
project.missingFiles = missingFiles; project.missingFiles = missingFiles;
if (missingFiles.indexOf('package.json') === -1) { if (missingFiles.indexOf('package.json') === -1) {
// We have a package.json in project.path+project.paths.root+"package.json"
project.paths['package.json'] = fspath.join(project.paths.root,"package.json"); project.paths['package.json'] = fspath.join(project.paths.root,"package.json");
promises.push(fs.readFile(fspath.join(project.path,project.paths['package.json']),"utf8").then(function(content) { promises.push(fs.readFile(fspath.join(project.path,project.paths['package.json']),"utf8").then(function(content) {
try { try {
project.package = util.parseJSON(content); project.package = util.parseJSON(content);
if (project.package.hasOwnProperty('node-red')) { if (project.package.hasOwnProperty('node-red')) {
if (project.package['node-red'].hasOwnProperty('settings')) { if (project.package['node-red'].hasOwnProperty('settings')) {
project.paths.flowFile = project.package['node-red'].settings.flowFile; project.paths.flowFile = fspath.join(project.paths.root,project.package['node-red'].settings.flowFile);
project.paths.credentialsFile = project.package['node-red'].settings.credentialsFile; project.paths.credentialsFile = fspath.join(project.paths.root,project.package['node-red'].settings.credentialsFile);
} }
} else { } else {
// TODO: package.json doesn't have a node-red section // TODO: package.json doesn't have a node-red section
@ -134,10 +135,6 @@ Project.prototype.load = function () {
}); });
}; };
Project.prototype.getFilePath = function(file) {
return fspath.join(this.path,this.paths.root,this.paths[file])
}
Project.prototype.initialise = function(user,data) { Project.prototype.initialise = function(user,data) {
var project = this; var project = this;
// if (!this.empty) { // if (!this.empty) {
@ -296,40 +293,39 @@ Project.prototype.update = function (user, data) {
if (data.hasOwnProperty('files')) { if (data.hasOwnProperty('files')) {
this.package['node-red'] = this.package['node-red'] || { settings: {}}; this.package['node-red'] = this.package['node-red'] || { settings: {}};
if (data.files.hasOwnProperty('package') && data.files.package !== fspath.join(this.paths.root,"package.json")) { if (data.files.hasOwnProperty('package') && data.files.package !== fspath.join(this.paths.root,"package.json")) {
// We have a package file. It could be one that doesn't exist yet, or // We have a package file. It could be one that doesn't exist yet,
// does exist and we need to load it. // or it does exist and we need to load it.
if (!/package\.json$/.test(data.files.package)) { if (!/package\.json$/.test(data.files.package)) {
return Promise.reject("Invalid package file: "+data.files.package) return Promise.reject("Invalid package file: "+data.files.package)
} }
var root = data.files.package.substring(0,data.files.package.length-12); var root = data.files.package.substring(0,data.files.package.length-12);
this.paths.root = root; this.paths.root = root;
this.paths['package.json'] = 'package.json'; this.paths['package.json'] = data.files.package;
globalProjectSettings.projects[this.name].rootPath = root; globalProjectSettings.projects[this.name].rootPath = root;
saveSettings = true; saveSettings = true;
// 1. check if it exists // 1. check if it exists
if (fs.existsSync(fspath.join(this.path,this.paths.root,this.paths['package.json']))) { if (fs.existsSync(fspath.join(this.path,this.paths['package.json']))) {
// Load the existing one.... // Load the existing one....
} else { } else {
var newPackage = defaultFileSet["package.json"](this); var newPackage = defaultFileSet["package.json"](this);
fs.writeFileSync(fspath.join(this.path,this.paths.root,this.paths['package.json']),newPackage); fs.writeFileSync(fspath.join(this.path,this.paths['package.json']),newPackage);
this.package = JSON.parse(newPackage); this.package = JSON.parse(newPackage);
} }
reloadProject = true; reloadProject = true;
flowFilesChanged = true; flowFilesChanged = true;
} }
if (data.files.hasOwnProperty('flow') && this.package['node-red'].settings.flowFile !== data.files.flow) { if (data.files.hasOwnProperty('flow') && this.package['node-red'].settings.flowFile !== data.files.flow.substring(this.paths.root.length)) {
this.paths.flowFile = data.files.flow; this.paths.flowFile = data.files.flow;
this.package['node-red'].settings.flowFile = data.files.flow; this.package['node-red'].settings.flowFile = data.files.flow.substring(this.paths.root.length);
savePackage = true; savePackage = true;
flowFilesChanged = true; flowFilesChanged = true;
} }
if (data.files.hasOwnProperty('credentials') && this.package['node-red'].settings.credentialsFile !== data.files.credentials) { if (data.files.hasOwnProperty('credentials') && this.package['node-red'].settings.credentialsFile !== data.files.credentials.substring(this.paths.root.length)) {
this.paths.credentialsFile = data.files.credentials; this.paths.credentialsFile = data.files.credentials;
this.package['node-red'].settings.credentialsFile = data.files.credentials; this.package['node-red'].settings.credentialsFile = data.files.credentials.substring(this.paths.root.length);
// Don't know if the credSecret is invalid or not so clear the flag // Don't know if the credSecret is invalid or not so clear the flag
delete this.credentialSecretInvalid; delete this.credentialSecretInvalid;
savePackage = true; savePackage = true;
flowFilesChanged = true; flowFilesChanged = true;
} }
@ -779,7 +775,7 @@ Project.prototype.removeRemote = function(user, remote) {
Project.prototype.getFlowFile = function() { Project.prototype.getFlowFile = function() {
// console.log("Project.getFlowFile = ",this.paths.flowFile); // console.log("Project.getFlowFile = ",this.paths.flowFile);
if (this.paths.flowFile) { if (this.paths.flowFile) {
return fspath.join(this.path,this.paths.root,this.paths.flowFile); return fspath.join(this.path,this.paths.flowFile);
} else { } else {
return null; return null;
} }
@ -796,7 +792,7 @@ Project.prototype.getFlowFileBackup = function() {
Project.prototype.getCredentialsFile = function() { Project.prototype.getCredentialsFile = function() {
// console.log("Project.getCredentialsFile = ",this.paths.credentialsFile); // console.log("Project.getCredentialsFile = ",this.paths.credentialsFile);
if (this.paths.credentialsFile) { if (this.paths.credentialsFile) {
return fspath.join(this.path,this.paths.root,this.paths.credentialsFile); return fspath.join(this.path,this.paths.credentialsFile);
} else { } else {
return this.paths.credentialsFile; return this.paths.credentialsFile;
} }
@ -920,14 +916,12 @@ function createDefaultProject(user, project) {
}); });
} }
function checkProjectFiles(project) { function checkProjectFiles(project) {
var projectPath = project.path;
var projectRoot = project.paths.root;
var promises = []; var promises = [];
var paths = []; var paths = [];
for (var file in defaultFileSet) { for (var file in defaultFileSet) {
if (defaultFileSet.hasOwnProperty(file)) { if (defaultFileSet.hasOwnProperty(file)) {
paths.push(file); paths.push(file);
promises.push(fs.stat(fspath.join(projectPath,projectRoot,file))); promises.push(fs.stat(fspath.join(project.path,project.paths.root,file)));
} }
} }
return when.settle(promises).then(function(results) { return when.settle(promises).then(function(results) {