"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/fs-extra/lib/streams/create-output-stream.js" (8 Mar 2017, 960 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 path = require('path')
    2 var fs = require('fs')
    3 var mkdir = require('../mkdirs')
    4 var WriteStream = fs.WriteStream
    5 
    6 function createOutputStream (file, options) {
    7   var dirExists = false
    8   var dir = path.dirname(file)
    9   options = options || {}
   10 
   11   // if fd is set with an actual number, file is created, hence directory is too
   12   if (options.fd) {
   13     return fs.createWriteStream(file, options)
   14   } else {
   15     // this hacks the WriteStream constructor from calling open()
   16     options.fd = -1
   17   }
   18 
   19   var ws = new WriteStream(file, options)
   20 
   21   var oldOpen = ws.open
   22   ws.open = function () {
   23     ws.fd = null // set actual fd
   24     if (dirExists) return oldOpen.call(ws)
   25 
   26     // this only runs once on first write
   27     mkdir.mkdirs(dir, function (err) {
   28       if (err) {
   29         ws.destroy()
   30         ws.emit('error', err)
   31         return
   32       }
   33       dirExists = true
   34       oldOpen.call(ws)
   35     })
   36   }
   37 
   38   ws.open()
   39 
   40   return ws
   41 }
   42 
   43 module.exports = createOutputStream