list-github-dir-content
Advanced tools
Comparing version 2.0.0 to 3.0.0-0
68
index.js
const fetch = require('node-fetch'); // Automatically excluded in browser bundles | ||
async function api(endpoint, token) { | ||
// Matches '/<user>/<repo>/tree/<ref>/<dir>' | ||
const urlParserRegex = /^[/]([^/]+)[/]([^/]+)[/]tree[/]([^/]+)[/](.*)/; | ||
function parseResource(resource) { | ||
if (typeof resource === 'string') { | ||
const parsedUrl = new URL(resource); | ||
resource = {}; | ||
[, resource.user, resource.repository, resource.ref, resource.directory] = | ||
urlParserRegex.exec(parsedUrl.pathname) || []; | ||
if (typeof resource.directory !== 'string') { | ||
throw new TypeError('Unable to parse GitHub URL'); | ||
} | ||
if (parsedUrl.hostname !== 'github.com') { | ||
resource.api = `https://${parsedUrl.host}/api/v3`; | ||
} | ||
} | ||
resource.api = resource.api || 'https://api.github.com'; | ||
resource.ref = resource.ref || 'HEAD'; | ||
// TODO: Validate | ||
return resource; | ||
} | ||
async function githubApi(api, endpoint, token) { | ||
token = token ? `&access_token=${token}` : ''; | ||
const response = await fetch(`https://api.github.com/repos/${endpoint}${token}`); | ||
const response = await fetch(`${api}/repos/${endpoint}${token}`); | ||
return response.json(); | ||
@@ -12,12 +36,15 @@ } | ||
async function viaContentsApi({ | ||
user, | ||
repository, | ||
ref = 'HEAD', | ||
directory, | ||
resource, | ||
token, | ||
getFullData = false | ||
}) { | ||
resource = parseResource(resource); | ||
const files = []; | ||
const requests = []; | ||
const contents = await api(`${user}/${repository}/contents/${directory}?ref=${ref}`, token); | ||
const contents = await githubApi( | ||
resource.api, | ||
`${resource.user}/${resource.repository}/contents/${resource.directory}?ref=${resource.ref}`, | ||
token | ||
); | ||
for (const item of contents) { | ||
@@ -28,6 +55,3 @@ if (item.type === 'file') { | ||
requests.push(viaContentsApi({ | ||
user, | ||
repository, | ||
ref, | ||
directory: item.path, | ||
resource, | ||
token, | ||
@@ -46,17 +70,21 @@ getFullData | ||
async function viaTreesApi({ | ||
user, | ||
repository, | ||
ref = 'HEAD', | ||
directory, | ||
resource, | ||
token, | ||
getFullData = false | ||
}) { | ||
if (!directory.endsWith('/')) { | ||
directory += '/'; | ||
resource = parseResource(resource); | ||
const files = []; | ||
const contents = await githubApi( | ||
resource.api, | ||
`${resource.user}/${resource.repository}/git/trees/${resource.ref}?recursive=1`, | ||
token | ||
); | ||
if (!resource.directory.endsWith('/')) { | ||
resource.directory += '/'; | ||
} | ||
const files = []; | ||
const contents = await api(`${user}/${repository}/git/trees/${ref}?recursive=1`, token); | ||
for (const item of contents.tree) { | ||
if (item.type === 'blob' && item.path.startsWith(directory)) { | ||
if (item.type === 'blob' && item.path.startsWith(resource.directory)) { | ||
files.push(getFullData ? item : item.path); | ||
@@ -63,0 +91,0 @@ } |
{ | ||
"name": "list-github-dir-content", | ||
"version": "2.0.0", | ||
"version": "3.0.0-0", | ||
"description": "List all the files in a GitHub repo’s directory", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -71,2 +71,10 @@ # list-github-dir-content [![Build Status](https://travis-ci.org/fregante/list-github-dir-content.svg?branch=master)](https://travis-ci.org/fregante/list-github-dir-content) | ||
#### api | ||
Type: `string` | ||
Default: `"https://api.github.com"` | ||
The GitHub API endpoint to use, like `https://ghe.example.com/api`. | ||
#### user | ||
@@ -73,0 +81,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7997
85
126
2