diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-03-31 13:51:31 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-03-31 13:51:31 -0700 |
commit | a88ea48206e157821075064097952335cec063ed (patch) | |
tree | f28077aa8b5049dd6818f3fd1fec5cad834b603b /bin/utils.js | |
parent | 264a506e8b3039b89fe0aa1d8e8f3f33cc360cff (diff) | |
download | gitbook-a88ea48206e157821075064097952335cec063ed.zip gitbook-a88ea48206e157821075064097952335cec063ed.tar.gz gitbook-a88ea48206e157821075064097952335cec063ed.tar.bz2 |
Initial gitbook binary
Diffstat (limited to 'bin/utils.js')
-rw-r--r-- | bin/utils.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/bin/utils.js b/bin/utils.js new file mode 100644 index 0000000..804d6f0 --- /dev/null +++ b/bin/utils.js @@ -0,0 +1,58 @@ +var Q = require('q'); +var _ = require('lodash'); + +var url = require('url'); +var cp = require('child_process'); + + +// Get the remote of a given repo +function gitURL(path) { + var d = Q.defer(); + + cp.exec("git config --get remote.origin.url", { + cwd: path, + env: process.env, + }, function(err, stdout, stderr) { + if(err) { + return d.reject(err); + } + + return d.resolve(stdout); + }); + + return d.promise + .then(function(output) { + return output.replace(/(\r\n|\n|\r)/gm, ""); + }); +} + +// Poorman's parsing +// Parse a git URL to a github ID : username/reponame +function githubID(_url) { + // Detect HTTPS repos + var parsed = url.parse(_url); + if(parsed.protocol === 'https:' && parsed.host === 'github.com') { + return parsed.path.slice(1, -4); + } + + // Detect SSH repos + if(_url.indexOf('git@') === 0) { + return _url.split(':', 2)[1].slice(0, -4); + } + + // None found + return null; +} + +function titleCase(str) +{ + return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); +} + + +// Exports +module.exports = { + gitURL: gitURL, + githubID: githubID, + titleCase: titleCase, +}; |