Skip to content

Commit 614612d

Browse files
authored
feat: Add glob support (#2)
1 parent 3dbd1a9 commit 614612d

File tree

5 files changed

+214
-90
lines changed

5 files changed

+214
-90
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# octoherd-script-get-files
22

3-
> An Octoherd script to download files from repositories
3+
> The easiest way to download files from GitHub.
44
55
[![@latest](https://img.shields.io/npm/v/octoherd-script-get-files.svg)](https://www.npmjs.com/package/octoherd-script-get-files)
66
[![Build Status](https://github.com/stefanbuck/octoherd-script-get-files/workflows/Test/badge.svg)](https://github.com/stefanbuck/octoherd-script-get-files/actions?query=workflow%3ATest+branch%3Amain)
@@ -12,30 +12,64 @@ Minimal usage
1212
```js
1313
npx octoherd-script-get-files \
1414
--source README.md \
15-
--target ./out
15+
--output ./out
1616
```
1717

1818
Pass all options as CLI flags to avoid user prompts
1919

2020
```js
2121
npx octoherd-script-get-files \
2222
-T ghp_0123456789abcdefghjklmnopqrstuvwxyzA \
23-
-R "stefanbuck/*" \
23+
-R "octolinker/*" \
2424
--source README.md \
25-
--target ./out
25+
--output ./out
2626
```
2727

2828
## Options
2929

3030
| option | type | description |
3131
| --------------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
32-
| `--source` | string | **Required.** Path to the destination directory |
33-
| `--target` | string | **Required.** File path to download. Note: Directories are not supported yet |
32+
| `--source` | string | **Required.** File to download. This can also be a Glob see [example](#examples). |
33+
| `--output` | string | **Required.** Specify a path to place the downloaded file or directory (instead of using the current working directory). Directories specified in the path will be created by this command. |
3434
| `--ignore-archived` or `--no-ignore-archived` | boolean | Default `true`. Ignores archive repositories |
3535
| `--octoherd-token`, `-T` | string | A personal access token ([create](https://github.com/settings/tokens/new?scopes=repo)). Script will create one if option is not set |
3636
| `--octoherd-repos`, `-R` | array of strings | One or multiple space-separated repositories in the form of `repo-owner/repo-name`. `repo-owner/*` will find all repositories for one owner. `*` will find all repositories the user has access to. Will prompt for repositories if not set |
3737
| `--octoherd-bypass-confirms` | boolean | Bypass prompts to confirm mutating requests |
3838

39+
## Examples
40+
41+
Download a single file
42+
43+
```js
44+
npx octoherd-script-get-files -R octolinker/octolinker --source=README.md --output=./out
45+
```
46+
47+
Download a single file by full path
48+
49+
```js
50+
npx octoherd-script-get-files -R octolinker/octolinker --source=.github/PULL_REQUEST_TEMPLATE.md --output=./out
51+
```
52+
53+
Download recursively all files with a certain file extension
54+
55+
```js
56+
npx octoherd-script-get-files -R octolinker/octolinker --source='**/*.html' --output=./out
57+
```
58+
59+
Download recursively all files from a specific folder
60+
61+
```js
62+
npx octoherd-script-get-files -R octolinker/octolinker --source='.github/**/*' --output=./out
63+
```
64+
65+
Download everything
66+
67+
```js
68+
npx octoherd-script-get-files -R octolinker/octolinker --source='**/*' --output=./out
69+
```
70+
71+
Don't know how to write Glob? Check out DigitalOcean's amazing [Glob testing tool](https://www.digitalocean.com/community/tools/glob).
72+
3973
## Contributing
4074

4175
See [CONTRIBUTING.md](CONTRIBUTING.md)

helper.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os from 'os'
2+
import fs from 'fs'
3+
import path from 'path'
4+
import minimatch from "minimatch"
5+
6+
export async function downloadFile(octokit, repository, target, source) {
7+
const filepath = [repository.owner.login, repository.name, source].join('/');
8+
9+
return octokit.request(
10+
`GET /repos/{owner}/{repo}/contents/${source}`,
11+
{
12+
owner: repository.owner.login,
13+
repo: repository.name,
14+
headers: {
15+
Accept: "application/vnd.github.v3.raw"
16+
}
17+
}
18+
).then((res) => {
19+
octokit.log.info(`Download ${filepath}`);
20+
21+
// Expand the ~ character to a users home directory
22+
const newTarget = target.replace("~", os.homedir)
23+
24+
const targetFile = path.join(newTarget, repository.owner.login, repository.name, source);
25+
const targetPath = path.join(newTarget, repository.owner.login, repository.name, path.dirname(source));
26+
27+
if (!fs.existsSync(targetPath)) {
28+
fs.mkdirSync(targetPath, { recursive: true });
29+
}
30+
31+
fs.writeFileSync(targetFile, res.data)
32+
}).catch(error => {
33+
if (error.status === 404) {
34+
octokit.log.warn(`File ${filepath} not found`);
35+
return false;
36+
}
37+
38+
throw error;
39+
})
40+
}
41+
42+
export async function getListOfFilesToDownload(octokit, repository, source) {
43+
const res = await octokit.request(
44+
`GET /repos/{owner}/{repo}/git/trees/HEAD?recursive=true`,
45+
{
46+
owner: repository.owner.login,
47+
repo: repository.name,
48+
headers: {
49+
Accept: "application/vnd.github.v3.raw"
50+
}
51+
}).catch(error => {
52+
if (error.status === 409) {
53+
octokit.log.warn(`Git Repository is empty`);
54+
return { data: { tree: [] } };
55+
}
56+
57+
throw error;
58+
})
59+
60+
const tree = res?.data?.tree?.filter(item => item.type === 'blob').map(item => item.path);
61+
62+
return tree.filter(item => minimatch(item, source))
63+
}

package-lock.json

Lines changed: 89 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"author": "Stefan Buck (stefanbuck.com)",
1919
"license": "ISC",
2020
"dependencies": {
21-
"@octoherd/cli": "^3.4.11"
21+
"@octoherd/cli": "^3.4.11",
22+
"is-glob": "^4.0.3",
23+
"minimatch": "^5.1.0"
2224
},
2325
"release": {
2426
"branches": [

0 commit comments

Comments
 (0)