mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-12 05:48:57 +00:00
cc340db0fa
This does a build, then spins up a HTTP server to serve the books contents
93 lines
2.0 KiB
JavaScript
93 lines
2.0 KiB
JavaScript
var Q = require('q');
|
|
var _ = require('lodash');
|
|
|
|
var http = require('http');
|
|
var send = require('send');
|
|
|
|
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();});
|
|
}
|
|
|
|
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,
|
|
};
|