"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/read-package-json/read-json.js" (7 Feb 2017, 9758 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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