diff options
author | isaacs <i@izs.me> | 2012-02-25 06:12:29 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-02-25 06:12:29 +0400 |
commit | 38207777fee4991b0ad8bb4c299516a6fd235e1c (patch) | |
tree | 95c7ac5c51500f57188a914b58e3acb0dbf6ff8d | |
parent | 776fbc381b74f787e1a4ad56ed4e7dac621fd77c (diff) |
Implement shrinkwrap using ls's json data structure.
-rw-r--r-- | lib/shrinkwrap.js | 77 |
1 files changed, 23 insertions, 54 deletions
diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index f30876598..59942d586 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -6,8 +6,8 @@ module.exports = exports = shrinkwrap var npm = require("./npm.js") , output = require("./utils/output.js") , log = require("./utils/log.js") - , fs = require('fs') - , path = require('path') + , fs = require("fs") + , path = require("path") shrinkwrap.usage = "npm shrinkwrap" @@ -18,63 +18,32 @@ function shrinkwrap (args, silent, cb) { log.warn("shrinkwrap doesn't take positional args.") } - npm.commands.ls([], true, function (er, pkginfo) { + npm.commands.ls([], true, function (er, _, pkginfo) { if (er) return cb(er) - - var wrapped = {} - - if (pkginfo.name) { - wrapped.name = pkginfo.name - } - - try { - shrinkwrapPkg(log, pkginfo.name, pkginfo, wrapped) - } catch (ex) { - return cb(ex); - } - - // leave the version field out of the top-level, since it's not used and - // could only be confusing if it gets out of date. - delete wrapped.version - - fs.writeFile( path.join(process.cwd(), "npm-shrinkwrap.json") - , new Buffer(JSON.stringify(wrapped, null, 2) + "\n") - , function (er) { - if (er) return cb(er) - output.write("wrote npm-shrinkwrap.json", function (er) { - cb(er, wrapped) - }) - }) + shrinkwrap_(pkginfo, silent, cb) }) } -function shrinkwrapPkg (log, pkgname, pkginfo, rv) { - var pkg, dep - - if (typeof (pkginfo) == 'string') - throw (new Error('required dependency not installed: ' + pkgname + - '@' + pkginfo)) - - if (pkginfo.hasOwnProperty('version')) { - rv.version = pkginfo.version +function shrinkwrap_ (pkginfo, silent, cb) { + if (pkginfo.problems) { + return cb(new Error("Problems were encountered\n" + +"Please correct and try again.\n" + +pkginfo.problems.join("\n"))) + } + try { + var swdata = JSON.stringify(pkginfo, null, 2) + "\n" + } catch (er) { + log.error("Error converting package info to json") + return cb(er) } - if (Object.keys(pkginfo.dependencies).length === 0) return; - - rv.dependencies = {} - - for (pkg in pkginfo.dependencies) { - dep = pkginfo.dependencies[pkg] - rv.dependencies[pkg] = {} - shrinkwrapPkg(log, pkg, dep, rv.dependencies[pkg]) - - // package.json must be consistent with the shrinkwrap bundle - if (dep.extraneous) { - throw (new Error('package is extraneous: ' + pkg + '@' + dep.version)) - } + var file = path.resolve(npm.prefix, "npm-shrinkwrap.json") - if (dep.invalid) { - throw (new Error('package is invalid: ' + pkg)) - } - } + fs.writeFile(file, swdata, function (er) { + if (er) return cb(er) + if (silent) return cb(null, pkginfo) + output.write("wrote npm-shrinkwrap.json", function (er) { + cb(er, pkginfo) + }) + }) } |