-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepository.js
91 lines (65 loc) · 2.59 KB
/
repository.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
'use strict';
const GitUrlParse = require('git-url-parse');
const Logger = require('../logger');
const OctokitWrapper = require('./octokit-wrapper');
const Utils = require('../utils');
const internals = {
cache: new Map()
};
exports.create = (repository) => {
if (repository.split('/').length === 2) {
repository = `https://github.com/${repository}`;
}
const parsedRepository = GitUrlParse(repository);
return {
getCommit: async () => {
const simpleGit = Utils.simpleGit();
const httpRepository = GitUrlParse.stringify(parsedRepository, 'http');
const result = await simpleGit.listRemote([httpRepository, 'HEAD']);
const [head] = result.split(/\s+/);
return head;
},
loadFile: async (filename, options = {}) => {
if (parsedRepository.source !== 'github.com') {
throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/detect-node-support');
}
const resource = `${parsedRepository.full_name}:${filename}@HEAD`;
Logger.log(['loader'], 'Loading: %s', resource);
const octokit = OctokitWrapper.create();
try {
let result;
if (internals.cache.has(resource)) {
Logger.log(['loader'], 'From cache: %s', resource);
result = internals.cache.get(resource);
}
else {
result = await octokit.repos.getContent({
owner: parsedRepository.owner,
repo: parsedRepository.name,
path: filename
});
}
internals.cache.set(resource, result);
Logger.log(['loader'], 'Loaded: %s', resource);
const content = Buffer.from(result.data.content, 'base64');
if (options.json) {
return JSON.parse(content.toString());
}
return content;
}
catch (err) {
if (err.status === 404) {
Logger.log(['loader'], 'Not found: %s', resource);
const error = new Error(`${repository} does not contain a ${filename}`);
error.code = 'ENOENT';
throw error;
}
Logger.error(['loader'], 'Failed to load: %s', resource);
throw err;
}
}
};
};
exports.clearCache = () => {
internals.cache = new Map();
};