/with-temp-file

Call a function with a temporary file, and clean up when the function returns!

Primary LanguageJavaScriptMIT LicenseMIT

with-temp-file

Build Status Coverage Status

Call a function with a temporary file, and clean up when the function returns!

usage

Call withTempFile with a function that takes the writeStream and optionally your own temporary file name. It will be called. It can return a promise or call the callback which is the third argument of the function.

const withTempFile = require('with-temp-file')

async function main () {
  const result = await withTempFile((ws, filename) => {
    ws.write(fileContents())
    ws.end()

    return fs.readFileSync(filename) + ''
  })

  console.log(result)

  const result2 = await withTempFile((ws, filename, cb) => {
    ws.write(fileContents2())
    ws.end()

    // We're done here
    ws.unlink()

    cb()
  }, __dirname + '/.' + Math.random())

  console.log(result2)

}