"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/write-file-atomic/index.js" (8 Mar 2017, 1329 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 'use strict'
    2 var fs = require('graceful-fs')
    3 var chain = require('slide').chain
    4 var MurmurHash3 = require('imurmurhash')
    5 
    6 function murmurhex () {
    7   var hash = new MurmurHash3()
    8   for (var ii = 0; ii < arguments.length; ++ii) hash.hash('' + arguments[ii])
    9   return hash.result()
   10 }
   11 var invocations = 0
   12 var getTmpname = function (filename) {
   13   return filename + '.' + murmurhex(__filename, process.pid, ++invocations)
   14 }
   15 
   16 module.exports = function writeFile (filename, data, options, callback) {
   17   if (options instanceof Function) {
   18     callback = options
   19     options = null
   20   }
   21   if (!options) options = {}
   22   var tmpfile = getTmpname(filename)
   23   chain([
   24     [fs, fs.writeFile, tmpfile, data, options],
   25     options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid],
   26     [fs, fs.rename, tmpfile, filename]
   27   ], function (err) {
   28     err ? fs.unlink(tmpfile, function () { callback(err) })
   29       : callback()
   30   })
   31 }
   32 
   33 module.exports.sync = function writeFileSync (filename, data, options) {
   34   if (!options) options = {}
   35   var tmpfile = getTmpname(filename)
   36   try {
   37     fs.writeFileSync(tmpfile, data, options)
   38     if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid)
   39     fs.renameSync(tmpfile, filename)
   40   } catch (err) {
   41     try { fs.unlinkSync(tmpfile) } catch (e) {}
   42     throw err
   43   }
   44 }