-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepository.js
More file actions
130 lines (95 loc) · 3.93 KB
/
repository.js
File metadata and controls
130 lines (95 loc) · 3.93 KB
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
'use strict';
const GitUrlParse = require('git-url-parse');
const Contents = require('./contents');
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;
},
loadFolder: async (path) => {
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}:${path}@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
});
}
internals.cache.set(resource, result);
Logger.log(['loader'], 'Loaded: %s', resource);
return result.data.map(({ name }) => name);
}
catch (err) {
if (err.status === 404) {
return []; // @todo: is this right?
}
Logger.error(['loader'], 'Failed to load: %s', resource);
throw err;
}
},
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 buffer = Buffer.from(result.data.content, 'base64');
return Contents.convert(buffer, options);
}
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();
};