Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

changed method of loading npm packages #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const fs = require('fs')
"allPackagesOutput" : "/path/to/allpackages.json"
, "repositoriesOutput" : "/path/to/repositories.json"
, "githubOutput" : "/path/to/githubusers.json"
, "aussieOutput" : "/path/to/aussieOutput.json"
Copy link
Member

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

Copy link
Member Author

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

, "githubAuthToken" : "yourgithubauthtoken"
}

Expand Down
71 changes: 49 additions & 22 deletions load-npm-data.js
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
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after function, before {

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
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
"author": "",
"license": "MIT",
"dependencies": {
"npm": "~1.2.18",
"async": "~0.2.7",
"request": "~2.20.0",
"function-rate-limit": "0.0.1"
"async": "~1.5.0",
"function-rate-limit": "0.0.1",
"request": "~2.67.0"
},
"private": true
}
}