"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/fstream-npm/fstream-npm.js" (7 Feb 2017, 10310 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 Ignore = require('fstream-ignore')
    2 var inherits = require('inherits')
    3 var path = require('path')
    4 var fs = require('fs')
    5 
    6 module.exports = Packer
    7 
    8 inherits(Packer, Ignore)
    9 
   10 function Packer (props) {
   11   if (!(this instanceof Packer)) {
   12     return new Packer(props)
   13   }
   14 
   15   if (typeof props === 'string') {
   16     props = { path: props }
   17   }
   18 
   19   props.ignoreFiles = props.ignoreFiles || [ '.npmignore',
   20                                              '.gitignore',
   21                                              'package.json' ]
   22 
   23   Ignore.call(this, props)
   24 
   25   this.bundled = props.bundled
   26   this.bundleLinks = props.bundleLinks
   27   this.package = props.package
   28 
   29   // only do the magic bundling stuff for the node_modules folder that
   30   // lives right next to a package.json file.
   31   this.bundleMagic = this.parent &&
   32                      this.parent.packageRoot &&
   33                      this.basename === 'node_modules'
   34 
   35   // in a node_modules folder, resolve symbolic links to
   36   // bundled dependencies when creating the package.
   37   props.follow = this.follow = this.bundleMagic
   38   // console.error("follow?", this.path, props.follow)
   39 
   40   if (this === this.root ||
   41       this.parent &&
   42       this.parent.bundleMagic &&
   43       this.basename.charAt(0) !== '.') {
   44     this.readBundledLinks()
   45   }
   46 
   47   this.on('entryStat', function (entry, props) {
   48     // files should *always* get into tarballs
   49     // in a user-writable state, even if they're
   50     // being installed from some wackey vm-mounted
   51     // read-only filesystem.
   52     entry.mode = props.mode = props.mode | parseInt('0200', 8)
   53   })
   54 }
   55 
   56 Packer.prototype.readBundledLinks = function () {
   57   if (this._paused) {
   58     this.once('resume', this.addIgnoreFiles)
   59     return
   60   }
   61 
   62   this.pause()
   63   fs.readdir(this.path + '/node_modules', function (er, list) {
   64     // no harm if there's no bundle
   65     var l = list && list.length
   66     if (er || l === 0) return this.resume()
   67 
   68     var errState = null
   69     var then = function then (er) {
   70       if (errState) return
   71       if (er) {
   72         errState = er
   73         return this.resume()
   74       }
   75       if (--l === 0) return this.resume()
   76     }.bind(this)
   77 
   78     list.forEach(function (pkg) {
   79       if (pkg.charAt(0) === '.') return then()
   80       var pd = this.path + '/node_modules/' + pkg
   81       fs.realpath(pd, function (er, rp) {
   82         if (er) return then()
   83         this.bundleLinks = this.bundleLinks || {}
   84         this.bundleLinks[pkg] = rp
   85         then()
   86       }.bind(this))
   87     }, this)
   88   }.bind(this))
   89 }
   90 
   91 Packer.prototype.applyIgnores = function (entry, partial, entryObj) {
   92   if (!entryObj || entryObj.type !== 'Directory') {
   93     // package.json files can never be ignored.
   94     if (entry === 'package.json') return true
   95 
   96     // readme files should never be ignored.
   97     if (entry.match(/^readme(\.[^\.]*)$/i)) return true
   98 
   99     // license files should never be ignored.
  100     if (entry.match(/^(license|licence)(\.[^\.]*)?$/i)) return true
  101 
  102     // copyright notice files should never be ignored.
  103     if (entry.match(/^(notice)(\.[^\.]*)?$/i)) return true
  104 
  105     // changelogs should never be ignored.
  106     if (entry.match(/^(changes|changelog|history)(\.[^\.]*)?$/i)) return true
  107   }
  108 
  109   // special rules.  see below.
  110   if (entry === 'node_modules' && this.packageRoot) return true
  111 
  112   // package.json main file should never be ignored.
  113   var mainFile = this.package && this.package.main
  114   if (mainFile && path.resolve(this.path, entry) === path.resolve(this.path, mainFile)) return true
  115 
  116   // some files are *never* allowed under any circumstances
  117   // (VCS folders, native build cruft, npm cruft, regular cruft)
  118   if (entry === '.git' ||
  119       entry === 'CVS' ||
  120       entry === '.svn' ||
  121       entry === '.hg' ||
  122       entry === '.lock-wscript' ||
  123       entry.match(/^\.wafpickle-[0-9]+$/) ||
  124       (this.parent && this.parent.packageRoot && this.basename === 'build' &&
  125        entry === 'config.gypi') ||
  126       entry === 'npm-debug.log' ||
  127       entry === '.npmrc' ||
  128       entry.match(/^\..*\.swp$/) ||
  129       entry === '.DS_Store' ||
  130       entry.match(/^\._/)
  131     ) {
  132     return false
  133   }
  134 
  135   // in a node_modules folder, we only include bundled dependencies
  136   // also, prevent packages in node_modules from being affected
  137   // by rules set in the containing package, so that
  138   // bundles don't get busted.
  139   // Also, once in a bundle, everything is installed as-is
  140   // To prevent infinite cycles in the case of cyclic deps that are
  141   // linked with npm link, even in a bundle, deps are only bundled
  142   // if they're not already present at a higher level.
  143   if (this.bundleMagic) {
  144     // bubbling up.  stop here and allow anything the bundled pkg allows
  145     if (entry.indexOf('/') !== -1) return true
  146 
  147     // never include the .bin.  It's typically full of platform-specific
  148     // stuff like symlinks and .cmd files anyway.
  149     if (entry === '.bin') return false
  150 
  151     // the package root.
  152     var p = this.parent
  153     // the package before this one.
  154     var pp = p && p.parent
  155 
  156     // if this entry has already been bundled, and is a symlink,
  157     // and it is the *same* symlink as this one, then exclude it.
  158     if (pp && pp.bundleLinks && this.bundleLinks &&
  159         pp.bundleLinks[entry] &&
  160         pp.bundleLinks[entry] === this.bundleLinks[entry]) {
  161       return false
  162     }
  163 
  164     // since it's *not* a symbolic link, if we're *already* in a bundle,
  165     // then we should include everything.
  166     if (pp && pp.package && pp.basename === 'node_modules') {
  167       return true
  168     }
  169 
  170     // only include it at this point if it's a bundleDependency
  171     var bd = this.package && this.package.bundleDependencies
  172 
  173     if (bd && !Array.isArray(bd)) {
  174       throw new Error(this.package.name + '\'s `bundledDependencies` should ' +
  175                       'be an array')
  176     }
  177 
  178     var shouldBundle = bd && bd.indexOf(entry) !== -1
  179     // if we're not going to bundle it, then it doesn't count as a bundleLink
  180     // if (this.bundleLinks && !shouldBundle) delete this.bundleLinks[entry]
  181     return shouldBundle
  182   }
  183   // if (this.bundled) return true
  184 
  185   return Ignore.prototype.applyIgnores.call(this, entry, partial, entryObj)
  186 }
  187 
  188 Packer.prototype.addIgnoreFiles = function () {
  189   var entries = this.entries
  190   // if there's a .npmignore, then we do *not* want to
  191   // read the .gitignore.
  192   if (entries.indexOf('.npmignore') !== -1) {
  193     var i = entries.indexOf('.gitignore')
  194     if (i !== -1) {
  195       entries.splice(i, 1)
  196     }
  197   }
  198 
  199   this.entries = entries
  200 
  201   Ignore.prototype.addIgnoreFiles.call(this)
  202 }
  203 
  204 Packer.prototype.readRules = function (buf, e) {
  205   if (e !== 'package.json') {
  206     return Ignore.prototype.readRules.call(this, buf, e)
  207   }
  208 
  209   buf = buf.toString().trim()
  210 
  211   if (buf.length === 0) return []
  212 
  213   try {
  214     var p = this.package = JSON.parse(buf)
  215   } catch (er) {
  216     // just pretend it's a normal old file, not magic at all.
  217     return []
  218   }
  219 
  220   if (this === this.root) {
  221     this.bundleLinks = this.bundleLinks || {}
  222     this.bundleLinks[p.name] = this._path
  223   }
  224 
  225   this.packageRoot = true
  226   this.emit('package', p)
  227 
  228   // make bundle deps predictable
  229   if (p.bundledDependencies && !p.bundleDependencies) {
  230     p.bundleDependencies = p.bundledDependencies
  231     delete p.bundledDependencies
  232   }
  233 
  234   if (!p.files || !Array.isArray(p.files)) return []
  235 
  236   // ignore everything except what's in the files array.
  237   return ['*'].concat(p.files.map(function (f) {
  238     return '!' + f
  239   })).concat(p.files.map(function (f) {
  240     return '!' + f.replace(/\/+$/, '') + '/**'
  241   }))
  242 }
  243 
  244 Packer.prototype.getChildProps = function (stat) {
  245   var props = Ignore.prototype.getChildProps.call(this, stat)
  246 
  247   props.package = this.package
  248 
  249   props.bundled = this.bundled && this.bundled.slice(0)
  250   props.bundleLinks = this.bundleLinks &&
  251     Object.create(this.bundleLinks)
  252 
  253   // Directories have to be read as Packers
  254   // otherwise fstream.Reader will create a DirReader instead.
  255   if (stat.isDirectory()) {
  256     props.type = this.constructor
  257   }
  258 
  259   // only follow symbolic links directly in the node_modules folder.
  260   props.follow = false
  261   return props
  262 }
  263 
  264 var order = [
  265   'package.json',
  266   '.npmignore',
  267   '.gitignore',
  268   /^README(\.md)?$/,
  269   'LICENCE',
  270   'LICENSE',
  271   /\.js$/
  272 ]
  273 
  274 Packer.prototype.sort = function (a, b) {
  275   for (var i = 0, l = order.length; i < l; i++) {
  276     var o = order[i]
  277     if (typeof o === 'string') {
  278       if (a === o) return -1
  279       if (b === o) return 1
  280     } else {
  281       if (a.match(o)) return -1
  282       if (b.match(o)) return 1
  283     }
  284   }
  285 
  286   // deps go in the back
  287   if (a === 'node_modules') return 1
  288   if (b === 'node_modules') return -1
  289 
  290   return Ignore.prototype.sort.call(this, a, b)
  291 }
  292 
  293 Packer.prototype.emitEntry = function (entry) {
  294   if (this._paused) {
  295     this.once('resume', this.emitEntry.bind(this, entry))
  296     return
  297   }
  298 
  299   // if there is a .gitignore, then we're going to
  300   // rename it to .npmignore in the output.
  301   if (entry.basename === '.gitignore') {
  302     entry.basename = '.npmignore'
  303     entry.path = path.resolve(entry.dirname, entry.basename)
  304   }
  305 
  306   // all *.gyp files are renamed to binding.gyp for node-gyp
  307   // but only when they are in the same folder as a package.json file.
  308   if (entry.basename.match(/\.gyp$/) &&
  309       this.entries.indexOf('package.json') !== -1) {
  310     entry.basename = 'binding.gyp'
  311     entry.path = path.resolve(entry.dirname, entry.basename)
  312   }
  313 
  314   // skip over symbolic links
  315   if (entry.type === 'SymbolicLink') {
  316     entry.abort()
  317     return
  318   }
  319 
  320   if (entry.type !== 'Directory') {
  321     // make it so that the folder in the tarball is named "package"
  322     var h = path.dirname((entry.root || entry).path)
  323     var t = entry.path.substr(h.length + 1).replace(/^[^\/\\]+/, 'package')
  324     var p = h + '/' + t
  325 
  326     entry.path = p
  327     entry.dirname = path.dirname(p)
  328     return Ignore.prototype.emitEntry.call(this, entry)
  329   }
  330 
  331   // we don't want empty directories to show up in package
  332   // tarballs.
  333   // don't emit entry events for dirs, but still walk through
  334   // and read them.  This means that we need to proxy up their
  335   // entry events so that those entries won't be missed, since
  336   // .pipe() doesn't do anythign special with "child" events, on
  337   // with "entry" events.
  338   var me = this
  339   entry.on('entry', function (e) {
  340     if (e.parent === entry) {
  341       e.parent = me
  342       me.emit('entry', e)
  343     }
  344   })
  345   entry.on('package', this.emit.bind(this, 'package'))
  346 }