This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
71 lines (63 loc) · 2.01 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
const fs = require('fs')
const path = require('path')
const got = require('got')
const dir = require('node-dir')
const tar = require('tar-fs')
const pathExists = require('path-exists').sync
const gunzip = require('gunzip-maybe')
const semver = require('semver')
const assert = require('assert')
function docs (input, callback) {
assert(typeof input === 'string', 'A valid branch name, SHA, version number, or directory is required as the first argument')
assert(typeof callback === 'function', 'A callback function is required as the second argument')
if (pathExists(input)) {
readFromDisk(input, callback)
} else {
download(input, callback)
}
}
function download (version, callback) {
// Prepend `v` to 1.2.3
if (version.match(/^\d+\.\d+\.\d+$/)) version = `v${version}`
var tarballUrl = `https://github.com/electron/electron/archive/${version}.tar.gz`
var electronDir
var tmpdir = require('os').tmpdir()
var tarball = path.join(tmpdir, `electron-${version}.tgz`)
var extractor = tar.extract(tmpdir, {
ignore: (name) => { return !name.match(/[\\/]docs[\\/]/) }
})
.on('entry', function extracting (header, stream, next) {
if (!electronDir) {
electronDir = path.join(tmpdir, header.name.split('/')[0])
}
})
.on('finish', function extracted () {
readFromDisk(path.join(electronDir, 'docs'), callback)
})
got.stream(tarballUrl)
.pipe(gunzip())
.pipe(extractor)
.on('error', function (e) {
callback(e)
})
}
function readFromDisk (directory, callback) {
var docs = []
dir.readFiles(
directory,
function (err, content, filename, next) {
var doc = {
slug: path.basename(filename, '.md'),
filename: path.relative(directory, filename),
markdown_content: content
}
if (typeof version !== 'undefined') docs.version = version
docs.push(doc)
next()
},
function (err, files) {
if (err) return callback(err)
callback(null, docs)
})
}
module.exports = require('pify')(docs)