From 4ab5711d06174fdcb7dcdfdccb3fd84c1f50d6ac Mon Sep 17 00:00:00 2001 From: Amoki Date: Wed, 4 Jun 2014 19:22:22 +0200 Subject: [PATCH 1/9] save, load and renew cursor --- README.md | 2 +- lib/helpers/cursor.js | 66 ++++++++++++++++++++++++++++++++++++++ lib/helpers/upload.js | 24 +++++++++++--- test/helpers/cursor.js | 44 +++++++++++++++++++++++++ test/helpers/list-files.js | 5 ++- test/helpers/upload.js | 40 +++++++++++++++++++++++ 6 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 lib/helpers/cursor.js create mode 100644 test/helpers/cursor.js create mode 100644 test/helpers/upload.js diff --git a/README.md b/README.md index dab64fb..162d6e9 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,4 @@ The command is long running, and will watch for file changes until the process d > You can omit the `$ACCESS_TOKEN` and enter it interactively. ## Reset -Every time you run this command, a JSON file is stored in `~/.anyfetch-file-watcher/$ACCESS_TOKEN/$STRIPPED_DIRECTORY.json` with cursor details. If you want to resend everything, remove this file. +Every time you run this command, a JSON file is stored in `~/.anyfetch-file-watcher/$STRIPPED_DIRECTORY.json` with cursor details. If you want to resend everything, remove this file. diff --git a/lib/helpers/cursor.js b/lib/helpers/cursor.js new file mode 100644 index 0000000..b05eeb4 --- /dev/null +++ b/lib/helpers/cursor.js @@ -0,0 +1,66 @@ +"use strict"; +var fs = require('fs'); +var async = require('async'); +var os = require('os'); + +var retrieveFiles = require('./list-files').retrieveFiles; +var pushInQueue = require('./upload'); + +var hasMkdir = false; + +var getSavePath = function(dir) { + var homedir = (process.platform === 'win32' || process.platform === 'win64') ? process.env.HOMEPATH : process.env.HOME; + if(!hasMkdir) { + try { + fs.mkdirSync(homedir + "/.anyfetch-file-watcher/"); + } + catch(err) {} + hasMkdir = true; + } + return homedir + "/.anyfetch-file-watcher/" + dir.trim().replace(/\//g, '').replace(/\\/g, ''); +}; + +module.exports = function update(dir, accessToken, cb) { + async.waterfall([ + function getOldCursor(cb) { + fs.readFile(getSavePath(dir), function(err) { + if(err && err.code !== 'ENOENT') { + cb(err); + } + else { + cb(null, null); + } + }); + }, + function updateCursor(oldCursor, cb) { + retrieveFiles(dir, oldCursor, cb); + }, + function deleteOldCursor(filesToUpload, newCursor, cb) { + if(fs.exists(getSavePath(dir))) { + fs.unlink(getSavePath(dir), function(err) { + cb(err, filesToUpload, newCursor); + }); + } + else { + cb(null, filesToUpload, newCursor); + } + + }, + function saveNewCursor(filesToUpload, newCursor, cb) { + fs.writeFile(getSavePath(dir), JSON.stringify(newCursor), function(err) { + cb(err, filesToUpload); + }); + }, + function uploadFiles(files, cb) { + Object.keys(files).forEach(function(key) { + var task = { + 'filePath': key, + 'accessToken': accessToken, + 'baseIdentifier': os.hostname() + }; + pushInQueue(task); + }); + cb(); + } + ], cb); +}; \ No newline at end of file diff --git a/lib/helpers/upload.js b/lib/helpers/upload.js index 69b33ac..45b404e 100644 --- a/lib/helpers/upload.js +++ b/lib/helpers/upload.js @@ -1,11 +1,12 @@ "use strict"; -var Anyfetch = require('anyfetch'); +var AnyfetchClient = require('anyfetch'); var fs = require('fs'); var path = require('path'); +var async = require('async'); -module.exports = function uploadFile(filePath, accessToken, baseIdentifier, cb) { - var anyfetch = new Anyfetch(); +var uploadFile = function(filePath, accessToken, baseIdentifier, cb) { + var anyfetch = new AnyfetchClient(); anyfetch.setAccessToken(accessToken); // Send a document to anyFetch @@ -17,9 +18,22 @@ module.exports = function uploadFile(filePath, accessToken, baseIdentifier, cb) // Wrap this in a function to avoid creating the stream before reading it. return { file: fs.createReadStream(filePath), - filename: path.baseName(filePath), + filename: path.basename(filePath), }; }; - anyfetch.sendDocumentAndFile(document, fileConfig, cb); + anyfetch.sendDocumentAndFile(document, fileConfig, function(err) { + console.log("UPPING,", path.basename(filePath)); + cb(err); + }); }; + +var queue = async.queue(function(task, cb) { + uploadFile(task.filePath, task.accessToken, task.baseIdentifier, cb); +}, 4); + +module.exports = function pushInQueue(task) { + queue.push(task); +}; + +module.exports.uploadFile = uploadFile; diff --git a/test/helpers/cursor.js b/test/helpers/cursor.js new file mode 100644 index 0000000..ce1394f --- /dev/null +++ b/test/helpers/cursor.js @@ -0,0 +1,44 @@ +'use strict'; + +require('should'); + +var path = require('path'); +var update = require('../../lib/helpers/cursor.js'); +var Anyfetch = require('anyfetch'); + + +describe('update', function() { + + process.env.ANYFETCH_API_URL = 'http://localhost:1338'; + var countFile = 0; + var cb = function(url){ + if (url.indexOf("/file") !== -1) { + countFile += 1; + } + }; + var apiServer; + + before(function() { + // Create a fake HTTP server + apiServer = Anyfetch.debug.createTestApiServer(cb); + apiServer.listen(1338); + }); + + after(function(){ + apiServer.close(); + }); + + it('should update account', function(done) { + + update(path.resolve(__dirname + "/../sample-directory"), "randomAccessToken", function(err) { + if(err) { + throw err; + } + setTimeout(function() { + countFile.should.be.eql(6); + done(); + }, 2000); + }); + + }); +}); diff --git a/test/helpers/list-files.js b/test/helpers/list-files.js index f2c4d00..bf7f495 100644 --- a/test/helpers/list-files.js +++ b/test/helpers/list-files.js @@ -39,7 +39,10 @@ describe("Retrieve file", function () { } // Should contain new files and updated files - fileToUpload.should.eql(['/txt3.txt', '/test/txt1.doc', '/test/txt2.txt']); + fileToUpload.should.include('/txt3.txt'); + fileToUpload.should.include('/test/txt1.doc'); + fileToUpload.should.include('/test/txt2.txt'); + fileToUpload.should.have.lengthOf(3); newCursor.should.eql({ '/txt1.txt': fs.statSync(__dirname + '/../sample-directory/txt1.txt').mtime.getTime(), '/txt2.txt': fs.statSync(__dirname + '/../sample-directory/txt2.txt').mtime.getTime(), diff --git a/test/helpers/upload.js b/test/helpers/upload.js new file mode 100644 index 0000000..a663079 --- /dev/null +++ b/test/helpers/upload.js @@ -0,0 +1,40 @@ +'use strict'; + +require('should'); + +var uploadFile = require('../../lib/helpers/upload.js').uploadFile; +var Anyfetch = require('anyfetch'); + +describe('uploadFile', function() { + + process.env.ANYFETCH_API_URL = 'http://localhost:1338'; + var countFile = 0; + var cb = function(url){ + if (url.indexOf("/file") !== -1) { + countFile += 1; + } + }; + var apiServer; + + before(function() { + // Create a fake HTTP server + apiServer = Anyfetch.debug.createTestApiServer(cb); + apiServer.listen(1338); + }); + + after(function(){ + apiServer.close(); + }); + + it('should upload the file', function(done) { + + uploadFile(__dirname + "/../sample-directory/txt1.txt", "randomAccessToken", "randomBaseIdentifier", function(err) { + if(err) { + throw err; + } + countFile.should.be.eql(1); + done(); + }); + + }); +}); From 53e3b64c653a839e6a85da13c5c43c5d8e7b3765 Mon Sep 17 00:00:00 2001 From: Neamar Date: Wed, 4 Jun 2014 20:54:01 +0200 Subject: [PATCH 2/9] Restored test suite --- lib/helpers/cursor.js | 5 +++-- lib/helpers/upload.js | 12 ++++++------ package.json | 2 +- test/helpers/cursor.js | 13 ++++++------- test/helpers/upload.js | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/helpers/cursor.js b/lib/helpers/cursor.js index b05eeb4..93b44fc 100644 --- a/lib/helpers/cursor.js +++ b/lib/helpers/cursor.js @@ -52,8 +52,9 @@ module.exports = function update(dir, accessToken, cb) { }); }, function uploadFiles(files, cb) { - Object.keys(files).forEach(function(key) { + files.forEach(function(key) { var task = { + 'dir': dir, 'filePath': key, 'accessToken': accessToken, 'baseIdentifier': os.hostname() @@ -63,4 +64,4 @@ module.exports = function update(dir, accessToken, cb) { cb(); } ], cb); -}; \ No newline at end of file +}; diff --git a/lib/helpers/upload.js b/lib/helpers/upload.js index 45b404e..f77b5b2 100644 --- a/lib/helpers/upload.js +++ b/lib/helpers/upload.js @@ -5,7 +5,7 @@ var fs = require('fs'); var path = require('path'); var async = require('async'); -var uploadFile = function(filePath, accessToken, baseIdentifier, cb) { +var uploadFile = function(dir, filePath, accessToken, baseIdentifier, cb) { var anyfetch = new AnyfetchClient(); anyfetch.setAccessToken(accessToken); @@ -17,19 +17,19 @@ var uploadFile = function(filePath, accessToken, baseIdentifier, cb) { var fileConfig = function() { // Wrap this in a function to avoid creating the stream before reading it. return { - file: fs.createReadStream(filePath), - filename: path.basename(filePath), + file: fs.createReadStream(dir + filePath), + filename: path.basename(dir + filePath), }; }; anyfetch.sendDocumentAndFile(document, fileConfig, function(err) { - console.log("UPPING,", path.basename(filePath)); + console.log("UPPING,", path.basename(dir + filePath)); cb(err); }); }; -var queue = async.queue(function(task, cb) { - uploadFile(task.filePath, task.accessToken, task.baseIdentifier, cb); +var queue = async.queue(function worker(task, cb) { + uploadFile(task.dir, task.filePath, task.accessToken, task.baseIdentifier, cb); }, 4); module.exports = function pushInQueue(task) { diff --git a/package.json b/package.json index 76917b5..8dcca36 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "0.3.15", "author": "Matthieu Bacconnier ", "scripts": { - "test": "NODE_ENV=test mocha --recursive -R spec test/", + "test": "NODE_ENV=test mocha --recursive -R spec test/ -t 5000", "lint": "jshint lib/ test/" }, "main": "./lib/", diff --git a/test/helpers/cursor.js b/test/helpers/cursor.js index ce1394f..dfabd49 100644 --- a/test/helpers/cursor.js +++ b/test/helpers/cursor.js @@ -7,20 +7,20 @@ var update = require('../../lib/helpers/cursor.js'); var Anyfetch = require('anyfetch'); -describe('update', function() { +describe('update() function', function() { process.env.ANYFETCH_API_URL = 'http://localhost:1338'; var countFile = 0; - var cb = function(url){ + var mockServerHandler = function(url){ if (url.indexOf("/file") !== -1) { countFile += 1; } }; - var apiServer; + var apiServer; before(function() { // Create a fake HTTP server - apiServer = Anyfetch.debug.createTestApiServer(cb); + apiServer = Anyfetch.debug.createTestApiServer(mockServerHandler); apiServer.listen(1338); }); @@ -29,15 +29,14 @@ describe('update', function() { }); it('should update account', function(done) { - update(path.resolve(__dirname + "/../sample-directory"), "randomAccessToken", function(err) { if(err) { throw err; } setTimeout(function() { - countFile.should.be.eql(6); + countFile.should.eql(5); done(); - }, 2000); + }, 200); }); }); diff --git a/test/helpers/upload.js b/test/helpers/upload.js index a663079..50523a6 100644 --- a/test/helpers/upload.js +++ b/test/helpers/upload.js @@ -28,7 +28,7 @@ describe('uploadFile', function() { it('should upload the file', function(done) { - uploadFile(__dirname + "/../sample-directory/txt1.txt", "randomAccessToken", "randomBaseIdentifier", function(err) { + uploadFile(__dirname + "/..", "/sample-directory/txt1.txt", "randomAccessToken", "randomBaseIdentifier", function(err) { if(err) { throw err; } From 003f52d8fd80b31b62e180339679df6e34260925 Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 5 Jun 2014 10:42:20 +0200 Subject: [PATCH 3/9] some optimisations --- lib/helpers/cursor.js | 25 +++++++------------------ lib/helpers/upload.js | 3 +-- test/helpers/upload.js | 4 ++-- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/lib/helpers/cursor.js b/lib/helpers/cursor.js index 93b44fc..afd21ee 100644 --- a/lib/helpers/cursor.js +++ b/lib/helpers/cursor.js @@ -4,48 +4,37 @@ var async = require('async'); var os = require('os'); var retrieveFiles = require('./list-files').retrieveFiles; -var pushInQueue = require('./upload'); +var pushToQueue = require('./upload'); var hasMkdir = false; var getSavePath = function(dir) { - var homedir = (process.platform === 'win32' || process.platform === 'win64') ? process.env.HOMEPATH : process.env.HOME; + var homeDir = process.env.HOMEPATH || process.env.HOME; if(!hasMkdir) { try { - fs.mkdirSync(homedir + "/.anyfetch-file-watcher/"); + fs.mkdirSync(homeDir + "/.anyfetch-file-watcher/"); } catch(err) {} hasMkdir = true; } - return homedir + "/.anyfetch-file-watcher/" + dir.trim().replace(/\//g, '').replace(/\\/g, ''); + return homeDir + "/.anyfetch-file-watcher/" + dir.trim().replace(/(\/|\\)/g, ''); }; module.exports = function update(dir, accessToken, cb) { async.waterfall([ function getOldCursor(cb) { - fs.readFile(getSavePath(dir), function(err) { + fs.readFile(getSavePath(dir), function(err, cursor) { if(err && err.code !== 'ENOENT') { cb(err); } else { - cb(null, null); + cb(null, cursor); } }); }, function updateCursor(oldCursor, cb) { retrieveFiles(dir, oldCursor, cb); }, - function deleteOldCursor(filesToUpload, newCursor, cb) { - if(fs.exists(getSavePath(dir))) { - fs.unlink(getSavePath(dir), function(err) { - cb(err, filesToUpload, newCursor); - }); - } - else { - cb(null, filesToUpload, newCursor); - } - - }, function saveNewCursor(filesToUpload, newCursor, cb) { fs.writeFile(getSavePath(dir), JSON.stringify(newCursor), function(err) { cb(err, filesToUpload); @@ -59,7 +48,7 @@ module.exports = function update(dir, accessToken, cb) { 'accessToken': accessToken, 'baseIdentifier': os.hostname() }; - pushInQueue(task); + pushToQueue(task); }); cb(); } diff --git a/lib/helpers/upload.js b/lib/helpers/upload.js index f77b5b2..2224c71 100644 --- a/lib/helpers/upload.js +++ b/lib/helpers/upload.js @@ -23,7 +23,6 @@ var uploadFile = function(dir, filePath, accessToken, baseIdentifier, cb) { }; anyfetch.sendDocumentAndFile(document, fileConfig, function(err) { - console.log("UPPING,", path.basename(dir + filePath)); cb(err); }); }; @@ -32,7 +31,7 @@ var queue = async.queue(function worker(task, cb) { uploadFile(task.dir, task.filePath, task.accessToken, task.baseIdentifier, cb); }, 4); -module.exports = function pushInQueue(task) { +module.exports = function pushToQueue(task) { queue.push(task); }; diff --git a/test/helpers/upload.js b/test/helpers/upload.js index 50523a6..f66e28b 100644 --- a/test/helpers/upload.js +++ b/test/helpers/upload.js @@ -9,7 +9,7 @@ describe('uploadFile', function() { process.env.ANYFETCH_API_URL = 'http://localhost:1338'; var countFile = 0; - var cb = function(url){ + var mockServerHandler = function(url){ if (url.indexOf("/file") !== -1) { countFile += 1; } @@ -18,7 +18,7 @@ describe('uploadFile', function() { before(function() { // Create a fake HTTP server - apiServer = Anyfetch.debug.createTestApiServer(cb); + apiServer = Anyfetch.debug.createTestApiServer(mockServerHandler); apiServer.listen(1338); }); From 7239bedf0fab0c63f7bd0468d963eb3a529ca4a1 Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 5 Jun 2014 10:46:31 +0200 Subject: [PATCH 4/9] no more gobal variable --- lib/helpers/cursor.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/helpers/cursor.js b/lib/helpers/cursor.js index afd21ee..5c9759c 100644 --- a/lib/helpers/cursor.js +++ b/lib/helpers/cursor.js @@ -6,19 +6,20 @@ var os = require('os'); var retrieveFiles = require('./list-files').retrieveFiles; var pushToQueue = require('./upload'); -var hasMkdir = false; var getSavePath = function(dir) { var homeDir = process.env.HOMEPATH || process.env.HOME; - if(!hasMkdir) { + if(!getSavePath.hasMkdir) { try { fs.mkdirSync(homeDir + "/.anyfetch-file-watcher/"); } catch(err) {} - hasMkdir = true; + getSavePath.hasMkdir = true; } return homeDir + "/.anyfetch-file-watcher/" + dir.trim().replace(/(\/|\\)/g, ''); }; +getSavePath.hasMkdir = false; + module.exports = function update(dir, accessToken, cb) { async.waterfall([ From 53a8862e99d08a6114800552b48bc790d0a4420e Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 5 Jun 2014 11:22:05 +0200 Subject: [PATCH 5/9] improve architecture --- lib/helpers/list-files.js | 1 - lib/helpers/save-path.js | 17 +++++++++++++++++ lib/{helpers/cursor.js => index.js} | 19 +++---------------- package.json | 2 +- test/{helpers/cursor.js => index.js} | 12 +++++++++--- 5 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 lib/helpers/save-path.js rename lib/{helpers/cursor.js => index.js} (67%) rename test/{helpers/cursor.js => index.js} (69%) diff --git a/lib/helpers/list-files.js b/lib/helpers/list-files.js index 28625ee..e071d69 100644 --- a/lib/helpers/list-files.js +++ b/lib/helpers/list-files.js @@ -59,7 +59,6 @@ module.exports.retrieveFiles = function retrieveFiles(dir, cursor, cb) { // Update documents from provider // Compare cursor and the file on the disk // If the files has been updated we add the file in the files to upload - if(!cursor) { // First run, cursor is empty. cursor = {}; diff --git a/lib/helpers/save-path.js b/lib/helpers/save-path.js new file mode 100644 index 0000000..03cf381 --- /dev/null +++ b/lib/helpers/save-path.js @@ -0,0 +1,17 @@ +"use strict"; +var fs = require("fs"); + +var getSavePath = function(dir) { + var homeDir = process.env.HOMEPATH || process.env.HOME; + if(!getSavePath.hasMkdir) { + try { + fs.mkdirSync(homeDir + "/.anyfetch-file-watcher/"); + } + catch(err) {} + getSavePath.hasMkdir = true; + } + return homeDir + "/.anyfetch-file-watcher/" + dir.trim().replace(/(\/|\\)/g, ''); +}; +getSavePath.hasMkdir = false; + +module.exports = getSavePath; \ No newline at end of file diff --git a/lib/helpers/cursor.js b/lib/index.js similarity index 67% rename from lib/helpers/cursor.js rename to lib/index.js index 5c9759c..becd817 100644 --- a/lib/helpers/cursor.js +++ b/lib/index.js @@ -3,22 +3,9 @@ var fs = require('fs'); var async = require('async'); var os = require('os'); -var retrieveFiles = require('./list-files').retrieveFiles; -var pushToQueue = require('./upload'); - - -var getSavePath = function(dir) { - var homeDir = process.env.HOMEPATH || process.env.HOME; - if(!getSavePath.hasMkdir) { - try { - fs.mkdirSync(homeDir + "/.anyfetch-file-watcher/"); - } - catch(err) {} - getSavePath.hasMkdir = true; - } - return homeDir + "/.anyfetch-file-watcher/" + dir.trim().replace(/(\/|\\)/g, ''); -}; -getSavePath.hasMkdir = false; +var retrieveFiles = require('./helpers/list-files').retrieveFiles; +var pushToQueue = require('./helpers/upload'); +var getSavePath = require('./helpers/save-path'); module.exports = function update(dir, accessToken, cb) { diff --git a/package.json b/package.json index 8dcca36..9f501b5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "anyfetch-file-watcher", "description": "Watch for file changes, and send them to anyFetch.", - "version": "0.3.15", + "version": "0.0.1", "author": "Matthieu Bacconnier ", "scripts": { "test": "NODE_ENV=test mocha --recursive -R spec test/ -t 5000", diff --git a/test/helpers/cursor.js b/test/index.js similarity index 69% rename from test/helpers/cursor.js rename to test/index.js index dfabd49..c64c4a5 100644 --- a/test/helpers/cursor.js +++ b/test/index.js @@ -3,12 +3,14 @@ require('should'); var path = require('path'); -var update = require('../../lib/helpers/cursor.js'); var Anyfetch = require('anyfetch'); +var fs = require('fs'); +var update = require('../lib/index.js'); +var getSavePath = require('../lib/helpers/save-path.js'); -describe('update() function', function() { +describe('update() function', function() { process.env.ANYFETCH_API_URL = 'http://localhost:1338'; var countFile = 0; var mockServerHandler = function(url){ @@ -26,10 +28,14 @@ describe('update() function', function() { after(function(){ apiServer.close(); + + // Clean cursor + + fs.unlinkSync(getSavePath(path.resolve(__dirname + "/../test/sample-directory"))); }); it('should update account', function(done) { - update(path.resolve(__dirname + "/../sample-directory"), "randomAccessToken", function(err) { + update(path.resolve(__dirname + "/../test/sample-directory"), "randomAccessToken", function(err) { if(err) { throw err; } From 9ffcd3647b18ee2dbf351e23436bb8cd8d91a1cf Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 5 Jun 2014 11:23:03 +0200 Subject: [PATCH 6/9] typo --- lib/helpers/list-files.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/helpers/list-files.js b/lib/helpers/list-files.js index e071d69..28625ee 100644 --- a/lib/helpers/list-files.js +++ b/lib/helpers/list-files.js @@ -59,6 +59,7 @@ module.exports.retrieveFiles = function retrieveFiles(dir, cursor, cb) { // Update documents from provider // Compare cursor and the file on the disk // If the files has been updated we add the file in the files to upload + if(!cursor) { // First run, cursor is empty. cursor = {}; From e741f6eaad3b04b875e9a009a607d681409af0d6 Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 5 Jun 2014 11:25:27 +0200 Subject: [PATCH 7/9] typo --- lib/helpers/save-path.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helpers/save-path.js b/lib/helpers/save-path.js index 03cf381..b851215 100644 --- a/lib/helpers/save-path.js +++ b/lib/helpers/save-path.js @@ -14,4 +14,4 @@ var getSavePath = function(dir) { }; getSavePath.hasMkdir = false; -module.exports = getSavePath; \ No newline at end of file +module.exports = getSavePath; From 9c2dec28c7372bda29d467d9fcbff502fe6870a5 Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 5 Jun 2014 11:34:12 +0200 Subject: [PATCH 8/9] improve architecture --- lib/helpers/save-path.js | 10 +++++++++- lib/index.js | 5 +++-- test/index.js | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/helpers/save-path.js b/lib/helpers/save-path.js index b851215..a7f775f 100644 --- a/lib/helpers/save-path.js +++ b/lib/helpers/save-path.js @@ -14,4 +14,12 @@ var getSavePath = function(dir) { }; getSavePath.hasMkdir = false; -module.exports = getSavePath; + +var saveCursor = function(dir, newCursor, cb) { + fs.writeFile(getSavePath(dir), JSON.stringify(newCursor), function(err) { + cb(err); + }); +}; + +module.exports = saveCursor; +module.exports.getSavePath = getSavePath; diff --git a/lib/index.js b/lib/index.js index becd817..238ac4f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,7 +5,8 @@ var os = require('os'); var retrieveFiles = require('./helpers/list-files').retrieveFiles; var pushToQueue = require('./helpers/upload'); -var getSavePath = require('./helpers/save-path'); +var saveCursor = require('./helpers/save-path'); +var getSavePath = saveCursor.getSavePath; module.exports = function update(dir, accessToken, cb) { @@ -24,7 +25,7 @@ module.exports = function update(dir, accessToken, cb) { retrieveFiles(dir, oldCursor, cb); }, function saveNewCursor(filesToUpload, newCursor, cb) { - fs.writeFile(getSavePath(dir), JSON.stringify(newCursor), function(err) { + saveCursor(dir, newCursor, function(err) { cb(err, filesToUpload); }); }, diff --git a/test/index.js b/test/index.js index c64c4a5..8946ef8 100644 --- a/test/index.js +++ b/test/index.js @@ -7,7 +7,7 @@ var Anyfetch = require('anyfetch'); var fs = require('fs'); var update = require('../lib/index.js'); -var getSavePath = require('../lib/helpers/save-path.js'); +var getSavePath = require('../lib/helpers/save-path.js').getSavePath; describe('update() function', function() { From 8d0643a93d57a0b2f365b26bd1ea472f8522b7b1 Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 5 Jun 2014 11:36:17 +0200 Subject: [PATCH 9/9] fix cb error --- lib/helpers/save-path.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/helpers/save-path.js b/lib/helpers/save-path.js index a7f775f..84a2c17 100644 --- a/lib/helpers/save-path.js +++ b/lib/helpers/save-path.js @@ -16,9 +16,7 @@ getSavePath.hasMkdir = false; var saveCursor = function(dir, newCursor, cb) { - fs.writeFile(getSavePath(dir), JSON.stringify(newCursor), function(err) { - cb(err); - }); + fs.writeFile(getSavePath(dir), JSON.stringify(newCursor), cb); }; module.exports = saveCursor;