New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

list-github-dir-content

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

list-github-dir-content - npm Package Compare versions

Comparing version 1.1.1 to 2.0.0

48

index.js

@@ -9,19 +9,27 @@ const fetch = require('node-fetch'); // Automatically excluded in browser bundles

function parseIdentifier(identifier) {
const [repo, ref = 'HEAD'] = identifier.split('#');
return {repo, ref};
}
// Great for downloads with few sub directories on big repos
// Cons: many requests if the repo has a lot of nested dirs
async function viaContentsApi(identifier, dir, token) {
async function viaContentsApi({
user,
repository,
ref = 'HEAD',
directory,
token,
getFullData = false
}) {
const files = [];
const requests = [];
const {repo, ref} = parseIdentifier(identifier);
const contents = await api(`${repo}/contents/${dir}?ref=${ref}`, token);
const contents = await api(`${user}/${repository}/contents/${directory}?ref=${ref}`, token);
for (const item of contents) {
if (item.type === 'file') {
files.push(item.path);
files.push(getFullData ? item : item.path);
} else if (item.type === 'dir') {
requests.push(viaContentsApi(repo, item.path, token));
requests.push(viaContentsApi({
user,
repository,
ref,
directory: item.path,
token,
getFullData
}));
}

@@ -36,13 +44,19 @@ }

// Cons: huge on huge repos + may be truncated
async function viaTreesApi(identifier, dir, token) {
if (!dir.endsWith('/')) {
dir += '/';
async function viaTreesApi({
user,
repository,
ref = 'HEAD',
directory,
token,
getFullData = false
}) {
if (!directory.endsWith('/')) {
directory += '/';
}
const files = [];
const {repo, ref} = parseIdentifier(identifier);
const contents = await api(`${repo}/git/trees/${ref}?recursive=1`, token);
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(dir)) {
files.push(item.path);
if (item.type === 'blob' && item.path.startsWith(directory)) {
files.push(getFullData ? item : item.path);
}

@@ -49,0 +63,0 @@ }

{
"name": "list-github-dir-content",
"version": "1.1.1",
"version": "2.0.0",
"description": "List all the files in a GitHub repo’s directory",

@@ -5,0 +5,0 @@ "keywords": [

@@ -20,7 +20,23 @@ # 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)

// They have the same output
const filesArray = await listContent.viaTreesApi('Microsoft/vscode', 'src', myToken);
const filesArray = await listContent.viaTreesApi({
user: 'microsoft',
repository: 'vscode',
directory: 'src',
token: myToken
});
// OR
const filesArray = await listContent.viaContentsApi('Microsoft/vscode', 'src', myToken);
const filesArray = await listContent.viaContentsApi({
user: 'microsoft',
repository: 'vscode',
directory: 'src',
token: myToken
});
// OR
const filesArray = await listContent.viaContentsApi('Microsoft/vscode#master', 'src', myToken);
const filesArray = await listContent.viaContentsApi({
user: 'microsoft',
repository: 'vscode',
ref: 'master',
directory: 'src',
token: myToken
});

@@ -38,4 +54,4 @@ // ['src/file.js', 'src/styles/main.css', ...]

### listContent.viaTreesApi(identifier, directory, token)
### listContent.viaContentsApi(identifier, directory, token)
### listContent.viaTreesApi(options)
### listContent.viaContentsApi(options)

@@ -55,9 +71,24 @@ Both methods return a Promise that resolves with an array of all the files in the chosen directory. They just vary in GitHub API method used. The paths will be relative to root (i.e. if `directory` is `dist/images`, the array will be `['dist/images/1.png', 'dist/images/2.png']`)

The following properties are available on the `options` object:
#### identifier
#### user
Type: `string`
`user/repo` or `user/repo#reference` combination, such as `Microsoft/vscode` or `Microsoft/vscode#master`. If the reference is omitted, the default branch will be used.
GitHub user or organization, such as `microsoft`.
#### repository
Type: `string`
The user's repository to read, like `vscode`.
#### ref
Type: `string`
Default: `"HEAD"`
The reference to use, for example a pointer (`"HEAD"`), a branch name (`"master"`) or a commit hash (`"71705e0"`).
#### directory

@@ -75,3 +106,13 @@

#### getFullData
Type: `boolean`
Default: `false`
When set to `true`, an array of metadata objects is returned instead of an array of file paths. Note that the metadata objects of `viaTreesApi` and `viaContentsApi` are different.
Take a look at the docs for either the [Git Trees API](https://developer.github.com/v3/git/trees/#response) and the [Contents API](https://developer.github.com/v3/repos/contents/#response) to see how the respective metadata is structured.
## License

@@ -78,0 +119,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc