Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

read-json.js 12KB

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