|
| 1 | +const fs = require('fs') |
| 2 | +const crypto = require('crypto') |
| 3 | +const os = require('os') |
| 4 | +const git = require('simple-git')() |
| 5 | +const uuid = require('uuid/v4') |
| 6 | +const micro = require('micro') |
| 7 | +const pify = require('pify') |
| 8 | +const rimraf = pify(require('rimraf')) |
| 9 | +const glob = pify(require('glob')) |
| 10 | +const marked = require('marked') |
| 11 | +const highlightjs = require('highlight.js') |
| 12 | +const fm = require('front-matter') |
| 13 | +const { resolve } = require('path') |
| 14 | +const readFile = pify(fs.readFile) |
| 15 | +const gitClone = pify(git.clone.bind(git)) |
| 16 | +const send = micro.send |
| 17 | +const json = micro.json |
| 18 | + |
| 19 | +// Use highlight.js for code blocks |
| 20 | +const renderer = new marked.Renderer() |
| 21 | +renderer.code = (code, language) => { |
| 22 | + const validLang = !!(language && highlightjs.getLanguage(language)) |
| 23 | + const highlighted = validLang ? highlightjs.highlight(language, code).value : code |
| 24 | + return `<pre><code class="hljs ${language}">${highlighted}</code></pre>` |
| 25 | +} |
| 26 | +marked.setOptions({ renderer }) |
| 27 | + |
| 28 | +// Fetch doc and menu files |
| 29 | +let docFiles = [] |
| 30 | +let _CACHE_DOC_FILES_ = {} |
| 31 | +let _CACHE_MENU_ = {} |
| 32 | +async function getFiles () { |
| 33 | + _CACHE_DOC_FILES_ = {} |
| 34 | + docFiles = await glob('*/**/*.md', { |
| 35 | + ignore: 'node_modules/**/*', |
| 36 | + nodir: true |
| 37 | + }) |
| 38 | + _CACHE_MENU_ = {} |
| 39 | + let filePaths = await glob('*/**/menu.json', { |
| 40 | + ignore: 'node_modules/**/*', |
| 41 | + nodir: true |
| 42 | + }) |
| 43 | + filePaths.forEach((path) => { |
| 44 | + let cache = _CACHE_MENU_ |
| 45 | + let keys = path.split('/').slice(0, -1) |
| 46 | + keys.forEach((key, i) => { |
| 47 | + if ((i + 1) === keys.length) { |
| 48 | + cache[key] = require(resolve(__dirname, path)) |
| 49 | + return |
| 50 | + } |
| 51 | + cache[key] = cache[key] || {} |
| 52 | + cache = cache[key] |
| 53 | + }) |
| 54 | + }) |
| 55 | +} |
| 56 | +// Get doc file and sent back it's attributes and html body |
| 57 | +async function getDocFile (path) { |
| 58 | + path = resolve(__dirname, path) |
| 59 | + if (process.env.NODE_ENV !== 'production' && _CACHE_DOC_FILES_[path]) { |
| 60 | + return _CACHE_DOC_FILES_[path] |
| 61 | + } |
| 62 | + let file = await readFile(path, 'utf-8') |
| 63 | + // transform markdown to html |
| 64 | + file = fm(file) |
| 65 | + _CACHE_DOC_FILES_[path] = { |
| 66 | + attrs: file.attributes, |
| 67 | + body: marked(file.body) |
| 68 | + } |
| 69 | + return _CACHE_DOC_FILES_[path] |
| 70 | +} |
| 71 | + |
| 72 | +// Refresh documentation files (pull from Github) |
| 73 | +async function pullDocFiles ({ req, res }) { |
| 74 | + const body = await json(req) |
| 75 | + console.log(body) |
| 76 | + // Only for production |
| 77 | + if (!process.env.GH_HOOK_SECRET || !req.headers['x-hub-signature']) { |
| 78 | + return send(res, 501) |
| 79 | + } |
| 80 | + // Check if X-Hub-Signature matches our secret |
| 81 | + let hmac = crypto.createHmac('sha1', process.env.GH_HOOK_SECRET) |
| 82 | + hmac.update(JSON.stringify(body)) |
| 83 | + let signature = 'sha1=' + hmac.digest('hex') |
| 84 | + if (req.headers['x-hub-signature'] !== signature) { |
| 85 | + return send(res, 403) |
| 86 | + } |
| 87 | + // Accept only push hook events |
| 88 | + if (req.headers['x-github-event'] === 'ping') { |
| 89 | + return send(res, 200, 'OK') |
| 90 | + } |
| 91 | + // Only push event authorized |
| 92 | + if (req.headers['x-github-event'] !== 'push') { |
| 93 | + return send(res, 501) |
| 94 | + } |
| 95 | + const clonePath = resolve(os.tmpdir(), uuid()) |
| 96 | + await gitClone('https://github.com/nuxt/docs.git', clonePath) |
| 97 | + console.log('Cloned', clonePath) |
| 98 | + await getFiles() |
| 99 | + await rimraf(clonePath) |
| 100 | + send(res, 200, 'OK') |
| 101 | +} |
| 102 | + |
| 103 | +// Server handle request method |
| 104 | +const server = micro(async function (req, res) { |
| 105 | + // If github hook |
| 106 | + if (req.method === 'POST' && req.url === '/hook') { |
| 107 | + return await pullDocFiles({ req, res }) |
| 108 | + } |
| 109 | + if (req.url === '/menu') { |
| 110 | + return send(res, 200, _CACHE_MENU_) |
| 111 | + } |
| 112 | + // remove first / |
| 113 | + let path = req.url.slice(1) |
| 114 | + // Check if path exists |
| 115 | + const docFile = docFiles.find((f) => f === path) |
| 116 | + if (!docFile) { |
| 117 | + return send(res, 404, 'File not found') |
| 118 | + } |
| 119 | + // Send back doc content |
| 120 | + send(res, 200, await getDocFile(path)) |
| 121 | +}) |
| 122 | + |
| 123 | +getFiles() |
| 124 | +.then(() => { |
| 125 | + const port = process.env.PORT || 4000 |
| 126 | + server.listen(port) |
| 127 | + console.log(`Server listening on localhost:${port}`) |
| 128 | +}) |
0 commit comments