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;
}
function Project(name) {
this.name = name;
this.path = fspath.join(projectsDir,name);
function Project(path) {
this.path = path;
this.name = fspath.basename(path);
this.paths = {};
this.files = {};
this.auth = {origin:{}};
this.missingFiles = [];
this.credentialSecret = null;
}
Project.prototype.load = function () {
@ -70,7 +68,9 @@ Project.prototype.load = function () {
// console.log(globalProjectSettings)
var projectSettings = {};
if (globalProjectSettings) {
projectSettings = globalProjectSettings.projects[this.name]||{};
if (globalProjectSettings.projects.hasOwnProperty(this.name)) {
projectSettings = globalProjectSettings.projects[this.name] || {};
}
}
this.credentialSecret = projectSettings.credentialSecret;
@ -746,7 +746,7 @@ Project.prototype.getCredentialsFileBackup = function() {
return getBackupFilename(this.getCredentialsFile());
}
Project.prototype.toJSON = function () {
Project.prototype.export = function () {
return {
name: this.name,
@ -786,28 +786,18 @@ function getBackupFilename(filename) {
return fspath.join(ffDir,"."+ffName+".backup");
}
function checkProjectExists(project) {
var projectPath = fspath.join(projectsDir,project);
function checkProjectExists(projectPath) {
return fs.pathExists(projectPath).then(function(exists) {
if (!exists) {
var e = new Error("Project not found: "+project);
var e = new Error("Project not found");
e.code = "project_not_found";
e.project = project;
var name = fspath.basename(projectPath);
e.project = name;
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) {
var projectPath = fspath.join(projectsDir,project.name);
// Create a basic skeleton of a project
@ -910,17 +900,23 @@ function createProject(user, metadata) {
} else {
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 projectPath = metadata.path;
return new Promise(function(resolve,reject) {
var projectPath = fspath.join(projectsDir,project);
fs.stat(projectPath, function(err,stat) {
if (!err) {
var e = new Error("NLS: Project already exists");
e.code = "project_exists";
return reject(e);
}
createProjectDirectory(project).then(function() {
fs.ensureDir(projectPath).then(function() {
var projects = settings.get('projects');
if (!projects) {
projects = {
@ -957,7 +953,7 @@ function createProject(user, metadata) {
return createDefaultProject(user, metadata);
}
}).then(function() {
resolve(getProject(project))
resolve(loadProject(projectPath))
}).catch(function(err) {
fs.remove(projectPath,function() {
reject(err);
@ -967,50 +963,21 @@ function createProject(user, metadata) {
})
}
function deleteProject(user, name) {
return checkProjectExists(name).then(function() {
if (currentProject && currentProject.name === name) {
var e = new Error("NLS: Can't delete the active project");
e.code = "cannot_delete_active_project";
throw e;
}
else {
return deleteProjectDirectory(name).then(function() {
function deleteProject(user, projectPath) {
return checkProjectExists(projectPath).then(function() {
return fs.remove(projectPath).then(function() {
var name = fspath.basename(projectPath);
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 = {
init: init,
get: getProject,
load: loadProject,
create: createProject,
delete: deleteProject,
list: listProjects
delete: deleteProject
}

View File

@ -127,10 +127,20 @@ function init(_settings, _runtime) {
activeProject = globalSettings.projects.activeProject;
}
if (settings.flowFile) {
// if flowFile is a known project name - use it
if (globalSettings.projects.projects.hasOwnProperty(settings.flowFile)) {
activeProject = settings.flowFile;
globalSettings.projects.activeProject = settings.flowFile;
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) {
@ -148,6 +158,24 @@ function init(_settings, _runtime) {
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) {
var userSettings = settings.getUserSettings(user)||{};
return userSettings.git;
@ -160,7 +188,11 @@ function getBackupFilename(filename) {
}
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;
flowsFullPath = project.getFlowFile();
flowsFileBackup = project.getFlowFileBackup();
@ -170,26 +202,20 @@ function loadProject(name) {
})
}
function listProjects(user) {
return Projects.list();
}
function getProject(user, name) {
checkActiveProject(name);
//return when.resolve(activeProject.info);
var username;
if (!user) {
username = "_";
} else {
username = user.username;
}
return Projects.get(name).then(function(project) {
return project.toJSON();
});
return Promise.resolve(activeProject.export());
}
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) {
@ -347,6 +373,7 @@ function createProject(user, metadata) {
metadata.files.oldCredentials = credentialsFile;
metadata.files.credentialSecret = currentEncryptionKey;
}
metadata.path = fspath.join(projectsDir,metadata.name);
return Projects.create(user, metadata).then(function(p) {
return setActiveProject(user, p.name);
}).then(function() {