From ec544518e8d3610bcd4f20e6c32447322f32770a Mon Sep 17 00:00:00 2001
From: Peter Jaszkowiak
Date: Mon, 2 Jan 2017 22:23:17 -0700
Subject: [PATCH] Use async v2
---
minifier.js | 14 +++++++++++---
package.json | 2 +-
src/file.js | 20 ++++++++++++++------
src/meta/dependencies.js | 25 ++++++++++++++++---------
src/meta/themes.js | 18 ++++++++++++++----
src/plugins.js | 12 ++++++++----
src/plugins/load.js | 4 +---
src/privileges/helpers.js | 8 ++------
src/rewards/index.js | 10 ++++++----
9 files changed, 73 insertions(+), 40 deletions(-)
diff --git a/minifier.js b/minifier.js
index 25c0177175..a83069ab85 100644
--- a/minifier.js
+++ b/minifier.js
@@ -17,13 +17,21 @@ Minifier.js.minify = function (scripts, minify, callback) {
});
async.filter(scripts, function (script, next) {
- file.exists(script, function (exists) {
+ file.exists(script, function (err, exists) {
+ if (err) {
+ return next(err);
+ }
+
if (!exists) {
console.warn('[minifier] file not found, ' + script);
}
- next(exists);
+ next(null, exists);
});
- }, function (scripts) {
+ }, function (err, scripts) {
+ if (err) {
+ return callback(err);
+ }
+
if (minify) {
minifyScripts(scripts, callback);
} else {
diff --git a/package.json b/package.json
index a9dbdfb82b..f80a1b12c9 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,7 @@
"coveralls": "istanbul cover _mocha --report lcovonly -- -R dot && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"dependencies": {
- "async": "~1.5.0",
+ "async": "^2.1.4",
"autoprefixer": "^6.2.3",
"bcryptjs": "~2.3.0",
"body-parser": "^1.9.0",
diff --git a/src/file.js b/src/file.js
index df820cc47f..a231ddd01d 100644
--- a/src/file.js
+++ b/src/file.js
@@ -87,19 +87,27 @@ file.allowedExtensions = function () {
file.exists = function (path, callback) {
fs.stat(path, function (err, stat) {
- callback(!err && stat);
+ if (err) {
+ if (err.code === 'ENOENT') {
+ return callback(null, false);
+ }
+ return callback(err);
+ }
+ return callback(null, true);
});
};
file.existsSync = function (path) {
- var exists = false;
try {
- exists = fs.statSync(path);
- } catch(err) {
- exists = false;
+ fs.statSync(path);
+ } catch (err) {
+ if (err.code === 'ENOENT') {
+ return false;
+ }
+ throw err;
}
- return !!exists;
+ return true;
};
module.exports = file;
diff --git a/src/meta/dependencies.js b/src/meta/dependencies.js
index 3f892c37ab..fdfa755198 100644
--- a/src/meta/dependencies.js
+++ b/src/meta/dependencies.js
@@ -19,14 +19,17 @@ module.exports = function (Meta) {
winston.verbose('Checking dependencies for outdated modules');
- async.every(modules, function (module, next) {
+ async.each(modules, function (module, next) {
fs.readFile(path.join(__dirname, '../../node_modules/', module, 'package.json'), {
encoding: 'utf-8'
}, function (err, pkgData) {
- // If a bundled plugin/theme is not present, skip the dep check (#3384)
- if (err && err.code === 'ENOENT' && (module === 'nodebb-rewards-essentials' || module.startsWith('nodebb-plugin') || module.startsWith('nodebb-theme'))) {
- winston.warn('[meta/dependencies] Bundled plugin ' + module + ' not found, skipping dependency check.');
- return next(true);
+ if (err) {
+ // If a bundled plugin/theme is not present, skip the dep check (#3384)
+ if (err.code === 'ENOENT' && (module === 'nodebb-rewards-essentials' || module.startsWith('nodebb-plugin') || module.startsWith('nodebb-theme'))) {
+ winston.warn('[meta/dependencies] Bundled plugin ' + module + ' not found, skipping dependency check.');
+ return next();
+ }
+ return next(err);
}
try {
@@ -34,20 +37,24 @@ module.exports = function (Meta) {
} catch(e) {
process.stdout.write('[' + 'missing'.red + '] ' + module.bold + ' is a required dependency but could not be found\n');
depsMissing = true;
- return next(true);
+ return next();
}
var ok = !semver.validRange(pkg.dependencies[module]) || semver.satisfies(pkgData.version, pkg.dependencies[module]);
if (ok || (pkgData._resolved && pkgData._resolved.indexOf('//github.com') !== -1)) {
- next(true);
+ next();
} else {
process.stdout.write('[' + 'outdated'.yellow + '] ' + module.bold + ' installed v' + pkgData.version + ', package.json requires ' + pkg.dependencies[module] + '\n');
depsOutdated = true;
- next(true);
+ next();
}
});
- }, function (ok) {
+ }, function (err) {
+ if (err) {
+ return callback(err);
+ }
+
if (depsMissing) {
callback(new Error('dependencies-missing'));
} else if (depsOutdated) {
diff --git a/src/meta/themes.js b/src/meta/themes.js
index be48d1e70d..777e1af5df 100644
--- a/src/meta/themes.js
+++ b/src/meta/themes.js
@@ -27,18 +27,28 @@ module.exports = function (Meta) {
async.filter(files, function (file, next) {
fs.stat(path.join(themePath, file), function (err, fileStat) {
if (err) {
- return next(false);
+ if (err.code === 'ENOENT') {
+ return next(null, false);
+ }
+ return next(err);
}
- next((fileStat.isDirectory() && file.slice(0, 13) === 'nodebb-theme-'));
+ next(null, (fileStat.isDirectory() && file.slice(0, 13) === 'nodebb-theme-'));
});
- }, function (themes) {
+ }, function (err, themes) {
+ if (err) {
+ return callback(err);
+ }
+
async.map(themes, function (theme, next) {
var config = path.join(themePath, theme, 'theme.json');
fs.readFile(config, function (err, file) {
if (err) {
- return next();
+ if (err.code === 'ENOENT') {
+ return next(null, null);
+ }
+ return next(err);
}
try {
var configObj = JSON.parse(file.toString());
diff --git a/src/plugins.js b/src/plugins.js
index b8ddbdc0fe..52378e7f38 100644
--- a/src/plugins.js
+++ b/src/plugins.js
@@ -161,9 +161,7 @@ var middleware;
});
// Filter out plugins with invalid paths
- async.filter(paths, file.exists, function (paths) {
- next(null, paths);
- });
+ async.filter(paths, file.exists, next);
},
function (paths, next) {
async.map(paths, Plugins.loadPluginInfo, next);
@@ -346,7 +344,13 @@ var middleware;
async.filter(dirs, function (dir, callback) {
fs.stat(dir, function (err, stats) {
- callback(!err && stats.isDirectory());
+ if (err) {
+ if (err.code === 'ENOENT') {
+ return callback(null, false);
+ }
+ return callback(err);
+ }
+ callback(null, stats.isDirectory());
});
}, function (plugins) {
next(null, plugins);
diff --git a/src/plugins/load.js b/src/plugins/load.js
index 60d584f99f..98a470f914 100644
--- a/src/plugins/load.js
+++ b/src/plugins/load.js
@@ -31,9 +31,7 @@ module.exports = function (Plugins) {
return path.join(__dirname, '../../node_modules/', plugin);
});
- async.filter(plugins, file.exists, function (plugins) {
- next(null, plugins);
- });
+ async.filter(plugins, file.exists, next);
},
], callback);
};
diff --git a/src/privileges/helpers.js b/src/privileges/helpers.js
index 9df9d8cd7b..02e1bed879 100644
--- a/src/privileges/helpers.js
+++ b/src/privileges/helpers.js
@@ -8,12 +8,8 @@ var helpers = {};
helpers.some = function (tasks, callback) {
async.some(tasks, function (task, next) {
- task(function (err, result) {
- next(!err && result);
- });
- }, function (result) {
- callback(null, result);
- });
+ task(next);
+ }, callback);
};
helpers.isUserAllowedTo = function (privilege, uid, cid, callback) {
diff --git a/src/rewards/index.js b/src/rewards/index.js
index 4ba403fce8..398abfb8f3 100644
--- a/src/rewards/index.js
+++ b/src/rewards/index.js
@@ -37,12 +37,14 @@ rewards.checkConditionAndRewardUser = function (uid, condition, method, callback
function (rewards, next) {
async.filter(rewards, function (reward, next) {
if (!reward) {
- return next(false);
+ return next(null, false);
}
- checkCondition(reward, method, next);
- }, function (eligible) {
- if (!eligible) {
+ checkCondition(reward, method, function (result) {
+ next(null, result);
+ });
+ }, function (err, eligible) {
+ if (err || !eligible) {
return next(false);
}