repolet 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +129 -0
- package/package.json +33 -0
- package/src/downloader.js +51 -0
- package/src/github.js +124 -0
- package/src/index.js +152 -0
- package/src/parser.js +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shiv Singh Baghel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
Repolet
|
|
2
|
+
|
|
3
|
+
Copy a complete GitHub repository or a specific GitHub folder directly to your local filesystem.
|
|
4
|
+
|
|
5
|
+
Repolet does not create a ZIP file and does not copy the .git history. It downloads the actual files into a normal local directory.
|
|
6
|
+
|
|
7
|
+
Features
|
|
8
|
+
|
|
9
|
+
Copy an entire GitHub repository
|
|
10
|
+
|
|
11
|
+
Copy only a specific GitHub subfolder
|
|
12
|
+
|
|
13
|
+
Support nested folders
|
|
14
|
+
|
|
15
|
+
Preserve binary files such as PNG, PDF, MP3, SVG, etc.
|
|
16
|
+
|
|
17
|
+
Custom destination folder
|
|
18
|
+
|
|
19
|
+
Recursive file discovery
|
|
20
|
+
|
|
21
|
+
GitHub token support
|
|
22
|
+
|
|
23
|
+
Download progress
|
|
24
|
+
|
|
25
|
+
Existing-folder protection
|
|
26
|
+
|
|
27
|
+
Installation
|
|
28
|
+
|
|
29
|
+
Using npm:
|
|
30
|
+
|
|
31
|
+
npm install -g repolet
|
|
32
|
+
|
|
33
|
+
Using pnpm:
|
|
34
|
+
|
|
35
|
+
pnpm add -g repolet
|
|
36
|
+
|
|
37
|
+
Or run directly with:
|
|
38
|
+
|
|
39
|
+
npx repolet <github-url>
|
|
40
|
+
|
|
41
|
+
Usage
|
|
42
|
+
|
|
43
|
+
Copy an entire repository
|
|
44
|
+
|
|
45
|
+
repolet https://github.com/user/repository
|
|
46
|
+
|
|
47
|
+
For example:
|
|
48
|
+
|
|
49
|
+
repolet https://github.com/Devendradhote001/KODR4-React kodr-repo
|
|
50
|
+
|
|
51
|
+
Creates:
|
|
52
|
+
|
|
53
|
+
kodr-repo/
|
|
54
|
+
├── public/
|
|
55
|
+
├── src/
|
|
56
|
+
├── package.json
|
|
57
|
+
└── ...
|
|
58
|
+
|
|
59
|
+
Copy a specific folder
|
|
60
|
+
|
|
61
|
+
repolet https://github.com/user/repository/tree/main/src my-src
|
|
62
|
+
|
|
63
|
+
For example:
|
|
64
|
+
|
|
65
|
+
repolet https://github.com/shivsingh78/my-portfolio/tree/main/src my-portfolio-src
|
|
66
|
+
|
|
67
|
+
Creates:
|
|
68
|
+
|
|
69
|
+
my-portfolio-src/
|
|
70
|
+
├── App.jsx
|
|
71
|
+
├── assets/
|
|
72
|
+
├── components/
|
|
73
|
+
├── index.css
|
|
74
|
+
└── main.jsx
|
|
75
|
+
|
|
76
|
+
Destination is optional
|
|
77
|
+
|
|
78
|
+
repolet https://github.com/user/repository/tree/main/src
|
|
79
|
+
|
|
80
|
+
Repolet will use the selected folder name as the destination.
|
|
81
|
+
|
|
82
|
+
GitHub Authentication
|
|
83
|
+
|
|
84
|
+
Public repositories can be downloaded without authentication.
|
|
85
|
+
|
|
86
|
+
For private repositories or higher GitHub API limits, set a GitHub fine-grained personal access token:
|
|
87
|
+
|
|
88
|
+
export GITHUB_TOKEN="your_token"
|
|
89
|
+
|
|
90
|
+
Then:
|
|
91
|
+
|
|
92
|
+
repolet https://github.com/user/private-repo private-repo
|
|
93
|
+
|
|
94
|
+
The token is read from:
|
|
95
|
+
|
|
96
|
+
GITHUB_TOKEN
|
|
97
|
+
|
|
98
|
+
Repolet does not store the token.
|
|
99
|
+
|
|
100
|
+
For a fine-grained token, use the minimum required repository permissions, such as:
|
|
101
|
+
|
|
102
|
+
Contents → Read-only
|
|
103
|
+
|
|
104
|
+
Options
|
|
105
|
+
|
|
106
|
+
repolet --help
|
|
107
|
+
repolet --version
|
|
108
|
+
|
|
109
|
+
Examples
|
|
110
|
+
|
|
111
|
+
Copy a complete repository:
|
|
112
|
+
|
|
113
|
+
repolet https://github.com/user/repository
|
|
114
|
+
|
|
115
|
+
Copy a folder:
|
|
116
|
+
|
|
117
|
+
repolet https://github.com/user/repository/tree/main/components
|
|
118
|
+
|
|
119
|
+
Copy to a custom directory:
|
|
120
|
+
|
|
121
|
+
repolet https://github.com/user/repository/tree/main/components ui-components
|
|
122
|
+
|
|
123
|
+
Requirements
|
|
124
|
+
|
|
125
|
+
Node.js 18 or newer
|
|
126
|
+
|
|
127
|
+
License
|
|
128
|
+
|
|
129
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "repolet",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Copy a complete GitHub repository or a specific folder directly to your local filesystem",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"repolet": "src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"start": "node src/index.js"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"github",
|
|
20
|
+
"git",
|
|
21
|
+
"clone",
|
|
22
|
+
"github-folder",
|
|
23
|
+
"repository",
|
|
24
|
+
"cli"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"repolet": "file:/home/shivsingh/harikirat/repolet/repolet-0.1.0.tgz"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import fs from "fs/promises";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export async function downloadFile(
|
|
5
|
+
file,
|
|
6
|
+
destinationRoot,
|
|
7
|
+
sourceRoot
|
|
8
|
+
) {
|
|
9
|
+
let relativePath;
|
|
10
|
+
|
|
11
|
+
if (sourceRoot) {
|
|
12
|
+
relativePath = path.relative(
|
|
13
|
+
sourceRoot,
|
|
14
|
+
file.path
|
|
15
|
+
);
|
|
16
|
+
} else {
|
|
17
|
+
relativePath = file.path;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const outputPath = path.join(
|
|
21
|
+
destinationRoot,
|
|
22
|
+
relativePath
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const directory = path.dirname(outputPath);
|
|
26
|
+
|
|
27
|
+
await fs.mkdir(directory, {
|
|
28
|
+
recursive: true
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const response = await fetch(
|
|
32
|
+
file.download_url
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Failed to download ${file.path}: ${response.status} ${response.statusText}`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const buffer = Buffer.from(
|
|
42
|
+
await response.arrayBuffer()
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
await fs.writeFile(
|
|
46
|
+
outputPath,
|
|
47
|
+
buffer
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return outputPath;
|
|
51
|
+
}
|
package/src/github.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
async function githubRequest(url) {
|
|
2
|
+
const headers = {
|
|
3
|
+
Accept: "application/vnd.github+json",
|
|
4
|
+
"User-Agent": "repolet"
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const token = process.env.GITHUB_TOKEN;
|
|
8
|
+
|
|
9
|
+
if (token) {
|
|
10
|
+
headers.Authorization = `Bearer ${token}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const response = await fetch(url, {
|
|
14
|
+
headers
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
if (response.status === 401) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"GitHub authentication failed. Check your GITHUB_TOKEN."
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (response.status === 403) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
"GitHub API rate limit exceeded. Set GITHUB_TOKEN to increase the API limit."
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (response.status === 404) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
"GitHub repository or branch not found, or you do not have permission to access it."
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
throw new Error(
|
|
37
|
+
`GitHub API request failed: ${response.status} ${response.statusText}`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return response.json();
|
|
42
|
+
}
|
|
43
|
+
async function getDefaultBranch(owner, repo) {
|
|
44
|
+
const url =
|
|
45
|
+
`https://api.github.com/repos/${owner}/${repo}`;
|
|
46
|
+
|
|
47
|
+
const data = await githubRequest(url);
|
|
48
|
+
|
|
49
|
+
return data.default_branch;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function getAllFiles({
|
|
53
|
+
owner,
|
|
54
|
+
repo,
|
|
55
|
+
branch,
|
|
56
|
+
path,
|
|
57
|
+
type
|
|
58
|
+
}) {
|
|
59
|
+
// --------------------------------
|
|
60
|
+
// 1. Determine branch
|
|
61
|
+
// --------------------------------
|
|
62
|
+
|
|
63
|
+
if (!branch) {
|
|
64
|
+
branch = await getDefaultBranch(
|
|
65
|
+
owner,
|
|
66
|
+
repo
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --------------------------------
|
|
71
|
+
// 2. Fetch complete repository tree
|
|
72
|
+
// --------------------------------
|
|
73
|
+
|
|
74
|
+
const url =
|
|
75
|
+
`https://api.github.com/repos/${owner}/${repo}/git/trees/${encodeURIComponent(branch)}` +
|
|
76
|
+
"?recursive=1";
|
|
77
|
+
|
|
78
|
+
const data = await githubRequest(url);
|
|
79
|
+
|
|
80
|
+
if (data.truncated) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
"GitHub returned a truncated repository tree. This repository is too large for the recursive tree request."
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// --------------------------------
|
|
87
|
+
// 3. Determine selected path
|
|
88
|
+
// --------------------------------
|
|
89
|
+
|
|
90
|
+
const prefix = path
|
|
91
|
+
? path.endsWith("/")
|
|
92
|
+
? path
|
|
93
|
+
: `${path}/`
|
|
94
|
+
: "";
|
|
95
|
+
|
|
96
|
+
// --------------------------------
|
|
97
|
+
// 4. Select files
|
|
98
|
+
// --------------------------------
|
|
99
|
+
|
|
100
|
+
const files = data.tree.filter((item) => {
|
|
101
|
+
if (item.type !== "blob") {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Whole repository
|
|
106
|
+
if (type === "repository") {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Selected folder
|
|
111
|
+
return item.path.startsWith(prefix);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// --------------------------------
|
|
115
|
+
// 5. Build download URLs
|
|
116
|
+
// --------------------------------
|
|
117
|
+
|
|
118
|
+
return files.map((item) => ({
|
|
119
|
+
type: "file",
|
|
120
|
+
path: item.path,
|
|
121
|
+
download_url:
|
|
122
|
+
`https://raw.githubusercontent.com/${owner}/${repo}/${encodeURIComponent(branch)}/${item.path}`
|
|
123
|
+
}));
|
|
124
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "fs/promises";
|
|
3
|
+
|
|
4
|
+
import { parseGitHubUrl } from "./parser.js";
|
|
5
|
+
import { getAllFiles } from "./github.js";
|
|
6
|
+
import { downloadFile } from "./downloader.js";
|
|
7
|
+
|
|
8
|
+
const VERSION = "0.1.0";
|
|
9
|
+
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
const cleanArgs = args.filter((arg) => arg !== "--");
|
|
12
|
+
|
|
13
|
+
const showHelp = cleanArgs.includes("--help") || cleanArgs.includes("-h");
|
|
14
|
+
|
|
15
|
+
const showVersion = cleanArgs.includes("--version") || cleanArgs.includes("-v");
|
|
16
|
+
|
|
17
|
+
if (showHelp) {
|
|
18
|
+
console.log(`
|
|
19
|
+
Repolet v${VERSION}
|
|
20
|
+
|
|
21
|
+
Copy a specific folder from a GitHub repository.
|
|
22
|
+
|
|
23
|
+
Usage:
|
|
24
|
+
repolet <github-folder-url> [destination]
|
|
25
|
+
|
|
26
|
+
Examples:
|
|
27
|
+
repolet https://github.com/user/repo/tree/main/src
|
|
28
|
+
repolet https://github.com/user/repo/tree/main/src my-src
|
|
29
|
+
|
|
30
|
+
Options:
|
|
31
|
+
-h, --help Show help
|
|
32
|
+
-v, --version Show version
|
|
33
|
+
`);
|
|
34
|
+
|
|
35
|
+
process.exit(0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (showVersion) {
|
|
39
|
+
console.log(`repolet v${VERSION}`);
|
|
40
|
+
process.exit(0);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const githubUrl = cleanArgs[0];
|
|
44
|
+
const customDestination = cleanArgs[1];
|
|
45
|
+
|
|
46
|
+
if (!githubUrl) {
|
|
47
|
+
console.log("Usage: repolet <github-folder-url> [destination]");
|
|
48
|
+
|
|
49
|
+
console.log("Run 'repolet --help' for more information.");
|
|
50
|
+
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
// --------------------------------
|
|
56
|
+
// 1. Parse GitHub URL
|
|
57
|
+
// --------------------------------
|
|
58
|
+
const folder = parseGitHubUrl(githubUrl);
|
|
59
|
+
|
|
60
|
+
console.log("\nRepository information:");
|
|
61
|
+
console.log(` Owner : ${folder.owner}`);
|
|
62
|
+
console.log(` Repo : ${folder.repo}`);
|
|
63
|
+
console.log(` Type : ${folder.type}`);
|
|
64
|
+
console.log(` Branch: ${folder.branch || "default"}`);
|
|
65
|
+
console.log(` Path : ${folder.path || "/"}`);
|
|
66
|
+
|
|
67
|
+
// --------------------------------
|
|
68
|
+
// 2. Determine destination
|
|
69
|
+
// --------------------------------
|
|
70
|
+
const destinationRoot =
|
|
71
|
+
customDestination ||
|
|
72
|
+
(folder.type === "folder" ? folder.path.split("/").pop() : folder.repo);
|
|
73
|
+
|
|
74
|
+
// --------------------------------
|
|
75
|
+
// 3. Check destination
|
|
76
|
+
// --------------------------------
|
|
77
|
+
try {
|
|
78
|
+
await fs.access(destinationRoot);
|
|
79
|
+
|
|
80
|
+
console.error(`\n❌ Destination already exists: ./${destinationRoot}`);
|
|
81
|
+
|
|
82
|
+
console.error("Choose another destination or remove the existing folder.");
|
|
83
|
+
|
|
84
|
+
process.exit(1);
|
|
85
|
+
} catch {
|
|
86
|
+
// Destination doesn't exist.
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// --------------------------------
|
|
90
|
+
// 4. Find files
|
|
91
|
+
// --------------------------------
|
|
92
|
+
console.log("\nScanning folder recursively...");
|
|
93
|
+
|
|
94
|
+
const files = await getAllFiles(folder);
|
|
95
|
+
|
|
96
|
+
console.log(`Found ${files.length} files.`);
|
|
97
|
+
|
|
98
|
+
if (files.length === 0) {
|
|
99
|
+
console.log("\n⚠️ Folder is empty.");
|
|
100
|
+
process.exit(0);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// --------------------------------
|
|
104
|
+
// 5. Download
|
|
105
|
+
// --------------------------------
|
|
106
|
+
console.log(`\nDownloading to: ./${destinationRoot}\n`);
|
|
107
|
+
|
|
108
|
+
let successful = 0;
|
|
109
|
+
let failed = 0;
|
|
110
|
+
|
|
111
|
+
for (let i = 0; i < files.length; i++) {
|
|
112
|
+
const file = files[i];
|
|
113
|
+
|
|
114
|
+
const progress = `[${i + 1}/${files.length}]`;
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const outputPath = await downloadFile(file, destinationRoot, folder.path);
|
|
118
|
+
|
|
119
|
+
successful++;
|
|
120
|
+
|
|
121
|
+
console.log(`✓ ${progress} ${outputPath}`);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
failed++;
|
|
124
|
+
|
|
125
|
+
console.error(`✗ ${progress} ${file.path}`);
|
|
126
|
+
|
|
127
|
+
console.error(` ${error.message}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// --------------------------------
|
|
132
|
+
// 6. Summary
|
|
133
|
+
// --------------------------------
|
|
134
|
+
console.log("\n--------------------------------");
|
|
135
|
+
console.log("Download finished");
|
|
136
|
+
console.log("--------------------------------");
|
|
137
|
+
|
|
138
|
+
console.log(`Successful: ${successful}`);
|
|
139
|
+
console.log(`Failed : ${failed}`);
|
|
140
|
+
console.log(`Location : ./${destinationRoot}`);
|
|
141
|
+
|
|
142
|
+
if (failed > 0) {
|
|
143
|
+
console.log("\n⚠️ Some files could not be downloaded.");
|
|
144
|
+
|
|
145
|
+
process.exitCode = 1;
|
|
146
|
+
} else {
|
|
147
|
+
console.log("\n✅ Download complete!");
|
|
148
|
+
}
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error("\n❌ Error:", error.message);
|
|
151
|
+
process.exit(1);
|
|
152
|
+
}
|
package/src/parser.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export function parseGitHubUrl(url) {
|
|
2
|
+
let parsedUrl;
|
|
3
|
+
|
|
4
|
+
try {
|
|
5
|
+
parsedUrl = new URL(url);
|
|
6
|
+
} catch {
|
|
7
|
+
throw new Error("Invalid URL.");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (parsedUrl.hostname !== "github.com") {
|
|
11
|
+
throw new Error("Only github.com URLs are supported.");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const parts = parsedUrl.pathname
|
|
15
|
+
.split("/")
|
|
16
|
+
.filter(Boolean);
|
|
17
|
+
|
|
18
|
+
if (parts.length < 2) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"Invalid GitHub URL. Expected /owner/repository"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const owner = parts[0];
|
|
25
|
+
|
|
26
|
+
// Remove .git if the user supplied:
|
|
27
|
+
// https://github.com/user/repo.git
|
|
28
|
+
const repo = parts[1].replace(/\.git$/, "");
|
|
29
|
+
|
|
30
|
+
// Whole repository:
|
|
31
|
+
//
|
|
32
|
+
// github.com/user/repo
|
|
33
|
+
//
|
|
34
|
+
if (parts.length === 2) {
|
|
35
|
+
return {
|
|
36
|
+
owner,
|
|
37
|
+
repo,
|
|
38
|
+
branch: null,
|
|
39
|
+
path: "",
|
|
40
|
+
type: "repository"
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Folder:
|
|
45
|
+
//
|
|
46
|
+
// github.com/user/repo/tree/main/src
|
|
47
|
+
//
|
|
48
|
+
if (parts[2] !== "tree") {
|
|
49
|
+
throw new Error(
|
|
50
|
+
"Only GitHub repository or folder URLs are supported."
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (parts.length < 5) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
"Invalid GitHub folder URL. Expected /owner/repo/tree/branch/folder"
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const branch = parts[3];
|
|
61
|
+
const pathParts = parts.slice(4);
|
|
62
|
+
|
|
63
|
+
if (!branch) {
|
|
64
|
+
throw new Error("GitHub branch is missing.");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (pathParts.length === 0) {
|
|
68
|
+
throw new Error("GitHub folder path is missing.");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
owner,
|
|
73
|
+
repo,
|
|
74
|
+
branch,
|
|
75
|
+
path: pathParts.join("/"),
|
|
76
|
+
type: "folder"
|
|
77
|
+
};
|
|
78
|
+
}
|