-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
79 lines (66 loc) · 2.19 KB
/
github.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
const { Octokit } = require('@octokit/rest');
const inquirer = require('inquirer');
require('dotenv').config();
async function fetchSchemaList(owner, repo, path) {
const octokit = process.env.GITHUB_TOKEN ? new Octokit({auth: process.env.GITHUB_TOKEN}) : new Octokit({});
try {
const response = await octokit.repos.getContent({
owner,
repo,
path,
});
const schemaListContent = Buffer.from(response.data.content, 'base64').toString('utf-8');
return JSON.parse(schemaListContent);
} catch (error) {
throw new Error(`Error fetching schema list: ${error.message}`);
}
}
async function promptForEntities(schemaList) {
const questions = [
{
type: 'checkbox',
name: 'entities',
message: 'Select entities to download:',
choices: schemaList.map(entity => {
const entityName = Object.keys(entity)[0];
const entityValue = entity[entityName];
return {
name: entityName,
value: entityValue,
};
}),
},
];
const answers = await inquirer.prompt(questions);
// Save the selected entities to an environment variable
process.env.SELECTED_ENTITIES = JSON.stringify(answers.entities);
return answers;
}
async function fetchMatchingFiles(owner, repo, path, prefix) {
const octokit = new Octokit({});
try {
const response = await octokit.repos.getContent({
owner,
repo,
path,
});
// Filter files with the specified prefix
const matchingFiles = response.data
.filter(file => {
const regex = new RegExp("^" + prefix);
return file.type === 'file' && file.name.match(regex);
})
.map(file => ({
name: file.name,
download_url: file.download_url,
}));
return matchingFiles;
} catch (error) {
throw new Error(`Error fetching matching files: ${error.message}`);
}
}
module.exports = {
fetchSchemaList,
promptForEntities,
fetchMatchingFiles
};