# safe-replace

> A micro-module for safely replacing a file.

Latest version **1.1.0** (published 2018-09-03) · (MIT OR Apache-2.0) license · 0 weekly downloads

## Install

```sh
npm install safe-replace
pnpm add safe-replace
yarn add safe-replace
bun add safe-replace
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-09-03 |
| First published | 2015-12-15 |
| Weekly downloads | 0 |
| License | (MIT OR Apache-2.0) |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AJ ONeal |
| Maintainers | coolaj86 |
| Keywords | cluster, race, condition, file, write, replace, config, json |

## Links

- npm: https://www.npmjs.com/package/safe-replace
- Repository: https://git.coolaj86.com/coolaj86/fs-safe-replace.js
- Homepage: https://git.coolaj86.com/coolaj86/fs-safe-replace.jse
- Issues: https://git.coolaj86.com/coolaj86/fs-safe-replace.js/issues
- npm.io page: https://npm.io/package/safe-replace

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-09-03
- 1.0.4 — 2018-09-03
- 1.0.3 — 2018-07-04
- 1.0.2 — 2015-12-15
- 1.0.1 — 2015-12-15
- 1.0.0 — 2015-12-15

## README

safe-replace
============

A micro-module for safely replacing a file.

This is intended to be generally safe even when a function that writes a file
is accidentally called twice (as may happen with node cluster).

Commandline Reference
---------------------

If I want to safely replace a file with a new version, I would do so like this:

```bash
# create the new version
touch keep.txt.RANDOM.tmp

# remove the previous backup
rm -f keep.txt.bak

# move the current version to the backup
mv keep.txt keep.txt.bak

# move the new version to the current
mv keep.txt.RANDOM.tmp keep.txt
```

If `keep.txt` became corrupt and I wanted to use the backup,
I would do this:

```bash
# copy the backup to the new version
rsync keep.txt.bak keep.txt
```

In Node
-------

I ported that proccess to node.

```
sfs.writeFileAsync
sfs.stageAsync
sfs.commitAsync
sfs.revertAsync
```

```js
// default behavior is to concat (filename + '.' + rnd() + '.tmp')
var safeReplace = require('safe-replace').create({ tmp: 'tmp', bak: 'bak' });

var data = new Buffer('A priceless document');
safeReplace.writeFileAsync('keep.txt', data, 'ascii').then(function () {
  fs.readdir('.', function (nodes) {
    console.log('file system nodes', nodes);
    // keep.txt
    // keep.txt.bak
  });
});

// let's say I want to write a tmp file and not commit it... weird
safeReplace.stageAsync('keep.txt', data, 'ascii').then(function (tmpname) {
  fs.readdir('.', function (nodes) {
    console.log('file system nodes', nodes);
    // keep.txt.ac71teh8mja.tmp
  });
});

// let's say I wrote keep.txt.x7t7sq926.tmp with my own mechanism
safeReplace.commitAsync('keep.txt.x7t7sq926.tmp', 'keep.txt').then(function () {
  fs.readdir('.', function (nodes) {
    console.log('file system nodes', nodes);
    // keep.txt
    // keep.txt.bak
  });
});

// let's say I want to revert the file from the '.bak'
safeReplace.revertAsync('keep.txt').then(function () {
  fs.readdir('.', function (nodes) {
    console.log('file system nodes', nodes);
    // keep.txt
    // keep.txt.bak
  });
});
```

---
_Source: https://npm.io/package/safe-replace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
