Skip to content

Commit

Permalink
feat(shrinkwrap): query registry if unable to patch missing resolved …
Browse files Browse the repository at this point in the history
…property locally
  • Loading branch information
JamieMason committed Apr 13, 2016
1 parent 3f26bc2 commit 1aa9964
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions task/resolveTarball.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
var chalk = require('chalk');
var glob = require('glob');

// modules
var shell = require('./lib/shell');

// public
module.exports = resolveTarball;

// implementation
function resolveTarball (deps) {
return Promise.all(deps.map(resolvePackage));
return Promise.all(deps.map(resolveLocally));
}

function resolvePackage (dep) {
function resolveLocally (dep) {
return new Promise(function (resolve, reject) {
var pkgPath = getPkgPath(dep);
var pkgJson = getPkgJson(pkgPath);
Expand All @@ -19,8 +22,9 @@ function resolvePackage (dep) {
console.info(chalk.green('✓ set missing "resolved" property for %s to %s'), dep.id, dep.shrinkwrap.resolved);
resolve();
} else {
console.error(chalk.red('failed to resolve tarball for %s'), dep.id);
reject(new Error('failed to resolve tarball for ' + dep.id));
console.info(chalk.gray('? %s has no "dist.tarball" in package.json, trying npm registry...'), dep.id);
resolveRemotely(dep)
.then(resolve, reject);
}
});
}
Expand All @@ -34,7 +38,23 @@ function getPkgJson (pkgPath) {
}

function getPkgPath (dep) {
return glob.sync('node_modules/**/' + dep.name + '/**/package.json', {
return glob.sync('node_modules/**/' + dep.name + '/package.json', {
realpath: true
})[0] || '';
}

function resolveRemotely (dep) {
return shell('npm view ' + dep.id + ' --json')
.then(success, fail)
.catch(fail);

function success (stdout) {
dep.shrinkwrap.resolved = JSON.parse(stdout).dist.tarball;
console.info(chalk.green('✓ set missing "resolved" property for %s to %s'), dep.id, dep.shrinkwrap.resolved);
}

function fail (err) {
console.error(chalk.red('! failed to resolve tarball for %s'), dep.id);
throw err;
}
}

0 comments on commit 1aa9964

Please sign in to comment.