2017-10-17 00:23:50 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
var fs = require('fs-extra');
|
|
|
|
var when = require('when');
|
|
|
|
var fspath = require("path");
|
|
|
|
|
|
|
|
var gitTools = require("./git");
|
|
|
|
var util = require("../util");
|
|
|
|
var defaultFileSet = require("./defaultFileSet");
|
|
|
|
|
|
|
|
var settings;
|
|
|
|
var runtime;
|
|
|
|
var log;
|
|
|
|
|
|
|
|
var projectsDir;
|
|
|
|
|
|
|
|
function Project(name) {
|
|
|
|
this.name = name;
|
|
|
|
this.path = fspath.join(projectsDir,name);
|
|
|
|
this.paths = {};
|
2017-10-17 11:14:50 +02:00
|
|
|
|
|
|
|
this.missingFiles = [];
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
this.credentialSecret = null;
|
|
|
|
}
|
|
|
|
Project.prototype.load = function () {
|
|
|
|
var project = this;
|
|
|
|
var globalProjectSettings = settings.get("projects");
|
|
|
|
// console.log(globalProjectSettings)
|
|
|
|
var projectSettings = {};
|
|
|
|
if (globalProjectSettings) {
|
|
|
|
projectSettings = globalProjectSettings.projects[this.name]||{};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.credentialSecret = projectSettings.credentialSecret;
|
2017-10-17 11:14:50 +02:00
|
|
|
|
2017-10-19 22:38:53 +02:00
|
|
|
// this.paths.flowFile = fspath.join(this.path,"flow.json");
|
|
|
|
// this.paths.credentialsFile = fspath.join(this.path,"flow_cred.json");
|
2017-10-17 11:14:50 +02:00
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
var promises = [];
|
2017-10-19 22:38:53 +02:00
|
|
|
return checkProjectFiles(project).then(function(missingFiles) {
|
2017-10-17 00:23:50 +02:00
|
|
|
if (missingFiles.length > 0) {
|
2017-10-17 11:14:50 +02:00
|
|
|
project.missingFiles = missingFiles;
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
if (missingFiles.indexOf('package.json') === -1) {
|
|
|
|
project.paths['package.json'] = fspath.join(project.path,"package.json");
|
|
|
|
promises.push(fs.readFile(project.paths['package.json'],"utf8").then(function(content) {
|
|
|
|
project.package = util.parseJSON(content);
|
2017-10-19 22:38:53 +02:00
|
|
|
if (project.package.hasOwnProperty('node-red')) {
|
|
|
|
if (project.package['node-red'].hasOwnProperty('settings')) {
|
|
|
|
project.paths.flowFile = project.package['node-red'].settings.flowFile;
|
|
|
|
project.paths.credentialsFile = project.package['node-red'].settings.credentialsFile;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: package.json doesn't have a node-red section
|
|
|
|
// is that a bad thing?
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
}));
|
2017-10-19 22:38:53 +02:00
|
|
|
} else {
|
|
|
|
project.package = {};
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
if (missingFiles.indexOf('README.md') === -1) {
|
|
|
|
project.paths['README.md'] = fspath.join(project.path,"README.md");
|
|
|
|
promises.push(fs.readFile(project.paths['README.md'],"utf8").then(function(content) {
|
2017-10-17 11:14:50 +02:00
|
|
|
project.description = content;
|
2017-10-17 00:23:50 +02:00
|
|
|
}));
|
|
|
|
} else {
|
2017-10-17 11:14:50 +02:00
|
|
|
project.description = "";
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
2017-10-19 22:38:53 +02:00
|
|
|
// if (missingFiles.indexOf('flow.json') !== -1) {
|
|
|
|
// console.log("MISSING FLOW FILE");
|
|
|
|
// } else {
|
|
|
|
// project.paths.flowFile = fspath.join(project.path,"flow.json");
|
|
|
|
// }
|
|
|
|
// if (missingFiles.indexOf('flow_cred.json') !== -1) {
|
|
|
|
// console.log("MISSING CREDS FILE");
|
|
|
|
// } else {
|
|
|
|
// project.paths.credentialsFile = fspath.join(project.path,"flow_cred.json");
|
|
|
|
// }
|
2017-10-17 00:23:50 +02:00
|
|
|
|
|
|
|
return when.settle(promises).then(function() {
|
|
|
|
return project;
|
|
|
|
})
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Project.prototype.update = function (data) {
|
2017-10-19 22:38:53 +02:00
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
var promises = [];
|
|
|
|
var project = this;
|
2017-10-19 22:38:53 +02:00
|
|
|
var saveSettings = false;
|
|
|
|
var saveREADME = false;
|
|
|
|
var savePackage = false;
|
|
|
|
var flowFilesChanged = false;
|
|
|
|
var credentialSecretChanged = false;
|
2017-10-17 00:23:50 +02:00
|
|
|
|
2017-10-19 22:38:53 +02:00
|
|
|
var globalProjectSettings = settings.get("projects");
|
|
|
|
if (!globalProjectSettings.projects.hasOwnProperty(this.name)) {
|
|
|
|
globalProjectSettings.projects[this.name] = {};
|
|
|
|
saveSettings = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data.credentialSecret && data.credentialSecret !== this.credentialSecret) {
|
2017-10-17 00:23:50 +02:00
|
|
|
var existingSecret = data.currentCredentialSecret;
|
|
|
|
var isReset = data.resetCredentialSecret;
|
|
|
|
var secret = data.credentialSecret;
|
|
|
|
|
|
|
|
// console.log("updating credentialSecret");
|
|
|
|
// console.log("request:");
|
|
|
|
// console.log(JSON.stringify(data,"",4));
|
|
|
|
// console.log(" this.credentialSecret",this.credentialSecret);
|
|
|
|
// console.log(" this.info", this.info);
|
|
|
|
|
|
|
|
if (!isReset && // not a reset
|
|
|
|
this.credentialSecret && // key already set
|
2017-10-17 11:14:50 +02:00
|
|
|
!this.credentialSecretInvalid && // key not invalid
|
2017-10-17 00:23:50 +02:00
|
|
|
this.credentialSecret !== existingSecret) { // key doesn't match provided existing key
|
|
|
|
var e = new Error("Cannot change credentialSecret without current key");
|
|
|
|
e.code = "missing_current_credential_key";
|
2017-10-19 22:38:53 +02:00
|
|
|
return when.reject(e);
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
this.credentialSecret = secret;
|
|
|
|
|
|
|
|
globalProjectSettings.projects[this.name].credentialSecret = project.credentialSecret;
|
2017-10-17 11:14:50 +02:00
|
|
|
delete this.credentialSecretInvalid;
|
2017-10-19 22:38:53 +02:00
|
|
|
saveSettings = true;
|
|
|
|
credentialSecretChanged = true;
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data.hasOwnProperty('description')) {
|
2017-10-19 22:38:53 +02:00
|
|
|
saveREADME = true;
|
2017-10-17 11:14:50 +02:00
|
|
|
this.description = data.description;
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('dependencies')) {
|
2017-10-19 22:38:53 +02:00
|
|
|
savePackage = true;
|
2017-10-17 00:23:50 +02:00
|
|
|
this.package.dependencies = data.dependencies;
|
|
|
|
}
|
|
|
|
if (data.hasOwnProperty('summary')) {
|
2017-10-19 22:38:53 +02:00
|
|
|
savePackage = true;
|
2017-10-17 00:23:50 +02:00
|
|
|
this.package.description = data.summary;
|
|
|
|
}
|
|
|
|
|
2017-10-19 22:38:53 +02:00
|
|
|
if (data.hasOwnProperty('files')) {
|
|
|
|
this.package['node-red'] = this.package['node-red'] || { settings: {}};
|
|
|
|
if (data.files.hasOwnProperty('flow') && this.package['node-red'].settings.flowFile !== data.files.flow) {
|
|
|
|
this.paths.flowFile = data.files.flow;
|
|
|
|
this.package['node-red'].settings.flowFile = data.files.flow;
|
|
|
|
savePackage = true;
|
|
|
|
flowFilesChanged = true;
|
|
|
|
}
|
|
|
|
if (data.files.hasOwnProperty('credentials') && this.package['node-red'].settings.credentialsFile !== data.files.credentials) {
|
|
|
|
this.paths.credentialsFile = data.files.credentials;
|
|
|
|
this.package['node-red'].settings.credentialsFile = data.files.credentials;
|
|
|
|
// Don't know if the credSecret is invalid or not so clear the flag
|
|
|
|
delete this.credentialSecretInvalid;
|
|
|
|
|
|
|
|
savePackage = true;
|
|
|
|
flowFilesChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (saveSettings) {
|
|
|
|
promises.push(settings.set("projects",globalProjectSettings));
|
|
|
|
}
|
|
|
|
if (saveREADME) {
|
|
|
|
promises.push(util.writeFile(this.paths['README.md'], this.description));
|
|
|
|
}
|
|
|
|
if (savePackage) {
|
|
|
|
promises.push(util.writeFile(this.paths['package.json'], JSON.stringify(this.package,"",4)));
|
|
|
|
}
|
|
|
|
return when.settle(promises).then(function(res) {
|
|
|
|
return {
|
|
|
|
flowFilesChanged: flowFilesChanged,
|
|
|
|
credentialSecretChanged: credentialSecretChanged
|
|
|
|
}
|
|
|
|
})
|
2017-10-17 00:23:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Project.prototype.getFiles = function () {
|
|
|
|
return gitTools.getFiles(this.path);
|
|
|
|
};
|
|
|
|
Project.prototype.stageFile = function(file) {
|
|
|
|
return gitTools.stageFile(this.path,file);
|
|
|
|
};
|
|
|
|
Project.prototype.unstageFile = function(file) {
|
|
|
|
return gitTools.unstageFile(this.path,file);
|
|
|
|
}
|
|
|
|
Project.prototype.commit = function(options) {
|
|
|
|
return gitTools.commit(this.path,options.message);
|
|
|
|
}
|
|
|
|
Project.prototype.getFileDiff = function(file,type) {
|
|
|
|
return gitTools.getFileDiff(this.path,file,type);
|
|
|
|
}
|
|
|
|
Project.prototype.getCommits = function(options) {
|
|
|
|
return gitTools.getCommits(this.path,options);
|
|
|
|
}
|
|
|
|
Project.prototype.getCommit = function(sha) {
|
|
|
|
return gitTools.getCommit(this.path,sha);
|
|
|
|
}
|
|
|
|
|
|
|
|
Project.prototype.getFlowFile = function() {
|
2017-10-19 22:38:53 +02:00
|
|
|
console.log("Project.getFlowFile = ",this.paths.flowFile);
|
|
|
|
if (this.paths.flowFile) {
|
|
|
|
return fspath.join(this.path,this.paths.flowFile);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
Project.prototype.getFlowFileBackup = function() {
|
|
|
|
return getBackupFilename(this.getFlowFile());
|
|
|
|
}
|
|
|
|
Project.prototype.getCredentialsFile = function() {
|
2017-10-19 22:38:53 +02:00
|
|
|
console.log("Project.getCredentialsFile = ",this.paths.credentialsFile);
|
|
|
|
if (this.paths.credentialsFile) {
|
|
|
|
return fspath.join(this.path,this.paths.credentialsFile);
|
|
|
|
} else {
|
|
|
|
return this.paths.credentialsFile;
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
Project.prototype.getCredentialsFileBackup = function() {
|
|
|
|
return getBackupFilename(this.getCredentialsFile());
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:14:50 +02:00
|
|
|
|
|
|
|
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: {
|
2017-10-19 22:38:53 +02:00
|
|
|
flow: this.paths.flowFile,
|
|
|
|
credentials: this.paths.credentialsFile
|
2017-10-17 11:14:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
function getBackupFilename(filename) {
|
|
|
|
var ffName = fspath.basename(filename);
|
|
|
|
var ffDir = fspath.dirname(filename);
|
|
|
|
return fspath.join(ffDir,"."+ffName+".backup");
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkProjectExists(project) {
|
|
|
|
var projectPath = fspath.join(projectsDir,project);
|
|
|
|
return fs.pathExists(projectPath).then(function(exists) {
|
|
|
|
if (!exists) {
|
|
|
|
var e = new Error("NLD: project not found");
|
|
|
|
e.code = "project_not_found";
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createProjectDirectory(project) {
|
|
|
|
var projectPath = fspath.join(projectsDir,project);
|
|
|
|
return fs.ensureDir(projectPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDefaultProject(project) {
|
|
|
|
var projectPath = fspath.join(projectsDir,project.name);
|
|
|
|
// Create a basic skeleton of a project
|
|
|
|
return gitTools.initRepo(projectPath).then(function() {
|
|
|
|
var promises = [];
|
|
|
|
for (var file in defaultFileSet) {
|
|
|
|
if (defaultFileSet.hasOwnProperty(file)) {
|
|
|
|
promises.push(util.writeFile(fspath.join(projectPath,file),defaultFileSet[file](project)));
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 11:14:50 +02:00
|
|
|
return when.all(promises).then(function() {
|
|
|
|
return gitTools.stageFile(projectPath,Object.keys(defaultFileSet));
|
|
|
|
}).then(function() {
|
|
|
|
return gitTools.commit(projectPath,"Create project");
|
|
|
|
})
|
2017-10-17 00:23:50 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkProjectFiles(project) {
|
2017-10-19 22:38:53 +02:00
|
|
|
var projectPath = project.path;
|
2017-10-17 00:23:50 +02:00
|
|
|
var promises = [];
|
|
|
|
var paths = [];
|
|
|
|
for (var file in defaultFileSet) {
|
|
|
|
if (defaultFileSet.hasOwnProperty(file)) {
|
|
|
|
paths.push(file);
|
|
|
|
promises.push(fs.stat(fspath.join(projectPath,file)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return when.settle(promises).then(function(results) {
|
|
|
|
var missing = [];
|
|
|
|
results.forEach(function(result,i) {
|
|
|
|
if (result.state === 'rejected') {
|
|
|
|
missing.push(paths[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return missing;
|
|
|
|
}).then(function(missing) {
|
|
|
|
// if (createMissing) {
|
|
|
|
// var promises = [];
|
|
|
|
// missing.forEach(function(file) {
|
|
|
|
// promises.push(util.writeFile(fspath.join(projectPath,file),defaultFileSet[file](project)));
|
|
|
|
// });
|
|
|
|
// return promises;
|
|
|
|
// } else {
|
|
|
|
return missing;
|
|
|
|
// }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createProject(metadata) {
|
|
|
|
var project = metadata.name;
|
|
|
|
return when.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() {
|
|
|
|
var projects = settings.get('projects');
|
|
|
|
projects.projects[project] = {};
|
|
|
|
if (metadata.credentialSecret) {
|
|
|
|
projects.projects[project].credentialSecret = metadata.credentialSecret;
|
|
|
|
}
|
2017-10-19 22:38:53 +02:00
|
|
|
if (metadata.remote) {
|
|
|
|
projects.projects[project].remote = metadata.remote;
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
return settings.set('projects',projects);
|
|
|
|
}).then(function() {
|
|
|
|
if (metadata.remote) {
|
|
|
|
return gitTools.clone(metadata.remote,projectPath).then(function(result) {
|
|
|
|
// Check this is a valid project
|
|
|
|
// If it is empty
|
|
|
|
// - if 'populate' flag is set, call populateProject
|
|
|
|
// - otherwise reject with suitable error to allow UI to confirm population
|
|
|
|
// If it is missing package.json/flow.json/flow_cred.json
|
|
|
|
// - reject as invalid project
|
|
|
|
|
|
|
|
// checkProjectFiles(project).then(function(results) {
|
|
|
|
// console.log("checkProjectFiles");
|
|
|
|
// console.log(results);
|
|
|
|
// });
|
|
|
|
|
|
|
|
resolve(getProject(project));
|
|
|
|
}).catch(function(error) {
|
|
|
|
fs.remove(projectPath,function() {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
createDefaultProject(metadata).then(function() { resolve(getProject(project))}).catch(reject);
|
|
|
|
}
|
|
|
|
}).catch(reject);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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().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 init(_settings, _runtime) {
|
|
|
|
settings = _settings;
|
|
|
|
runtime = _runtime;
|
|
|
|
log = runtime.log;
|
|
|
|
projectsDir = fspath.join(settings.userDir,"projects");
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
get: getProject,
|
|
|
|
create: createProject,
|
|
|
|
list: listProjects
|
|
|
|
|
|
|
|
}
|