Compare commits

...

4 Commits

Author SHA1 Message Date
AJ ONeal 54b2954229 provide instruction for old node users 2018-09-03 13:18:01 -06:00
AJ ONeal b4466d937d v1.0.3 2018-07-04 02:40:14 -06:00
AJ ONeal 7b8536d121 v1.0.2 2015-12-14 23:04:59 -08:00
AJ ONeal cbba2bbbf0 clarify use of promises 2015-12-14 23:04:55 -08:00
3 changed files with 64 additions and 37 deletions

View File

@ -38,30 +38,19 @@ In Node
I ported that proccess to node.
```
sfs.writeFileAsync
sfs.stageAsync
sfs.commitAsync
sfs.revertAsync
```
```js
// default behavior is to concat (filename + '.' + 'new')
var safeReplace = require('safe-replace').create({ new: 'new', bak: 'bak' });
// 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.writeFile('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 wrote keep.txt.x7t7sq926.tmp with my own mechanism
safeReplace.commit('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.revert('keep.txt').then(function () {
safeReplace.writeFileAsync('keep.txt', data, 'ascii').then(function () {
fs.readdir('.', function (nodes) {
console.log('file system nodes', nodes);
// keep.txt
@ -70,12 +59,28 @@ safeReplace.revert('keep.txt').then(function () {
});
// let's say I want to write a tmp file and not commit it... weird
safeReplace.stage('keep.txt', data, 'ascii').then(function (tmpname) {
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
// keep.txt.ac71teh8mja.tmp
});
});
```

View File

@ -1,7 +1,25 @@
'use strict';
var PromiseA = require('bluebird').Promise;
var fs = PromiseA.promisifyAll(require('fs'));
var PromiseA;
try {
PromiseA = require('bluebird');
} catch(e) {
PromiseA = global.Promise;
}
var util = require('util');
var promisify = util.promisify || PromiseA.promisify;
if (!PromiseA || !promisify) {
throw new Error("DON'T PANIC. Everything is A-OK."
+ " However, you're on a really old version of node. All you need to do is `npm install --save bluebird`"
+ " (in your project directory, which is probably '" + require('path').dirname(require.main.filename) + "')"
+ " and everything will work just fine.");
}
var fs = require('fs');
var writeFileAsync = promisify(fs.writeFile);
var unlinkAsync = promisify(fs.unlink);
var renameAsync = promisify(fs.rename);
var crypto = require('crypto');
function noop() {
@ -35,32 +53,32 @@ function create(options) {
*/
var sfs = {
writeFile: function (filename, data, options) {
writeFileAsync: function (filename, data, options) {
return sfs.stage(filename, data, options).then(function (tmpname) {
//console.log(filename);
return sfs.commit(tmpname, filename);
});
}
, stage: function (filename, data, options) {
, stageAsync: function (filename, data, options) {
var tmpname = tmpnamefn(filename);
//console.log(tmpname);
return fs.writeFileAsync(tmpname, data, options).then(function () {
return writeFileAsync(tmpname, data, options).then(function () {
return tmpname;
});
}
, commit: function (tmpname, filename) {
, commitAsync: function (tmpname, filename) {
var bakname = baknamefn(filename);
// this may not exist
return fs.unlinkAsync(bakname).then(noop, noop).then(function () {
return unlinkAsync(bakname).then(noop, noop).then(function () {
// this may not exist
//console.log(namefn(filename), '->', bakname);
return fs.renameAsync(filename, bakname).then(function () {
return renameAsync(filename, bakname).then(function () {
//console.log('created bak');
}, noop);
}).then(function () {
// this must be successful
//console.log(filename, '->', filename);
return fs.renameAsync(tmpname, filename).then(noop, function (err) {
return renameAsync(tmpname, filename).then(noop, function (err) {
//console.error(err);
return sfs.revert(filename).then(function () {
return PromiseA.reject(err);
@ -68,7 +86,7 @@ function create(options) {
});
});
}
, revert: function (filename) {
, revertAsync: function (filename) {
return new PromiseA(function (resolve, reject) {
var bakname = baknamefn(filename);
var tmpname = tmpnamefn(filename);
@ -89,6 +107,10 @@ function create(options) {
, baknamefn: baknamefn
, create: create
};
sfs.writeFile = sfs.writeFileAsync;
sfs.stage = sfs.stageAsync;
sfs.commit = sfs.commitAsync;
sfs.revert = sfs.revertAsync;
return sfs;
}

View File

@ -1,6 +1,6 @@
{
"name": "safe-replace",
"version": "1.0.1",
"version": "1.1.0",
"description": "A micro-module for safely replacing a file.",
"main": "index.js",
"scripts": {
@ -8,7 +8,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/coolaj86/node-safe-replace.git"
"url": "https://git.coolaj86.com/coolaj86/fs-safe-replace.js.git"
},
"keywords": [
"cluster",
@ -23,7 +23,7 @@
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "(MIT OR Apache-2.0)",
"bugs": {
"url": "https://github.com/coolaj86/node-safe-replace/issues"
"url": "https://git.coolaj86.com/coolaj86/fs-safe-replace.js/issues"
},
"homepage": "https://github.com/coolaj86/node-safe-replace#readme"
"homepage": "https://git.coolaj86.com/coolaj86/fs-safe-replace.jse"
}