2017-09-20 11:30:07 +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 nodeFn = require('when/node/function');
|
|
|
|
var crypto = require('crypto');
|
|
|
|
|
|
|
|
var storageSettings = require("../settings");
|
|
|
|
var util = require("../util");
|
|
|
|
var gitTools = require("./git");
|
2018-01-19 00:13:55 +01:00
|
|
|
var sshTools = require("./ssh");
|
2017-09-20 11:30:07 +02:00
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
var Projects = require("./Project");
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
var settings;
|
|
|
|
var runtime;
|
2018-01-16 17:18:18 +01:00
|
|
|
var log;
|
2017-09-20 11:30:07 +02:00
|
|
|
|
2018-01-23 00:14:38 +01:00
|
|
|
var projectsEnabled = false;
|
2017-12-13 10:44:48 +01:00
|
|
|
var projectLogMessages = [];
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
var projectsDir;
|
2017-10-17 00:23:50 +02:00
|
|
|
var activeProject
|
2017-09-20 11:30:07 +02:00
|
|
|
|
2018-01-24 22:05:48 +01:00
|
|
|
var globalGitUser = false;
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
function init(_settings, _runtime) {
|
|
|
|
settings = _settings;
|
|
|
|
runtime = _runtime;
|
|
|
|
log = runtime.log;
|
2017-10-17 00:23:50 +02:00
|
|
|
|
2017-12-13 10:44:48 +01:00
|
|
|
try {
|
2018-01-23 00:14:38 +01:00
|
|
|
if (settings.editorTheme.projects.enabled === true) {
|
|
|
|
projectsEnabled = true;
|
|
|
|
} else if (settings.editorTheme.projects.enabled === false) {
|
2017-12-13 10:44:48 +01:00
|
|
|
projectLogMessages.push(log._("storage.localfilesystem.projects.disabled"))
|
|
|
|
}
|
|
|
|
} catch(err) {
|
2018-01-23 00:14:38 +01:00
|
|
|
projectLogMessages.push(log._("storage.localfilesystem.projects.disabledNoFlag"))
|
|
|
|
projectsEnabled = false;
|
2017-12-13 10:44:48 +01:00
|
|
|
}
|
2017-09-20 11:30:07 +02:00
|
|
|
|
|
|
|
if (settings.flowFile) {
|
|
|
|
flowsFile = settings.flowFile;
|
|
|
|
// handle Unix and Windows "C:\"
|
|
|
|
if ((flowsFile[0] == "/") || (flowsFile[1] == ":")) {
|
|
|
|
// Absolute path
|
|
|
|
flowsFullPath = flowsFile;
|
|
|
|
} else if (flowsFile.substring(0,2) === "./") {
|
|
|
|
// Relative to cwd
|
|
|
|
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fs.statSync(fspath.join(process.cwd(),flowsFile));
|
|
|
|
// Found in cwd
|
|
|
|
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
|
|
|
} catch(err) {
|
|
|
|
// Use userDir
|
|
|
|
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
flowsFile = 'flows_'+require('os').hostname()+'.json';
|
|
|
|
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
|
|
|
}
|
|
|
|
var ffExt = fspath.extname(flowsFullPath);
|
|
|
|
var ffBase = fspath.basename(flowsFullPath,ffExt);
|
|
|
|
|
|
|
|
flowsFileBackup = getBackupFilename(flowsFullPath);
|
|
|
|
credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
|
|
|
|
credentialsFileBackup = getBackupFilename(credentialsFile)
|
|
|
|
|
2017-12-13 10:44:48 +01:00
|
|
|
var setupProjectsPromise;
|
|
|
|
|
|
|
|
if (projectsEnabled) {
|
2018-01-19 00:13:55 +01:00
|
|
|
return sshTools.init(settings,runtime).then(function() {
|
2018-01-24 22:05:48 +01:00
|
|
|
gitTools.init(_settings, _runtime).then(function(gitConfig) {
|
2018-02-02 12:37:18 +01:00
|
|
|
if (!gitConfig || /^1\./.test(gitConfig.version)) {
|
|
|
|
if (!gitConfig) {
|
|
|
|
projectLogMessages.push(log._("storage.localfilesystem.projects.git-not-found"))
|
|
|
|
} else {
|
|
|
|
projectLogMessages.push(log._("storage.localfilesystem.projects.git-version-old",{version:gitConfig.version}))
|
|
|
|
}
|
2018-01-19 00:13:55 +01:00
|
|
|
projectsEnabled = false;
|
2018-02-01 21:35:18 +01:00
|
|
|
try {
|
|
|
|
// As projects have to be turned on, we know this property
|
|
|
|
// must exist at this point, so turn it off.
|
|
|
|
// TODO: when on-by-default, this will need to do more
|
|
|
|
// work to disable.
|
|
|
|
settings.editorTheme.projects.enabled = false;
|
|
|
|
} catch(err) {
|
|
|
|
}
|
2018-01-19 00:13:55 +01:00
|
|
|
} else {
|
2018-01-24 22:05:48 +01:00
|
|
|
globalGitUser = gitConfig.user;
|
2018-01-19 00:13:55 +01:00
|
|
|
Projects.init(settings,runtime);
|
|
|
|
sshTools.init(settings,runtime);
|
|
|
|
projectsDir = fspath.join(settings.userDir,"projects");
|
|
|
|
if (!settings.readOnly) {
|
|
|
|
return fs.ensureDir(projectsDir)
|
|
|
|
//TODO: this is accessing settings from storage directly as settings
|
|
|
|
// has not yet been initialised. That isn't ideal - can this be deferred?
|
|
|
|
.then(storageSettings.getSettings)
|
|
|
|
.then(function(globalSettings) {
|
|
|
|
var saveSettings = false;
|
|
|
|
if (!globalSettings.projects) {
|
|
|
|
globalSettings.projects = {
|
|
|
|
projects: {}
|
|
|
|
}
|
2018-01-08 17:10:54 +01:00
|
|
|
saveSettings = true;
|
2018-01-19 00:13:55 +01:00
|
|
|
} else {
|
|
|
|
activeProject = globalSettings.projects.activeProject;
|
2018-01-08 17:10:54 +01:00
|
|
|
}
|
2018-01-19 00:13:55 +01:00
|
|
|
if (settings.flowFile) {
|
|
|
|
if (globalSettings.projects.projects.hasOwnProperty(settings.flowFile)) {
|
|
|
|
activeProject = settings.flowFile;
|
|
|
|
globalSettings.projects.activeProject = settings.flowFile;
|
|
|
|
saveSettings = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!activeProject) {
|
|
|
|
projectLogMessages.push(log._("storage.localfilesystem.no-active-project"))
|
|
|
|
}
|
|
|
|
if (saveSettings) {
|
|
|
|
return storageSettings.saveSettings(globalSettings);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2018-01-19 00:13:55 +01:00
|
|
|
});
|
2017-12-13 10:44:48 +01:00
|
|
|
});
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2017-12-13 10:44:48 +01:00
|
|
|
return Promise.resolve();
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function getUserGitSettings(user) {
|
|
|
|
var userSettings = settings.getUserSettings(user)||{};
|
|
|
|
return userSettings.git;
|
|
|
|
}
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
function getBackupFilename(filename) {
|
|
|
|
var ffName = fspath.basename(filename);
|
|
|
|
var ffDir = fspath.dirname(filename);
|
|
|
|
return fspath.join(ffDir,"."+ffName+".backup");
|
|
|
|
}
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
function loadProject(name) {
|
|
|
|
return Projects.get(name).then(function(project) {
|
|
|
|
activeProject = project;
|
|
|
|
flowsFullPath = project.getFlowFile();
|
|
|
|
flowsFileBackup = project.getFlowFileBackup();
|
|
|
|
credentialsFile = project.getCredentialsFile();
|
|
|
|
credentialsFileBackup = project.getCredentialsFileBackup();
|
|
|
|
return project;
|
2017-09-20 11:30:07 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function listProjects(user) {
|
|
|
|
return Projects.list();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProject(user, name) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(name);
|
2017-10-17 00:23:50 +02:00
|
|
|
//return when.resolve(activeProject.info);
|
2017-12-04 12:42:44 +01:00
|
|
|
var username;
|
|
|
|
if (!user) {
|
|
|
|
username = "_";
|
|
|
|
} else {
|
|
|
|
username = user.username;
|
|
|
|
}
|
|
|
|
return Projects.get(name).then(function(project) {
|
2017-12-17 00:43:08 +01:00
|
|
|
return project.toJSON();
|
2017-12-04 12:42:44 +01:00
|
|
|
});
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:28:26 +01:00
|
|
|
function deleteProject(user, name) {
|
|
|
|
return Projects.delete(user, name);
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:14:50 +02:00
|
|
|
function checkActiveProject(project) {
|
|
|
|
if (!activeProject || activeProject.name !== project) {
|
|
|
|
//TODO: throw better err
|
2017-11-22 00:31:41 +01:00
|
|
|
throw new Error("Cannot operate on inactive project wanted:"+project+" current:"+(activeProject&&activeProject.name));
|
2017-10-17 11:14:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function getFiles(user, project) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(project);
|
2017-10-17 00:23:50 +02:00
|
|
|
return activeProject.getFiles();
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function stageFile(user, project,file) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(project);
|
2017-10-17 00:23:50 +02:00
|
|
|
return activeProject.stageFile(file);
|
2017-10-07 01:18:20 +02:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function unstageFile(user, project,file) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(project);
|
2017-10-17 00:23:50 +02:00
|
|
|
return activeProject.unstageFile(file);
|
2017-10-07 01:18:20 +02:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function commit(user, project,options) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(project);
|
2017-12-04 12:42:44 +01:00
|
|
|
return activeProject.commit(user, options);
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function getFileDiff(user, project,file,type) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(project);
|
2017-10-17 00:23:50 +02:00
|
|
|
return activeProject.getFileDiff(file,type);
|
2017-10-09 01:11:07 +02:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function getCommits(user, project,options) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(project);
|
2017-12-11 18:05:12 +01:00
|
|
|
return activeProject.getCommits(options);
|
2017-10-10 00:37:19 +02:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function getCommit(user, project,sha) {
|
2017-10-17 11:14:50 +02:00
|
|
|
checkActiveProject(project);
|
2017-10-17 00:23:50 +02:00
|
|
|
return activeProject.getCommit(sha);
|
2017-10-10 00:37:19 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function getFile(user, project,filePath,sha) {
|
2017-10-25 16:26:24 +02:00
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.getFile(filePath,sha);
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2017-12-11 18:05:12 +01:00
|
|
|
function revertFile(user, project,filePath) {
|
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.revertFile(filePath).then(function() {
|
|
|
|
return reloadActiveProject("revert");
|
|
|
|
})
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function push(user, project,remoteBranchName,setRemote) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
2017-12-06 23:39:30 +01:00
|
|
|
return activeProject.push(user,remoteBranchName,setRemote);
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function pull(user, project,remoteBranchName,setRemote) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
2017-12-06 23:39:30 +01:00
|
|
|
return activeProject.pull(user,remoteBranchName,setRemote).then(function() {
|
2017-11-25 00:12:35 +01:00
|
|
|
return reloadActiveProject("pull");
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function getStatus(user, project) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
2017-12-06 23:39:30 +01:00
|
|
|
return activeProject.status(user);
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function resolveMerge(user, project,file,resolution) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.resolveMerge(file,resolution);
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function abortMerge(user, project) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
2017-11-25 00:12:35 +01:00
|
|
|
return activeProject.abortMerge().then(function() {
|
|
|
|
return reloadActiveProject("abort-merge")
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function getBranches(user, project,isRemote) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
2017-12-06 23:39:30 +01:00
|
|
|
return activeProject.getBranches(user, isRemote);
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-12-08 17:31:42 +01:00
|
|
|
|
|
|
|
function deleteBranch(user, project, branch, isRemote, force) {
|
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.deleteBranch(user, branch, isRemote, force);
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function setBranch(user, project,branchName,isCreate) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
2017-11-25 00:12:35 +01:00
|
|
|
return activeProject.setBranch(branchName,isCreate).then(function() {
|
|
|
|
return reloadActiveProject("change-branch");
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function getBranchStatus(user, project,branchName) {
|
2017-11-22 00:31:41 +01:00
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.getBranchStatus(branchName);
|
|
|
|
}
|
2017-12-20 15:37:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
function getRemotes(user, project) {
|
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.getRemotes(user);
|
|
|
|
}
|
|
|
|
function addRemote(user, project, options) {
|
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.addRemote(user, options.name, options);
|
|
|
|
}
|
|
|
|
function removeRemote(user, project, remote) {
|
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.removeRemote(user, remote);
|
|
|
|
}
|
2017-12-21 18:40:24 +01:00
|
|
|
function updateRemote(user, project, remote, body) {
|
|
|
|
checkActiveProject(project);
|
|
|
|
return activeProject.updateRemote(user, remote, body);
|
|
|
|
}
|
2017-12-20 15:37:34 +01:00
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
function getActiveProject(user) {
|
2017-09-20 11:30:07 +02:00
|
|
|
return activeProject;
|
|
|
|
}
|
|
|
|
|
2017-11-25 00:12:35 +01:00
|
|
|
function reloadActiveProject(action) {
|
2017-09-20 11:30:07 +02:00
|
|
|
return runtime.nodes.stopFlows().then(function() {
|
|
|
|
return runtime.nodes.loadFlows(true).then(function() {
|
2017-11-25 00:12:35 +01:00
|
|
|
runtime.events.emit("runtime-event",{id:"project-update", payload:{ project: activeProject.name, action:action}});
|
2017-10-07 01:18:20 +02:00
|
|
|
}).catch(function(err) {
|
2017-09-20 11:30:07 +02:00
|
|
|
// We're committed to the project change now, so notify editors
|
|
|
|
// that it has changed.
|
2017-11-25 00:12:35 +01:00
|
|
|
runtime.events.emit("runtime-event",{id:"project-update", payload:{ project: activeProject.name, action:action}});
|
2017-09-20 11:30:07 +02:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function createProject(user, metadata) {
|
|
|
|
// var userSettings = getUserGitSettings(user);
|
2018-01-25 11:13:07 +01:00
|
|
|
if (metadata.files && metadata.migrateFiles) {
|
2017-12-19 01:56:02 +01:00
|
|
|
// We expect there to be no active project in this scenario
|
|
|
|
if (activeProject) {
|
|
|
|
throw new Error("Cannot migrate as there is an active project");
|
|
|
|
}
|
|
|
|
var currentEncryptionKey = settings.get('credentialSecret');
|
|
|
|
if (currentEncryptionKey === undefined) {
|
|
|
|
currentEncryptionKey = settings.get('_credentialSecret');
|
|
|
|
}
|
|
|
|
if (!metadata.hasOwnProperty('credentialSecret')) {
|
|
|
|
metadata.credentialSecret = currentEncryptionKey;
|
|
|
|
}
|
2018-01-08 15:46:38 +01:00
|
|
|
if (!metadata.files.flow) {
|
|
|
|
metadata.files.flow = fspath.basename(flowsFullPath);
|
|
|
|
}
|
|
|
|
if (!metadata.files.credentials) {
|
|
|
|
metadata.files.credentials = fspath.basename(credentialsFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata.files.oldFlow = flowsFullPath;
|
|
|
|
metadata.files.oldCredentials = credentialsFile;
|
2017-12-19 01:56:02 +01:00
|
|
|
metadata.files.credentialSecret = currentEncryptionKey;
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
return Projects.create(null,metadata).then(function(p) {
|
|
|
|
return setActiveProject(user, p.name);
|
2017-10-17 11:14:50 +02:00
|
|
|
}).then(function() {
|
2017-12-04 12:42:44 +01:00
|
|
|
return getProject(user, metadata.name);
|
2017-10-17 00:23:50 +02:00
|
|
|
})
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function setActiveProject(user, projectName) {
|
2017-10-17 00:23:50 +02:00
|
|
|
return loadProject(projectName).then(function(project) {
|
2017-09-20 11:30:07 +02:00
|
|
|
var globalProjectSettings = settings.get("projects");
|
2017-10-17 00:23:50 +02:00
|
|
|
globalProjectSettings.activeProject = project.name;
|
2017-09-20 11:30:07 +02:00
|
|
|
return settings.set("projects",globalProjectSettings).then(function() {
|
2017-12-19 01:56:02 +01:00
|
|
|
log.info(log._("storage.localfilesystem.projects.changing-project",{project:(activeProject&&activeProject.name)||"none"}));
|
2017-09-20 11:30:07 +02:00
|
|
|
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
|
2017-10-17 00:23:50 +02:00
|
|
|
// console.log("Updated file targets to");
|
|
|
|
// console.log(flowsFullPath)
|
|
|
|
// console.log(credentialsFile)
|
2017-11-25 00:12:35 +01:00
|
|
|
return reloadActiveProject("loaded");
|
2017-09-20 11:30:07 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2018-01-08 15:46:38 +01:00
|
|
|
|
|
|
|
function initialiseProject(user, project, data) {
|
|
|
|
if (!activeProject || activeProject.name !== project) {
|
|
|
|
// TODO standardise
|
|
|
|
throw new Error("Cannot initialise inactive project");
|
|
|
|
}
|
|
|
|
return activeProject.initialise(user,data).then(function(result) {
|
|
|
|
flowsFullPath = activeProject.getFlowFile();
|
|
|
|
flowsFileBackup = activeProject.getFlowFileBackup();
|
|
|
|
credentialsFile = activeProject.getCredentialsFile();
|
|
|
|
credentialsFileBackup = activeProject.getCredentialsFileBackup();
|
|
|
|
runtime.nodes.setCredentialSecret(activeProject.credentialSecret);
|
|
|
|
return reloadActiveProject("updated");
|
|
|
|
});
|
|
|
|
}
|
2017-12-04 12:42:44 +01:00
|
|
|
function updateProject(user, project, data) {
|
2017-10-17 00:23:50 +02:00
|
|
|
if (!activeProject || activeProject.name !== project) {
|
|
|
|
// TODO standardise
|
|
|
|
throw new Error("Cannot update inactive project");
|
|
|
|
}
|
2017-10-19 22:38:53 +02:00
|
|
|
// In case this triggers a credential secret change
|
|
|
|
var isReset = data.resetCredentialSecret;
|
|
|
|
var wasInvalid = activeProject.credentialSecretInvalid;
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
return activeProject.update(user,data).then(function(result) {
|
2017-10-19 22:38:53 +02:00
|
|
|
|
|
|
|
if (result.flowFilesChanged) {
|
|
|
|
flowsFullPath = activeProject.getFlowFile();
|
|
|
|
flowsFileBackup = activeProject.getFlowFileBackup();
|
|
|
|
credentialsFile = activeProject.getCredentialsFile();
|
|
|
|
credentialsFileBackup = activeProject.getCredentialsFileBackup();
|
2017-11-25 00:12:35 +01:00
|
|
|
return reloadActiveProject("updated");
|
2017-10-19 22:38:53 +02:00
|
|
|
} else if (result.credentialSecretChanged) {
|
|
|
|
if (isReset || !wasInvalid) {
|
|
|
|
if (isReset) {
|
|
|
|
runtime.nodes.clearCredentials();
|
|
|
|
}
|
|
|
|
runtime.nodes.setCredentialSecret(activeProject.credentialSecret);
|
|
|
|
return runtime.nodes.exportCredentials()
|
|
|
|
.then(runtime.storage.saveCredentials)
|
|
|
|
.then(function() {
|
|
|
|
if (wasInvalid) {
|
2017-11-25 00:12:35 +01:00
|
|
|
return reloadActiveProject("updated");
|
2017-10-19 22:38:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (wasInvalid) {
|
2017-11-25 00:12:35 +01:00
|
|
|
return reloadActiveProject("updated");
|
2017-10-19 22:38:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
function setCredentialSecret(data) { //existingSecret,secret) {
|
|
|
|
var isReset = data.resetCredentialSecret;
|
2017-10-17 11:14:50 +02:00
|
|
|
var wasInvalid = activeProject.credentialSecretInvalid;
|
2017-10-17 00:23:50 +02:00
|
|
|
return activeProject.update(data).then(function() {
|
|
|
|
if (isReset || !wasInvalid) {
|
|
|
|
if (isReset) {
|
|
|
|
runtime.nodes.clearCredentials();
|
|
|
|
}
|
|
|
|
runtime.nodes.setCredentialSecret(activeProject.credentialSecret);
|
|
|
|
return runtime.nodes.exportCredentials()
|
|
|
|
.then(runtime.storage.saveCredentials)
|
|
|
|
.then(function() {
|
|
|
|
if (wasInvalid) {
|
2017-11-25 00:12:35 +01:00
|
|
|
return reloadActiveProject("updated");
|
2017-10-17 00:23:50 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (wasInvalid) {
|
2017-11-25 00:12:35 +01:00
|
|
|
return reloadActiveProject("updated");
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
})
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 00:23:50 +02:00
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
var initialFlowLoadComplete = false;
|
|
|
|
|
|
|
|
var flowsFile;
|
|
|
|
var flowsFullPath;
|
|
|
|
var flowsFileBackup;
|
|
|
|
var credentialsFile;
|
|
|
|
var credentialsFileBackup;
|
|
|
|
|
|
|
|
function getFlows() {
|
|
|
|
if (!initialFlowLoadComplete) {
|
|
|
|
initialFlowLoadComplete = true;
|
|
|
|
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
|
2017-10-07 01:18:20 +02:00
|
|
|
if (activeProject) {
|
2018-01-24 22:54:18 +01:00
|
|
|
// At this point activeProject will be a string, so go load it and
|
|
|
|
// swap in an instance of Project
|
2017-10-17 00:23:50 +02:00
|
|
|
return loadProject(activeProject).then(function() {
|
2017-12-13 10:44:48 +01:00
|
|
|
log.info(log._("storage.localfilesystem.projects.active-project",{project:activeProject.name||"none"}));
|
2017-10-17 00:23:50 +02:00
|
|
|
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
|
|
|
|
return getFlows();
|
|
|
|
});
|
|
|
|
} else {
|
2017-12-13 10:44:48 +01:00
|
|
|
if (projectsEnabled) {
|
|
|
|
log.warn(log._("storage.localfilesystem.projects.no-active-project"))
|
|
|
|
} else {
|
|
|
|
projectLogMessages.forEach(log.warn);
|
|
|
|
}
|
2017-10-17 00:23:50 +02:00
|
|
|
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
|
2017-10-07 01:18:20 +02:00
|
|
|
}
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2017-10-19 22:38:53 +02:00
|
|
|
if (activeProject) {
|
2018-01-08 15:46:38 +01:00
|
|
|
var error;
|
|
|
|
if (activeProject.isEmpty()) {
|
|
|
|
log.warn("Project repository is empty");
|
|
|
|
error = new Error("Project repository is empty");
|
|
|
|
error.code = "project_empty";
|
|
|
|
return when.reject(error);
|
|
|
|
}
|
2017-10-19 22:38:53 +02:00
|
|
|
if (!activeProject.getFlowFile()) {
|
2018-01-08 15:46:38 +01:00
|
|
|
log.warn("Project has no flow file");
|
|
|
|
error = new Error("Project has no flow file");
|
2017-10-19 22:38:53 +02:00
|
|
|
error.code = "missing_flow_file";
|
|
|
|
return when.reject(error);
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 11:30:07 +02:00
|
|
|
return util.readFile(flowsFullPath,flowsFileBackup,[],'flow');
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveFlows(flows) {
|
|
|
|
if (settings.readOnly) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.renameSync(flowsFullPath,flowsFileBackup);
|
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
|
|
|
|
var flowData;
|
|
|
|
|
|
|
|
if (settings.flowFilePretty) {
|
|
|
|
flowData = JSON.stringify(flows,null,4);
|
|
|
|
} else {
|
|
|
|
flowData = JSON.stringify(flows);
|
|
|
|
}
|
|
|
|
return util.writeFile(flowsFullPath, flowData);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCredentials() {
|
|
|
|
return util.readFile(credentialsFile,credentialsFileBackup,{},'credentials');
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveCredentials(credentials) {
|
|
|
|
if (settings.readOnly) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.renameSync(credentialsFile,credentialsFileBackup);
|
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
var credentialData;
|
|
|
|
if (settings.flowFilePretty) {
|
|
|
|
credentialData = JSON.stringify(credentials,null,4);
|
|
|
|
} else {
|
|
|
|
credentialData = JSON.stringify(credentials);
|
|
|
|
}
|
|
|
|
return util.writeFile(credentialsFile, credentialData);
|
|
|
|
}
|
|
|
|
|
2018-01-08 15:46:38 +01:00
|
|
|
function getFlowFilename() {
|
|
|
|
if (flowsFullPath) {
|
|
|
|
return fspath.basename(flowsFullPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function getCredentialsFilename() {
|
|
|
|
if (flowsFullPath) {
|
|
|
|
return fspath.basename(credentialsFile);
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 11:30:07 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init,
|
2017-12-04 12:42:44 +01:00
|
|
|
listProjects: listProjects,
|
2017-09-20 11:30:07 +02:00
|
|
|
getActiveProject: getActiveProject,
|
|
|
|
setActiveProject: setActiveProject,
|
|
|
|
getProject: getProject,
|
2017-12-07 15:28:26 +01:00
|
|
|
deleteProject: deleteProject,
|
2017-09-20 11:30:07 +02:00
|
|
|
createProject: createProject,
|
2018-01-08 15:46:38 +01:00
|
|
|
initialiseProject: initialiseProject,
|
2017-09-20 11:30:07 +02:00
|
|
|
updateProject: updateProject,
|
|
|
|
getFiles: getFiles,
|
2017-10-25 16:26:24 +02:00
|
|
|
getFile: getFile,
|
2017-12-11 18:05:12 +01:00
|
|
|
revertFile: revertFile,
|
2017-10-07 01:18:20 +02:00
|
|
|
stageFile: stageFile,
|
|
|
|
unstageFile: unstageFile,
|
|
|
|
commit: commit,
|
2017-10-09 01:11:07 +02:00
|
|
|
getFileDiff: getFileDiff,
|
2017-10-10 00:37:19 +02:00
|
|
|
getCommits: getCommits,
|
|
|
|
getCommit: getCommit,
|
2017-11-22 00:31:41 +01:00
|
|
|
push: push,
|
|
|
|
pull: pull,
|
|
|
|
getStatus:getStatus,
|
|
|
|
resolveMerge: resolveMerge,
|
|
|
|
abortMerge: abortMerge,
|
|
|
|
getBranches: getBranches,
|
2017-12-08 17:31:42 +01:00
|
|
|
deleteBranch: deleteBranch,
|
2017-11-22 00:31:41 +01:00
|
|
|
setBranch: setBranch,
|
|
|
|
getBranchStatus:getBranchStatus,
|
2017-12-20 15:37:34 +01:00
|
|
|
getRemotes: getRemotes,
|
|
|
|
addRemote: addRemote,
|
|
|
|
removeRemote: removeRemote,
|
2017-12-21 18:40:24 +01:00
|
|
|
updateRemote: updateRemote,
|
2018-01-08 15:46:38 +01:00
|
|
|
getFlowFilename: getFlowFilename,
|
|
|
|
getCredentialsFilename: getCredentialsFilename,
|
2018-01-24 22:05:48 +01:00
|
|
|
getGlobalGitUser: function() { return globalGitUser },
|
2017-09-20 11:30:07 +02:00
|
|
|
getFlows: getFlows,
|
|
|
|
saveFlows: saveFlows,
|
|
|
|
getCredentials: getCredentials,
|
2018-01-19 00:13:55 +01:00
|
|
|
saveCredentials: saveCredentials,
|
|
|
|
|
|
|
|
ssh: sshTools
|
2017-09-20 11:30:07 +02:00
|
|
|
|
|
|
|
};
|