All files npm-utils.js

90.32% Statements 28/31
69.23% Branches 9/13
100% Functions 8/8
96.43% Lines 27/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 511x 1x 1x   1x 7x 7x 7x 7x 1x           6x         12x   1x   12x 12x 12x 12x 12x 7x 6x   5x 5x   5x 5x   5x         1x     5x     1x  
const {exec} = require('child_process');
const {resolver} = require('resolve-package-json');
const _ = require('lodash/fp');
 
const getVersionList = name => {
  Iif (!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 shouldResolve = pkg => /.*@([\^~]|.*x)/.test(pkg);
 
const resolveVersionRange = async pkg => {
  // unfortunately no named capture groups in Node 6..
  const match = pkg.match(/^(@?[^@]+)(?:@(.*?))?$/);
  Iif (!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);
      Iif (!_.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};