Compare commits

..

No commits in common. "master" and "v1.0.1" have entirely different histories.

3 changed files with 29 additions and 56 deletions

View File

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

View File

@ -1,25 +1,7 @@
'use strict'; 'use strict';
var PromiseA; var PromiseA = require('bluebird').Promise;
try { var fs = PromiseA.promisifyAll(require('fs'));
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'); var crypto = require('crypto');
function noop() { function noop() {
@ -53,32 +35,32 @@ function create(options) {
*/ */
var sfs = { var sfs = {
writeFileAsync: function (filename, data, options) { writeFile: function (filename, data, options) {
return sfs.stage(filename, data, options).then(function (tmpname) { return sfs.stage(filename, data, options).then(function (tmpname) {
//console.log(filename); //console.log(filename);
return sfs.commit(tmpname, filename); return sfs.commit(tmpname, filename);
}); });
} }
, stageAsync: function (filename, data, options) { , stage: function (filename, data, options) {
var tmpname = tmpnamefn(filename); var tmpname = tmpnamefn(filename);
//console.log(tmpname); //console.log(tmpname);
return writeFileAsync(tmpname, data, options).then(function () { return fs.writeFileAsync(tmpname, data, options).then(function () {
return tmpname; return tmpname;
}); });
} }
, commitAsync: function (tmpname, filename) { , commit: function (tmpname, filename) {
var bakname = baknamefn(filename); var bakname = baknamefn(filename);
// this may not exist // this may not exist
return unlinkAsync(bakname).then(noop, noop).then(function () { return fs.unlinkAsync(bakname).then(noop, noop).then(function () {
// this may not exist // this may not exist
//console.log(namefn(filename), '->', bakname); //console.log(namefn(filename), '->', bakname);
return renameAsync(filename, bakname).then(function () { return fs.renameAsync(filename, bakname).then(function () {
//console.log('created bak'); //console.log('created bak');
}, noop); }, noop);
}).then(function () { }).then(function () {
// this must be successful // this must be successful
//console.log(filename, '->', filename); //console.log(filename, '->', filename);
return renameAsync(tmpname, filename).then(noop, function (err) { return fs.renameAsync(tmpname, filename).then(noop, function (err) {
//console.error(err); //console.error(err);
return sfs.revert(filename).then(function () { return sfs.revert(filename).then(function () {
return PromiseA.reject(err); return PromiseA.reject(err);
@ -86,7 +68,7 @@ function create(options) {
}); });
}); });
} }
, revertAsync: function (filename) { , revert: function (filename) {
return new PromiseA(function (resolve, reject) { return new PromiseA(function (resolve, reject) {
var bakname = baknamefn(filename); var bakname = baknamefn(filename);
var tmpname = tmpnamefn(filename); var tmpname = tmpnamefn(filename);
@ -107,10 +89,6 @@ function create(options) {
, baknamefn: baknamefn , baknamefn: baknamefn
, create: create , create: create
}; };
sfs.writeFile = sfs.writeFileAsync;
sfs.stage = sfs.stageAsync;
sfs.commit = sfs.commitAsync;
sfs.revert = sfs.revertAsync;
return sfs; return sfs;
} }

View File

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