defaults.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // defaults, types, and shorthands.
  2. var path = require('path')
  3. var url = require('url')
  4. var Stream = require('stream').Stream
  5. var semver = require('semver')
  6. var stableFamily = semver.parse(process.version)
  7. var nopt = require('nopt')
  8. var os = require('os')
  9. var osenv = require('osenv')
  10. var umask = require('../utils/umask')
  11. var hasUnicode = require('has-unicode')
  12. var log
  13. try {
  14. log = require('npmlog')
  15. } catch (er) {
  16. var util = require('util')
  17. log = { warn: function (m) {
  18. console.warn(m + ' ' + util.format.apply(util, [].slice.call(arguments, 1)))
  19. } }
  20. }
  21. exports.Umask = Umask
  22. function Umask () {}
  23. function validateUmask (data, k, val) {
  24. return umask.validate(data, k, val)
  25. }
  26. function validateSemver (data, k, val) {
  27. if (!semver.valid(val)) return false
  28. data[k] = semver.valid(val)
  29. }
  30. function validateStream (data, k, val) {
  31. if (!(val instanceof Stream)) return false
  32. data[k] = val
  33. }
  34. nopt.typeDefs.semver = { type: semver, validate: validateSemver }
  35. nopt.typeDefs.Stream = { type: Stream, validate: validateStream }
  36. nopt.typeDefs.Umask = { type: Umask, validate: validateUmask }
  37. nopt.invalidHandler = function (k, val, type) {
  38. log.warn('invalid config', k + '=' + JSON.stringify(val))
  39. if (Array.isArray(type)) {
  40. if (type.indexOf(url) !== -1) type = url
  41. else if (type.indexOf(path) !== -1) type = path
  42. }
  43. switch (type) {
  44. case Umask:
  45. log.warn('invalid config', 'Must be umask, octal number in range 0000..0777')
  46. break
  47. case url:
  48. log.warn('invalid config', "Must be a full url with 'http://'")
  49. break
  50. case path:
  51. log.warn('invalid config', 'Must be a valid filesystem path')
  52. break
  53. case Number:
  54. log.warn('invalid config', 'Must be a numeric value')
  55. break
  56. case Stream:
  57. log.warn('invalid config', 'Must be an instance of the Stream class')
  58. break
  59. }
  60. }
  61. if (!stableFamily || (+stableFamily.minor % 2)) stableFamily = null
  62. else stableFamily = stableFamily.major + '.' + stableFamily.minor
  63. var defaults
  64. var temp = osenv.tmpdir()
  65. var home = osenv.home()
  66. var uidOrPid = process.getuid ? process.getuid() : process.pid
  67. if (home) process.env.HOME = home
  68. else home = path.resolve(temp, 'npm-' + uidOrPid)
  69. var cacheExtra = process.platform === 'win32' ? 'npm-cache' : '.npm'
  70. var cacheRoot = (process.platform === 'win32' && process.env.APPDATA) || home
  71. var cache = path.resolve(cacheRoot, cacheExtra)
  72. var globalPrefix
  73. Object.defineProperty(exports, 'defaults', {get: function () {
  74. if (defaults) return defaults
  75. if (process.env.PREFIX) {
  76. globalPrefix = process.env.PREFIX
  77. } else if (process.platform === 'win32') {
  78. // c:\node\node.exe --> prefix=c:\node\
  79. globalPrefix = path.dirname(process.execPath)
  80. } else {
  81. // /usr/local/bin/node --> prefix=/usr/local
  82. globalPrefix = path.dirname(path.dirname(process.execPath))
  83. // destdir only is respected on Unix
  84. if (process.env.DESTDIR) {
  85. globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
  86. }
  87. }
  88. defaults = {
  89. access: null,
  90. 'allow-same-version': false,
  91. 'always-auth': false,
  92. also: null,
  93. audit: true,
  94. 'audit-level': 'low',
  95. 'auth-type': 'legacy',
  96. 'before': null,
  97. 'bin-links': true,
  98. browser: null,
  99. ca: null,
  100. cafile: null,
  101. cache: cache,
  102. 'cache-lock-stale': 60000,
  103. 'cache-lock-retries': 10,
  104. 'cache-lock-wait': 10000,
  105. 'cache-max': Infinity,
  106. 'cache-min': 10,
  107. cert: null,
  108. cidr: null,
  109. color: process.env.NO_COLOR == null,
  110. depth: Infinity,
  111. description: true,
  112. dev: false,
  113. 'dry-run': false,
  114. editor: osenv.editor(),
  115. 'engine-strict': false,
  116. force: false,
  117. 'format-package-lock': true,
  118. fund: true,
  119. 'fetch-retries': 2,
  120. 'fetch-retry-factor': 10,
  121. 'fetch-retry-mintimeout': 10000,
  122. 'fetch-retry-maxtimeout': 60000,
  123. git: 'git',
  124. 'git-tag-version': true,
  125. 'commit-hooks': true,
  126. global: false,
  127. globalconfig: path.resolve(globalPrefix, 'etc', 'npmrc'),
  128. 'global-style': false,
  129. group: process.platform === 'win32' ? 0
  130. : process.env.SUDO_GID || (process.getgid && process.getgid()),
  131. 'ham-it-up': false,
  132. heading: 'npm',
  133. 'if-present': false,
  134. 'ignore-prepublish': false,
  135. 'ignore-scripts': false,
  136. 'init-module': path.resolve(home, '.npm-init.js'),
  137. 'init-author-name': '',
  138. 'init-author-email': '',
  139. 'init-author-url': '',
  140. 'init-version': '1.0.0',
  141. 'init-license': 'ISC',
  142. json: false,
  143. key: null,
  144. 'legacy-bundling': false,
  145. link: false,
  146. 'local-address': undefined,
  147. loglevel: 'notice',
  148. logstream: process.stderr,
  149. 'logs-max': 10,
  150. long: false,
  151. maxsockets: 50,
  152. message: '%s',
  153. 'metrics-registry': null,
  154. 'node-options': null,
  155. 'node-version': process.version,
  156. 'offline': false,
  157. 'onload-script': false,
  158. only: null,
  159. optional: true,
  160. otp: null,
  161. 'package-lock': true,
  162. 'package-lock-only': false,
  163. parseable: false,
  164. 'prefer-offline': false,
  165. 'prefer-online': false,
  166. prefix: globalPrefix,
  167. preid: '',
  168. production: process.env.NODE_ENV === 'production',
  169. 'progress': !process.env.TRAVIS && !process.env.CI,
  170. proxy: null,
  171. 'https-proxy': null,
  172. 'noproxy': null,
  173. 'user-agent': 'npm/{npm-version} ' +
  174. 'node/{node-version} ' +
  175. '{platform} ' +
  176. '{arch} ' +
  177. '{ci}',
  178. 'read-only': false,
  179. 'rebuild-bundle': true,
  180. registry: 'https://registry.npmjs.org/',
  181. rollback: true,
  182. save: true,
  183. 'save-bundle': false,
  184. 'save-dev': false,
  185. 'save-exact': false,
  186. 'save-optional': false,
  187. 'save-prefix': '^',
  188. 'save-prod': false,
  189. scope: '',
  190. 'script-shell': null,
  191. 'scripts-prepend-node-path': 'warn-only',
  192. searchopts: '',
  193. searchexclude: null,
  194. searchlimit: 20,
  195. searchstaleness: 15 * 60,
  196. 'send-metrics': false,
  197. shell: osenv.shell(),
  198. shrinkwrap: true,
  199. 'sign-git-commit': false,
  200. 'sign-git-tag': false,
  201. 'sso-poll-frequency': 500,
  202. 'sso-type': 'oauth',
  203. 'strict-ssl': true,
  204. tag: 'latest',
  205. 'tag-version-prefix': 'v',
  206. timing: false,
  207. tmp: temp,
  208. unicode: hasUnicode(),
  209. 'unsafe-perm': process.platform === 'win32' ||
  210. process.platform === 'cygwin' ||
  211. !(process.getuid && process.setuid &&
  212. process.getgid && process.setgid) ||
  213. process.getuid() !== 0,
  214. 'update-notifier': true,
  215. usage: false,
  216. user: (process.platform === 'win32' || os.type() === 'OS400') ? 0 : 'nobody',
  217. userconfig: path.resolve(home, '.npmrc'),
  218. umask: process.umask ? process.umask() : umask.fromString('022'),
  219. version: false,
  220. versions: false,
  221. viewer: process.platform === 'win32' ? 'browser' : 'man',
  222. _exit: true
  223. }
  224. return defaults
  225. }})
  226. exports.types = {
  227. access: [null, 'restricted', 'public'],
  228. 'allow-same-version': Boolean,
  229. 'always-auth': Boolean,
  230. also: [null, 'dev', 'development'],
  231. audit: Boolean,
  232. 'audit-level': ['low', 'moderate', 'high', 'critical'],
  233. 'auth-type': ['legacy', 'sso', 'saml', 'oauth'],
  234. 'before': [null, Date],
  235. 'bin-links': Boolean,
  236. browser: [null, String],
  237. ca: [null, String, Array],
  238. cafile: path,
  239. cache: path,
  240. 'cache-lock-stale': Number,
  241. 'cache-lock-retries': Number,
  242. 'cache-lock-wait': Number,
  243. 'cache-max': Number,
  244. 'cache-min': Number,
  245. cert: [null, String],
  246. cidr: [null, String, Array],
  247. color: ['always', Boolean],
  248. depth: Number,
  249. description: Boolean,
  250. dev: Boolean,
  251. 'dry-run': Boolean,
  252. editor: String,
  253. 'engine-strict': Boolean,
  254. force: Boolean,
  255. fund: Boolean,
  256. 'format-package-lock': Boolean,
  257. 'fetch-retries': Number,
  258. 'fetch-retry-factor': Number,
  259. 'fetch-retry-mintimeout': Number,
  260. 'fetch-retry-maxtimeout': Number,
  261. git: String,
  262. 'git-tag-version': Boolean,
  263. 'commit-hooks': Boolean,
  264. global: Boolean,
  265. globalconfig: path,
  266. 'global-style': Boolean,
  267. group: [Number, String],
  268. 'https-proxy': [null, url],
  269. 'user-agent': String,
  270. 'ham-it-up': Boolean,
  271. 'heading': String,
  272. 'if-present': Boolean,
  273. 'ignore-prepublish': Boolean,
  274. 'ignore-scripts': Boolean,
  275. 'init-module': path,
  276. 'init-author-name': String,
  277. 'init-author-email': String,
  278. 'init-author-url': ['', url],
  279. 'init-license': String,
  280. 'init-version': semver,
  281. json: Boolean,
  282. key: [null, String],
  283. 'legacy-bundling': Boolean,
  284. link: Boolean,
  285. 'local-address': getLocalAddresses(),
  286. loglevel: ['silent', 'error', 'warn', 'notice', 'http', 'timing', 'info', 'verbose', 'silly'],
  287. logstream: Stream,
  288. 'logs-max': Number,
  289. long: Boolean,
  290. maxsockets: Number,
  291. message: String,
  292. 'metrics-registry': [null, String],
  293. 'node-options': [null, String],
  294. 'node-version': [null, semver],
  295. 'noproxy': [null, String, Array],
  296. offline: Boolean,
  297. 'onload-script': [null, String],
  298. only: [null, 'dev', 'development', 'prod', 'production'],
  299. optional: Boolean,
  300. 'package-lock': Boolean,
  301. otp: [null, String],
  302. 'package-lock-only': Boolean,
  303. parseable: Boolean,
  304. 'prefer-offline': Boolean,
  305. 'prefer-online': Boolean,
  306. prefix: path,
  307. preid: String,
  308. production: Boolean,
  309. progress: Boolean,
  310. proxy: [null, false, url], // allow proxy to be disabled explicitly
  311. 'read-only': Boolean,
  312. 'rebuild-bundle': Boolean,
  313. registry: [null, url],
  314. rollback: Boolean,
  315. save: Boolean,
  316. 'save-bundle': Boolean,
  317. 'save-dev': Boolean,
  318. 'save-exact': Boolean,
  319. 'save-optional': Boolean,
  320. 'save-prefix': String,
  321. 'save-prod': Boolean,
  322. scope: String,
  323. 'script-shell': [null, String],
  324. 'scripts-prepend-node-path': [false, true, 'auto', 'warn-only'],
  325. searchopts: String,
  326. searchexclude: [null, String],
  327. searchlimit: Number,
  328. searchstaleness: Number,
  329. 'send-metrics': Boolean,
  330. shell: String,
  331. shrinkwrap: Boolean,
  332. 'sign-git-commit': Boolean,
  333. 'sign-git-tag': Boolean,
  334. 'sso-poll-frequency': Number,
  335. 'sso-type': [null, 'oauth', 'saml'],
  336. 'strict-ssl': Boolean,
  337. tag: String,
  338. timing: Boolean,
  339. tmp: path,
  340. unicode: Boolean,
  341. 'unsafe-perm': Boolean,
  342. 'update-notifier': Boolean,
  343. usage: Boolean,
  344. user: [Number, String],
  345. userconfig: path,
  346. umask: Umask,
  347. version: Boolean,
  348. 'tag-version-prefix': String,
  349. versions: Boolean,
  350. viewer: String,
  351. _exit: Boolean
  352. }
  353. function getLocalAddresses () {
  354. var interfaces
  355. // #8094: some environments require elevated permissions to enumerate
  356. // interfaces, and synchronously throw EPERM when run without
  357. // elevated privileges
  358. try {
  359. interfaces = os.networkInterfaces()
  360. } catch (e) {
  361. interfaces = {}
  362. }
  363. return Object.keys(interfaces).map(
  364. nic => interfaces[nic].map(({address}) => address)
  365. ).reduce((curr, next) => curr.concat(next), []).concat(undefined)
  366. }
  367. exports.shorthands = {
  368. before: ['--enjoy-by'],
  369. s: ['--loglevel', 'silent'],
  370. d: ['--loglevel', 'info'],
  371. dd: ['--loglevel', 'verbose'],
  372. ddd: ['--loglevel', 'silly'],
  373. noreg: ['--no-registry'],
  374. N: ['--no-registry'],
  375. reg: ['--registry'],
  376. 'no-reg': ['--no-registry'],
  377. silent: ['--loglevel', 'silent'],
  378. verbose: ['--loglevel', 'verbose'],
  379. quiet: ['--loglevel', 'warn'],
  380. q: ['--loglevel', 'warn'],
  381. h: ['--usage'],
  382. H: ['--usage'],
  383. '?': ['--usage'],
  384. help: ['--usage'],
  385. v: ['--version'],
  386. f: ['--force'],
  387. desc: ['--description'],
  388. 'no-desc': ['--no-description'],
  389. 'local': ['--no-global'],
  390. l: ['--long'],
  391. m: ['--message'],
  392. p: ['--parseable'],
  393. porcelain: ['--parseable'],
  394. readonly: ['--read-only'],
  395. g: ['--global'],
  396. S: ['--save'],
  397. D: ['--save-dev'],
  398. E: ['--save-exact'],
  399. O: ['--save-optional'],
  400. P: ['--save-prod'],
  401. y: ['--yes'],
  402. n: ['--no-yes'],
  403. B: ['--save-bundle'],
  404. C: ['--prefix']
  405. }