This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
changed method of loading npm packages #4
Open
MauriceButler
wants to merge
2
commits into
polyhack:master
Choose a base branch
from
MauriceButler:npm-package-load
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// if your GitHub location field matches this then we'll guess you're Aussie | ||
const GITHUB_REPO_REGEX = /github.com[:\/]([\.\-\w]+)\/([^$\/\.]+)/ | ||
|
||
const npm = require('npm') | ||
, NPM_ALL_PACKAGES_URL = 'https://skimdb.npmjs.com/registry/_all_docs' | ||
, NPM_SINGLE_PACKAGE_URL = 'https://registry.npmjs.org/{packageId}/latest' | ||
, request = require('request').defaults({json:true}) | ||
, async = require('async'); | ||
|
||
function matchGitHubRepo (npmPackage, repo) { | ||
var match = repo | ||
|
@@ -15,34 +17,59 @@ function matchGitHubRepo (npmPackage, repo) { | |
} | ||
} | ||
|
||
// load the list of all npm libs with 'repo' pointing to GitHub | ||
function loadNpmData (callback) { | ||
function getPackageData (repositories, allPackages, packageData, callback) { | ||
request(NPM_SINGLE_PACKAGE_URL.replace('{packageId}', packageData.id), function (err, response, data) { | ||
if (err) { | ||
// log and continue usually just a timeout, possibly needs retry logic | ||
console.log('error getting data for package: ' + packageData.id, err.message) | ||
return callback() | ||
} | ||
|
||
// Bad maintainers property there are MANY just skip for much speed increase | ||
if (!data.maintainers || !Array.isArray(data.maintainers)) { | ||
return callback() | ||
} | ||
|
||
var repo = matchGitHubRepo(data.name, data.repository); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semi |
||
|
||
if (repo) { | ||
repositories.push(repo) | ||
} | ||
|
||
allPackages.push({ | ||
name : data.name | ||
, maintainers : (data.maintainers || []).map(function (m) { return m && m.name }) | ||
, githubUser : repo ? repo.githubUser : null | ||
, githubRepo : repo ? repo.githubRepo : null | ||
, description : data.description | ||
}) | ||
|
||
callback() | ||
}) | ||
} | ||
|
||
function getAllPackages (callback) { | ||
var repositories = [] | ||
, allPackages = [] | ||
|
||
npm.load(function (err) { | ||
if (err) return callback(err) | ||
|
||
npm.registry.get('/-/all', function (err, data) { | ||
if (err) return callback(err) | ||
// https://github.com/npm/npm-registry-couchapp/issues/162 | ||
request(NPM_ALL_PACKAGES_URL, function(err, response, body){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
Object.keys(data).forEach(function (k) { | ||
var repo = matchGitHubRepo(data[k].name, data[k].repository) | ||
if (repo) | ||
repositories.push(repo) | ||
if (!body || !body.rows) { | ||
body = { rows: [] }; | ||
} | ||
|
||
allPackages.push({ | ||
name : data[k].name | ||
, maintainers : (data[k].maintainers || []).map(function (m) { return m.name }) | ||
, githubUser : repo ? repo.githubUser : null | ||
, githubRepo : repo ? repo.githubRepo : null | ||
, description : data[k].description | ||
}) | ||
}) | ||
async.mapLimit(body.rows, 10, getPackageData.bind(null, repositories, allPackages), function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
callback(null, { repositories: repositories, allPackages: allPackages }) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = loadNpmData | ||
module.exports = getAllPackages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this correct? I don't think this is needed here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to as is used on line 87