Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Keep track of the name (if known) when creating temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 12, 2011
1 parent f531dca commit 916e1ba
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ function add (args, cb) {
if (pkg.match(/^https?:\/\//)) return addRemoteTarball(pkg, cb)
else addLocal(pkg, cb)
}
function addRemoteTarball (url, shasum, cb) {
log.verbose([url, shasum], "addRemoteTarball")

function addRemoteTarball (url, shasum, name, cb) {
if (!cb) cb = name, name = ""
if (!cb) cb = shasum, shasum = null
log.verbose([url, shasum], "addRemoteTarball")
// todo: take a shasum, and validate it.
var tmp = path.join(npm.tmp, Date.now()+"-"+Math.random(), "tmp.tgz")
mkdir(path.dirname(tmp), function (er) {
Expand All @@ -203,9 +205,10 @@ function addRemoteTarball (url, shasum, cb) {
})
function done (er) {
if (er) return cb(er)
addLocalTarball(tmp, cb)
addLocalTarball(tmp, name, cb)
}
}

function addNameVersion (name, ver, cb) {
registry.get(name, ver, function (er, data, json, response) {
if (er) return cb(er)
Expand All @@ -221,32 +224,36 @@ function addNameVersion (name, ver, cb) {
})
function fetchit () {
return addRemoteTarball( data.dist.tarball.replace(/^https/,'http')
, data.dist.shasum, cb)
, data.dist.shasum, name+"-"+ver, cb)
}
})
}

function addLocal (p, cb) {
function addLocal (p, name, cb) {
if (!cb) cb = name, name = ""
// figure out if this is a folder or file.
fs.stat(p, function (er, s) {
if (er) return log.er(cb, "Doesn't exist: "+p)(er)
if (s.isDirectory()) addLocalDirectory(p, cb)
else addLocalTarball(p, cb)
if (s.isDirectory()) addLocalDirectory(p, name, cb)
else addLocalTarball(p, name, cb)
})
}
function addLocalTarball (p, cb) {

function addLocalTarball (p, name, cb) {
if (!cb) cb = name, name = ""
// if it's a tar, and not in place,
// then unzip to .tmp, add the tmp folder, and clean up tmp
if (p.indexOf(npm.tmp) === 0) return addTmpTarball(p, cb)
if (p.indexOf(npm.tmp) === 0) return addTmpTarball(p, name, cb)

if (p.indexOf(npm.cache) === 0) {
if (path.basename(p) !== "package.tgz") return cb(new Error(
"Not a valid cache tarball name: "+p))
return addPlacedTarball(p, cb)
return addPlacedTarball(p, name, cb)
}

// just copy it over and then add the temp tarball file.
var tmp = path.join(npm.tmp, Date.now() + "-" + Math.random(), "tmp.tgz")
var tmp = path.join(npm.tmp, name + Date.now()
+ "-" + Math.random(), "tmp.tgz")
mkdir(path.dirname(tmp), function (er) {
if (er) return cb(er)
var from = fs.createReadStream(p)
Expand All @@ -262,13 +269,15 @@ function addLocalTarball (p, cb) {
if (errState) return
fs.chmod(tmp, 0644, function (er) {
if (er) return cb(er)
addTmpTarball(tmp, cb)
addTmpTarball(tmp, name, cb)
})
})
sys.pump(from, to)
})
}
function addPlacedTarball (p, cb) {

function addPlacedTarball (p, name, cb) {
if (!cb) cb = name, name = ""
// now we know it's in place already as .cache/name/ver/package.tgz
// unpack to .cache/name/ver/package/, read the package.json,
// and fire cb with the json data.
Expand Down Expand Up @@ -297,7 +306,9 @@ function addPlacedTarball (p, cb) {
})
})
}
function addLocalDirectory (p, cb) {

function addLocalDirectory (p, name, cb) {
if (!cb) cb = name, name = ""
// if it's a folder, then read the package.json,
// tar it to the proper place, and add the cache tar
if (p.indexOf(npm.cache) === 0) return cb(new Error(
Expand All @@ -315,11 +326,13 @@ function addLocalDirectory (p, cb) {
, tgz = path.basename(p) === "package" ? placed : tmptgz
packTar(tgz, p, data, function (er) {
if (er) return log.er(cb,"couldn't pack "+p+ " to "+tgz)(er)
addLocalTarball(tgz, cb)
addLocalTarball(tgz, name, cb)
})
})
}
function addTmpTarball (tgz, cb) {

function addTmpTarball (tgz, name, cb) {
if (!cb) cb = name, name = ""
var contents = path.join(path.dirname(tgz),"contents")
unpackTar(tgz, contents, function (er) {
if (er) return log.er(cb, "couldn't unpack "+tgz+" to "+contents)(er)
Expand All @@ -338,7 +351,7 @@ function addTmpTarball (tgz, cb) {
var newName = path.join(contents, "package")
fs.rename(folder, newName, function (er) {
if (er) return log.er(cb, "couldn't rename "+folder+" to package")(er)
addLocalDirectory(newName, cb)
addLocalDirectory(newName, name, cb)
})
})
})
Expand Down

0 comments on commit 916e1ba

Please sign in to comment.