Merge pull request #1 from GitbookIO/feature/cli

Initial gitbook binary
This commit is contained in:
Aaron O'Mullan
2014-03-31 13:52:41 -07:00
3 changed files with 122 additions and 0 deletions
Executable
+60
View File
@@ -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();
}
+58
View File
@@ -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,
};
+4
View File
@@ -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"