Add initial version control sidebar with commit function

This commit is contained in:
Nick O'Leary
2017-10-07 00:18:20 +01:00
parent 522f7e6844
commit 9a2fd0e2b2
18 changed files with 852 additions and 116 deletions

View File

@@ -37,7 +37,8 @@ module.exports = {
projects: list
};
res.json(response);
}).otherwise(function(err) {
}).catch(function(err) {
console.log(err.stack);
if (err.code) {
res.status(400).json({error:err.code, message: err.message});
} else {
@@ -52,7 +53,8 @@ module.exports = {
runtime.storage.projects.getProject(name).then(function(data) {
res.json(data);
});
}).otherwise(function(err) {
}).catch(function(err) {
console.log(err.stack);
if (err.code) {
res.status(400).json({error:err.code, message: err.message});
} else {
@@ -69,7 +71,7 @@ module.exports = {
if (req.params.id !== currentProject) {
runtime.storage.projects.setActiveProject(req.params.id).then(function() {
res.redirect(303,req.baseUrl + '/');
}).otherwise(function(err) {
}).catch(function(err) {
if (err.code) {
res.status(400).json({error:err.code, message: err.message});
} else {
@@ -85,7 +87,7 @@ module.exports = {
req.body.hasOwnProperty('summary')) {
runtime.storage.projects.updateProject(req.params.id, req.body).then(function() {
res.redirect(303,req.baseUrl + '/');
}).otherwise(function(err) {
}).catch(function(err) {
if (err.code) {
res.status(400).json({error:err.code, message: err.message});
} else {
@@ -104,7 +106,7 @@ module.exports = {
} else {
res.status(404).end();
}
}).otherwise(function(err) {
}).catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
@@ -120,7 +122,67 @@ module.exports = {
runtime.storage.projects.getFiles(req.params.id).then(function(data) {
res.json(data);
})
.otherwise(function(err) {
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
});
app.post(/([^\/]+)\/stage\/(.+)$/, function(req,res) {
var projectName = req.params[0];
var file = req.params[1];
runtime.storage.projects.stageFile(projectName,file).then(function(data) {
res.redirect(303,req.baseUrl+"/"+projectName+"/files");
})
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
});
app.post("/:id/stage", function(req,res) {
var projectName = req.params.id;
var files = req.body.files;
runtime.storage.projects.stageFile(projectName,files).then(function(data) {
res.redirect(303,req.baseUrl+"/"+projectName+"/files");
})
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
});
app.post("/:id/commit", function(req,res) {
var projectName = req.params.id;
runtime.storage.projects.commit(projectName,req.body).then(function(data) {
res.redirect(303,req.baseUrl+"/"+projectName+"/files");
})
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
});
app.delete(/([^\/]+)\/stage\/(.+)$/, function(req,res) {
var projectName = req.params[0];
var file = req.params[1];
runtime.storage.projects.unstageFile(projectName,file).then(function(data) {
res.redirect(303,req.baseUrl+"/"+projectName+"/files");
})
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})
});
app.delete("/:id/stage", function(req, res) {
var projectName = req.params.id;
runtime.storage.projects.unstageFile(projectName).then(function(data) {
res.redirect(303,req.baseUrl+"/"+projectName+"/files");
})
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()});
})

View File

@@ -55,7 +55,7 @@ var localfilesystem = {
if (!settings.userDir) {
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
if (!settings.readOnly) {
promises.push(util.promiseDir(fspath.join(settings.userDir,"node_modules")));
promises.push(fs.ensureDir(fspath.join(settings.userDir,"node_modules")));
}
}
}

View File

@@ -121,7 +121,7 @@ function getLibraryEntry(type,path) {
});
return dirs.concat(files);
});
}).otherwise(function(err) {
}).catch(function(err) {
// if path is empty, then assume it was a folder, return empty
if (path === ""){
return [];
@@ -137,7 +137,7 @@ function getLibraryEntry(type,path) {
// check for path.json as an alternative if flows
if (type === "flows" && !/\.json$/.test(path)) {
return getLibraryEntry(type,path+".json")
.otherwise(function(e) {
.catch(function(e) {
throw err;
});
} else {
@@ -152,7 +152,7 @@ module.exports = {
libDir = fspath.join(settings.userDir,"lib");
libFlowsDir = fspath.join(libDir,"flows");
if (!settings.readOnly) {
return util.promiseDir(libFlowsDir);
return fs.ensureDir(libFlowsDir);
} else {
return when.resolve();
}
@@ -176,7 +176,7 @@ module.exports = {
if (type === "flows" && settings.flowFilePretty) {
body = JSON.stringify(JSON.parse(body),null,4);
}
return util.promiseDir(fspath.dirname(fn)).then(function () {
return fs.ensureDir(fspath.dirname(fn)).then(function () {
util.writeFile(fn,headers+body);
});
}

View File

@@ -0,0 +1,33 @@
/**
* 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.
**/
module.exports = {
"package.json": function(project) {
return JSON.stringify({
"name": project.name,
"description": project.summary||"A Node-RED Project",
"version": "0.0.1",
"dependencies": {}
},"",4);
},
"README.md": function(project) {
return project.name+"\n"+("=".repeat(project.name.length))+"\n\n"+(project.summary||"A Node-RED Project")+"\n\n";
},
"settings.json": function() { return "{}" },
"flow.json": function() { return "[]" },
"flow_cred.json": function() { return "{}" },
".gitignore": function() { return "*.backup" ;}
}

View File

@@ -67,6 +67,112 @@ function isAuthError(err) {
// lines.forEach(console.log);
}
function cleanFilename(name) {
if (name[0] !== '"') {
return name;
}
return name.substring(1,name.length-1);
}
function parseFilenames(name) {
var re = /([^ "]+|(".*?"))($| -> ([^ ]+|(".*"))$)/;
var m = re.exec(name);
var result = [];
if (m) {
result.push(cleanFilename(m[1]));
if (m[4]) {
result.push(cleanFilename(m[4]));
}
}
return result;
}
function getFiles(localRepo) {
// parseFilename('"test with space"');
// parseFilename('"test with space" -> knownFile.txt');
// parseFilename('"test with space" -> "un -> knownFile.txt"');
var files = {};
return runCommand(gitCommand,["ls-files","--cached","--others","--exclude-standard"],localRepo).then(function(output) {
var lines = output.split("\n");
lines.forEach(function(l) {
if (l==="") {
return;
}
var fullName = cleanFilename(l);
// parseFilename(l);
var parts = fullName.split("/");
var p = files;
var name;
for (var i = 0;i<parts.length-1;i++) {
var name = parts.slice(0,i+1).join("/")+"/";
if (!p.hasOwnProperty(name)) {
p[name] = {
type:"d"
}
}
}
files[fullName] = {
type: "f"
}
})
return runCommand(gitCommand,["status","--porcelain"],localRepo).then(function(output) {
var lines = output.split("\n");
var unknownDirs = [];
lines.forEach(function(line) {
if (line==="") {
return;
}
if (line[0] === "#") {
return;
}
var status = line.substring(0,2);
var fileName;
var names;
if (status !== '??') {
names = parseFilenames(line.substring(3));
} else {
names = [cleanFilename(line.substring(3))];
}
fileName = names[0];
if (names.length > 1) {
fileName = names[1];
}
// parseFilename(fileName);
if (fileName.charCodeAt(0) === 34) {
fileName = fileName.substring(1,fileName.length-1);
}
if (files.hasOwnProperty(fileName)) {
files[fileName].status = status;
} else {
files[fileName] = {
type: "f",
status: status
};
}
if (names.length > 1) {
files[fileName].oldName = names[0];
}
if (status === "??" && fileName[fileName.length-1] === '/') {
unknownDirs.push(fileName);
}
})
var allFilenames = Object.keys(files);
allFilenames.forEach(function(f) {
var entry = files[f];
if (!entry.hasOwnProperty('status')) {
unknownDirs.forEach(function(uf) {
if (f.startsWith(uf)) {
entry.status = "??"
}
});
}
})
// console.log(files);
return files;
})
})
}
var gitCommand = "git";
module.exports = {
initRepo: function(cwd) {
@@ -82,5 +188,26 @@ module.exports = {
clone: function(repo, cwd) {
var args = ["clone",repo,"."];
return runCommand(gitCommand,args,cwd);
},
getFiles: getFiles,
stageFile: function(cwd,file) {
var args = ["add"];
if (Array.isArray(file)) {
args = args.concat(file);
} else {
args.push(file);
}
return runCommand(gitCommand,args,cwd);
},
unstageFile: function(cwd, file) {
var args = ["reset","--"];
if (file) {
args.push(file);
}
return runCommand(gitCommand,args,cwd);
},
commit: function(cwd, message) {
var args = ["commit","-m",message];
return runCommand(gitCommand,args,cwd);
}
}

View File

@@ -29,6 +29,7 @@ var runtime;
var projectsDir;
var defaultFileSet = require("./defaultFileSet");
function init(_settings, _runtime) {
settings = _settings;
@@ -69,7 +70,7 @@ function init(_settings, _runtime) {
credentialsFileBackup = getBackupFilename(credentialsFile)
if (!settings.readOnly) {
return util.promiseDir(projectsDir)
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)
@@ -103,7 +104,7 @@ function getBackupFilename(filename) {
}
function listProjects() {
return nodeFn.call(fs.readdir, projectsDir).then(function(fns) {
return fs.readdir(projectsDir).then(function(fns) {
var dirs = [];
fns.sort().filter(function(fn) {
var fullPath = fspath.join(projectsDir,fn);
@@ -151,14 +152,14 @@ function getProject(project) {
projectData.missingFiles = missingFiles;
}
if (missingFiles.indexOf('package.json') === -1) {
promises.push(nodeFn.call(fs.readFile,fspath.join(projectPath,"package.json"),"utf8").then(function(content) {
promises.push(fs.readFile(fspath.join(projectPath,"package.json"),"utf8").then(function(content) {
var package = util.parseJSON(content);
projectData.summary = package.description||"";
projectData.dependencies = package.dependencies||{};
}));
}
if (missingFiles.indexOf('README.md') === -1) {
promises.push(nodeFn.call(fs.readFile,fspath.join(projectPath,"README.md"),"utf8").then(function(content) {
promises.push(fs.readFile(fspath.join(projectPath,"README.md"),"utf8").then(function(content) {
projectData.description = content;
}));
} else {
@@ -174,9 +175,9 @@ function getProject(project) {
// if (err) {
// return resolve(null);
// }
// resolve(nodeFn.call(fs.readFile,projectPackage,'utf8').then(util.parseJSON));
// resolve(fs.readFile(projectPackage,'utf8').then(util.parseJSON));
// })
}).otherwise(function(err) {
}).catch(function(err) {
console.log(err);
var e = new Error("NLD: project not found");
e.code = "project_not_found";
@@ -191,7 +192,6 @@ function setCredentialSecret(project,data) { //existingSecret,secret) {
var wasInvalid = false;
var globalProjectSettings = settings.get("projects");
globalProjectSettings.projects = globalProjectSettings.projects || {};
if (globalProjectSettings.projects.hasOwnProperty(project)) {
if (!isReset &&
globalProjectSettings.projects[project].credentialSecret &&
@@ -239,10 +239,12 @@ function createProject(metadata) {
return reject(e);
}
createProjectDirectory(project).then(function() {
var projects = settings.get('projects');
projects[project] = {};
if (metadata.credentialSecret) {
return setCredentialSecret(project,{credentialSecret: credentialSecret});
projects[project].credentialSecret = metadata.credentialSecret;
}
return when.resolve();
return settings.set('projects',projects);
}).then(function() {
if (metadata.remote) {
return gitTools.pull(metadata.remote,projectPath).then(function(result) {
@@ -259,43 +261,26 @@ function createProject(metadata) {
});
resolve(project);
}).otherwise(function(error) {
}).catch(function(error) {
fs.remove(projectPath,function() {
reject(error);
});
})
} else {
createDefaultProject(metadata).then(function() { resolve(project)}).otherwise(reject);
createDefaultProject(metadata).then(function() { resolve(project)}).catch(reject);
}
}).otherwise(reject);
}).catch(reject);
})
})
}
function createProjectDirectory(project) {
var projectPath = fspath.join(projectsDir,project);
return util.promiseDir(projectPath).then(function() {
return fs.ensureDir(projectPath).then(function() {
return gitTools.initRepo(projectPath)
});
}
var defaultFileSet = {
"package.json": function(project) {
return JSON.stringify({
"name": project.name,
"description": project.summary||"A Node-RED Project",
"version": "0.0.1",
"dependencies": {}
},"",4);
},
"README.md": function(project) {
return project.name+"\n"+("=".repeat(project.name.length))+"\n\n"+(project.summary||"A Node-RED Project")+"\n\n";
},
"settings.json": function() { return "{}" },
"flow.json": function() { return "[]" },
"flow_cred.json": function() { return "{}" }
}
function createDefaultProject(project) {
var projectPath = fspath.join(projectsDir,project.name);
// Create a basic skeleton of a project
@@ -309,10 +294,13 @@ function createDefaultProject(project) {
}
function checkProjectExists(project) {
var projectPath = fspath.join(projectsDir,project);
return nodeFn.call(fs.stat,projectPath).otherwise(function(err) {
var e = new Error("NLD: project not found");
e.code = "project_not_found";
throw e;
return fs.pathExists(projectPath).then(function(exists) {
console.log(projectPath,exists);
if (!exists) {
var e = new Error("NLD: project not found");
e.code = "project_not_found";
throw e;
}
});
}
function checkProjectFiles(project) {
@@ -322,7 +310,7 @@ function checkProjectFiles(project) {
for (var file in defaultFileSet) {
if (defaultFileSet.hasOwnProperty(file)) {
paths.push(file);
promises.push(nodeFn.call(fs.stat,fspath.join(projectPath,file)));
promises.push(fs.stat(fspath.join(projectPath,file)));
}
}
return when.settle(promises).then(function(results) {
@@ -349,51 +337,24 @@ function checkProjectFiles(project) {
function getFiles(project) {
var projectPath = fspath.join(projectsDir,project);
return nodeFn.call(listFiles,projectPath,"/");
return gitTools.getFiles(projectPath);
}
function stageFile(project,file) {
var projectPath = fspath.join(projectsDir,project);
return gitTools.stageFile(projectPath,file);
}
function unstageFile(project,file) {
var projectPath = fspath.join(projectsDir,project);
return gitTools.unstageFile(projectPath,file);
}
function commit(project,options) {
var projectPath = fspath.join(projectsDir,project);
return gitTools.commit(projectPath,options.message);
}
function getFile(project,path) {
}
function listFiles(root,path,done) {
var entries = [];
var fullPath = fspath.join(root,path);
fs.readdir(fullPath, function(err,fns) {
var childCount = fns.length;
fns.sort().forEach(function(fn) {
if (fn === ".git") {
childCount--;
return;
}
var child = {
path: fspath.join(path,fn),
name: fn
};
entries.push(child);
var childFullPath = fspath.join(fullPath,fn);
fs.lstat(childFullPath, function(err, stats) {
if (stats.isDirectory()) {
child.type = 'd';
listFiles(root,child.path,function(err,children) {
child.children = children;
childCount--;
if (childCount === 0) {
done(null,entries);
}
})
} else {
child.type = 'f';
childCount--;
console.log(child,childCount)
if (childCount === 0) {
done(null,entries);
}
}
});
});
});
}
var activeProject
function getActiveProject() {
return activeProject;
@@ -403,7 +364,7 @@ function reloadActiveProject(project) {
return runtime.nodes.stopFlows().then(function() {
return runtime.nodes.loadFlows(true).then(function() {
runtime.events.emit("runtime-event",{id:"project-change",payload:{ project: project}});
}).otherwise(function(err) {
}).catch(function(err) {
// We're committed to the project change now, so notify editors
// that it has changed.
runtime.events.emit("runtime-event",{id:"project-change",payload:{ project: project}});
@@ -456,7 +417,7 @@ function updateProject(project,data) {
} else if (data.hasOwnProperty('dependencies') || data.hasOwnProperty('summary')) {
var projectPath = fspath.join(projectsDir,project);
var packageJSON = fspath.join(projectPath,"package.json");
return nodeFn.call(fs.readFile,packageJSON,"utf8").then(function(content) {
return fs.readFile(packageJSON,"utf8").then(function(content) {
var package = util.parseJSON(content);
if (data.dependencies) {
package.dependencies = data.dependencies;
@@ -483,8 +444,10 @@ function getFlows() {
if (!initialFlowLoadComplete) {
initialFlowLoadComplete = true;
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
if (activeProject) {
log.info(log._("storage.localfilesystem.active-project",{project:activeProject||"none"}));
}
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
log.info(log._("storage.localfilesystem.active-project",{project:activeProject||"none"}));
}
return util.readFile(flowsFullPath,flowsFileBackup,[],'flow');
}
@@ -541,6 +504,9 @@ module.exports = {
createProject: createProject,
updateProject: updateProject,
getFiles: getFiles,
stageFile: stageFile,
unstageFile: unstageFile,
commit: commit,
getFlows: getFlows,
saveFlows: saveFlows,

View File

@@ -73,7 +73,6 @@ function readFile(path,backupPath,emptyResponse,type) {
}
module.exports = {
promiseDir: nodeFn.lift(fs.mkdirs),
/**
* Write content to a file using UTF8 encoding.
* This forces a fsync before completing to ensure