All files npm-utils.js

100% Statements 31/31
92.31% Branches 12/13
100% Functions 8/8
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 527x 7x 7x   7x 20x 19x 19x 19x 6x           13x           25x   7x   26x 26x 25x 25x 25x 16x 12x   9x 9x   9x 9x 2x 7x         7x     11x     7x  
const {exec} = require('child_process');
const {resolver} = require('resolve-package-json');
const _ = require('lodash/fp');
 
const getVersionList = name => {
  if (!name) return Promise.reject(new Error('Empty name given as argument'));
  return new Promise((resolve, reject) =>
    exec(`npm show ${name} versions --json`, (err, stdout, stderr) => {
      if (err) {
        return reject(
          /Registry returned 404 for GET on|404 Not found|code E404/.test(stderr)
            ? new Error("The package you were looking for doesn't exist.")
            : err
        );
      }
      return resolve(JSON.parse(stdout));
    })
  );
};
 
//const shouResolve = pkg => /.*@([~^]|.*x)/.test(pkg);
const shouldResolve = pkg => /.*@([\^~]|.*x)/.test(pkg);
 
const resolveVersionRange = async pkg => {
  // unfortunately no named capture groups in Node 6..
  const match = pkg.match(/^(@?[^@]+)(?:@(.*?))?$/);
  if (!match) throw new Error(`Unable to parse package name ${pkg}`);
  const packageName = match[1];
  const version = match[2];
  if (!shouldResolve(pkg)) {
    await getVersionList(packageName);
    return pkg;
  }
  return new Promise((resolve, reject) => {
    resolver({[packageName]: version}, function(err, result) {
      /* istanbul ignore if*/
      if (err) return reject(err);
      if (!_.has(`dependencies.${packageName}`, result))
        return reject(new Error(`Specified version range '${version}' is not resolvable`));
      return resolve(`${packageName}@${result.dependencies[packageName].version}`);
    });
  });
};
 
const getDependencyList = _.pipe(
  _.getOr({}, 'dependencies'),
  _.toPairs,
  _.map(([key, value]) => `${key}@${value}`)
);
 
module.exports = {getVersionList, shouldResolve, resolveVersionRange, getDependencyList};