Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native npm commands #4

Closed
wants to merge 2 commits into from
Closed
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
57 changes: 18 additions & 39 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var axios = require('axios');
var util = require('util');
var exec = util.promisify(require('child_process').exec);

var chalk = require('chalk');
var HttpStatus = require('http-status-codes');
var wordWrap = require('word-wrap');

var currentOutStream = process.stdout;
Expand All @@ -20,60 +21,38 @@ var printErr = function printErr(err) {
print('\n\n');
};

var genApiEndpoint = function genApiEndpoint(pkgName) {
return `https://registry.npmjs.org/${encodeURIComponent(pkgName)}/latest`;
};

var fetchPkgMeta = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(pkgName) {
var apiUrl, response, status;
var _ref2, stdout, stderr;

return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
apiUrl = genApiEndpoint(pkgName);
_context.prev = 1;
_context.next = 4;
return axios.get(apiUrl);
_context.next = 2;
return exec(`npm info ${pkgName} --json description version author license homepage repository npm`);

case 4:
response = _context.sent;
return _context.abrupt('return', response.data);
case 2:
_ref2 = _context.sent;
stdout = _ref2.stdout;
stderr = _ref2.stderr;

case 8:
_context.prev = 8;
_context.t0 = _context['catch'](1);

if (!_context.t0.response) {
_context.next = 19;
if (!stderr) {
_context.next = 7;
break;
}

status = _context.t0.response.status;
throw new Error(stderr);

if (!(status === HttpStatus.NOT_FOUND)) {
_context.next = 16;
break;
}

throw new Error('package not found in registry');

case 16:
throw new Error(`${HttpStatus.getStatusText(status).toLowerCase()} ${status}`);
case 7:
return _context.abrupt('return', JSON.parse(stdout));

case 17:
_context.next = 20;
break;

case 19:
throw new Error('something went wrong');

case 20:
case 8:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[1, 8]]);
}, _callee, undefined);
}));

return function fetchPkgMeta(_x) {
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@
},
"homepage": "https://github.com/rousan/npmf#readme",
"dependencies": {
"axios": "^0.18.0",
"babel-core": "^6.26.3",
"babel-polyfill": "^6.26.0",
"chalk": "^2.4.2",
"commander": "2.14.1",
"http-status-codes": "^1.3.0",
"lodash": "^4.17.5",
"package-json": "^6.0.0",
"word-wrap": "^1.2.3"
},
Expand Down
27 changes: 8 additions & 19 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const axios = require('axios');
const util = require('util');
const exec = util.promisify(require('child_process').exec);

const chalk = require('chalk');
const HttpStatus = require('http-status-codes');
const wordWrap = require('word-wrap');

const currentOutStream = process.stdout;
Expand All @@ -14,25 +15,13 @@ const printErr = (err) => {
print('\n\n');
};

const genApiEndpoint = pkgName => `https://registry.npmjs.org/${encodeURIComponent(pkgName)}/latest`;

const fetchPkgMeta = async (pkgName) => {
const apiUrl = genApiEndpoint(pkgName);
try {
const response = await axios.get(apiUrl);
return response.data;
} catch (err) {
if (err.response) {
const { status } = err.response;
if (status === HttpStatus.NOT_FOUND) {
throw new Error('package not found in registry');
} else {
throw new Error(`${HttpStatus.getStatusText(status).toLowerCase()} ${status}`);
}
} else {
throw new Error('something went wrong');
}
const { stdout, stderr } = await exec(`npm info ${pkgName} --json description version author license homepage repository npm`);

if (stderr) {
throw new Error(stderr);
}
return JSON.parse(stdout);
};

const wrapWords = (text, indent) => wordWrap(
Expand Down