Compare commits
	
		
			4 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 54b2954229 | |||
| b4466d937d | |||
|  | 7b8536d121 | ||
|  | cbba2bbbf0 | 
							
								
								
									
										51
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								README.md
									
									
									
									
									
								
							| @ -38,30 +38,19 @@ 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 + '.' + 'new') | // default behavior is to concat (filename + '.' + rnd() + '.tmp') | ||||||
| var safeReplace = require('safe-replace').create({ new: 'new', bak: 'bak' }); | var safeReplace = require('safe-replace').create({ tmp: 'tmp', bak: 'bak' }); | ||||||
| 
 | 
 | ||||||
| var data = new Buffer('A priceless document'); | var data = new Buffer('A priceless document'); | ||||||
| safeReplace.writeFile('keep.txt', data, 'ascii').then(function () { | 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 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 () { |  | ||||||
|   fs.readdir('.', function (nodes) { |   fs.readdir('.', function (nodes) { | ||||||
|     console.log('file system nodes', nodes); |     console.log('file system nodes', nodes); | ||||||
|     // keep.txt |     // 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 | // 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) { |   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 | ||||||
|     // keep.txt.ac71teh8mja.tmp |  | ||||||
|   }); |   }); | ||||||
| }); | }); | ||||||
| ``` | ``` | ||||||
|  | |||||||
							
								
								
									
										42
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								index.js
									
									
									
									
									
								
							| @ -1,7 +1,25 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
| 
 | 
 | ||||||
| var PromiseA = require('bluebird').Promise; | var PromiseA; | ||||||
| var fs = PromiseA.promisifyAll(require('fs')); | 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'); | var crypto = require('crypto'); | ||||||
| 
 | 
 | ||||||
| function noop() { | function noop() { | ||||||
| @ -35,32 +53,32 @@ function create(options) { | |||||||
|   */ |   */ | ||||||
| 
 | 
 | ||||||
|   var sfs = { |   var sfs = { | ||||||
|     writeFile: function (filename, data, options) { |     writeFileAsync: 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); | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
|   , stage: function (filename, data, options) { |   , stageAsync: function (filename, data, options) { | ||||||
|       var tmpname = tmpnamefn(filename); |       var tmpname = tmpnamefn(filename); | ||||||
|       //console.log(tmpname);
 |       //console.log(tmpname);
 | ||||||
|       return fs.writeFileAsync(tmpname, data, options).then(function () { |       return writeFileAsync(tmpname, data, options).then(function () { | ||||||
|         return tmpname; |         return tmpname; | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
|   , commit: function (tmpname, filename) { |   , commitAsync: function (tmpname, filename) { | ||||||
|       var bakname = baknamefn(filename); |       var bakname = baknamefn(filename); | ||||||
|       // this may not exist
 |       // 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
 |         // this may not exist
 | ||||||
|         //console.log(namefn(filename), '->', bakname);
 |         //console.log(namefn(filename), '->', bakname);
 | ||||||
|         return fs.renameAsync(filename, bakname).then(function () { |         return 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 fs.renameAsync(tmpname, filename).then(noop, function (err) { |         return 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); | ||||||
| @ -68,7 +86,7 @@ function create(options) { | |||||||
|         }); |         }); | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
|   , revert: function (filename) { |   , revertAsync: 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); | ||||||
| @ -89,6 +107,10 @@ 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; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "safe-replace", |   "name": "safe-replace", | ||||||
|   "version": "1.0.1", |   "version": "1.1.0", | ||||||
|   "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": "git+https://github.com/coolaj86/node-safe-replace.git" |     "url": "https://git.coolaj86.com/coolaj86/fs-safe-replace.js.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://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" | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user