node/deps/npm/lib/shrinkwrap.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2012-02-25 10:51:22 +08:00
// emit JSON describing versions of all packages currently installed (for later
// use with shrinkwrap install)
module.exports = exports = shrinkwrap
var npm = require("./npm.js")
2012-06-11 12:29:47 +08:00
, log = require("npmlog")
2012-02-25 10:51:22 +08:00
, fs = require("fs")
, path = require("path")
shrinkwrap.usage = "npm shrinkwrap"
function shrinkwrap (args, silent, cb) {
if (typeof cb !== "function") cb = silent, silent = false
if (args.length) {
2012-06-11 12:29:47 +08:00
log.warn("shrinkwrap", "doesn't take positional args")
2012-02-25 10:51:22 +08:00
}
npm.commands.ls([], true, function (er, _, pkginfo) {
if (er) return cb(er)
shrinkwrap_(pkginfo, silent, cb)
})
}
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) {
2012-06-11 12:29:47 +08:00
log.error("shrinkwrap", "Error converting package info to json")
2012-02-25 10:51:22 +08:00
return cb(er)
}
var file = path.resolve(npm.prefix, "npm-shrinkwrap.json")
fs.writeFile(file, swdata, function (er) {
if (er) return cb(er)
if (silent) return cb(null, pkginfo)
2012-07-18 02:37:39 +08:00
console.log("wrote npm-shrinkwrap.json")
cb(null, pkginfo)
2012-02-25 10:51:22 +08:00
})
}