clean-write

Cleanup corrupted files on write errors


Keywords
write, clean, cleanup, safe, fs, filesystem, error-handling, error, exceptions, exception-handling, exit, corruption, nodejs, javascript, es6, library, server, files, concurrency, guards
License
Apache-2.0
Install
npm install clean-write@0.0.1

Documentation

Codecov Travis Node Gitter Twitter Medium

Cleanup corrupted files on write errors.

When an exception is thrown while a file is being written, that file will be corrupted. The same problem occurs when the process exits unexpectedly (for example with CTRL-C).

This library removes corrupted files in such cases.

Example

const cleanWrite = require('clean-write')

const { writeFile } = require('fs')
const { promisify } = require('util')
const writeFileAsync = promisify(writeFile)

const filePath = './path/to/file.js'
const writeFunction = async function() {
  await writeFileAsync(filePath, 'example')
}

// If an exception happens or if the program exits, the file will be removed
await cleanWrite(writeFunction, filePath)

Install

npm install clean-write

Usage

cleanWrite(writeFunction, filePath)

writeFunction: () => Promise<void>
filePath: string
Returns: Promise<void>

The file is removed if either:

  • writeFunction() throws an exception or returns a rejected Promise
  • the process exits before writeFunction() completes

Support

If you found a bug or would like a new feature, don't hesitate to submit an issue on GitHub.

For other questions, feel free to chat with us on Gitter.

Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.

Contributing

This project was made with ❤️. The simplest way to give back is by starring and sharing it online.

If the documentation is unclear or has a typo, please click on the page's Edit button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!

ehmicky
ehmicky

💻 🎨 🤔 📖