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

Refector how Project object is instantiated

This commit is contained in:
Nick O'Leary 2018-05-04 16:24:00 +01:00
parent 4a4513a746
commit 0ca3cabbe8
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 78 additions and 86 deletions

View File

@ -53,15 +53,13 @@ function getGitUser(user) {
} }
return null; return null;
} }
function Project(name) { function Project(path) {
this.name = name; this.path = path;
this.path = fspath.join(projectsDir,name); this.name = fspath.basename(path);
this.paths = {}; this.paths = {};
this.files = {}; this.files = {};
this.auth = {origin:{}}; this.auth = {origin:{}};
this.missingFiles = []; this.missingFiles = [];
this.credentialSecret = null; this.credentialSecret = null;
} }
Project.prototype.load = function () { Project.prototype.load = function () {
@ -70,7 +68,9 @@ Project.prototype.load = function () {
// console.log(globalProjectSettings) // console.log(globalProjectSettings)
var projectSettings = {}; var projectSettings = {};
if (globalProjectSettings) { if (globalProjectSettings) {
projectSettings = globalProjectSettings.projects[this.name]||{}; if (globalProjectSettings.projects.hasOwnProperty(this.name)) {
projectSettings = globalProjectSettings.projects[this.name] || {};
}
} }
this.credentialSecret = projectSettings.credentialSecret; this.credentialSecret = projectSettings.credentialSecret;
@ -746,7 +746,7 @@ Project.prototype.getCredentialsFileBackup = function() {
return getBackupFilename(this.getCredentialsFile()); return getBackupFilename(this.getCredentialsFile());
} }
Project.prototype.toJSON = function () { Project.prototype.export = function () {
return { return {
name: this.name, name: this.name,
@ -786,28 +786,18 @@ function getBackupFilename(filename) {
return fspath.join(ffDir,"."+ffName+".backup"); return fspath.join(ffDir,"."+ffName+".backup");
} }
function checkProjectExists(project) { function checkProjectExists(projectPath) {
var projectPath = fspath.join(projectsDir,project);
return fs.pathExists(projectPath).then(function(exists) { return fs.pathExists(projectPath).then(function(exists) {
if (!exists) { if (!exists) {
var e = new Error("Project not found: "+project); var e = new Error("Project not found");
e.code = "project_not_found"; e.code = "project_not_found";
e.project = project; var name = fspath.basename(projectPath);
e.project = name;
throw e; throw e;
} }
}); });
} }
function createProjectDirectory(project) {
var projectPath = fspath.join(projectsDir,project);
return fs.ensureDir(projectPath);
}
function deleteProjectDirectory(project) {
var projectPath = fspath.join(projectsDir,project);
return fs.remove(projectPath);
}
function createDefaultProject(user, project) { function createDefaultProject(user, project) {
var projectPath = fspath.join(projectsDir,project.name); var projectPath = fspath.join(projectsDir,project.name);
// Create a basic skeleton of a project // Create a basic skeleton of a project
@ -910,17 +900,23 @@ function createProject(user, metadata) {
} else { } else {
username = user.username; username = user.username;
} }
if (!metadata.path) {
throw new Error("Project missing path property");
}
if (!metadata.name) {
throw new Error("Project missing name property");
}
var project = metadata.name; var project = metadata.name;
var projectPath = metadata.path;
return new Promise(function(resolve,reject) { return new Promise(function(resolve,reject) {
var projectPath = fspath.join(projectsDir,project);
fs.stat(projectPath, function(err,stat) { fs.stat(projectPath, function(err,stat) {
if (!err) { if (!err) {
var e = new Error("NLS: Project already exists"); var e = new Error("NLS: Project already exists");
e.code = "project_exists"; e.code = "project_exists";
return reject(e); return reject(e);
} }
createProjectDirectory(project).then(function() { fs.ensureDir(projectPath).then(function() {
var projects = settings.get('projects'); var projects = settings.get('projects');
if (!projects) { if (!projects) {
projects = { projects = {
@ -957,7 +953,7 @@ function createProject(user, metadata) {
return createDefaultProject(user, metadata); return createDefaultProject(user, metadata);
} }
}).then(function() { }).then(function() {
resolve(getProject(project)) resolve(loadProject(projectPath))
}).catch(function(err) { }).catch(function(err) {
fs.remove(projectPath,function() { fs.remove(projectPath,function() {
reject(err); reject(err);
@ -967,50 +963,21 @@ function createProject(user, metadata) {
}) })
} }
function deleteProject(user, name) { function deleteProject(user, projectPath) {
return checkProjectExists(name).then(function() { return checkProjectExists(projectPath).then(function() {
if (currentProject && currentProject.name === name) { return fs.remove(projectPath).then(function() {
var e = new Error("NLS: Can't delete the active project"); var name = fspath.basename(projectPath);
e.code = "cannot_delete_active_project"; var projects = settings.get('projects');
throw e; delete projects.projects[name];
} return settings.set('projects', projects);
else {
return deleteProjectDirectory(name).then(function() {
var projects = settings.get('projects');
delete projects.projects[name];
return settings.set('projects', projects);
});
}
});
}
var currentProject;
function getProject(name) {
return checkProjectExists(name).then(function() {
if (currentProject && currentProject.name === name) {
return currentProject;
}
currentProject = new Project(name);
return currentProject.load();
});
}
function listProjects() {
return fs.readdir(projectsDir).then(function(fns) {
var dirs = [];
fns.sort(function(A,B) {
return A.toLowerCase().localeCompare(B.toLowerCase());
}).filter(function(fn) {
var fullPath = fspath.join(projectsDir,fn);
if (fn[0] != ".") {
var stats = fs.lstatSync(fullPath);
if (stats.isDirectory()) {
dirs.push(fn);
}
}
}); });
return dirs; });
}
function loadProject(projectPath) {
return checkProjectExists(projectPath).then(function() {
var project = new Project(projectPath);
return project.load();
}); });
} }
@ -1024,9 +991,7 @@ function init(_settings, _runtime) {
module.exports = { module.exports = {
init: init, init: init,
get: getProject, load: loadProject,
create: createProject, create: createProject,
delete: deleteProject, delete: deleteProject
list: listProjects
} }

View File

@ -127,10 +127,20 @@ function init(_settings, _runtime) {
activeProject = globalSettings.projects.activeProject; activeProject = globalSettings.projects.activeProject;
} }
if (settings.flowFile) { if (settings.flowFile) {
// if flowFile is a known project name - use it
if (globalSettings.projects.projects.hasOwnProperty(settings.flowFile)) { if (globalSettings.projects.projects.hasOwnProperty(settings.flowFile)) {
activeProject = settings.flowFile; activeProject = settings.flowFile;
globalSettings.projects.activeProject = settings.flowFile; globalSettings.projects.activeProject = settings.flowFile;
saveSettings = true; saveSettings = true;
} else {
// if it resolves to a dir - use it... but:
// - where to get credsecret from?
// - what if the name clashes with a known project?
// var stat = fs.statSync(settings.flowFile);
// if (stat && stat.isDirectory()) {
// activeProject = settings.flowFile;
// }
} }
} }
if (!activeProject) { if (!activeProject) {
@ -148,6 +158,24 @@ function init(_settings, _runtime) {
return Promise.resolve(); return Promise.resolve();
} }
function listProjects() {
return fs.readdir(projectsDir).then(function(fns) {
var dirs = [];
fns.sort(function(A,B) {
return A.toLowerCase().localeCompare(B.toLowerCase());
}).filter(function(fn) {
var fullPath = fspath.join(projectsDir,fn);
if (fn[0] != ".") {
var stats = fs.lstatSync(fullPath);
if (stats.isDirectory()) {
dirs.push(fn);
}
}
});
return dirs;
});
}
function getUserGitSettings(user) { function getUserGitSettings(user) {
var userSettings = settings.getUserSettings(user)||{}; var userSettings = settings.getUserSettings(user)||{};
return userSettings.git; return userSettings.git;
@ -160,7 +188,11 @@ function getBackupFilename(filename) {
} }
function loadProject(name) { function loadProject(name) {
return Projects.get(name).then(function(project) { var projectPath = name;
if (projectPath.indexOf(fspath.sep) === -1) {
projectPath = fspath.join(projectsDir,name);
}
return Projects.load(projectPath).then(function(project) {
activeProject = project; activeProject = project;
flowsFullPath = project.getFlowFile(); flowsFullPath = project.getFlowFile();
flowsFileBackup = project.getFlowFileBackup(); flowsFileBackup = project.getFlowFileBackup();
@ -170,26 +202,20 @@ function loadProject(name) {
}) })
} }
function listProjects(user) {
return Projects.list();
}
function getProject(user, name) { function getProject(user, name) {
checkActiveProject(name); checkActiveProject(name);
//return when.resolve(activeProject.info); //return when.resolve(activeProject.info);
var username; return Promise.resolve(activeProject.export());
if (!user) {
username = "_";
} else {
username = user.username;
}
return Projects.get(name).then(function(project) {
return project.toJSON();
});
} }
function deleteProject(user, name) { function deleteProject(user, name) {
return Projects.delete(user, name); if (activeProject && activeProject.name === name) {
var e = new Error("NLS: Can't delete the active project");
e.code = "cannot_delete_active_project";
throw e;
}
var projectPath = fspath.join(projectsDir,name);
return Projects.delete(user, projectPath);
} }
function checkActiveProject(project) { function checkActiveProject(project) {
@ -347,6 +373,7 @@ function createProject(user, metadata) {
metadata.files.oldCredentials = credentialsFile; metadata.files.oldCredentials = credentialsFile;
metadata.files.credentialSecret = currentEncryptionKey; metadata.files.credentialSecret = currentEncryptionKey;
} }
metadata.path = fspath.join(projectsDir,metadata.name);
return Projects.create(user, metadata).then(function(p) { return Projects.create(user, metadata).then(function(p) {
return setActiveProject(user, p.name); return setActiveProject(user, p.name);
}).then(function() { }).then(function() {