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

Add custom project.toJSON to simplify state mangement

This commit is contained in:
Nick O'Leary 2017-10-17 10:14:50 +01:00
parent 4569cb432d
commit 5218a3fbac
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 70 additions and 46 deletions

View File

@ -188,18 +188,15 @@ RED.projects = (function() {
} }
} }
RED.deploy.setDeployInflight(true);
RED.projects.settings.switchProject(projectData.name);
sendRequest({ sendRequest({
url: "projects", url: "projects",
type: "POST", type: "POST",
responses: { responses: {
200: function(data) { 200: function(data) {
switchProject(projectData.name,function(err,data) { dialog.dialog( "close" );
if (err) {
console.log("unexpected_error",error)
} else {
dialog.dialog( "close" );
}
})
}, },
400: { 400: {
'project_exists': function(error) { 'project_exists': function(error) {
@ -217,7 +214,9 @@ RED.projects = (function() {
} }
} }
} }
},projectData) },projectData).always(function() {
RED.deploy.setDeployInflight(false);
})

View File

@ -49,10 +49,8 @@ module.exports = {
app.post("/", function(req,res) { app.post("/", function(req,res) {
// Create project // Create project
runtime.storage.projects.createProject(req.body).then(function(name) { runtime.storage.projects.createProject(req.body).then(function(data) {
runtime.storage.projects.getProject(name).then(function(data) { res.json(data);
res.json(data);
});
}).catch(function(err) { }).catch(function(err) {
console.log(err.stack); console.log(err.stack);
if (err.code) { if (err.code) {

View File

@ -189,7 +189,7 @@ var api = module.exports = {
if (projectKey) { if (projectKey) {
// This is a project with a bad key. Mark it as invalid // This is a project with a bad key. Mark it as invalid
// TODO: this delves too deep into Project structure // TODO: this delves too deep into Project structure
activeProject.info.settings.credentialSecretInvalid = true; activeProject.credentialSecretInvalid = true;
return when.reject(error); return when.reject(error);
} }
return when.reject(error); return when.reject(error);
@ -198,7 +198,8 @@ var api = module.exports = {
credentialCache = credentials; credentialCache = credentials;
} }
if (clearInvalidFlag) { if (clearInvalidFlag) {
delete activeProject.info.settings.credentialSecretInvalid; // TODO: this delves too deep into Project structure
delete activeProject.credentialSecretInvalid;
} }
}); });
}, },

View File

@ -33,10 +33,9 @@ function Project(name) {
this.name = name; this.name = name;
this.path = fspath.join(projectsDir,name); this.path = fspath.join(projectsDir,name);
this.paths = {}; this.paths = {};
this.info = {
name: name, this.missingFiles = [];
settings: {}
};
this.credentialSecret = null; this.credentialSecret = null;
} }
Project.prototype.load = function () { Project.prototype.load = function () {
@ -49,32 +48,28 @@ Project.prototype.load = function () {
} }
this.credentialSecret = projectSettings.credentialSecret; this.credentialSecret = projectSettings.credentialSecret;
// console.log(this.name,"credSec",this.credentialSecret )
if (typeof projectSettings.credentialSecret === "string") { this.paths.flowFile = fspath.join(this.path,"flow.json");
this.info.settings.credentialsEncrypted = true; this.paths.credentialsFile = fspath.join(this.path,"flow_cred.json");
} else {
this.info.settings.credentialsEncrypted = false;
}
var promises = []; var promises = [];
return checkProjectFiles(project.name).then(function(missingFiles) { return checkProjectFiles(project.name).then(function(missingFiles) {
if (missingFiles.length > 0) { if (missingFiles.length > 0) {
project.info.missingFiles = missingFiles; project.missingFiles = missingFiles;
} }
if (missingFiles.indexOf('package.json') === -1) { if (missingFiles.indexOf('package.json') === -1) {
project.paths['package.json'] = fspath.join(project.path,"package.json"); project.paths['package.json'] = fspath.join(project.path,"package.json");
promises.push(fs.readFile(project.paths['package.json'],"utf8").then(function(content) { promises.push(fs.readFile(project.paths['package.json'],"utf8").then(function(content) {
project.package = util.parseJSON(content); project.package = util.parseJSON(content);
project.info.summary = project.package.description||"";
project.info.dependencies = project.package.dependencies||{};
})); }));
} }
if (missingFiles.indexOf('README.md') === -1) { if (missingFiles.indexOf('README.md') === -1) {
project.paths['README.md'] = fspath.join(project.path,"README.md"); project.paths['README.md'] = fspath.join(project.path,"README.md");
promises.push(fs.readFile(project.paths['README.md'],"utf8").then(function(content) { promises.push(fs.readFile(project.paths['README.md'],"utf8").then(function(content) {
project.info.description = content; project.description = content;
})); }));
} else { } else {
project.info.description = ""; project.description = "";
} }
return when.settle(promises).then(function() { return when.settle(promises).then(function() {
@ -101,7 +96,7 @@ Project.prototype.update = function (data) {
if (!isReset && // not a reset if (!isReset && // not a reset
this.credentialSecret && // key already set this.credentialSecret && // key already set
!this.info.settings.credentialSecretInvalid && // key not invalid !this.credentialSecretInvalid && // key not invalid
this.credentialSecret !== existingSecret) { // key doesn't match provided existing key this.credentialSecret !== existingSecret) { // key doesn't match provided existing key
var e = new Error("Cannot change credentialSecret without current key"); var e = new Error("Cannot change credentialSecret without current key");
e.code = "missing_current_credential_key"; e.code = "missing_current_credential_key";
@ -112,7 +107,7 @@ Project.prototype.update = function (data) {
var globalProjectSettings = settings.get("projects"); var globalProjectSettings = settings.get("projects");
globalProjectSettings.projects[this.name] = globalProjectSettings.projects[this.name]||{} globalProjectSettings.projects[this.name] = globalProjectSettings.projects[this.name]||{}
globalProjectSettings.projects[this.name].credentialSecret = project.credentialSecret; globalProjectSettings.projects[this.name].credentialSecret = project.credentialSecret;
delete this.info.settings.credentialSecretInvalid; delete this.credentialSecretInvalid;
return settings.set("projects",globalProjectSettings); return settings.set("projects",globalProjectSettings);
} }
@ -121,21 +116,19 @@ Project.prototype.update = function (data) {
filesToUpdate[this.paths['README.md']] = function() { filesToUpdate[this.paths['README.md']] = function() {
return data.description; return data.description;
}; };
this.info.description = data.description; this.description = data.description;
} }
if (data.hasOwnProperty('dependencies')) { if (data.hasOwnProperty('dependencies')) {
filesToUpdate[this.paths['package.json']] = function() { filesToUpdate[this.paths['package.json']] = function() {
return JSON.stringify(project.package,"",4) return JSON.stringify(project.package,"",4)
}; };
this.package.dependencies = data.dependencies; this.package.dependencies = data.dependencies;
this.info.dependencies = data.dependencies;
} }
if (data.hasOwnProperty('summary')) { if (data.hasOwnProperty('summary')) {
filesToUpdate[this.paths['package.json']] = function() { filesToUpdate[this.paths['package.json']] = function() {
return JSON.stringify(project.package,"",4) return JSON.stringify(project.package,"",4)
}; };
this.package.description = data.summary; this.package.description = data.summary;
this.info.summary = data.summary;
} }
var files = Object.keys(filesToUpdate); var files = Object.keys(filesToUpdate);
@ -168,18 +161,37 @@ Project.prototype.getCommit = function(sha) {
} }
Project.prototype.getFlowFile = function() { Project.prototype.getFlowFile = function() {
return fspath.join(this.path,"flow.json"); return this.paths.flowFile;
} }
Project.prototype.getFlowFileBackup = function() { Project.prototype.getFlowFileBackup = function() {
return getBackupFilename(this.getFlowFile()); return getBackupFilename(this.getFlowFile());
} }
Project.prototype.getCredentialsFile = function() { Project.prototype.getCredentialsFile = function() {
return fspath.join(this.path,"flow_cred.json"); return this.paths.credentialsFile;
} }
Project.prototype.getCredentialsFileBackup = function() { Project.prototype.getCredentialsFileBackup = function() {
return getBackupFilename(this.getCredentialsFile()); return getBackupFilename(this.getCredentialsFile());
} }
Project.prototype.toJSON = function () {
return {
name: this.name,
summary: this.package.description,
description: this.description,
dependencies: this.package.dependencies,
settings: {
credentialsEncrypted: (typeof this.credentialSecret === "string"),
credentialSecretInvalid: this.credentialSecretInvalid
},
files: {
flowFile: this.paths.flowFile&&this.paths.flowFile.substring(this.path.length),
credentialsFile: this.paths.credentialsFile&&this.paths.credentialsFile.substring(this.path.length)
}
}
};
function getBackupFilename(filename) { function getBackupFilename(filename) {
var ffName = fspath.basename(filename); var ffName = fspath.basename(filename);
var ffDir = fspath.dirname(filename); var ffDir = fspath.dirname(filename);
@ -212,7 +224,11 @@ function createDefaultProject(project) {
promises.push(util.writeFile(fspath.join(projectPath,file),defaultFileSet[file](project))); promises.push(util.writeFile(fspath.join(projectPath,file),defaultFileSet[file](project)));
} }
} }
return when.all(promises); return when.all(promises).then(function() {
return gitTools.stageFile(projectPath,Object.keys(defaultFileSet));
}).then(function() {
return gitTools.commit(projectPath,"Create project");
})
}); });
} }

View File

@ -113,35 +113,43 @@ function loadProject(name) {
} }
function getProject(name) { function getProject(name) {
if (!activeProject || activeProject.name !== name) { checkActiveProject(name);
//TODO: throw better err
throw new Error("Cannot get inactive project");
}
//return when.resolve(activeProject.info); //return when.resolve(activeProject.info);
return Projects.get(name).then(function(p) { return Projects.get(name);
return p.info
})
} }
function checkActiveProject(project) {
if (!activeProject || activeProject.name !== project) {
//TODO: throw better err
throw new Error("Cannot operate on inactive project");
}
}
function getFiles(project) { function getFiles(project) {
checkActiveProject(project);
return activeProject.getFiles(); return activeProject.getFiles();
} }
function stageFile(project,file) { function stageFile(project,file) {
checkActiveProject(project);
return activeProject.stageFile(file); return activeProject.stageFile(file);
} }
function unstageFile(project,file) { function unstageFile(project,file) {
checkActiveProject(project);
return activeProject.unstageFile(file); return activeProject.unstageFile(file);
} }
function commit(project,options) { function commit(project,options) {
checkActiveProject(project);
return activeProject.commit(options); return activeProject.commit(options);
} }
function getFileDiff(project,file,type) { function getFileDiff(project,file,type) {
checkActiveProject(project);
return activeProject.getFileDiff(file,type); return activeProject.getFileDiff(file,type);
} }
function getCommits(project,options) { function getCommits(project,options) {
checkActiveProject(project);
return activeProject.getCommits(options); return activeProject.getCommits(options);
} }
function getCommit(project,sha) { function getCommit(project,sha) {
checkActiveProject(project);
return activeProject.getCommit(sha); return activeProject.getCommit(sha);
} }
@ -167,7 +175,9 @@ function reloadActiveProject() {
} }
function createProject(metadata) { function createProject(metadata) {
return Projects.create(metadata).then(function(p) { return Projects.create(metadata).then(function(p) {
return p.name; return setActiveProject(p.name);
}).then(function() {
return getProject(metadata.name);
}) })
} }
function setActiveProject(projectName) { function setActiveProject(projectName) {
@ -198,7 +208,7 @@ function updateProject(project,data) {
} }
function setCredentialSecret(data) { //existingSecret,secret) { function setCredentialSecret(data) { //existingSecret,secret) {
var isReset = data.resetCredentialSecret; var isReset = data.resetCredentialSecret;
var wasInvalid = activeProject.info.settings.credentialSecretInvalid; var wasInvalid = activeProject.credentialSecretInvalid;
return activeProject.update(data).then(function() { return activeProject.update(data).then(function() {
if (isReset || !wasInvalid) { if (isReset || !wasInvalid) {
if (isReset) { if (isReset) {