Skip to content

Commit 015141b

Browse files
committed
feat: 添加 token 参数
1 parent 775105a commit 015141b

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
用于从 GitHub 仓库下载特定内容的命令行工具。它允许你指定仓库的所有者和名称、输出目录、分支、子路径、glob 表达式等参数,方便地下载你需要的文件
44

5+
> GitHub 对于未认证的用户会有速率限制 <https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api> 你可以在环境变量中添加 `GITHUB_TOKEN` 或者 `-t`避免超出限制, 你可以在 <https://github.com/settings/personal-access-tokens> 创建 `GITHUB_TOKEN`
6+
57
# 安装
68

79
```shell
@@ -21,10 +23,13 @@ git-dl [options] [command] <owner/repo> [output-dir]
2123

2224
选项说明:
2325

24-
- `-b, --branch <char>`:指定要下载的分支名称,默认值为 `main`
25-
- `-s, --subpath <char>`:指定要下载的子路径。
26-
- `-g, --glob <char>`:指定 glob 表达式,用于筛选要下载的文件。
27-
- `-d, --debug`:启用调试模式,显示详细的调试日志。
26+
+ -`V`, --version output the version number
27+
+ -`b`, --branch <char> branch name (default: "main")
28+
+ -`s`, --subpath <char> subpath
29+
+ -`g`, --glob <char> glob expressions
30+
+ -`d`, --debug show debug log
31+
+ -`t`, --token <char> github token
32+
+ -`h`, --help display help for command
2833

2934
示例:
3035

src/github.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Options, Tree } from './type.js';
2-
import { createWriteStream } from 'node:fs';
3-
import { access, constants, mkdir } from 'node:fs/promises';
2+
import { Buffer } from 'node:buffer';
3+
// import { createWriteStream } from 'node:fs';
4+
import { access, constants, mkdir, writeFile } from 'node:fs/promises';
45
import { join, parse } from 'node:path';
56
import process from 'node:process';
6-
import { pipeline } from 'node:stream/promises';
7+
// import { pipeline } from 'node:stream/promises';
78
import { minimatch } from 'minimatch';
89
import { request } from './request.js';
910

@@ -31,7 +32,7 @@ export async function getGitTree(options: Options, base: string) {
3132
if (result.status === '404') {
3233
throw new Error(`${owner}/${repo}(${branch}): ${result.message}`);
3334
}
34-
throw new Error(result);
35+
throw new Error(JSON.stringify(result, null, 2));
3536
}
3637

3738
const tree = result.tree.map((v: Tree) => {
@@ -50,10 +51,12 @@ export function getOutPutPath(options: Options) {
5051
}
5152

5253
export async function writeFileFromItem(item: Tree) {
53-
const blob = (await request(item._url)).body;
54-
54+
const blob: any = await (await request(item.url)).json();
5555
await mkdirRecursive(parse(item._out).dir);
56-
return pipeline(blob, createWriteStream(item._out));
56+
return writeFile(item._out, Buffer.from(blob?.content, 'base64'));
57+
// const blob = (await request(item._url)).body;
58+
// await mkdirRecursive(parse(item._out).dir);
59+
// return pipeline(blob, createWriteStream(item._out));
5760
}
5861

5962
export async function mkdirRecursive(dir: string, options?: { existsHandler: any }) {

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ program
1616
.option('-s, --subpath <char>', 'subpath')
1717
.option('-g, --glob <char>', 'glob expressions')
1818
.option('-d, --debug', 'show debug log')
19+
.option('-t, --token <char>', 'github token')
1920
.action(async (ownerRepoName: string, outputDir, options) => {
2021
const [owner, repo] = ownerRepoName.split('/');
2122

2223
globalThis.debug = options.debug;
24+
globalThis.token = options.token;
2325

2426
await download({
2527
owner,

src/request.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { env } from 'node:process';
12
import { debug } from './logger.js';
23

3-
export async function request(url: string, init?: RequestInit) {
4+
export async function request(url: string, init: RequestInit = {}) {
5+
const TOKEN = globalThis.token || env.GITHUB_TOKEN;
6+
if (TOKEN) {
7+
init.headers = new Headers({
8+
Authorization: `Bearer ${TOKEN}`,
9+
});
10+
}
411
debug(`requset: ${url}`);
512
let result: Promise<Response>;
613
let count = 0;

0 commit comments

Comments
 (0)