-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.js
63 lines (52 loc) · 1.78 KB
/
helper.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
import os from 'os'
import fs from 'fs'
import path from 'path'
import minimatch from "minimatch"
export async function downloadFile(octokit, repository, target, source) {
const filepath = [repository.owner.login, repository.name, source].join('/');
return octokit.request(
`GET /repos/{owner}/{repo}/contents/${source}`,
{
owner: repository.owner.login,
repo: repository.name,
headers: {
Accept: "application/vnd.github.v3.raw"
}
}
).then((res) => {
octokit.log.info(`Download ${filepath}`);
// Expand the ~ character to a users home directory
const newTarget = target.replace("~", os.homedir)
const targetFile = path.join(newTarget, repository.owner.login, repository.name, source);
const targetPath = path.join(newTarget, repository.owner.login, repository.name, path.dirname(source));
if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath, { recursive: true });
}
fs.writeFileSync(targetFile, res.data)
}).catch(error => {
if (error.status === 404) {
octokit.log.warn(`File ${filepath} not found`);
return false;
}
throw error;
})
}
export async function getListOfFilesToDownload(octokit, repository, source) {
const res = await octokit.request(
`GET /repos/{owner}/{repo}/git/trees/HEAD?recursive=true`,
{
owner: repository.owner.login,
repo: repository.name,
headers: {
Accept: "application/vnd.github.v3.raw"
}
}).catch(error => {
if (error.status === 409) {
octokit.log.warn(`Git Repository is empty`);
return { data: { tree: [] } };
}
throw error;
})
const tree = res?.data?.tree?.filter(item => item.type === 'blob').map(item => item.path);
return tree.filter(item => minimatch(item, source))
}