mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 01:25:16 +00:00
Add cli command serve
This does a build, then spins up a HTTP server to serve the books contents
This commit is contained in:
+31
-3
@@ -20,18 +20,20 @@ prog
|
||||
.option('-d, --dir <source_directory>', 'Source directory of book, containing Markdown files');
|
||||
|
||||
|
||||
var buildFunc;
|
||||
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) {
|
||||
.action(buildFunc = function(dir, options) {
|
||||
dir = dir || process.cwd();
|
||||
outputDir = options.output || path.join(dir, '_book');
|
||||
|
||||
console.log('Starting build ...');
|
||||
// Get repo's URL
|
||||
utils.gitURL(dir)
|
||||
return utils.gitURL(dir)
|
||||
.then(function(url) {
|
||||
// Get ID of repo
|
||||
return utils.githubID(url);
|
||||
@@ -50,9 +52,35 @@ prog
|
||||
);
|
||||
})
|
||||
.then(function(output) {
|
||||
console.log("Done!");
|
||||
console.log("Successfuly built !");
|
||||
}, function(err) {
|
||||
console.log(err.stack || err);
|
||||
throw err;
|
||||
})
|
||||
.then(_.constant(outputDir));
|
||||
});
|
||||
|
||||
prog
|
||||
.command('serve [source_dir]')
|
||||
.description('Build then serve a gitbook from a directory')
|
||||
.option('-p, --port <port>', 'Port for server to listen on', 4000)
|
||||
.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) {
|
||||
buildFunc(dir, options)
|
||||
.then(function(outputDir) {
|
||||
console.log();
|
||||
console.log('Starting server ...');
|
||||
return utils.serveDir(outputDir, options.port);
|
||||
})
|
||||
.then(function() {
|
||||
console.log('Serving book on http://localhost:'+options.port);
|
||||
console.log();
|
||||
console.log('Press CTRL+C to quit ...');
|
||||
})
|
||||
.fail(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
var Q = require('q');
|
||||
var _ = require('lodash');
|
||||
|
||||
var http = require('http');
|
||||
var send = require('send');
|
||||
|
||||
var url = require('url');
|
||||
var cp = require('child_process');
|
||||
|
||||
@@ -49,10 +52,41 @@ function titleCase(str)
|
||||
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
|
||||
}
|
||||
|
||||
function serveDir(dir, port) {
|
||||
var d = Q.defer();
|
||||
|
||||
var server = http.createServer(function(req, res){
|
||||
// Render error
|
||||
function error(err) {
|
||||
res.statusCode = err.status || 500;
|
||||
res.end(err.message);
|
||||
}
|
||||
|
||||
// Redirect to directory's index.html
|
||||
function redirect() {
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', req.url + '/');
|
||||
res.end('Redirecting to ' + req.url + '/');
|
||||
}
|
||||
|
||||
// Send file
|
||||
send(req, url.parse(req.url).pathname)
|
||||
.root(dir)
|
||||
.on('error', error)
|
||||
.on('directory', redirect)
|
||||
.pipe(res);
|
||||
}).listen(port);
|
||||
|
||||
d.resolve(server);
|
||||
|
||||
return d.promise;
|
||||
}
|
||||
|
||||
|
||||
// Exports
|
||||
module.exports = {
|
||||
gitURL: gitURL,
|
||||
githubID: githubID,
|
||||
titleCase: titleCase,
|
||||
serveDir: serveDir,
|
||||
};
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
"lodash": "2.4.1",
|
||||
"marked": "0.3.2",
|
||||
"swig": "1.3.2",
|
||||
"send": "0.2.0",
|
||||
"fstream-ignore": "0.0.7",
|
||||
"commander": "2.2.0",
|
||||
"fs-extra": "0.8.1"
|
||||
|
||||
Reference in New Issue
Block a user