Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2014-05-14 04:44:56 +0400
committerisaacs <i@izs.me>2014-05-14 23:41:32 +0400
commit1707fcedb7503251eb77719aba7ac52931721205 (patch)
tree2dbb06489bdb58ecbb02d96464e0ce430b6675fd /lib
parent08e0b101f3190d9548a6530096844674b16ce8d7 (diff)
npmconf@1.0.0, Refactor config/uid/prefix loading process
Closes #4751
Diffstat (limited to 'lib')
-rw-r--r--lib/npm.js126
-rw-r--r--lib/utils/find-prefix.js57
2 files changed, 5 insertions, 178 deletions
diff --git a/lib/npm.js b/lib/npm.js
index d1932a1e4..c0c744f99 100644
--- a/lib/npm.js
+++ b/lib/npm.js
@@ -24,11 +24,6 @@ var EventEmitter = require("events").EventEmitter
, abbrev = require("abbrev")
, which = require("which")
, semver = require("semver")
- , findPrefix = require("./utils/find-prefix.js")
- , getUid = require("uid-number")
- , mkdirp = require("mkdirp")
- , slide = require("slide")
- , chain = slide.chain
, RegClient = require("npm-registry-client")
, charSpin = require("char-spinner")
@@ -42,23 +37,6 @@ npm.config = {
}
}
-// /usr/local is often a read-only fs, which is not
-// well handled by node or mkdirp. Just double-check
-// in the case of errors when making the prefix dirs.
-function mkdir (p, cb) {
- mkdirp(p, function (er, made) {
- // it could be that we couldn't create it, because it
- // already exists, and is on a read-only fs.
- if (er) {
- return fs.stat(p, function (er2, st) {
- if (er2 || !st.isDirectory()) return cb(er)
- return cb(null, made)
- })
- }
- return cb(er, made)
- })
-}
-
npm.commands = {}
try {
@@ -370,118 +348,24 @@ function load (npm, cli, cb) {
// at this point the configs are all set.
// go ahead and spin up the registry client.
- var token = config.get("_token")
- if (typeof token === "string") {
- try {
- token = JSON.parse(token)
- config.set("_token", token, "user")
- config.save("user")
- } catch (e) { token = null }
- }
-
npm.registry = new RegClient(npm.config)
- // save the token cookie in the config file
- if (npm.registry.couchLogin) {
- npm.registry.couchLogin.tokenSet = function (tok) {
- npm.config.set("_token", tok, "user")
- // ignore save error. best effort.
- npm.config.save("user")
- }
- }
-
var umask = npm.config.get("umask")
npm.modes = { exec: 0777 & (~umask)
, file: 0666 & (~umask)
, umask: umask }
- chain([ [ loadPrefix, npm, cli ]
- , [ setUser, config, config.root ]
- , [ loadUid, npm ]
- ], cb)
- })
- })
-}
-
-function loadPrefix (npm, config, cb) {
- // try to guess at a good node_modules location.
- var p
- , gp
- if (!Object.prototype.hasOwnProperty.call(config, "prefix")) {
- p = process.cwd()
- } else {
- p = npm.config.get("prefix")
- }
- gp = npm.config.get("prefix")
-
- findPrefix(p, function (er, p) {
- Object.defineProperty(npm, "localPrefix",
- { get : function () { return p }
- , set : function (r) { return p = r }
- , enumerable : true
- })
- // the prefix MUST exist, or else nothing works.
- if (!npm.config.get("global")) {
- mkdir(p, next)
- } else {
- next(er)
- }
- })
-
- gp = path.resolve(gp)
- Object.defineProperty(npm, "globalPrefix",
- { get : function () { return gp }
- , set : function (r) { return gp = r }
- , enumerable : true
- })
- // the prefix MUST exist, or else nothing works.
- mkdir(gp, next)
-
-
- var i = 2
- , errState = null
- function next (er) {
- if (errState) return
- if (er) return cb(errState = er)
- if (--i === 0) return cb()
- }
-}
-
+ var gp = Object.getOwnPropertyDescriptor(config, "globalPrefix")
+ Object.defineProperty(npm, "globalPrefix", gp)
-function loadUid (npm, cb) {
- // if we're not in unsafe-perm mode, then figure out who
- // to run stuff as. Do this first, to support `npm update npm -g`
- if (!npm.config.get("unsafe-perm")) {
- getUid(npm.config.get("user"), npm.config.get("group"), cb)
- } else {
- process.nextTick(cb)
- }
-}
+ var lp = Object.getOwnPropertyDescriptor(config, "localPrefix")
+ Object.defineProperty(npm, "localPrefix", lp)
-function setUser (cl, dc, cb) {
- // If global, leave it as-is.
- // If not global, then set the user to the owner of the prefix folder.
- // Just set the default, so it can be overridden.
- if (cl.get("global")) return cb()
- if (process.env.SUDO_UID) {
- dc.user = +(process.env.SUDO_UID)
- return cb()
- }
-
- var prefix = path.resolve(cl.get("prefix"))
- mkdir(prefix, function (er) {
- if (er) {
- log.error("could not create prefix dir", prefix)
- return cb(er)
- }
- fs.stat(prefix, function (er, st) {
- dc.user = st && st.uid
- return cb(er)
+ return cb()
})
})
}
-
Object.defineProperty(npm, "prefix",
{ get : function () {
return npm.config.get("global") ? npm.globalPrefix : npm.localPrefix
diff --git a/lib/utils/find-prefix.js b/lib/utils/find-prefix.js
deleted file mode 100644
index a61d9e136..000000000
--- a/lib/utils/find-prefix.js
+++ /dev/null
@@ -1,57 +0,0 @@
-// try to find the most reasonable prefix to use
-
-module.exports = findPrefix
-
-var fs = require("graceful-fs")
- , path = require("path")
- , npm = require("../npm.js")
-
-function findPrefix (p, cb_) {
- function cb (er, p) {
- process.nextTick(function () {
- cb_(er, p)
- })
- }
-
- p = path.resolve(p)
- // if there's no node_modules folder, then
- // walk up until we hopefully find one.
- // if none anywhere, then use cwd.
- var walkedUp = false
- while (path.basename(p) === "node_modules") {
- p = path.dirname(p)
- walkedUp = true
- }
- if (walkedUp) return cb(null, p)
-
- findPrefix_(p, p, cb)
-}
-
-function findPrefix_ (p, original, cb) {
- if (p === "/"
- || (process.platform === "win32" && p.match(/^[a-zA-Z]:(\\|\/)?$/))) {
- return cb(null, original)
- }
- fs.readdir(p, function (er, files) {
- // an error right away is a bad sign.
- // unless the prefix was simply a non
- // existent directory.
- if (er && p === original) {
- if (er.code === "ENOENT") return cb(null, original);
- return cb(er)
- }
-
- // walked up too high or something.
- if (er) return cb(null, original)
-
- if (files.indexOf("node_modules") !== -1
- || files.indexOf("package.json") !== -1) {
- return cb(null, p)
- }
-
- var d = path.dirname(p)
- if (d === p) return cb(null, original)
-
- return findPrefix_(d, original, cb)
- })
-}