-
Notifications
You must be signed in to change notification settings - Fork 7
/
gatsby-node.js
99 lines (82 loc) · 3.57 KB
/
gatsby-node.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
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const Octokat = require('octokat');
const crypto = require('crypto');
const fileType = require('file-type');
const mime = require('mime-types');
const path = require('path');
const minimatch = require('minimatch');
exports.sourceNodes = (() => {
var _ref = _asyncToGenerator(function* ({ boundActionCreators }, { user, repository, tree = false, releases = false, secrets = {} }) {
if (!user || !repository) {
throw 'You need to define user & repository for gatsby-source-github to work';
}
const octo = new Octokat(secrets);
const createNode = boundActionCreators.createNode;
console.time(`fetch Github data`);
secrets.token && console.log('Using Github Token');
console.log(`starting to fetch data from the Github API. Warning: This may take a long time.`);
const repo = octo.repos(user, repository);
if (tree) {
const patternFilter = function patternFilter(file) {
return minimatch(file.path, tree && tree.pattern || '**');
};
const data = yield repo.git.trees('HEAD').fetch({ recursive: 1 });
const files = yield Promise.all(data.tree.filter(function (file) {
return file.type !== 'tree';
}).filter(patternFilter).map(function (file) {
return octo.fromUrl(file.url).fetch().then(function (result) {
const buffer = Buffer.from(result.content, 'base64');
const type = fileType(buffer);
const mimeType = type ? type.mime : mime.lookup(file.path) || 'plaintext';
return {
user: user,
repository: repository,
path: file.path,
fileAbsolutePath: file.url,
relativePath: file.path,
base: path.basename(file.path),
relativeDirectory: path.dirname(file.path),
url: file.url,
type: file.type,
mime: mimeType,
sha: file.sha,
content: buffer.toString('utf8')
};
});
}));
files.forEach(function (file) {
return createNode(_extends({}, file, {
id: file.url,
parent: `__SOURCE__`,
children: [],
internal: {
mediaType: file.mime,
type: 'GithubFile',
contentDigest: crypto.createHash(`md5`).update(file.content).digest(`hex`)
}
}));
});
}
if (releases) {
const data = yield repo.releases.fetch();
data.items.forEach(function (item) {
createNode(_extends({}, item, {
id: item.url,
parent: null,
children: [],
internal: {
type: 'GithubRelease',
contentDigest: crypto.createHash(`md5`).update(JSON.stringify(item)).digest(`hex`)
}
}));
});
}
console.timeEnd(`fetch Github data`);
return;
});
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
})();