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");
|
2017-12-15 16:07:47 +01:00
|
|
|
var os = require('os');
|
2017-10-17 00:23:50 +02:00
|
|
|
|
|
|
|
var gitTools = require("./git");
|
|
|
|
var util = require("../util");
|
|
|
|
var defaultFileSet = require("./defaultFileSet");
|
2018-01-19 00:13:55 +01:00
|
|
|
var sshKeys = require("./ssh");
|
2017-10-17 00:23:50 +02:00
|
|
|
var settings;
|
|
|
|
var runtime;
|
|
|
|
var log;
|
|
|
|
|
|
|
|
var projectsDir;
|
|
|
|
|
2017-11-25 00:12:35 +01:00
|
|
|
var authCache = require("./git/authCache");
|
2017-11-23 01:27:13 +01:00
|
|
|
|
2018-01-18 23:17:48 +01:00
|
|
|
// TODO: DRY - red/api/editor/sshkeys !
|
|
|
|
function getSSHKeyUsername(userObj) {
|
|
|
|
var username = '__default';
|
|
|
|
if ( userObj && userObj.name ) {
|
|
|
|
username = userObj.name;
|
|
|
|
}
|
|
|
|
return username;
|
|
|
|
}
|
2018-01-24 16:51:11 +01:00
|
|
|
function getGitUser(user) {
|
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
|
|
|
var userSettings = settings.getUserSettings(username);
|
|
|
|
if (userSettings && userSettings.git) {
|
|
|
|
return userSettings.git.user;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
function Project(name) {
|
|
|
|
this.name = name;
|
|
|
|
this.path = fspath.join(projectsDir,name);
|
|
|
|
this.paths = {};
|
2018-01-08 15:46:38 +01:00
|
|
|
this.files = {};
|
2017-11-23 01:27:13 +01:00
|
|
|
this.auth = {origin:{}};
|
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-12-04 12:42:44 +01:00
|
|
|
this.git = projectSettings.git || { user:{} };
|
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) {
|
2017-11-22 00:31:41 +01:00
|
|
|
try {
|
|
|
|
project.package = util.parseJSON(content);
|
|
|
|
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-19 22:38:53 +02:00
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
} catch(err) {
|
|
|
|
// package.json isn't valid JSON... is a merge underway?
|
|
|
|
project.package = {};
|
2017-10-19 22:38:53 +02:00
|
|
|
}
|
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
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
promises.push(project.loadRemotes());
|
2017-11-22 00:31:41 +01:00
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
return when.settle(promises).then(function() {
|
|
|
|
return project;
|
|
|
|
})
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-01-08 15:46:38 +01:00
|
|
|
Project.prototype.initialise = function(user,data) {
|
|
|
|
var project = this;
|
|
|
|
if (!this.empty) {
|
|
|
|
throw new Error("Cannot initialise non-empty project");
|
|
|
|
}
|
|
|
|
var files = Object.keys(defaultFileSet);
|
|
|
|
var promises = [];
|
|
|
|
|
|
|
|
if (data.hasOwnProperty('credentialSecret')) {
|
|
|
|
var projects = settings.get('projects');
|
|
|
|
projects.projects[project.name] = projects.projects[project.name] || {};
|
|
|
|
projects.projects[project.name].credentialSecret = data.credentialSecret;
|
|
|
|
promises.push(settings.set('projects',projects));
|
|
|
|
}
|
|
|
|
|
|
|
|
project.files.flow = data.files.flow;
|
|
|
|
project.files.credentials = data.files.credentials;
|
|
|
|
var flowFilePath = fspath.join(project.path,project.files.flow);
|
|
|
|
var credsFilePath = getCredentialsFilename(flowFilePath);
|
|
|
|
promises.push(util.writeFile(flowFilePath,"[]"));
|
|
|
|
promises.push(util.writeFile(credsFilePath,"{}"));
|
|
|
|
files.push(project.files.flow);
|
|
|
|
files.push(project.files.credentials);
|
|
|
|
for (var file in defaultFileSet) {
|
|
|
|
if (defaultFileSet.hasOwnProperty(file)) {
|
|
|
|
promises.push(util.writeFile(fspath.join(project.path,file),defaultFileSet[file](project)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return when.all(promises).then(function() {
|
|
|
|
return gitTools.stageFile(project.path,files);
|
|
|
|
}).then(function() {
|
2018-01-24 16:51:11 +01:00
|
|
|
return gitTools.commit(project.path,"Create project files",getGitUser(user));
|
2018-01-08 15:46:38 +01:00
|
|
|
}).then(function() {
|
|
|
|
return project.load()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
Project.prototype.loadRemotes = function() {
|
|
|
|
var project = this;
|
|
|
|
return gitTools.getRemotes(project.path).then(function(remotes) {
|
|
|
|
project.remotes = remotes;
|
|
|
|
}).then(function() {
|
2018-01-09 16:06:05 +01:00
|
|
|
project.branches = {};
|
|
|
|
return project.status();
|
2017-12-04 12:42:44 +01:00
|
|
|
}).then(function() {
|
2018-02-01 12:25:56 +01:00
|
|
|
if (project.remotes) {
|
|
|
|
var allRemotes = Object.keys(project.remotes);
|
|
|
|
var match = "";
|
|
|
|
if (project.branches.remote) {
|
|
|
|
allRemotes.forEach(function(remote) {
|
|
|
|
if (project.branches.remote.indexOf(remote) === 0 && match.length < remote.length) {
|
|
|
|
match = remote;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
project.currentRemote = project.parseRemoteBranch(project.branches.remote).remote;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete project.currentRemote;
|
2017-12-21 18:40:24 +01:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Project.prototype.parseRemoteBranch = function (remoteBranch) {
|
|
|
|
if (!remoteBranch) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
var project = this;
|
|
|
|
var allRemotes = Object.keys(project.remotes);
|
|
|
|
var match = "";
|
|
|
|
allRemotes.forEach(function(remote) {
|
|
|
|
if (remoteBranch.indexOf(remote) === 0 && match.length < remote.length) {
|
|
|
|
match = remote;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
remote: match,
|
|
|
|
branch: remoteBranch.substring(match.length+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-01-08 15:46:38 +01:00
|
|
|
Project.prototype.isEmpty = function () {
|
|
|
|
return this.empty;
|
|
|
|
};
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
Project.prototype.update = function (user, data) {
|
2017-12-06 23:39:30 +01:00
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
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-12-04 12:42:44 +01:00
|
|
|
|
|
|
|
if (data.hasOwnProperty('git')) {
|
|
|
|
if (data.git.hasOwnProperty('user')) {
|
|
|
|
globalProjectSettings.projects[this.name].git = globalProjectSettings.projects[this.name].git || {};
|
|
|
|
globalProjectSettings.projects[this.name].git.user = globalProjectSettings.projects[this.name].git.user || {};
|
|
|
|
globalProjectSettings.projects[this.name].git.user[username] = {
|
|
|
|
name: data.git.user.name,
|
|
|
|
email: data.git.user.email
|
|
|
|
}
|
|
|
|
this.git.user[username] = {
|
|
|
|
name: data.git.user.name,
|
|
|
|
email: data.git.user.email
|
|
|
|
}
|
|
|
|
saveSettings = true;
|
|
|
|
}
|
|
|
|
if (data.git.hasOwnProperty('remotes')) {
|
|
|
|
var remoteNames = Object.keys(data.git.remotes);
|
|
|
|
var remotesChanged = false;
|
2017-12-08 17:31:42 +01:00
|
|
|
var modifyRemotesPromise = Promise.resolve();
|
2017-12-04 12:42:44 +01:00
|
|
|
remoteNames.forEach(function(name) {
|
|
|
|
if (data.git.remotes[name].removed) {
|
|
|
|
remotesChanged = true;
|
|
|
|
modifyRemotesPromise = modifyRemotesPromise.then(function() { gitTools.removeRemote(project.path,name) });
|
|
|
|
} else {
|
|
|
|
if (data.git.remotes[name].url) {
|
|
|
|
remotesChanged = true;
|
|
|
|
modifyRemotesPromise = modifyRemotesPromise.then(function() { gitTools.addRemote(project.path,name,data.git.remotes[name])});
|
|
|
|
}
|
|
|
|
if (data.git.remotes[name].username && data.git.remotes[name].password) {
|
|
|
|
var url = data.git.remotes[name].url || project.remotes[name].fetch;
|
2017-12-06 23:39:30 +01:00
|
|
|
authCache.set(project.name,url,username,data.git.remotes[name]);
|
2017-12-04 12:42:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (remotesChanged) {
|
|
|
|
modifyRemotesPromise = modifyRemotesPromise.then(function() {
|
|
|
|
return project.loadRemotes();
|
|
|
|
});
|
|
|
|
promises.push(modifyRemotesPromise);
|
2017-11-23 01:27:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
|
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 () {
|
2018-01-08 15:46:38 +01:00
|
|
|
return gitTools.getFiles(this.path).catch(function(err) {
|
|
|
|
if (/ambiguous argument/.test(err.message)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
});
|
2017-10-17 00:23:50 +02:00
|
|
|
};
|
|
|
|
Project.prototype.stageFile = function(file) {
|
|
|
|
return gitTools.stageFile(this.path,file);
|
|
|
|
};
|
|
|
|
Project.prototype.unstageFile = function(file) {
|
|
|
|
return gitTools.unstageFile(this.path,file);
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
Project.prototype.commit = function(user, options) {
|
2018-01-24 16:51:11 +01:00
|
|
|
return gitTools.commit(this.path,options.message,getGitUser(user));
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
Project.prototype.getFileDiff = function(file,type) {
|
|
|
|
return gitTools.getFileDiff(this.path,file,type);
|
|
|
|
}
|
|
|
|
Project.prototype.getCommits = function(options) {
|
2018-01-08 15:46:38 +01:00
|
|
|
return gitTools.getCommits(this.path,options).catch(function(err) {
|
|
|
|
if (/ambiguous argument/.test(err.message) || /does not have any commits yet/.test(err.message)) {
|
|
|
|
return {
|
|
|
|
count:0,
|
|
|
|
commits:[],
|
|
|
|
total: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
})
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
Project.prototype.getCommit = function(sha) {
|
|
|
|
return gitTools.getCommit(this.path,sha);
|
|
|
|
}
|
2017-10-25 16:26:24 +02:00
|
|
|
Project.prototype.getFile = function (filePath,treeish) {
|
|
|
|
if (treeish !== "_") {
|
|
|
|
return gitTools.getFile(this.path, filePath, treeish);
|
|
|
|
} else {
|
|
|
|
return fs.readFile(fspath.join(this.path,filePath),"utf8");
|
|
|
|
}
|
|
|
|
};
|
2017-12-11 18:05:12 +01:00
|
|
|
Project.prototype.revertFile = function (filePath) {
|
|
|
|
var self = this;
|
|
|
|
return gitTools.revertFile(this.path, filePath).then(function() {
|
|
|
|
return self.load();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
|
2017-12-06 23:39:30 +01:00
|
|
|
Project.prototype.status = function(user) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var self = this;
|
2017-11-23 01:27:13 +01:00
|
|
|
|
|
|
|
var fetchPromise;
|
2017-12-04 12:42:44 +01:00
|
|
|
if (this.remotes) {
|
|
|
|
fetchPromise = gitTools.getRemoteBranch(self.path).then(function(remoteBranch) {
|
2017-12-04 14:26:47 +01:00
|
|
|
if (remoteBranch) {
|
|
|
|
var allRemotes = Object.keys(self.remotes);
|
|
|
|
var match = "";
|
|
|
|
allRemotes.forEach(function(remote) {
|
|
|
|
if (remoteBranch.indexOf(remote) === 0 && match.length < remote.length) {
|
|
|
|
match = remote;
|
|
|
|
}
|
|
|
|
})
|
2017-12-06 23:39:30 +01:00
|
|
|
return self.fetch(user, match);
|
2017-12-04 14:26:47 +01:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
});
|
2017-11-23 01:27:13 +01:00
|
|
|
} else {
|
2017-12-08 17:31:42 +01:00
|
|
|
fetchPromise = Promise.resolve();
|
2017-11-23 01:27:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var completeStatus = function(fetchError) {
|
|
|
|
var promises = [
|
|
|
|
gitTools.getStatus(self.path),
|
|
|
|
fs.exists(fspath.join(self.path,".git","MERGE_HEAD"))
|
|
|
|
];
|
|
|
|
return when.all(promises).then(function(results) {
|
|
|
|
var result = results[0];
|
|
|
|
if (results[1]) {
|
|
|
|
result.merging = true;
|
|
|
|
}
|
|
|
|
self.branches.local = result.branches.local;
|
|
|
|
self.branches.remote = result.branches.remote;
|
2018-01-08 15:46:38 +01:00
|
|
|
if (fetchError && !/ambiguous argument/.test(fetchError.message)) {
|
2017-11-23 01:27:13 +01:00
|
|
|
result.branches.remoteError = {
|
2017-12-04 12:42:44 +01:00
|
|
|
remote: fetchError.remote,
|
2017-11-23 01:27:13 +01:00
|
|
|
code: fetchError.code
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 16:06:05 +01:00
|
|
|
if (result.commits.total === 0 && Object.keys(result.files).length === 0) {
|
|
|
|
if (!self.empty) {
|
|
|
|
runtime.events.emit("runtime-event",{
|
|
|
|
id:"runtime-state",
|
|
|
|
payload:{
|
|
|
|
type:"warning",
|
|
|
|
error:"project_empty",
|
|
|
|
text:"notification.warnings.project_empty"},
|
|
|
|
retain:true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
self.empty = true;
|
|
|
|
} else {
|
|
|
|
if (self.empty) {
|
|
|
|
if (self.paths.flowFile) {
|
|
|
|
runtime.events.emit("runtime-event",{id:"runtime-state",retain:true});
|
|
|
|
} else {
|
|
|
|
runtime.events.emit("runtime-event",{
|
|
|
|
id:"runtime-state",
|
|
|
|
payload:{
|
|
|
|
type:"warning",
|
|
|
|
error:"missing_flow_file",
|
|
|
|
text:"notification.warnings.missing_flow_file"},
|
|
|
|
retain:true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete self.empty;
|
|
|
|
}
|
2017-11-23 01:27:13 +01:00
|
|
|
return result;
|
2018-01-08 15:46:38 +01:00
|
|
|
}).catch(function(err) {
|
|
|
|
if (/ambiguous argument/.test(err.message)) {
|
|
|
|
return {
|
|
|
|
files:{},
|
|
|
|
commits:{total:0},
|
|
|
|
branches:{}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
throw err;
|
2017-11-23 01:27:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return fetchPromise.then(completeStatus).catch(function(e) {
|
2018-01-08 15:46:38 +01:00
|
|
|
// if (e.code !== 'git_auth_failed') {
|
|
|
|
// console.log("Fetch failed");
|
|
|
|
// console.log(e);
|
|
|
|
// }
|
2017-11-23 01:27:13 +01:00
|
|
|
return completeStatus(e);
|
2017-11-22 00:31:41 +01:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2017-12-06 23:39:30 +01:00
|
|
|
Project.prototype.push = function (user,remoteBranchName,setRemote) {
|
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
|
|
|
var remote = this.parseRemoteBranch(remoteBranchName||this.branches.remote);
|
|
|
|
return gitTools.push(this.path, remote.remote || this.currentRemote,remote.branch, setRemote, authCache.get(this.name,this.remotes[remote.remote || this.currentRemote].fetch,username));
|
2017-11-22 00:31:41 +01:00
|
|
|
};
|
|
|
|
|
2017-12-06 23:39:30 +01:00
|
|
|
Project.prototype.pull = function (user,remoteBranchName,setRemote) {
|
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
2017-11-23 21:52:15 +01:00
|
|
|
var self = this;
|
2017-11-22 00:31:41 +01:00
|
|
|
if (setRemote) {
|
|
|
|
return gitTools.setUpstream(this.path, remoteBranchName).then(function() {
|
2018-02-01 18:08:22 +01:00
|
|
|
self.currentRemote = self.parseRemoteBranch(remoteBranchName).remote;
|
|
|
|
return gitTools.pull(self.path, null, null, authCache.get(self.name,self.remotes[self.currentRemote].fetch,username));
|
2017-11-22 00:31:41 +01:00
|
|
|
})
|
|
|
|
} else {
|
2017-12-04 12:42:44 +01:00
|
|
|
var remote = this.parseRemoteBranch(remoteBranchName);
|
2018-02-01 18:08:22 +01:00
|
|
|
return gitTools.pull(this.path, remote.remote, remote.branch, authCache.get(this.name,this.remotes[remote.remote||self.currentRemote].fetch,username));
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Project.prototype.resolveMerge = function (file,resolutions) {
|
|
|
|
var filePath = fspath.join(this.path,file);
|
|
|
|
var self = this;
|
|
|
|
return fs.readFile(filePath,"utf8").then(function(content) {
|
|
|
|
var lines = content.split("\n");
|
|
|
|
var result = [];
|
|
|
|
var ignoreBlock = false;
|
|
|
|
var currentBlock;
|
|
|
|
for (var i=1;i<=lines.length;i++) {
|
|
|
|
if (resolutions.hasOwnProperty(i)) {
|
|
|
|
currentBlock = resolutions[i];
|
|
|
|
if (currentBlock.selection === "A") {
|
|
|
|
ignoreBlock = false;
|
|
|
|
} else {
|
|
|
|
ignoreBlock = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (currentBlock) {
|
|
|
|
if (currentBlock.separator === i) {
|
|
|
|
if (currentBlock.selection === "A") {
|
|
|
|
ignoreBlock = true;
|
|
|
|
} else {
|
|
|
|
ignoreBlock = false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else if (currentBlock.changeEnd === i) {
|
|
|
|
currentBlock = null;
|
|
|
|
continue;
|
|
|
|
} else if (ignoreBlock) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push(lines[i-1]);
|
|
|
|
}
|
|
|
|
var finalResult = result.join("\n");
|
|
|
|
return util.writeFile(filePath,finalResult).then(function() {
|
|
|
|
return self.stageFile(file);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Project.prototype.abortMerge = function () {
|
|
|
|
return gitTools.abortMerge(this.path);
|
|
|
|
};
|
|
|
|
|
2017-12-06 23:39:30 +01:00
|
|
|
Project.prototype.getBranches = function (user, isRemote) {
|
2017-11-23 01:27:13 +01:00
|
|
|
var self = this;
|
|
|
|
var fetchPromise;
|
2017-12-04 12:42:44 +01:00
|
|
|
if (isRemote) {
|
2017-12-06 23:39:30 +01:00
|
|
|
fetchPromise = self.fetch(user);
|
2017-11-23 01:27:13 +01:00
|
|
|
} else {
|
2017-12-08 17:31:42 +01:00
|
|
|
fetchPromise = Promise.resolve();
|
2017-11-23 01:27:13 +01:00
|
|
|
}
|
|
|
|
return fetchPromise.then(function() {
|
2017-12-04 12:42:44 +01:00
|
|
|
return gitTools.getBranches(self.path,isRemote);
|
2017-11-23 01:27:13 +01:00
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
};
|
2017-12-04 12:42:44 +01:00
|
|
|
|
2017-12-08 17:31:42 +01:00
|
|
|
Project.prototype.deleteBranch = function (user, branch, isRemote, force) {
|
|
|
|
// TODO: isRemote==true support
|
|
|
|
// TODO: make sure we don't try to delete active branch
|
|
|
|
return gitTools.deleteBranch(this.path,branch,isRemote, force);
|
|
|
|
};
|
|
|
|
|
2017-12-06 23:39:30 +01:00
|
|
|
Project.prototype.fetch = function(user,remoteName) {
|
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
var project = this;
|
|
|
|
if (remoteName) {
|
2017-12-06 23:39:30 +01:00
|
|
|
return gitTools.fetch(project.path,remoteName,authCache.get(project.name,project.remotes[remoteName].fetch,username)).catch(function(err) {
|
2017-12-04 12:42:44 +01:00
|
|
|
err.remote = remoteName;
|
|
|
|
throw err;
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
var remotes = Object.keys(this.remotes);
|
2017-12-08 17:31:42 +01:00
|
|
|
var promise = Promise.resolve();
|
2017-12-04 12:42:44 +01:00
|
|
|
remotes.forEach(function(remote) {
|
|
|
|
promise = promise.then(function() {
|
2017-12-06 23:39:30 +01:00
|
|
|
return gitTools.fetch(project.path,remote,authCache.get(project.name,project.remotes[remote].fetch,username))
|
2017-12-04 12:42:44 +01:00
|
|
|
}).catch(function(err) {
|
|
|
|
err.remote = remote;
|
|
|
|
throw err;
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 00:31:41 +01:00
|
|
|
Project.prototype.setBranch = function (branchName, isCreate) {
|
|
|
|
var self = this;
|
|
|
|
return gitTools.checkoutBranch(this.path, branchName, isCreate).then(function() {
|
|
|
|
return self.load();
|
|
|
|
})
|
|
|
|
};
|
|
|
|
Project.prototype.getBranchStatus = function (branchName) {
|
|
|
|
return gitTools.getBranchStatus(this.path,branchName);
|
|
|
|
};
|
2017-12-20 15:37:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Project.prototype.getRemotes = function (user) {
|
|
|
|
return gitTools.getRemotes(this.path).then(function(remotes) {
|
|
|
|
var result = [];
|
|
|
|
for (var name in remotes) {
|
|
|
|
if (remotes.hasOwnProperty(name)) {
|
|
|
|
remotes[name].name = name;
|
|
|
|
result.push(remotes[name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {remotes:result};
|
|
|
|
})
|
|
|
|
};
|
|
|
|
Project.prototype.addRemote = function(user,remote,options) {
|
|
|
|
var project = this;
|
|
|
|
return gitTools.addRemote(this.path,remote,options).then(function() {
|
|
|
|
return project.loadRemotes()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Project.prototype.updateRemote = function(user,remote,options) {
|
2017-12-21 18:40:24 +01:00
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.auth) {
|
|
|
|
var url = this.remotes[remote].fetch;
|
|
|
|
if (options.auth.keyFile) {
|
2018-01-18 23:17:48 +01:00
|
|
|
options.auth.key_path = sshKeys.getPrivateKeyPath(getSSHKeyUsername(user), options.auth.keyFile);
|
2017-12-21 18:40:24 +01:00
|
|
|
}
|
|
|
|
authCache.set(this.name,url,username,options.auth);
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
2017-12-20 15:37:34 +01:00
|
|
|
}
|
|
|
|
Project.prototype.removeRemote = function(user, remote) {
|
|
|
|
// TODO: if this was the last remote using this url, then remove the authCache
|
|
|
|
// details.
|
|
|
|
var project = this;
|
|
|
|
return gitTools.removeRemote(this.path,remote).then(function() {
|
|
|
|
return project.loadRemotes()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
Project.prototype.getFlowFile = function() {
|
2018-01-18 23:17:48 +01:00
|
|
|
// console.log("Project.getFlowFile = ",this.paths.flowFile);
|
2017-10-19 22:38:53 +02:00
|
|
|
if (this.paths.flowFile) {
|
|
|
|
return fspath.join(this.path,this.paths.flowFile);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
2017-11-23 01:27:13 +01:00
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
Project.prototype.getFlowFileBackup = function() {
|
2018-01-25 12:14:16 +01:00
|
|
|
var flowFile = this.getFlowFile();
|
|
|
|
if (flowFile) {
|
|
|
|
return getBackupFilename(flowFile);
|
|
|
|
}
|
|
|
|
return null;
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
Project.prototype.getCredentialsFile = function() {
|
2018-01-18 23:17:48 +01:00
|
|
|
// console.log("Project.getCredentialsFile = ",this.paths.credentialsFile);
|
2017-10-19 22:38:53 +02:00
|
|
|
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,
|
2017-11-22 00:31:41 +01:00
|
|
|
dependencies: this.package.dependencies||{},
|
2018-01-08 15:46:38 +01:00
|
|
|
empty: this.empty,
|
2017-10-17 11:14:50 +02:00
|
|
|
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-11-22 00:31:41 +01:00
|
|
|
},
|
2017-12-04 12:42:44 +01:00
|
|
|
git: {
|
|
|
|
remotes: this.remotes,
|
|
|
|
branches: this.branches
|
|
|
|
}
|
2017-10-17 11:14:50 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-25 16:26:24 +02:00
|
|
|
|
|
|
|
function getCredentialsFilename(filename) {
|
|
|
|
// TODO: DRY - ./index.js
|
|
|
|
var ffDir = fspath.dirname(filename);
|
|
|
|
var ffExt = fspath.extname(filename);
|
|
|
|
var ffBase = fspath.basename(filename,ffExt);
|
|
|
|
return fspath.join(ffDir,ffBase+"_cred"+ffExt);
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
function getBackupFilename(filename) {
|
2017-10-25 16:26:24 +02:00
|
|
|
// TODO: DRY - ./index.js
|
2017-10-17 00:23:50 +02:00
|
|
|
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) {
|
2018-01-24 22:54:18 +01:00
|
|
|
var e = new Error("Project not found: "+project);
|
2017-10-17 00:23:50 +02:00
|
|
|
e.code = "project_not_found";
|
2018-01-24 22:54:18 +01:00
|
|
|
e.project = project;
|
2017-10-17 00:23:50 +02:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createProjectDirectory(project) {
|
|
|
|
var projectPath = fspath.join(projectsDir,project);
|
|
|
|
return fs.ensureDir(projectPath);
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:28:26 +01:00
|
|
|
function deleteProjectDirectory(project) {
|
|
|
|
var projectPath = fspath.join(projectsDir,project);
|
|
|
|
return fs.remove(projectPath);
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function createDefaultProject(user, project) {
|
2017-10-17 00:23:50 +02:00
|
|
|
var projectPath = fspath.join(projectsDir,project.name);
|
|
|
|
// Create a basic skeleton of a project
|
|
|
|
return gitTools.initRepo(projectPath).then(function() {
|
|
|
|
var promises = [];
|
2017-12-19 01:56:02 +01:00
|
|
|
var files = Object.keys(defaultFileSet);
|
|
|
|
if (project.files) {
|
|
|
|
if (project.files.flow && !/\.\./.test(project.files.flow)) {
|
|
|
|
var flowFilePath;
|
|
|
|
var credsFilePath;
|
|
|
|
|
2018-01-25 11:13:07 +01:00
|
|
|
if (project.migrateFiles) {
|
2018-01-08 15:46:38 +01:00
|
|
|
var baseFlowFileName = project.files.flow || fspath.basename(project.files.oldFlow);
|
|
|
|
var baseCredentialFileName = project.files.credentials || fspath.basename(project.files.oldCredentials);
|
2017-12-19 01:56:02 +01:00
|
|
|
files.push(baseFlowFileName);
|
|
|
|
files.push(baseCredentialFileName);
|
|
|
|
flowFilePath = fspath.join(projectPath,baseFlowFileName);
|
|
|
|
credsFilePath = fspath.join(projectPath,baseCredentialFileName);
|
2018-01-31 22:31:45 +01:00
|
|
|
if (fs.existsSync(project.files.oldFlow)) {
|
|
|
|
log.trace("Migrating "+project.files.oldFlow+" to "+flowFilePath);
|
|
|
|
promises.push(fs.copy(project.files.oldFlow,flowFilePath));
|
|
|
|
} else {
|
|
|
|
log.trace(project.files.oldFlow+" does not exist - creating blank file");
|
|
|
|
promises.push(util.writeFile(flowFilePath,"[]"));
|
|
|
|
}
|
2018-01-08 15:46:38 +01:00
|
|
|
log.trace("Migrating "+project.files.oldCredentials+" to "+credsFilePath);
|
2017-12-19 01:56:02 +01:00
|
|
|
runtime.nodes.setCredentialSecret(project.credentialSecret);
|
|
|
|
promises.push(runtime.nodes.exportCredentials().then(function(creds) {
|
|
|
|
var credentialData;
|
|
|
|
if (settings.flowFilePretty) {
|
|
|
|
credentialData = JSON.stringify(creds,null,4);
|
|
|
|
} else {
|
|
|
|
credentialData = JSON.stringify(creds);
|
|
|
|
}
|
|
|
|
return util.writeFile(credsFilePath,credentialData);
|
|
|
|
}));
|
2018-01-25 11:13:07 +01:00
|
|
|
delete project.migrateFiles;
|
2017-12-19 01:56:02 +01:00
|
|
|
project.files.flow = baseFlowFileName;
|
|
|
|
project.files.credentials = baseCredentialFileName;
|
|
|
|
} else {
|
|
|
|
project.files.credentials = project.files.credentials || getCredentialsFilename(project.files.flow);
|
|
|
|
files.push(project.files.flow);
|
|
|
|
files.push(project.files.credentials);
|
|
|
|
flowFilePath = fspath.join(projectPath,project.files.flow);
|
|
|
|
credsFilePath = getCredentialsFilename(flowFilePath);
|
|
|
|
promises.push(util.writeFile(flowFilePath,"[]"));
|
|
|
|
promises.push(util.writeFile(credsFilePath,"{}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
for (var file in defaultFileSet) {
|
|
|
|
if (defaultFileSet.hasOwnProperty(file)) {
|
|
|
|
promises.push(util.writeFile(fspath.join(projectPath,file),defaultFileSet[file](project)));
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 01:56:02 +01:00
|
|
|
|
2017-10-17 11:14:50 +02:00
|
|
|
return when.all(promises).then(function() {
|
2017-10-25 16:26:24 +02:00
|
|
|
return gitTools.stageFile(projectPath,files);
|
2017-10-17 11:14:50 +02:00
|
|
|
}).then(function() {
|
2018-01-24 16:51:11 +01:00
|
|
|
return gitTools.commit(projectPath,"Create project",getGitUser(user));
|
2017-10-17 11:14:50 +02:00
|
|
|
})
|
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;
|
|
|
|
// }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function createProject(user, metadata) {
|
2017-12-06 23:39:30 +01:00
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
var project = metadata.name;
|
2017-12-08 17:31:42 +01:00
|
|
|
return new Promise(function(resolve,reject) {
|
2017-10-17 00:23:50 +02:00
|
|
|
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');
|
2018-01-23 12:26:05 +01:00
|
|
|
if (!projects) {
|
|
|
|
projects = {
|
|
|
|
projects:{}
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
projects.projects[project] = {};
|
2017-10-25 16:26:24 +02:00
|
|
|
if (metadata.hasOwnProperty('credentialSecret')) {
|
2017-10-17 00:23:50 +02:00
|
|
|
projects.projects[project].credentialSecret = metadata.credentialSecret;
|
|
|
|
}
|
|
|
|
return settings.set('projects',projects);
|
|
|
|
}).then(function() {
|
2017-12-04 12:42:44 +01:00
|
|
|
if (metadata.git && metadata.git.remotes && metadata.git.remotes.origin) {
|
|
|
|
var originRemote = metadata.git.remotes.origin;
|
2017-11-23 01:27:13 +01:00
|
|
|
var auth;
|
2017-12-04 12:42:44 +01:00
|
|
|
if (originRemote.hasOwnProperty("username") && originRemote.hasOwnProperty("password")) {
|
2017-12-06 23:39:30 +01:00
|
|
|
authCache.set(project,originRemote.url,username,{ // TODO: hardcoded remote name
|
2017-12-04 12:42:44 +01:00
|
|
|
username: originRemote.username,
|
|
|
|
password: originRemote.password
|
2017-11-23 01:27:13 +01:00
|
|
|
}
|
2017-11-25 00:12:35 +01:00
|
|
|
);
|
2017-12-06 23:39:30 +01:00
|
|
|
auth = authCache.get(project,originRemote.url,username);
|
2017-11-23 01:27:13 +01:00
|
|
|
}
|
2018-01-11 12:19:04 +01:00
|
|
|
else if (originRemote.hasOwnProperty("keyFile") && originRemote.hasOwnProperty("passphrase")) {
|
2017-12-15 16:07:47 +01:00
|
|
|
authCache.set(project,originRemote.url,username,{ // TODO: hardcoded remote name
|
2018-01-18 23:17:48 +01:00
|
|
|
key_path: sshKeys.getPrivateKeyPath(getSSHKeyUsername(user), originRemote.keyFile),
|
2017-12-15 16:07:47 +01:00
|
|
|
passphrase: originRemote.passphrase
|
|
|
|
}
|
|
|
|
);
|
|
|
|
auth = authCache.get(project,originRemote.url,username);
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
return gitTools.clone(originRemote,auth,projectPath).then(function(result) {
|
2017-10-17 00:23:50 +02:00
|
|
|
// 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);
|
|
|
|
// });
|
2018-01-08 15:46:38 +01:00
|
|
|
// return gitTools.getFiles(projectPath).then(function() {
|
|
|
|
// // It wasn't an empty repository.
|
|
|
|
// // TODO: check for required files - checkProjectFiles
|
|
|
|
//
|
|
|
|
// }).catch(function(err) {
|
|
|
|
// if (/ambiguous argument/.test(err.message)) {
|
|
|
|
// // Empty repository
|
|
|
|
// err.code = "project_empty";
|
|
|
|
// err.message = "Project is empty";
|
|
|
|
// }
|
|
|
|
// throw err;
|
|
|
|
// });
|
2017-10-17 00:23:50 +02:00
|
|
|
resolve(getProject(project));
|
|
|
|
}).catch(function(error) {
|
|
|
|
fs.remove(projectPath,function() {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
} else {
|
2017-12-04 12:42:44 +01:00
|
|
|
createDefaultProject(user, metadata).then(function() { resolve(getProject(project))}).catch(reject);
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
}).catch(reject);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:28:26 +01:00
|
|
|
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() {
|
|
|
|
var projects = settings.get('projects');
|
|
|
|
delete projects.projects[name];
|
|
|
|
return settings.set('projects', projects);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
var currentProject;
|
|
|
|
|
|
|
|
function getProject(name) {
|
|
|
|
return checkProjectExists(name).then(function() {
|
|
|
|
if (currentProject && currentProject.name === name) {
|
|
|
|
return currentProject;
|
|
|
|
}
|
|
|
|
currentProject = new Project(name);
|
|
|
|
return currentProject.load();
|
2017-11-22 00:31:41 +01:00
|
|
|
});
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function listProjects() {
|
|
|
|
return fs.readdir(projectsDir).then(function(fns) {
|
|
|
|
var dirs = [];
|
2018-01-10 18:37:41 +01:00
|
|
|
fns.sort(function(A,B) {
|
|
|
|
return A.toLowerCase().localeCompare(B.toLowerCase());
|
|
|
|
}).filter(function(fn) {
|
2017-10-17 00:23:50 +02:00
|
|
|
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");
|
2018-01-17 00:04:39 +01:00
|
|
|
authCache.init();
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
get: getProject,
|
|
|
|
create: createProject,
|
2017-12-07 15:28:26 +01:00
|
|
|
delete: deleteProject,
|
2017-10-17 00:23:50 +02:00
|
|
|
list: listProjects
|
|
|
|
|
|
|
|
}
|