Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Support different default branches #8

Merged
merged 5 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [10, 12, 14]
node: [12, 14, 16]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
43 changes: 30 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,40 @@ class Downloader {
this._octokit = new Octokit(options.github);
}

async getTree(owner, repo, options = {}) {
var sha = options.sha || 'master';
var cacheKey = `${owner}/${repo}#${sha}`;
async recurseTree(owner, repo, directory, options = {}) {
var {
data
} = await this._octokit.repos.getContent({
owner,
repo,
ref: options.sha,
path: directory,
});

var recurseDirs = data.map((node) => {
if (node.type === 'dir') {
return this.recurseTree(owner, repo, node.path, options);
}
return {
path: node.path,
type: node.type,
sha: node.sha,
};
});

return Promise.all(recurseDirs).then((nodes) => nodes.flat());
}

async getTree(owner, repo, directory, options = {}) {
var sha = options.sha;
var cacheKey = sha ? `${owner}/${repo}#${sha}` : `${owner}/${repo}`;

var cachedTree = await this.cache.get(cacheKey);
if (cachedTree) {
return cachedTree;
}

var {
data: { tree },
} = await this._octokit.git.getTree({
owner,
repo,
tree_sha: sha,
recursive: true,
});
var tree = await this.recurseTree(owner, repo, directory, options);

await this.cache.set(cacheKey, tree);

Expand All @@ -64,10 +81,10 @@ class Downloader {
}

async fetchFiles(owner, repo, directory, options = {}) {
var tree = await this.getTree(owner, repo, options);
var tree = await this.getTree(owner, repo, directory, options);

var files = tree
.filter((node) => node.path.startsWith(directory) && node.type === 'blob')
.filter((node) => node.path.startsWith(directory) && node.type === 'file')
.map(async (node) => {
var { data } = await this._octokit.git.getBlob({
owner,
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Blaine Bublitz <blaine.bublitz@gmail.com> (https://github.com/phated)",
"license": "MIT",
"engines": {
"node": ">=10.13.0"
"node": ">=12.0.0"
},
"main": "index.js",
"files": [
Expand All @@ -21,15 +21,15 @@
"test": "nyc mocha --async-only"
},
"dependencies": {
"@octokit/rest": "^17.10.0",
"@octokit/rest": "^18.12.0",
"keyv": "^4.0.1",
"minimist": "^1.2.0"
},
"devDependencies": {
"eslint": "^6.8.0",
"expect": "^26.0.1",
"eslint": "^7.0.0",
"expect": "^27.0.0",
"keyv-file": "^0.2.0",
"mocha": "^7.2.0",
"mocha": "^8.0.0",
"nyc": "^15.1.0"
},
"prettier": {
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('github-download-directory', function () {
});

it('uses the cache values if available', async function () {
var key = 'phated/github-download-directory#master';
var key = 'phated/github-download-directory';
var cachedTree = await downloader.cache.get(key);
await downloader.cache.set(key, cachedTree.filter(onlyIndex));

Expand Down