read-json.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. var fs
  2. try {
  3. fs = require('graceful-fs')
  4. } catch (er) {
  5. fs = require('fs')
  6. }
  7. var path = require('path')
  8. var glob = require('glob')
  9. var normalizeData = require('normalize-package-data')
  10. var safeJSON = require('json-parse-better-errors')
  11. var util = require('util')
  12. var normalizePackageBin = require('npm-normalize-package-bin')
  13. module.exports = readJson
  14. // put more stuff on here to customize.
  15. readJson.extraSet = [
  16. bundleDependencies,
  17. gypfile,
  18. serverjs,
  19. scriptpath,
  20. authors,
  21. readme,
  22. mans,
  23. bins,
  24. githead
  25. ]
  26. var typoWarned = {}
  27. var cache = {}
  28. function readJson (file, log_, strict_, cb_) {
  29. var log, strict, cb
  30. for (var i = 1; i < arguments.length - 1; i++) {
  31. if (typeof arguments[i] === 'boolean') {
  32. strict = arguments[i]
  33. } else if (typeof arguments[i] === 'function') {
  34. log = arguments[i]
  35. }
  36. }
  37. if (!log) log = function () {}
  38. cb = arguments[ arguments.length - 1 ]
  39. readJson_(file, log, strict, cb)
  40. }
  41. function readJson_ (file, log, strict, cb) {
  42. fs.readFile(file, 'utf8', function (er, d) {
  43. parseJson(file, er, d, log, strict, cb)
  44. })
  45. }
  46. function stripBOM (content) {
  47. // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  48. // because the buffer-to-string conversion in `fs.readFileSync()`
  49. // translates it to FEFF, the UTF-16 BOM.
  50. if (content.charCodeAt(0) === 0xFEFF) content = content.slice(1)
  51. return content
  52. }
  53. function jsonClone (obj) {
  54. if (obj == null) {
  55. return obj
  56. } else if (Array.isArray(obj)) {
  57. var newarr = new Array(obj.length)
  58. for (var ii in obj) {
  59. newarr[ii] = obj[ii]
  60. }
  61. } else if (typeof obj === 'object') {
  62. var newobj = {}
  63. for (var kk in obj) {
  64. newobj[kk] = jsonClone[kk]
  65. }
  66. } else {
  67. return obj
  68. }
  69. }
  70. function parseJson (file, er, d, log, strict, cb) {
  71. if (er && er.code === 'ENOENT') {
  72. return fs.stat(path.dirname(file), function (err, stat) {
  73. if (!err && stat && !stat.isDirectory()) {
  74. // ENOTDIR isn't used on Windows, but npm expects it.
  75. er = Object.create(er)
  76. er.code = 'ENOTDIR'
  77. return cb(er)
  78. } else {
  79. return indexjs(file, er, log, strict, cb)
  80. }
  81. })
  82. }
  83. if (er) return cb(er)
  84. if (cache[d]) return cb(null, jsonClone(cache[d]))
  85. var data
  86. try {
  87. data = safeJSON(stripBOM(d))
  88. } catch (er) {
  89. data = parseIndex(d)
  90. if (!data) return cb(parseError(er, file))
  91. }
  92. extrasCached(file, d, data, log, strict, cb)
  93. }
  94. function extrasCached (file, d, data, log, strict, cb) {
  95. extras(file, data, log, strict, function (err, data) {
  96. if (!err) {
  97. cache[d] = jsonClone(data)
  98. }
  99. cb(err, data)
  100. })
  101. }
  102. function indexjs (file, er, log, strict, cb) {
  103. if (path.basename(file) === 'index.js') return cb(er)
  104. var index = path.resolve(path.dirname(file), 'index.js')
  105. fs.readFile(index, 'utf8', function (er2, d) {
  106. if (er2) return cb(er)
  107. if (cache[d]) return cb(null, cache[d])
  108. var data = parseIndex(d)
  109. if (!data) return cb(er)
  110. extrasCached(file, d, data, log, strict, cb)
  111. })
  112. }
  113. readJson.extras = extras
  114. function extras (file, data, log_, strict_, cb_) {
  115. var log, strict, cb
  116. for (var i = 2; i < arguments.length - 1; i++) {
  117. if (typeof arguments[i] === 'boolean') {
  118. strict = arguments[i]
  119. } else if (typeof arguments[i] === 'function') {
  120. log = arguments[i]
  121. }
  122. }
  123. if (!log) log = function () {}
  124. cb = arguments[i]
  125. var set = readJson.extraSet
  126. var n = set.length
  127. var errState = null
  128. set.forEach(function (fn) {
  129. fn(file, data, then)
  130. })
  131. function then (er) {
  132. if (errState) return
  133. if (er) return cb(errState = er)
  134. if (--n > 0) return
  135. final(file, data, log, strict, cb)
  136. }
  137. }
  138. function scriptpath (file, data, cb) {
  139. if (!data.scripts) return cb(null, data)
  140. var k = Object.keys(data.scripts)
  141. k.forEach(scriptpath_, data.scripts)
  142. cb(null, data)
  143. }
  144. function scriptpath_ (key) {
  145. var s = this[key]
  146. // This is never allowed, and only causes problems
  147. if (typeof s !== 'string') return delete this[key]
  148. var spre = /^(\.[/\\])?node_modules[/\\].bin[\\/]/
  149. if (s.match(spre)) {
  150. this[key] = this[key].replace(spre, '')
  151. }
  152. }
  153. function gypfile (file, data, cb) {
  154. var dir = path.dirname(file)
  155. var s = data.scripts || {}
  156. if (s.install || s.preinstall) return cb(null, data)
  157. glob('*.gyp', { cwd: dir }, function (er, files) {
  158. if (er) return cb(er)
  159. if (data.gypfile === false) return cb(null, data)
  160. gypfile_(file, data, files, cb)
  161. })
  162. }
  163. function gypfile_ (file, data, files, cb) {
  164. if (!files.length) return cb(null, data)
  165. var s = data.scripts || {}
  166. s.install = 'node-gyp rebuild'
  167. data.scripts = s
  168. data.gypfile = true
  169. return cb(null, data)
  170. }
  171. function serverjs (file, data, cb) {
  172. var dir = path.dirname(file)
  173. var s = data.scripts || {}
  174. if (s.start) return cb(null, data)
  175. glob('server.js', { cwd: dir }, function (er, files) {
  176. if (er) return cb(er)
  177. serverjs_(file, data, files, cb)
  178. })
  179. }
  180. function serverjs_ (file, data, files, cb) {
  181. if (!files.length) return cb(null, data)
  182. var s = data.scripts || {}
  183. s.start = 'node server.js'
  184. data.scripts = s
  185. return cb(null, data)
  186. }
  187. function authors (file, data, cb) {
  188. if (data.contributors) return cb(null, data)
  189. var af = path.resolve(path.dirname(file), 'AUTHORS')
  190. fs.readFile(af, 'utf8', function (er, ad) {
  191. // ignore error. just checking it.
  192. if (er) return cb(null, data)
  193. authors_(file, data, ad, cb)
  194. })
  195. }
  196. function authors_ (file, data, ad, cb) {
  197. ad = ad.split(/\r?\n/g).map(function (line) {
  198. return line.replace(/^\s*#.*$/, '').trim()
  199. }).filter(function (line) {
  200. return line
  201. })
  202. data.contributors = ad
  203. return cb(null, data)
  204. }
  205. function readme (file, data, cb) {
  206. if (data.readme) return cb(null, data)
  207. var dir = path.dirname(file)
  208. var globOpts = { cwd: dir, nocase: true, mark: true }
  209. glob('{README,README.*}', globOpts, function (er, files) {
  210. if (er) return cb(er)
  211. // don't accept directories.
  212. files = files.filter(function (file) {
  213. return !file.match(/\/$/)
  214. })
  215. if (!files.length) return cb()
  216. var fn = preferMarkdownReadme(files)
  217. var rm = path.resolve(dir, fn)
  218. readme_(file, data, rm, cb)
  219. })
  220. }
  221. function preferMarkdownReadme (files) {
  222. var fallback = 0
  223. var re = /\.m?a?r?k?d?o?w?n?$/i
  224. for (var i = 0; i < files.length; i++) {
  225. if (files[i].match(re)) {
  226. return files[i]
  227. } else if (files[i].match(/README$/)) {
  228. fallback = i
  229. }
  230. }
  231. // prefer README.md, followed by README; otherwise, return
  232. // the first filename (which could be README)
  233. return files[fallback]
  234. }
  235. function readme_ (file, data, rm, cb) {
  236. var rmfn = path.basename(rm)
  237. fs.readFile(rm, 'utf8', function (er, rm) {
  238. // maybe not readable, or something.
  239. if (er) return cb()
  240. data.readme = rm
  241. data.readmeFilename = rmfn
  242. return cb(er, data)
  243. })
  244. }
  245. function mans (file, data, cb) {
  246. var m = data.directories && data.directories.man
  247. if (data.man || !m) return cb(null, data)
  248. m = path.resolve(path.dirname(file), m)
  249. glob('**/*.[0-9]', { cwd: m }, function (er, mans) {
  250. if (er) return cb(er)
  251. mans_(file, data, mans, cb)
  252. })
  253. }
  254. function mans_ (file, data, mans, cb) {
  255. var m = data.directories && data.directories.man
  256. data.man = mans.map(function (mf) {
  257. return path.resolve(path.dirname(file), m, mf)
  258. })
  259. return cb(null, data)
  260. }
  261. function bins (file, data, cb) {
  262. data = normalizePackageBin(data)
  263. var m = data.directories && data.directories.bin
  264. if (data.bin || !m) return cb(null, data)
  265. m = path.resolve(path.dirname(file), m)
  266. glob('**', { cwd: m }, function (er, bins) {
  267. if (er) return cb(er)
  268. bins_(file, data, bins, cb)
  269. })
  270. }
  271. function bins_ (file, data, bins, cb) {
  272. var m = (data.directories && data.directories.bin) || '.'
  273. data.bin = bins.reduce(function (acc, mf) {
  274. if (mf && mf.charAt(0) !== '.') {
  275. var f = path.basename(mf)
  276. acc[f] = path.join(m, mf)
  277. }
  278. return acc
  279. }, {})
  280. return cb(null, normalizePackageBin(data))
  281. }
  282. function bundleDependencies (file, data, cb) {
  283. var bd = 'bundleDependencies'
  284. var bdd = 'bundledDependencies'
  285. // normalize key name
  286. if (data[bdd] !== undefined) {
  287. if (data[bd] === undefined) data[bd] = data[bdd]
  288. delete data[bdd]
  289. }
  290. if (data[bd] === false) delete data[bd]
  291. else if (data[bd] === true) {
  292. data[bd] = Object.keys(data.dependencies || {})
  293. } else if (data[bd] !== undefined && !Array.isArray(data[bd])) {
  294. delete data[bd]
  295. }
  296. return cb(null, data)
  297. }
  298. function githead (file, data, cb) {
  299. if (data.gitHead) return cb(null, data)
  300. var dir = path.dirname(file)
  301. var head = path.resolve(dir, '.git/HEAD')
  302. fs.readFile(head, 'utf8', function (er, head) {
  303. if (er) return cb(null, data)
  304. githead_(file, data, dir, head, cb)
  305. })
  306. }
  307. function githead_ (file, data, dir, head, cb) {
  308. if (!head.match(/^ref: /)) {
  309. data.gitHead = head.trim()
  310. return cb(null, data)
  311. }
  312. var headRef = head.replace(/^ref: /, '').trim()
  313. var headFile = path.resolve(dir, '.git', headRef)
  314. fs.readFile(headFile, 'utf8', function (er, head) {
  315. if (er || !head) {
  316. var packFile = path.resolve(dir, '.git/packed-refs')
  317. return fs.readFile(packFile, 'utf8', function (er, refs) {
  318. if (er || !refs) {
  319. return cb(null, data)
  320. }
  321. refs = refs.split('\n')
  322. for (var i = 0; i < refs.length; i++) {
  323. var match = refs[i].match(/^([0-9a-f]{40}) (.+)$/)
  324. if (match && match[2].trim() === headRef) {
  325. data.gitHead = match[1]
  326. break
  327. }
  328. }
  329. return cb(null, data)
  330. })
  331. }
  332. head = head.replace(/^ref: /, '').trim()
  333. data.gitHead = head
  334. return cb(null, data)
  335. })
  336. }
  337. /**
  338. * Warn if the bin references don't point to anything. This might be better in
  339. * normalize-package-data if it had access to the file path.
  340. */
  341. function checkBinReferences_ (file, data, warn, cb) {
  342. if (!(data.bin instanceof Object)) return cb()
  343. var keys = Object.keys(data.bin)
  344. var keysLeft = keys.length
  345. if (!keysLeft) return cb()
  346. function handleExists (relName, result) {
  347. keysLeft--
  348. if (!result) warn('No bin file found at ' + relName)
  349. if (!keysLeft) cb()
  350. }
  351. keys.forEach(function (key) {
  352. var dirName = path.dirname(file)
  353. var relName = data.bin[key]
  354. /* istanbul ignore if - impossible, bins have been normalized */
  355. if (typeof relName !== 'string') {
  356. var msg = 'Bin filename for ' + key +
  357. ' is not a string: ' + util.inspect(relName)
  358. warn(msg)
  359. delete data.bin[key]
  360. handleExists(relName, true)
  361. return
  362. }
  363. var binPath = path.resolve(dirName, relName)
  364. fs.stat(binPath, (err) => handleExists(relName, !err))
  365. })
  366. }
  367. function final (file, data, log, strict, cb) {
  368. var pId = makePackageId(data)
  369. function warn (msg) {
  370. if (typoWarned[pId]) return
  371. if (log) log('package.json', pId, msg)
  372. }
  373. try {
  374. normalizeData(data, warn, strict)
  375. } catch (error) {
  376. return cb(error)
  377. }
  378. checkBinReferences_(file, data, warn, function () {
  379. typoWarned[pId] = true
  380. cb(null, data)
  381. })
  382. }
  383. function makePackageId (data) {
  384. var name = cleanString(data.name)
  385. var ver = cleanString(data.version)
  386. return name + '@' + ver
  387. }
  388. function cleanString (str) {
  389. return (!str || typeof (str) !== 'string') ? '' : str.trim()
  390. }
  391. // /**package { "name": "foo", "version": "1.2.3", ... } **/
  392. function parseIndex (data) {
  393. data = data.split(/^\/\*\*package(?:\s|$)/m)
  394. if (data.length < 2) return null
  395. data = data[1]
  396. data = data.split(/\*\*\/$/m)
  397. if (data.length < 2) return null
  398. data = data[0]
  399. data = data.replace(/^\s*\*/mg, '')
  400. try {
  401. return safeJSON(data)
  402. } catch (er) {
  403. return null
  404. }
  405. }
  406. function parseError (ex, file) {
  407. var e = new Error('Failed to parse json\n' + ex.message)
  408. e.code = 'EJSONPARSE'
  409. e.file = file
  410. return e
  411. }