Skip to content
Browse files

add npm-cache-dir@0.0.2

  • Loading branch information...
1 parent 30b6783 commit cef8c40ea3330efc9ef9f4186881ce7f3c87b40a @robertkowalski committed Feb 23, 2014
View
27 node_modules/npm-cache-dir/LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) Robert Kowalski and Isaac Z. Schlueter ("Authors")
+All rights reserved.
+
+The BSD License
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
15 node_modules/npm-cache-dir/README.md
@@ -0,0 +1,15 @@
+[![Build Status](https://travis-ci.org/robertkowalski/npm-cache-dir.png?branch=master)](https://travis-ci.org/robertkowalski/npm-cache-dir)
+
+# npm-cache-dir
+
+Get stats about npm's cache dir and create the directory
+lazily
+
+## API
+
+### .getCacheStat(npmCache, log, cb)
+Get fs.stats about npm's cache dir, and create the cachedir
+if it does not exist
+
+### .makeCacheDir(npmCache, log, cb)
+Create the cache dir according to the setting in `npm.cache`
View
60 node_modules/npm-cache-dir/index.js
@@ -0,0 +1,60 @@
+var fs = require("fs")
+ , mkdir = require("mkdirp")
+ , chownr = require("chownr")
+
+// to maintain the cache dir's permissions consistently.
+var cacheStat = null
+
+exports.getCacheStat = getCacheStat
+function getCacheStat (npmCache, log, cb) {
+ if (cacheStat) return cb(null, cacheStat)
+ fs.stat(npmCache, function (er, st) {
+ if (er) return makeCacheDir(npmCache, log, cb)
+ if (!st.isDirectory()) {
+ log.error("getCacheStat", "invalid cache dir %j", npmCache)
+ return cb(er)
+ }
+ return cb(null, cacheStat = st)
+ })
+}
+
+exports.makeCacheDir = makeCacheDir
+function makeCacheDir (npmCache, log, cb) {
+ if (!process.getuid) return mkdir(npmCache, cb)
+
+ var uid = +process.getuid()
+ , gid = +process.getgid()
+
+ if (uid === 0) {
+ if (process.env.SUDO_UID) uid = +process.env.SUDO_UID
+ if (process.env.SUDO_GID) gid = +process.env.SUDO_GID
+ }
+
+ if (uid !== 0 || !process.env.HOME) {
+ cacheStat = {uid: uid, gid: gid}
+ return mkdir(npmCache, afterMkdir)
+ }
+
+ fs.stat(process.env.HOME, function (er, st) {
+ if (er) {
+ log.error("makeCacheDir", "homeless?")
+ return cb(er)
+ }
+ cacheStat = st
+ log.silly("makeCacheDir", "cache dir uid, gid", [st.uid, st.gid])
+ return mkdir(npmCache, afterMkdir)
+ })
+
+ function afterMkdir (er, made) {
+ if (er || !cacheStat || isNaN(cacheStat.uid) || isNaN(cacheStat.gid)) {
+ return cb(er, cacheStat)
+ }
+
+ if (!made) return cb(er, cacheStat)
+
+ // ensure that the ownership is correct.
+ chownr(made, cacheStat.uid, cacheStat.gid, function (er) {
+ return cb(er, cacheStat)
+ })
+ }
+}
View
42 node_modules/npm-cache-dir/package.json
@@ -0,0 +1,42 @@
+{
+ "name": "npm-cache-dir",
+ "version": "1.0.0",
+ "description": "I am checking and creating npm's cache dir",
+ "main": "index.js",
+ "dependencies": {
+ "mkdirp": "~0.3.5",
+ "chownr": "0"
+ },
+ "devDependencies": {
+ "rimraf": "~2.2.6",
+ "tap": "~0.4.8"
+ },
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/robertkowalski/npm-cache-dir.git"
+ },
+ "homepage": "https://github.com/robertkowalski/npm-cache-dir",
+ "keywords": [
+ "npm,",
+ "cache"
+ ],
+ "author": {
+ "name": "Robert Kowalski",
+ "email": "rok@kowalski.gd"
+ },
+ "license": "BSD-2-Clause",
+ "bugs": {
+ "url": "https://github.com/robertkowalski/npm-cache-dir/issues"
+ },
+ "readme": "[![Build Status](https://travis-ci.org/robertkowalski/npm-cache-dir.png?branch=master)](https://travis-ci.org/robertkowalski/npm-cache-dir)\n\n# npm-cache-dir\n\nGet stats about npm's cache dir and create the directory\nlazily\n\n## API\n\n### .getCacheStat(npmCache, log, cb)\nGet fs.stats about npm's cache dir, and create the cachedir\nif it does not exist\n\n### .makeCacheDir(npmCache, log, cb)\nCreate the cache dir according to the setting in `npm.cache`\n",
+ "readmeFilename": "README.md",
+ "_id": "npm-cache-dir@1.0.0",
+ "dist": {
+ "shasum": "2a9ce717292daa16492fd9a1aafed4a6be540bb0"
+ },
+ "_from": "npm-cache-dir@1.0.0",
+ "_resolved": "https://registry.npmjs.org/npm-cache-dir/-/npm-cache-dir-1.0.0.tgz"
+}
View
56 node_modules/npm-cache-dir/test/index.js
@@ -0,0 +1,56 @@
+var cacheDir = require("../")
+ , test = require("tap").test
+ , mkdirp = require("mkdirp")
+ , rimraf = require("rimraf")
+ , path = require("path")
+ , fs = require("fs")
+ , fixtures = path.join(__dirname, "fixtures")
+
+var npm = { cache: path.join(fixtures, "mycache") }
+ , log = function () {}
+
+function setup () {
+ rimraf.sync(fixtures)
+ mkdirp.sync(fixtures)
+}
+
+test("getCacheStat creates a directory if not exists", function (t) {
+ setup()
+ fs.exists(npm.cache, function (e) {
+ t.notOk(e, "cache does not exist")
+ cacheDir.getCacheStat(npm.cache, log, function () {
+ fs.exists(npm.cache, function (e) {
+ t.ok(e, "cache exists")
+ t.end()
+ })
+ })
+ })
+})
+
+test("getCacheStat returns stats", function (t) {
+ setup()
+ cacheDir.getCacheStat(npm.cache, log, function (err, d) {
+ t.ok(d.uid, "uid set")
+ t.ok(d.gid, "gid set")
+ t.end()
+ })
+})
+
+test("makeCacheDir creates directories", function (t) {
+ setup()
+ npm.cache = path.join(fixtures, "npm-test-2")
+ fs.exists(npm.cache, function (e) {
+ t.notOk(e, "cache does not exist")
+ cacheDir.makeCacheDir(npm.cache, log, function () {
+ fs.exists(npm.cache, function (e) {
+ t.ok(e, "cache exists")
+ t.end()
+ })
+ })
+ })
+})
+
+test("cleanup", function (t) {
+ rimraf.sync(fixtures)
+ t.end()
+})
View
2 package.json
@@ -60,6 +60,7 @@
"mkdirp": "~0.3.5",
"node-gyp": "~0.12.2",
"nopt": "^2.2.0",
+ "npm-cache-dir": "~1.0.0",
"npm-install-checks": "~1.0.0",
"npm-registry-client": "^0.4.4",
"npm-user-validate": "0.0.3",
@@ -113,6 +114,7 @@
"node-gyp",
"nopt",
"normalize-package-data",
+ "npm-cache-dir",
"npm-install-checks",
"npm-registry-client",
"npm-user-validate",

0 comments on commit cef8c40

Please sign in to comment.
Something went wrong with that request. Please try again.