-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (156 loc) · 5.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict'
var la = require('lazy-ass')
var check = require('check-more-types')
/* global process, require */
/* eslint new-cap: 0 */
/* eslint no-console: 0 */
function gitPages (options) {
var userConfig = require('./src/config')(options)
var repoConfig = userConfig.repos
var express = require('express')
var morgan = require('morgan')
var fs = require('fs')
var Q = require('q')
var R = require('ramda')
var quote = require('quote')
var join = require('path').join
var extname = require('path').extname
var marked = require('marked')
// var directToSubApp = require('./src/sub-app')
var app = express()
app.use(morgan('dev'))
console.log('Will serve pages for repos', R.keys(repoConfig).join(', '))
require('./app/controller')(app, repoConfig)
var storagePath = userConfig.storagePath
if (!fs.existsSync(storagePath)) {
console.log('making storage', quote(storagePath))
fs.mkdirSync(storagePath)
}
var repoCommands = require('./src/repo')({
storagePath: storagePath,
useHttps: userConfig.useHttps
})
function repoToFolder (repo) {
la(check.object(repo), 'missing repo')
la(check.unemptyString(repo.name), 'missing repo name', repo)
// function passPath (path) {
// la(check.unemptyString(path), 'copied folder should return path', path)
// console.log('Local folder in %s', path)
// return path
// }
// could be git repo or another folder
var isGitRepo = check.unemptyString(repo.git)
console.log('pulling repo %s, is git?', repo.name, isGitRepo)
var clone = R.partial(repoCommands.clone, repo.name, repo)
var pull = isGitRepo
? R.partial(repoCommands.pull, repo.name, repo.branch)
: clone
var shell = R.partial(repoCommands.shell, repo.exec)
function noop () {}
var commitId = isGitRepo
? R.partial(repoCommands.lastCommit, repo.name)
: noop
function rememberCommit (commit) {
la(check.object(commit), 'expected commit obj for', repo.name, 'got', commit)
la(check.commitId(commit.hash), 'expected commit for', repo.name, 'got', commit)
repo.commit = commit
console.log('remembering commit %s', commit)
return commit
}
var setCommit = isGitRepo ? R.pipeP(commitId, rememberCommit) : noop
return R.pipeP(pull, shell, setCommit)
}
app.get('/pull/:repo', function (req, res) {
var name = req.params.repo
if (!name) {
console.log('cannot pull repo without name', req.params)
return res.sendStatus(400)
}
console.log('received pull for repo %s', quote(name))
var config = repoConfig[name]
if (!config) {
console.log('cannot find repo %s', quote(name))
return res.status(404).send('Cannot find repo ' + name)
}
if (!check.has(config, 'name')) {
config.name = name
}
// no need to clone, the repo is already there
// var shell = R.partial(repoCommands.shell, config.exec)
function sendOk (commit) {
if (commit) {
la(check.object(commit), 'expected commit obj for', name, 'got', commit)
la(check.commitId(commit.hash), 'expected commit for', name, 'got', commit)
return res.status(200).send(commit).end()
}
res.status(200).send().end()
}
repoToFolder(config)()
.then(sendOk)
.done()
})
var extensionRenderers = {
'.md': function renderMarkdown (res, path) {
fs.readFile(path, 'utf8', function (err, data) {
if (err) {
throw new Error('Could not read ' + path + ' ' + err.message)
}
res.send(marked(data.toString()))
})
}
}
function repoRouteFor (repoName) {
// var repo = repoConfig[repoName]
var repoPath = join(storagePath, repoName)
return function repoRoute (req, res) {
var index = repoConfig[repoName].index
var full = join(repoPath, index)
var fileExt = extname(full)
if (R.has(fileExt, extensionRenderers)) {
extensionRenderers[fileExt](res, full)
} else {
res.sendFile(full)
}
}
}
// TODO: process each repo in order, not all at once
// to avoid multiple commands trying to execute in separate folders
function fetchRepo (repoName) {
la(check.unemptyString(repoName), 'missing repo name', repoName)
var repo = repoConfig[repoName]
if (!check.has(repo, 'name')) {
repo.name = repoName
}
var clone = R.partial(repoCommands.clone, repoName, repo)
var route = function route () {
console.log('setting up route for repo', quote(repoName))
app.get('/' + repoName, repoRouteFor(repoName))
}
return R.pipeP(clone, R.always(repo), repoToFolder, route)()
}
var repos = R.keys(repoConfig)
var fetchReposOneByOne =
repos
.map(function (name) {
return R.partial(fetchRepo, name)
})
.reduce(Q.when, Q())
function start () {
var PORT = process.env.PORT || userConfig.port
app.listen(PORT, '0.0.0.0')
console.log('Running on http://0.0.0.0:' + PORT)
}
function onError (err) {
console.error('Caught a problem', err.message)
console.error(err.stack)
}
fetchReposOneByOne
.then(function setupSubapps () {
// app.use(directToSubApp)
app.use(express.static(storagePath))
}).then(start).catch(onError).done()
}
module.exports = gitPages
if (!module.parent) {
throw new Error('Please run from another module, or use bin script')
}