From 3c41b2624aa79888b92ad55b527aa4489b33cc92 Mon Sep 17 00:00:00 2001 From: Nick Niemeir Date: Thu, 16 Jan 2014 05:59:13 +0000 Subject: [PATCH] First pass at removing sync calls --- package.json | 3 +- red/storage/localfilesystem.js | 126 ++++++++++----------------------- 2 files changed, 39 insertions(+), 90 deletions(-) diff --git a/package.json b/package.json index cc5d6f9dc..d58f8e259 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "xml2js":"~0.2.8", "sentiment":"~0.2.1", "irc":"~0.3.6", - "follow-redirects":"~0.0.3" + "follow-redirects":"~0.0.3", + "mkdirp":"~0.3.5" }, "devDependencies": { "mocha": "~1.12.0", diff --git a/red/storage/localfilesystem.js b/red/storage/localfilesystem.js index 1e639d631..b64d228f4 100644 --- a/red/storage/localfilesystem.js +++ b/red/storage/localfilesystem.js @@ -16,8 +16,13 @@ var fs = require('fs'); var when = require('when'); +var nodeFn = require('when/node/function'); +var keys = require('when/keys'); var util = require('util'); var fspath = require("path"); +var mkdirp = require("mkdirp"); + +var promiseDir = nodeFn.lift(mkdirp); var settings; var flowsFile; @@ -31,19 +36,21 @@ function listFiles(dir) { var dirs = {}; var files = []; var dirCount = 0; - fs.readdirSync(dir).sort().filter(function(fn) { + return nodeFn.call(fs.readdir, dir).then(function (contents) { + contents.sort().forEach(function(fn) { var stats = fs.lstatSync(dir+"/"+fn); if (stats.isDirectory()) { dirCount += 1; - dirs[fn] = listFiles(dir+"/"+fn); + dirs[fn] = listFiles(dir+"/"+fn) } else { files.push(fn.split(".")[0]); } - }); - var result = {}; - if (dirCount > 0) { result.d = dirs; } - if (files.length > 0) { result.f = files; } - return result; + }) + var result = {}; + if (dirCount > 0) { result.d = keys.all(dirs); } + if (files.length > 0) { result.f = when.resolve(files); } + return keys.all(result); + }) } function getFileMeta(root,path) { @@ -116,24 +123,16 @@ function getFileBody(root,path) { function writeFile(root,path,meta,body,res) { var fn = fspath.join(root,path); - - var parts = path.split("/"); - var dirname = root; - for (var i = 0;i