Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pedromsilvapt/safe-file-write

Write to a file, making sure it's parent folders exist, and that if the write operation is cancelled mid-writing, it does not corrupt any possible file there previously.
https://github.com/pedromsilvapt/safe-file-write

Last synced: 11 days ago
JSON representation

Write to a file, making sure it's parent folders exist, and that if the write operation is cancelled mid-writing, it does not corrupt any possible file there previously.

Awesome Lists containing this project

README

        

# safe-file-write

> Write to a file, making sure it's parent folders exist, and that if the write operation is cancelled mid-writing, it does not corrupt any possible file there previously.

# Installation
You can install this package globally, so that it is accessible everywhere in your system.

```shell
npm install --save safe-file-write
```

# Usage
Just import the module and call the function. Ir returns a function the resolves when the writing has finished.

```typescript
import writeFileSafe from 'write-file-safe';

writeFileSafe( '/path/to/file.txt', contents )
.then( () => console.log( 'FINISHED' ) )
.catch( error => console.error( error ) );
```

There is also a synchronous version.

```typescript
try {
import { writeFileSafeSync } from 'write-file-safe';

writeFileSafeSync( '/path/to/file.txt', contents );

console.log( 'FINISHED' );
} catch ( error ) {
error => console.error( error )
}
```