Skip to content

Commit

Permalink
Merge GitHub support from 'origin/github-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
delfrrr committed Aug 10, 2018
2 parents eba62db + 70ed4e6 commit 3b60ec4
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 13 deletions.
119 changes: 109 additions & 10 deletions lib/getPackageDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const semver = require('semver');
const getLicenseStr = require('./getLicenseStr');
const getLicenseType = require('./getLicenseType');
const url = require('url');
const colors = require('colors/safe');
const printError = require('./printError');
const readline = require('readline');

const packageDetailsCache = {};
Expand All @@ -23,7 +23,7 @@ function checkResponse(r) {
if (r.ok) {
return r.json();
}
throw new Error(`Response is not ok`);
throw new Error(`Response is not ok ${r.status} ${r.statusText} ${r.url}`);
}

/**
Expand All @@ -50,6 +50,88 @@ function getVersion(versionLoose, versions) {
return version;
}

const gitHubApiUrl = 'https://api.github.com/';

/**
* @param {string} owner
* @param {string} repo
* @returns {Object} details
* @returns {String} details.license
* @returns {Number} details.size
* @returns {String} details.modified
*/
function getSizeAndLicenseFromGitHub(owner, repo) {
const repoInfoUrl = `${gitHubApiUrl}repos/${owner}/${repo}`;// I believe size of downloaded repo will not depend on ref
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 1);
process.stdout.write(`GET ${repoInfoUrl}`);
return fetch(repoInfoUrl).then(checkResponse).then(({
size: sizeKb,
license: licenseObj,
updated_at: modified
}) => {
const size = sizeKb * 1024;
const license = licenseObj && licenseObj.spdx_id || 'Unknown';
return { size, license, modified };
});
}

/**
* @see https://developer.github.com/v3/repos/contents/
* @param {string} owner
* @param {string} repo
* @param {string} ref
* @returns {object} dependencies, name, version
*/
function getPackageJsonFromGitHub(owner, repo, ref) {
const packageJsonUrl = `${gitHubApiUrl}repos/${owner}/${repo}/contents/package.json?ref=${ref}`;
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 1);
process.stdout.write(`GET ${packageJsonUrl}`);
return fetch(packageJsonUrl).then(checkResponse).then(({ download_url: downloadUrl }) => {
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 1);
process.stdout.write(`GET ${downloadUrl}`);
return fetch(downloadUrl).then(checkResponse);
}).then((packageJson) => {
const dependencies = packageJson.dependencies || {};
const { name, version } = packageJson;
return { dependencies, name, version };
}, (e) => {
printError(`Cannot fetch package.json from GitHub for ${owner}/${repo}`);
throw e;
});
}

/**
* @param {object} urlObj
* @param {string} versionLoose
* @returns {object} details like dependencies, version, size, license, modified
*/
function getPackageDetailsFromGitHub({ host, path, hash, protocol }, versionLoose) {
let owner;
let repo;
if (protocol === 'github:') {
owner = host;
repo = path.slice(1);
} else {
[owner, repo] = String(path).slice(1).replace(/\.git$/, '').split('/');
}
if (!owner || !repo) {
throw new Error(`Cannot parse github dependency url ${versionLoose}`);
}
let ref = 'master';
if (hash && hash.slice(1)) {
ref = hash.slice(1);
}
return Promise.all([
getSizeAndLicenseFromGitHub(owner, repo),
getPackageJsonFromGitHub(owner, repo, ref)
]).then((detailsAr) => {
return Object.assign({}, ...detailsAr);
});
}

/**
* @param {string} name package name
* @param {string} versionLoose version selector
Expand All @@ -61,14 +143,31 @@ module.exports = function getPackageDetails(
) {
const versionUrlObj = url.parse(versionLoose);
if (versionUrlObj.protocol) {
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 1);
console.error(
colors.red(`${
versionUrlObj.protocol.replace(':', '')
} is not supported by npm-consider, skipping ${
versionLoose
}`));
if (versionUrlObj.host === 'github.com' || versionUrlObj.protocol === 'github:') {
// TODO: cache result
return getPackageDetailsFromGitHub(versionUrlObj, versionLoose).then(({
dependencies, version, size, license, modified
}) => {
return {
name,
modified,
version,
license,
licenseType: getLicenseType(license),
dependencies,
versionLoose,
size
};
}, (e) => {
printError(e);
return Promise.resolve(null);
});
}
printError(`${
versionUrlObj.protocol
} is not supported by npm-consider, skipping ${
versionLoose
}`);
return Promise.resolve(null);
}
const key = `${name}@${versionLoose}`;
Expand Down
5 changes: 3 additions & 2 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const filesize = require('filesize');
const getSimpleTable = require('./getSimpleTable');
const program = require('commander');
const readline = require('readline');
const printError = require('./printError');

/**
* indicates if any limits of test config are not satisfied
Expand Down Expand Up @@ -175,7 +176,7 @@ module.exports = function install() {
if (options.test) {
// TODO: verify that test config is ok
if (testFailed) {
console.error(colors.red(`Limits provided in package.json are not satisfied`));
printError(`Limits provided in package.json are not satisfied`);
process.exit(1);
}
return;
Expand All @@ -184,7 +185,7 @@ module.exports = function install() {
});
})
.catch((e) => {
console.error(e);
printError(e);
process.exit(1);
});
};
15 changes: 15 additions & 0 deletions lib/printError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* cleans a line and outputs the error
*/

const colors = require('colors/safe');
const readline = require('readline');

/**
* @param {string} str
*/
module.exports = function printError(str) {
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 1);
console.error(colors.red(str));
};
3 changes: 2 additions & 1 deletion lib/walkDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const Queue = require('promise-queue');
const getPackageDetails = require('./getPackageDetails');
const printError = require('./printError');

/**
* recursive walk
Expand Down Expand Up @@ -43,7 +44,7 @@ function walk(
}
})
.catch((e) => {
console.error(e);
printError(e);
process.exit(1);
});
});
Expand Down

0 comments on commit 3b60ec4

Please sign in to comment.