"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm-install-checks/index.js" (11 Apr 2017, 4151 Bytes) of package /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 = require('fs')
    2 var path = require('path')
    3 var util = require('util')
    4 var semver = require('semver')
    5 
    6 exports.checkEngine = checkEngine
    7 function checkEngine (target, npmVer, nodeVer, force, strict, cb) {
    8   var nodev = force ? null : nodeVer
    9   var eng = target.engines
   10   if (!eng) return cb()
   11   if (nodev && eng.node && !semver.satisfies(nodev, eng.node) ||
   12       eng.npm && !semver.satisfies(npmVer, eng.npm)) {
   13     var er = new Error(util.format('Unsupported engine for %s: wanted: %j (current: %j)',
   14       target._id, eng, {node: nodev, npm: npmVer}))
   15     er.code = 'ENOTSUP'
   16     er.required = eng
   17     er.pkgid = target._id
   18     if (strict) {
   19       return cb(er)
   20     } else {
   21       return cb(null, er)
   22     }
   23   }
   24   return cb()
   25 }
   26 
   27 exports.checkPlatform = checkPlatform
   28 function checkPlatform (target, force, cb) {
   29   var platform = process.platform
   30   var arch = process.arch
   31   var osOk = true
   32   var cpuOk = true
   33 
   34   if (force) {
   35     return cb()
   36   }
   37 
   38   if (target.os) {
   39     osOk = checkList(platform, target.os)
   40   }
   41   if (target.cpu) {
   42     cpuOk = checkList(arch, target.cpu)
   43   }
   44   if (!osOk || !cpuOk) {
   45     var er = new Error(util.format('Unsupported platform for %s: wanted %j (current: %j)',
   46       target._id, target, {os: platform, cpu: arch}))
   47     er.code = 'EBADPLATFORM'
   48     er.os = target.os || ['any']
   49     er.cpu = target.cpu || ['any']
   50     er.pkgid = target._id
   51     return cb(er)
   52   }
   53   return cb()
   54 }
   55 
   56 function checkList (value, list) {
   57   var tmp
   58   var match = false
   59   var blc = 0
   60   if (typeof list === 'string') {
   61     list = [list]
   62   }
   63   if (list.length === 1 && list[0] === 'any') {
   64     return true
   65   }
   66   for (var i = 0; i < list.length; ++i) {
   67     tmp = list[i]
   68     if (tmp[0] === '!') {
   69       tmp = tmp.slice(1)
   70       if (tmp === value) {
   71         return false
   72       }
   73       ++blc
   74     } else {
   75       match = match || tmp === value
   76     }
   77   }
   78   return match || blc === list.length
   79 }
   80 
   81 exports.checkCycle = checkCycle
   82 function checkCycle (target, ancestors, cb) {
   83   // there are some very rare and pathological edge-cases where
   84   // a cycle can cause npm to try to install a never-ending tree
   85   // of stuff.
   86   // Simplest:
   87   //
   88   // A -> B -> A' -> B' -> A -> B -> A' -> B' -> A -> ...
   89   //
   90   // Solution: Simply flat-out refuse to install any name@version
   91   // that is already in the prototype tree of the ancestors object.
   92   // A more correct, but more complex, solution would be to symlink
   93   // the deeper thing into the new location.
   94   // Will do that if anyone whines about this irl.
   95   //
   96   // Note: `npm install foo` inside of the `foo` package will abort
   97   // earlier if `--force` is not set.  However, if it IS set, then
   98   // we need to still fail here, but just skip the first level. Of
   99   // course, it'll still fail eventually if it's a true cycle, and
  100   // leave things in an undefined state, but that's what is to be
  101   // expected when `--force` is used.  That is why getPrototypeOf
  102   // is used *twice* here: to skip the first level of repetition.
  103 
  104   var p = Object.getPrototypeOf(Object.getPrototypeOf(ancestors))
  105   var name = target.name
  106   var version = target.version
  107   while (p && p !== Object.prototype && p[name] !== version) {
  108     p = Object.getPrototypeOf(p)
  109   }
  110   if (p[name] !== version) return cb()
  111 
  112   var er = new Error(target._id + ': Unresolvable cycle detected')
  113   var tree = [target._id, JSON.parse(JSON.stringify(ancestors))]
  114   var t = Object.getPrototypeOf(ancestors)
  115   while (t && t !== Object.prototype) {
  116     if (t === p) t.THIS_IS_P = true
  117     tree.push(JSON.parse(JSON.stringify(t)))
  118     t = Object.getPrototypeOf(t)
  119   }
  120   er.pkgid = target._id
  121   er.code = 'ECYCLE'
  122   return cb(er)
  123 }
  124 
  125 exports.checkGit = checkGit
  126 function checkGit (folder, cb) {
  127   // if it's a git repo then don't touch it!
  128   fs.lstat(folder, function (er, s) {
  129     if (er || !s.isDirectory()) return cb()
  130     else checkGit_(folder, cb)
  131   })
  132 }
  133 
  134 function checkGit_ (folder, cb) {
  135   fs.stat(path.resolve(folder, '.git'), function (er, s) {
  136     if (!er && s.isDirectory()) {
  137       var e = new Error(folder + ': Appears to be a git repo or submodule.')
  138       e.path = folder
  139       e.code = 'EISGIT'
  140       return cb(e)
  141     }
  142     cb()
  143   })
  144 }