diff options
-rwxr-xr-x | bin/gitbook.js | 60 | ||||
-rw-r--r-- | bin/utils.js | 58 | ||||
-rw-r--r-- | package.json | 4 |
3 files changed, 122 insertions, 0 deletions
diff --git a/bin/gitbook.js b/bin/gitbook.js new file mode 100755 index 0000000..6ec3e79 --- /dev/null +++ b/bin/gitbook.js @@ -0,0 +1,60 @@ +#! /usr/bin/env node + +// Requires +var _ = require('lodash'); +var path = require('path'); +var prog = require('commander'); + +var pkg = require('../package.json'); +var generate = require("../lib/generate"); + +var utils = require('./utils'); + + +// General options +prog +.version(pkg.version) +.option('-d, --dir <source_directory>', 'Source directory of book, containing Markdown files'); + + +prog +.command('build [source_dir]') +.description('Build a gitbook from a directory') +.option('-o, --output <directory>', 'Path to output directory, defaults to ./_book') +.option('-t, --title <name>', 'Name of the book to generate, defaults to repo name') +.option('-g, --github <repo_path>', 'ID of github repo like : username/repo') +.action(function(dir, options) { + dir = dir || process.cwd(); + outputDir = options.output || path.join(dir, '_book'); + + // Get repo's URL + utils.gitURL(dir) + .then(function(url) { + // Get ID of repo + return utils.githubID(url); + }) + .then(function(repoID) { + var parts = repoID.split('/', 2); + var user = parts[0], repo = parts[1]; + + return generate.folder( + dir, + outputDir, + { + title: options.title || utils.titleCase(repo), + github: options.github || repoID + } + ); + }) + .then(function(output) { + console.log(output); + }, function(err) { + console.log(err.stack, err); + }); +}); + + +// Parse and fallback to help if no args +if(_.isEmpty(prog.parse(process.argv).args) && process.argv.length === 2) { + prog.help(); +} 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, +}; diff --git a/package.json b/package.json index 56cfdad..52cc9c7 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "swig": "1.3.2", "glob": "3.2.9", "q": "1.0.1", + "commander": "2.2.0", "fs-extra": "0.8.1" }, "devDependencies": { @@ -21,6 +22,9 @@ "scripts": { "test": "export TESTING=true; mocha --reporter list" }, + "bin": { + "gitbook": "./bin/gitbook.js" + }, "repository": { "type": "git", "url": "https://github.com/GitbookIO/gitbook.git" |