diff options
author | isaacs <i@izs.me> | 2010-05-10 06:34:45 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-05-10 06:34:45 +0400 |
commit | a5c897ac8a19cc73389fd8a83403bf68775d2fd4 (patch) | |
tree | c4723b46d9b67168f5df1203d759ff5a356e6b23 /lib | |
parent | 1f4067820fd72065759980a1516566051ddc3974 (diff) |
Export a few utility functions for other commands to use.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cache.js | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/lib/cache.js b/lib/cache.js index 72feaee14..f258033be 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -1,5 +1,7 @@ -module.exports = cache +exports = module.exports = cache +exports.read = read +exports.unpack = unpack var mkdir = require("./utils/mkdir-p") , exec = require("./utils/exec") @@ -23,6 +25,25 @@ function cache (args, cb) { } } +// if the pkg and ver are in the cache, then +// just do a readJson and return. +// if they're not, then fetch them from the registry. +var cacheSeen = {} +function read (name, ver, cb) { + var jsonFile = path.join(npm.cache, name, ver, "package.json") + function c (er, data) { + if (!er) cacheSeen[data._id] = data + return cb(er, data) + } + if (name+"-"+ver in cacheSeen) { + return cb(null, cacheSeen[name+"-"+ver]) + } + readJson(jsonFile, function (er, data) { + if (er) return addNameVersion(name, ver, c) + c(er, data) + }) +} + // npm cache ls [<pkg> [<ver>]] function ls (args, cb) { var show = path.join.apply(path, args) @@ -30,7 +51,7 @@ function ls (args, cb) { , msg = "cache ls"+(show?" "+show:"") read = path.join(read, show) mkdir(npm.cache, function (er) { - if (er) log.er(cb, "no cache dir")(er) + if (er) return log.er(cb, "no cache dir")(er) fs.readdir(read, function (er, files) { if (er) { log("nothing found", "cache") @@ -62,6 +83,13 @@ function clean (args, cb) { // npm cache add <pkg> <ver> // npm cache add <tarball> // npm cache add <folder> +exports.add = function (pkg, ver, cb) { + if (!cb && typeof ver === "function") { + cb = ver + ver = null + } + return add([pkg, ver], cb) +} function add (args, cb) { var pkg = args.shift() , ver = args.shift() @@ -94,6 +122,8 @@ function addNameVersion (name, ver, cb) { , tgz = path.join(folder, "package.tgz") mkdir(folder, function (er) { if (er) return cb(er) + if (!data.dist || !data.dist.tarball) return cb(new Error( + "No dist.tarball in package data")) fetch(data.dist.tarball, tgz, function (er) { if (er) return cb(er) addLocalTarball(tgz, cb) @@ -163,6 +193,17 @@ function addLocalDirectory (p, cb) { }) } +function unpack (pkg, ver, unpackTarget, cb) { + log([pkg, ver], "unpack") + read(pkg, ver, function (er, data) { + if (er) return log.er(cb, "Could not read data for "+pkg+"@"+ver)(er) + unpackTar( path.join(npm.cache, pkg, ver, "package.tgz") + , unpackTarget + , cb + ) + }) +} + function unpackTar (tarball, unpackTarget, cb) { mkdir(unpackTarget, function (er) { if (er) return log.er(cb, "Could not create "+unpackTarget)(er) |