-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (59 loc) · 2.28 KB
/
index.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
const axios = require('axios');
const fs = require('fs');
const path = require('path');
async function copyFolderFromGitHub(owner, repo, branch, folderPath, targetPath, token) {
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${folderPath}?ref=${branch}`;
try {
const response = await axios.get(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const contents = response.data;
if (Array.isArray(contents)) {
for (const item of contents) {
if (item.type === 'file') {
const fileUrl = item.download_url;
const filePath = path.join(targetPath, item.path);
const fileResponse = await axios.get(fileUrl, {
responseType: 'stream',
headers: {
Authorization: `Bearer ${token}`,
},
});
const fileDir = path.dirname(filePath);
if (!fs.existsSync(fileDir)) {
fs.mkdirSync(fileDir, { recursive: true });
}
const writer = fs.createWriteStream(filePath);
fileResponse.data.pipe(writer);
await new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
console.log(`Copied file: ${filePath}`);
} else if (item.type === 'dir') {
const nestedFolderPath = path.join(folderPath, item.name);
const nestedTargetPath = path.join(targetPath, item.name);
await copyFolderFromGitHub(owner, repo, branch, nestedFolderPath, nestedTargetPath, token);
}
}
console.log(`Folder copied successfully: ${targetPath}`);
} else {
console.log('No folder found at the specified path.');
}
} catch (error) {
console.error('An error occurred while copying the folder:', error.message);
}
}
// Usage example
// Usage example
// copyFolderFromGitHub('username', 'repo-name', 'main', 'path/to/folder', 'target/folder', 'your-access-token');
const token = "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"
const targetPath = "./aws"
const owner = "aistiak"
const repo = "daily-learn"
const branch = "main"
const outDir = "./out"
// Usage example
copyFolderFromGitHub(owner, repo, branch,targetPath, outDir,token);