starting command line stuff

This commit is contained in:
Peter Cottle 2012-08-13 14:09:55 -07:00
parent c4cc6cd57e
commit c12e0c6f2d
8 changed files with 380 additions and 184 deletions

143
src/commandline.js Normal file
View file

@ -0,0 +1,143 @@
/**
* class Command
* @desc A parser for commands given
*/
function Command(str) {
this.rawCommand = str;
this.results = {
msgs: []
};
this.command = null;
this.parse(str);
}
Command.prototype.getShortcutMap = function() {
return {
gc: 'git commit',
ga: 'git add',
gchk: 'git checkout',
gr: 'git rebase'
};
};
Command.prototype.getRegexMap = function() {
return {
commit: /^commit /,
add: /^add /,
checkout: /^checkout /,
rebase: /^rebase /,
reset: /^reset /
};
};
Command.prototype.parse = function(str) {
// first check if shortcut exists, and replace
str = this.getShortcutMap()[str] || str;
// see if begins with git
if (str.slice(0,3) !== 'git') {
return this.nonGitCommand();
}
// now slice off command part
this.command = str.slice(4);
//TODO: underscore.js here
for (var method in this.getRegexMap()) {
var regex = this.getRegexMap()[method];
if (regex.exec(this.command)) {
this.options = this.comand.slice(method.length + 1);
// break out here
return this[method]();
}
}
this.results.msgs.push('The git command "' + command +
'" is not supported, sorry!');
};
Command.prototype.nonGitCommand = function() {
this.results.error = {
msg: 'Git only commands, sorry!'
};
};
Command.prototype.commit = function() {
this.results.msgs.push(
'Commiting with options "' + this.options + '"
);
// commit for us means simply either ammending the current commit
// or just popping a new commit on top
var optionMap = {
// supported options
'--amend': false,
// pass through options, dont care but shouldnt fatal
'-a': false,
'-c': false,
'-C': false
};
this.options = new OptionParser(this.command, optionMap);
this.results.exec = function(gitEngine) {
gitEngine.commit(optionMap);
};
};
Command.prototype.add = function() {
this.results.msgs.push(
"This demo is meant to demonstrate git branching, so don't worry " +
"about adding / staging files. Just go ahead and commit away!"
);
};
Command.prototype.checkout = function() {
};
Command.prototype.rebase = function() {
};
Command.reset = function() {
};
/**
* OptionParser
*/
function OptionParser(str, supportedMap) {
this.str = str;
this.supportedMap = supportedMap;
this.results = {
unsupportedOptions: []
};
this.explodeAndSet();
}
OptionParser.prototype.explodeAndSet = function() {
var exploded = this.str.split(' ');
var options =[];
// TODO: underscore
for (var i = 0; i < exploded.length; i++) {
var part = exploded[i];
if (part.slice(0,1) == '-') {
options.push(part);
}
}
// TODO: undersore
for (var i = 0; i < options.length; i++) {
var option = options[i];
if (this.supportedMap[option] !== undefined) {
this.supportedMap[option] = true;
} else {
this.results.unsupportedOptions.push(option);
}
}
// done!
};