763f5b73126418b39832f256a9022a6cf1dd199b
[platform/upstream/nodejs.git] / deps / npm / lib / utils / config-defs.js
1 // defaults, types, and shorthands.
2
3
4 var path = require("path")
5   , url = require("url")
6   , Stream = require("stream").Stream
7   , semver = require("semver")
8   , stableFamily = semver.parse(process.version)
9   , nopt = require("nopt")
10   , log = require("npmlog")
11   , npm = require("../npm.js")
12   , osenv = require("osenv")
13
14 function Octal () {}
15 function validateOctal (data, k, val) {
16   // must be either an integer or an octal string.
17   if (typeof val === "number") {
18     data[k] = "0" + val.toString(8)
19   }
20   if (typeof val === "string") {
21     if (val.charAt(0) !== "0" || isNaN(val)) return false
22     data[k] = "0" + parseInt(val, 8).toString(8)
23   }
24 }
25
26 function validateSemver (data, k, val) {
27   if (!semver.valid(val)) return false
28   data[k] = semver.valid(val)
29 }
30
31 function validateStream (data, k, val) {
32   if (!(val instanceof Stream)) return false
33   data[k] = val
34 }
35
36 nopt.typeDefs.semver = { type: semver, validate: validateSemver }
37 nopt.typeDefs.Octal = { type: Octal, validate: validateOctal }
38 nopt.typeDefs.Stream = { type: Stream, validate: validateStream }
39
40 nopt.invalidHandler = function (k, val, type, data) {
41   log.warn("invalid config", k + "=" + JSON.stringify(val))
42
43   if (Array.isArray(type)) {
44     if (type.indexOf(url) !== -1) type = url
45     else if (type.indexOf(path) !== -1) type = path
46   }
47
48   switch (type) {
49     case Octal:
50       log.warn("invalid config", "Must be octal number, starting with 0")
51       break
52     case url:
53       log.warn("invalid config", "Must be a full url with 'http://'")
54       break
55     case path:
56       log.warn("invalid config", "Must be a valid filesystem path")
57       break
58     case Number:
59       log.warn("invalid config", "Must be a numeric value")
60       break
61     case Stream:
62       log.warn("invalid config", "Must be an instance of the Stream class")
63       break
64   }
65 }
66
67 if (!stableFamily || (+stableFamily[2] % 2)) stableFamily = null
68 else stableFamily = stableFamily[1] + "." + stableFamily[2]
69
70 var defaults
71
72 var temp = osenv.tmpdir()
73 var home = osenv.home()
74
75 if (home) process.env.HOME = home
76 else home = temp
77
78 var globalPrefix
79 Object.defineProperty(exports, "defaults", {get: function () {
80   if (defaults) return defaults
81
82   if (process.env.PREFIX) {
83     globalPrefix = process.env.PREFIX
84   } else if (process.platform === "win32") {
85     // c:\node\node.exe --> prefix=c:\node\
86     globalPrefix = path.dirname(process.execPath)
87   } else {
88     // /usr/local/bin/node --> prefix=/usr/local
89     globalPrefix = path.dirname(path.dirname(process.execPath))
90
91     // destdir only is respected on Unix
92     if (process.env.DESTDIR) {
93       globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
94     }
95   }
96
97   return defaults =
98     { "always-auth" : false
99
100       // are there others?
101     , browser : process.platform === "darwin" ? "open"
102               : process.platform === "win32" ? "start"
103               : "google-chrome"
104
105     , ca : // the npm CA certificate.
106       "-----BEGIN CERTIFICATE-----\n"+
107       "MIIChzCCAfACCQDauvz/KHp8ejANBgkqhkiG9w0BAQUFADCBhzELMAkGA1UEBhMC\n"+
108       "VVMxCzAJBgNVBAgTAkNBMRAwDgYDVQQHEwdPYWtsYW5kMQwwCgYDVQQKEwNucG0x\n"+
109       "IjAgBgNVBAsTGW5wbSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkxDjAMBgNVBAMTBW5w\n"+
110       "bUNBMRcwFQYJKoZIhvcNAQkBFghpQGl6cy5tZTAeFw0xMTA5MDUwMTQ3MTdaFw0y\n"+
111       "MTA5MDIwMTQ3MTdaMIGHMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEDAOBgNV\n"+
112       "BAcTB09ha2xhbmQxDDAKBgNVBAoTA25wbTEiMCAGA1UECxMZbnBtIENlcnRpZmlj\n"+
113       "YXRlIEF1dGhvcml0eTEOMAwGA1UEAxMFbnBtQ0ExFzAVBgkqhkiG9w0BCQEWCGlA\n"+
114       "aXpzLm1lMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLI4tIqPpRW+ACw9GE\n"+
115       "OgBlJZwK5f8nnKCLK629Pv5yJpQKs3DENExAyOgDcyaF0HD0zk8zTp+ZsLaNdKOz\n"+
116       "Gn2U181KGprGKAXP6DU6ByOJDWmTlY6+Ad1laYT0m64fERSpHw/hjD3D+iX4aMOl\n"+
117       "y0HdbT5m1ZGh6SJz3ZqxavhHLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAC4ySDbC\n"+
118       "l7W1WpLmtLGEQ/yuMLUf6Jy/vr+CRp4h+UzL+IQpCv8FfxsYE7dhf/bmWTEupBkv\n"+
119       "yNL18lipt2jSvR3v6oAHAReotvdjqhxddpe5Holns6EQd1/xEZ7sB1YhQKJtvUrl\n"+
120       "ZNufy1Jf1r0ldEGeA+0ISck7s+xSh9rQD2Op\n"+
121       "-----END CERTIFICATE-----\n"
122
123     , cache : process.platform === "win32"
124             ? path.resolve(process.env.APPDATA || home || temp, "npm-cache")
125             : path.resolve( home || temp, ".npm")
126
127     , "cache-lock-stale": 60000
128     , "cache-lock-retries": 10
129     , "cache-lock-wait": 10000
130
131     , "cache-max": Infinity
132     , "cache-min": 0
133
134     , color : true
135     , coverage: false
136     , depth: Infinity
137     , description : true
138     , dev : false
139     , editor : osenv.editor()
140     , "engine-strict": false
141     , force : false
142
143     , "fetch-retries": 2
144     , "fetch-retry-factor": 10
145     , "fetch-retry-mintimeout": 10000
146     , "fetch-retry-maxtimeout": 60000
147
148     , git: "git"
149
150     , global : false
151     , globalconfig : path.resolve(globalPrefix, "etc", "npmrc")
152     , globalignorefile : path.resolve( globalPrefix, "etc", "npmignore")
153     , group : process.platform === "win32" ? 0
154             : process.env.SUDO_GID || (process.getgid && process.getgid())
155     , ignore: ""
156     , "init-module": path.resolve(home, '.npm-init.js')
157     , "init.version" : "0.0.0"
158     , "init.author.name" : ""
159     , "init.author.email" : ""
160     , "init.author.url" : ""
161     , json: false
162     , link: false
163     , loglevel : "http"
164     , logstream : process.stderr
165     , long : false
166     , message : "%s"
167     , "node-version" : process.version
168     , npaturl : "http://npat.npmjs.org/"
169     , npat : false
170     , "onload-script" : false
171     , parseable : false
172     , pre: false
173     , prefix : globalPrefix
174     , production: process.env.NODE_ENV === "production"
175     , "proprietary-attribs": true
176     , proxy : process.env.HTTP_PROXY || process.env.http_proxy || null
177     , "https-proxy" : process.env.HTTPS_PROXY || process.env.https_proxy ||
178                       process.env.HTTP_PROXY || process.env.http_proxy || null
179     , "user-agent" : "npm/" + npm.version + " node/" + process.version
180     , "rebuild-bundle" : true
181     , registry : "https://registry.npmjs.org/"
182     , rollback : true
183     , save : false
184     , "save-bundle": false
185     , "save-dev" : false
186     , "save-optional" : false
187     , searchopts: ""
188     , searchexclude: null
189     , searchsort: "name"
190     , shell : osenv.shell()
191     , "sign-git-tag": false
192     , "strict-ssl": true
193     , tag : "latest"
194     , tmp : temp
195     , unicode : true
196     , "unsafe-perm" : process.platform === "win32"
197                     || process.platform === "cygwin"
198                     || !( process.getuid && process.setuid
199                        && process.getgid && process.setgid )
200                     || process.getuid() !== 0
201     , usage : false
202     , user : process.platform === "win32" ? 0 : "nobody"
203     , username : ""
204     , userconfig : path.resolve(home, ".npmrc")
205     , userignorefile : path.resolve(home, ".npmignore")
206     , umask: 022
207     , version : false
208     , versions : false
209     , viewer: process.platform === "win32" ? "browser" : "man"
210     , yes: null
211
212     , _exit : true
213     }
214 }})
215
216 exports.types =
217   { "always-auth" : Boolean
218   , browser : String
219   , ca: [null, String]
220   , cache : path
221   , "cache-lock-stale": Number
222   , "cache-lock-retries": Number
223   , "cache-lock-wait": Number
224   , "cache-max": Number
225   , "cache-min": Number
226   , color : ["always", Boolean]
227   , coverage: Boolean
228   , depth : Number
229   , description : Boolean
230   , dev : Boolean
231   , editor : String
232   , "engine-strict": Boolean
233   , force : Boolean
234   , "fetch-retries": Number
235   , "fetch-retry-factor": Number
236   , "fetch-retry-mintimeout": Number
237   , "fetch-retry-maxtimeout": Number
238   , git: String
239   , global : Boolean
240   , globalconfig : path
241   , globalignorefile: path
242   , group : [Number, String]
243   , "https-proxy" : [null, url]
244   , "user-agent" : String
245   , ignore : String
246   , "init-module": path
247   , "init.version" : [null, semver]
248   , "init.author.name" : String
249   , "init.author.email" : String
250   , "init.author.url" : ["", url]
251   , json: Boolean
252   , link: Boolean
253   , loglevel : ["silent","win","error","warn","http","info","verbose","silly"]
254   , logstream : Stream
255   , long : Boolean
256   , message: String
257   , "node-version" : [null, semver]
258   , npaturl : url
259   , npat : Boolean
260   , "onload-script" : [null, String]
261   , parseable : Boolean
262   , pre: Boolean
263   , prefix: path
264   , production: Boolean
265   , "proprietary-attribs": Boolean
266   , proxy : [null, url]
267   , "rebuild-bundle" : Boolean
268   , registry : [null, url]
269   , rollback : Boolean
270   , save : Boolean
271   , "save-bundle": Boolean
272   , "save-dev" : Boolean
273   , "save-optional" : Boolean
274   , searchopts : String
275   , searchexclude: [null, String]
276   , searchsort: [ "name", "-name"
277                 , "description", "-description"
278                 , "author", "-author"
279                 , "date", "-date"
280                 , "keywords", "-keywords" ]
281   , shell : String
282   , "sign-git-tag": Boolean
283   , "strict-ssl": Boolean
284   , tag : String
285   , tmp : path
286   , unicode : Boolean
287   , "unsafe-perm" : Boolean
288   , usage : Boolean
289   , user : [Number, String]
290   , username : String
291   , userconfig : path
292   , userignorefile : path
293   , umask: Octal
294   , version : Boolean
295   , versions : Boolean
296   , viewer: String
297   , yes: [false, null, Boolean]
298   , _exit : Boolean
299   }
300
301 exports.shorthands =
302   { s : ["--loglevel", "silent"]
303   , d : ["--loglevel", "info"]
304   , dd : ["--loglevel", "verbose"]
305   , ddd : ["--loglevel", "silly"]
306   , noreg : ["--no-registry"]
307   , reg : ["--registry"]
308   , "no-reg" : ["--no-registry"]
309   , silent : ["--loglevel", "silent"]
310   , verbose : ["--loglevel", "verbose"]
311   , quiet: ["--loglevel", "warn"]
312   , q: ["--loglevel", "warn"]
313   , h : ["--usage"]
314   , H : ["--usage"]
315   , "?" : ["--usage"]
316   , help : ["--usage"]
317   , v : ["--version"]
318   , f : ["--force"]
319   , gangster : ["--force"]
320   , gangsta : ["--force"]
321   , desc : ["--description"]
322   , "no-desc" : ["--no-description"]
323   , "local" : ["--no-global"]
324   , l : ["--long"]
325   , m : ["--message"]
326   , p : ["--parseable"]
327   , porcelain : ["--parseable"]
328   , g : ["--global"]
329   , S : ["--save"]
330   , D : ["--save-dev"]
331   , O : ["--save-optional"]
332   , y : ["--yes"]
333   , n : ["--no-yes"]
334   , B : ["--save-bundle"]
335   }