]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/fstream-npm/fstream-npm.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / fstream-npm / fstream-npm.js
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
82       // scoped packages
83       if (pkg.charAt(0) === '@') {
84         fs.readdir(pd, function (er, slist) {
85           var sl = slist && slist.length
86           if (er || sl === 0) return then(er)
87
88           l += sl
89           slist.forEach(function (spkg) {
90             if (spkg.charAt(0) === '.') return then()
91             var spd = pd + '/' + spkg
92             fs.realpath(spd, function (er, rp) {
93               if (er) return then()
94               this.bundleLinks = this.bundleLinks || {}
95               this.bundleLinks[pkg + '/' + spkg] = rp
96               then()
97             }.bind(this))
98           }, this)
99           then()
100         }.bind(this))
101         return
102       }
103
104       fs.realpath(pd, function (er, rp) {
105         if (er) return then()
106         this.bundleLinks = this.bundleLinks || {}
107         this.bundleLinks[pkg] = rp
108         then()
109       }.bind(this))
110     }, this)
111   }.bind(this))
112 }
113
114 Packer.prototype.applyIgnores = function (entry, partial, entryObj) {
115   if (!entryObj || entryObj.type !== 'Directory') {
116     // package.json files can never be ignored.
117     if (entry === 'package.json') return true
118
119     // readme files should never be ignored.
120     if (entry.match(/^readme(\.[^\.]*)$/i)) return true
121
122     // license files should never be ignored.
123     if (entry.match(/^(license|licence)(\.[^\.]*)?$/i)) return true
124
125     // copyright notice files should never be ignored.
126     if (entry.match(/^(notice)(\.[^\.]*)?$/i)) return true
127
128     // changelogs should never be ignored.
129     if (entry.match(/^(changes|changelog|history)(\.[^\.]*)?$/i)) return true
130   }
131
132   // special rules.  see below.
133   if (entry === 'node_modules' && this.packageRoot) return true
134
135   // package.json main file should never be ignored.
136   var mainFile = this.package && this.package.main
137   if (mainFile && path.resolve(this.path, entry) === path.resolve(this.path, mainFile)) return true
138
139   // some files are *never* allowed under any circumstances
140   // (VCS folders, native build cruft, npm cruft, regular cruft)
141   if (entry === '.git' ||
142       entry === 'CVS' ||
143       entry === '.svn' ||
144       entry === '.hg' ||
145       entry === '.lock-wscript' ||
146       entry.match(/^\.wafpickle-[0-9]+$/) ||
147       (this.parent && this.parent.packageRoot && this.basename === 'build' &&
148        entry === 'config.gypi') ||
149       entry === 'npm-debug.log' ||
150       entry === '.npmrc' ||
151       entry.match(/^\..*\.swp$/) ||
152       entry === '.DS_Store' ||
153       entry.match(/^\._/)
154     ) {
155     return false
156   }
157
158   // in a node_modules folder, we only include bundled dependencies
159   // also, prevent packages in node_modules from being affected
160   // by rules set in the containing package, so that
161   // bundles don't get busted.
162   // Also, once in a bundle, everything is installed as-is
163   // To prevent infinite cycles in the case of cyclic deps that are
164   // linked with npm link, even in a bundle, deps are only bundled
165   // if they're not already present at a higher level.
166   if (this.bundleMagic) {
167     if (entry.charAt(0) === '@') {
168       var firstSlash = entry.indexOf('/')
169       // continue to list the packages in this scope
170       if (firstSlash === -1) return true
171
172       // bubbling up.  stop here and allow anything the bundled pkg allows
173       if (entry.indexOf('/', firstSlash + 1) !== -1) return true
174     }
175     // bubbling up.  stop here and allow anything the bundled pkg allows
176     else if (entry.indexOf('/') !== -1) return true
177
178     // never include the .bin.  It's typically full of platform-specific
179     // stuff like symlinks and .cmd files anyway.
180     if (entry === '.bin') return false
181
182     // the package root.
183     var p = this.parent
184     // the package before this one.
185     var pp = p && p.parent
186
187     // if this entry has already been bundled, and is a symlink,
188     // and it is the *same* symlink as this one, then exclude it.
189     if (pp && pp.bundleLinks && this.bundleLinks &&
190         pp.bundleLinks[entry] &&
191         pp.bundleLinks[entry] === this.bundleLinks[entry]) {
192       return false
193     }
194
195     // since it's *not* a symbolic link, if we're *already* in a bundle,
196     // then we should include everything.
197     if (pp && pp.package && pp.basename === 'node_modules') {
198       return true
199     }
200
201     // only include it at this point if it's a bundleDependency
202     var bd = this.package && this.package.bundleDependencies
203
204     if (bd && !Array.isArray(bd)) {
205       throw new Error(this.package.name + '\'s `bundledDependencies` should ' +
206                       'be an array')
207     }
208
209     var shouldBundle = bd && bd.indexOf(entry) !== -1
210     // if we're not going to bundle it, then it doesn't count as a bundleLink
211     // if (this.bundleLinks && !shouldBundle) delete this.bundleLinks[entry]
212     return shouldBundle
213   }
214   // if (this.bundled) return true
215
216   return Ignore.prototype.applyIgnores.call(this, entry, partial, entryObj)
217 }
218
219 Packer.prototype.addIgnoreFiles = function () {
220   var entries = this.entries
221   // if there's a .npmignore, then we do *not* want to
222   // read the .gitignore.
223   if (entries.indexOf('.npmignore') !== -1) {
224     var i = entries.indexOf('.gitignore')
225     if (i !== -1) {
226       entries.splice(i, 1)
227     }
228   }
229
230   this.entries = entries
231
232   Ignore.prototype.addIgnoreFiles.call(this)
233 }
234
235 Packer.prototype.readRules = function (buf, e) {
236   if (e !== 'package.json') {
237     return Ignore.prototype.readRules.call(this, buf, e)
238   }
239
240   buf = buf.toString().trim()
241
242   if (buf.length === 0) return []
243
244   try {
245     var p = this.package = JSON.parse(buf)
246   } catch (er) {
247     // just pretend it's a normal old file, not magic at all.
248     return []
249   }
250
251   if (this === this.root) {
252     this.bundleLinks = this.bundleLinks || {}
253     this.bundleLinks[p.name] = this._path
254   }
255
256   this.packageRoot = true
257   this.emit('package', p)
258
259   // make bundle deps predictable
260   if (p.bundledDependencies && !p.bundleDependencies) {
261     p.bundleDependencies = p.bundledDependencies
262     delete p.bundledDependencies
263   }
264
265   if (!p.files || !Array.isArray(p.files)) return []
266
267   // ignore everything except what's in the files array.
268   return ['*'].concat(p.files.map(function (f) {
269     return '!' + f
270   })).concat(p.files.map(function (f) {
271     return '!' + f.replace(/\/+$/, '') + '/**'
272   }))
273 }
274
275 Packer.prototype.getChildProps = function (stat) {
276   var props = Ignore.prototype.getChildProps.call(this, stat)
277
278   props.package = this.package
279
280   props.bundled = this.bundled && this.bundled.slice(0)
281   props.bundleLinks = this.bundleLinks &&
282     Object.create(this.bundleLinks)
283
284   // Directories have to be read as Packers
285   // otherwise fstream.Reader will create a DirReader instead.
286   if (stat.isDirectory()) {
287     props.type = this.constructor
288   }
289
290   // only follow symbolic links directly in the node_modules folder.
291   props.follow = false
292   return props
293 }
294
295 var order = [
296   'package.json',
297   '.npmignore',
298   '.gitignore',
299   /^README(\.md)?$/,
300   'LICENCE',
301   'LICENSE',
302   /\.js$/
303 ]
304
305 Packer.prototype.sort = function (a, b) {
306   for (var i = 0, l = order.length; i < l; i++) {
307     var o = order[i]
308     if (typeof o === 'string') {
309       if (a === o) return -1
310       if (b === o) return 1
311     } else {
312       if (a.match(o)) return -1
313       if (b.match(o)) return 1
314     }
315   }
316
317   // deps go in the back
318   if (a === 'node_modules') return 1
319   if (b === 'node_modules') return -1
320
321   return Ignore.prototype.sort.call(this, a, b)
322 }
323
324 Packer.prototype.emitEntry = function (entry) {
325   if (this._paused) {
326     this.once('resume', this.emitEntry.bind(this, entry))
327     return
328   }
329
330   // if there is a .gitignore, then we're going to
331   // rename it to .npmignore in the output.
332   if (entry.basename === '.gitignore') {
333     entry.basename = '.npmignore'
334     entry.path = path.resolve(entry.dirname, entry.basename)
335   }
336
337   // all *.gyp files are renamed to binding.gyp for node-gyp
338   // but only when they are in the same folder as a package.json file.
339   if (entry.basename.match(/\.gyp$/) &&
340       this.entries.indexOf('package.json') !== -1) {
341     entry.basename = 'binding.gyp'
342     entry.path = path.resolve(entry.dirname, entry.basename)
343   }
344
345   // skip over symbolic links
346   if (entry.type === 'SymbolicLink') {
347     entry.abort()
348     return
349   }
350
351   if (entry.type !== 'Directory') {
352     // make it so that the folder in the tarball is named "package"
353     var h = path.dirname((entry.root || entry).path)
354     var t = entry.path.substr(h.length + 1).replace(/^[^\/\\]+/, 'package')
355     var p = h + '/' + t
356
357     entry.path = p
358     entry.dirname = path.dirname(p)
359     return Ignore.prototype.emitEntry.call(this, entry)
360   }
361
362   // we don't want empty directories to show up in package
363   // tarballs.
364   // don't emit entry events for dirs, but still walk through
365   // and read them.  This means that we need to proxy up their
366   // entry events so that those entries won't be missed, since
367   // .pipe() doesn't do anythign special with "child" events, on
368   // with "entry" events.
369   var me = this
370   entry.on('entry', function (e) {
371     if (e.parent === entry) {
372       e.parent = me
373       me.emit('entry', e)
374     }
375   })
376   entry.on('package', this.emit.bind(this, 'package'))
377 }