From 5d46fd7fc4bbe6742b77960212d6ff3d7a7f7b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Wed, 21 Jan 2015 04:00:09 +0100 Subject: [PATCH] Moved all mods logic in router into separate class --- app.js | 5 +- lib/mods.js | 57 +++++++++++++++++ lib/mods/traverse.js | 49 ++++++++++++++ routes/mods.js | 148 ++++++++++++------------------------------- 4 files changed, 151 insertions(+), 108 deletions(-) create mode 100644 lib/mods.js create mode 100644 lib/mods/traverse.js diff --git a/app.js b/app.js index 2da480b..2125d65 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,7 @@ var Resource = require('express-resource'); var config = require('./config'); var Manager = require('./lib/manager'); +var Mods = require('./lib/mods'); var app = express(); var server = require('http').Server(app); @@ -16,11 +17,13 @@ app.use(express.static(__dirname + '/public')); var manager = new Manager(config); manager.load(); +var mods = new Mods(config); + var servers = require('./routes/servers')(manager); app.resource('api/logs', require('./routes/logs')); app.resource('api/missions', require('./routes/missions')); -app.resource('api/mods', require('./routes/mods')); +app.resource('api/mods', require('./routes/mods')(mods)); var serversResource = app.resource('api/servers', servers); app.resource('api/settings', require('./routes/settings')); diff --git a/lib/mods.js b/lib/mods.js new file mode 100644 index 0000000..89ad949 --- /dev/null +++ b/lib/mods.js @@ -0,0 +1,57 @@ +var async = require('async'); +var fs = require('fs'); +var path = require('path'); +var playwithsix = require('playwithsix'); + +var traverse = require('./mods/traverse'); + +function isPlayWithSixMod(modPath, cb) { + var pwsFile = path.join(modPath, '.synq.json'); + fs.exists(pwsFile, function (exists) { + if (cb) { + cb(exists); + } + }); +} + +var Mods = function (config) { + this.config = config; +}; + +Mods.prototype.download = function (mod, cb) { + playwithsix.downloadMod(this.config.path, mod, cb); +}; + +Mods.prototype.getMods = function (callback) { + var self = this; + fs.readdir(self.config.path, function (err, files) { + if (err) { + callback(err); + } else { + var mods = files.filter(function (file) { + return file.charAt(0) == "@"; + }); + + playwithsix.checkOutdated(self.config.path, function (err, outdatedMods) { + async.map(mods, function (mod, cb) { + var modPath = path.join(self.config.path, mod); + isPlayWithSixMod(modPath, function (isPlayWithSixMod) { + cb(null, { + name: mod, + outdated: outdatedMods && outdatedMods.indexOf(mod) >= 0, + playWithSix: isPlayWithSixMod, + }); + }); + }, function (err, mods) { + callback(err, mods); + }); + }); + } + }); +}; + +Mods.prototype.traverse = function (mod, cb) { + traverse(path.join(this.config.path, mod), cb); +}; + +module.exports = Mods; diff --git a/lib/mods/traverse.js b/lib/mods/traverse.js new file mode 100644 index 0000000..d4fa727 --- /dev/null +++ b/lib/mods/traverse.js @@ -0,0 +1,49 @@ +var nodefn = require('when/node/function'); +var fs = require('fs'); +var path = require('path'); +var when = require('when'); + +function walk (directory) { + createFile = function (file, stat) { + return { + type: "file", + name: file, + size: stat.size + }; + }; + + createFolder = function (folder) { + return { + type: "folder", + name: folder, + files: [] + }; + }; + + var results = []; + + return when.map(nodefn.call(fs.readdir, directory), function(file) { + var absolutePath = path.join(directory, file); + return nodefn.call(fs.stat, absolutePath).then(function(stat) { + if (stat.isFile()) { + return results.push(createFile(file, stat)); + } + + folder = createFolder(file); + return walk(absolutePath).then(function(filesInDir) { + folder.files = filesInDir; + results.push(folder); + }); + }); + }).then(function() { + return results; + }); +} + +module.exports = function (path, cb) { + walk(path).then(function(files) { + cb(null, files); + }).otherwise(function(error) { + cb(error); + }); +}; diff --git a/routes/mods.js b/routes/mods.js index 540619a..bb1557a 100644 --- a/routes/mods.js +++ b/routes/mods.js @@ -1,114 +1,48 @@ -var fs = require('fs') -, path = require('path') -, async = require('async') -, playwithsix = require('playwithsix') -, when = require('when') -, nodefn = require('when/node/function'); - -var config = require('./../config'); - -function downloadMod(mod, cb) { - playwithsix.downloadMod(config.path, mod, cb); -} - -function walk (directory) { - createFile = function (file, stat) { - return { - type: "file", - name: file, - size: stat.size - }; - }; - - createFolder = function (folder) { - return { - type: "folder", - name: folder, - files: [] - }; - }; - - var results = []; - - return when.map(nodefn.call(fs.readdir, directory), function(file) { - var absolutePath = path.join(directory, file); - return nodefn.call(fs.stat, absolutePath).then(function(stat) { - if (stat.isFile()) { - return results.push(createFile(file, stat)); - } - - folder = createFolder(file); - return walk(absolutePath).then(function(filesInDir) { - folder.files = filesInDir; - results.push(folder); - }); - }); - }).then(function() { - return results; - }); -}; - -function isPlayWithSixMod(modPath, cb) { - var pwsFile = path.join(modPath, '.synq.json'); - fs.exists(pwsFile, function (exists) { - if (cb) { - cb(exists); - } - }); -}; - -exports.index = function(req, res){ - fs.readdir(config.path, function (err, files) { - if (err) { - res.send(err); - } else { - var mods = files.filter(function (file) { - return file.charAt(0) == "@"; - }); - - playwithsix.checkOutdated(config.path, function (err, outdatedMods) { - async.map(mods, function (mod, cb) { - var modPath = path.join(config.path, mod); - isPlayWithSixMod(modPath, function (isPlayWithSixMod) { - cb(null, { name: mod, outdated: outdatedMods && outdatedMods.indexOf(mod) >= 0, playWithSix: isPlayWithSixMod }); - }); - }, function (err, mods) { +module.exports = function (modsManager) { + return { + index: function(req, res){ + modsManager.getMods(function (err, mods) { + if (err) { + res.send(500, err); + } else { res.send(mods); - }); + } }); - } - }); -}; + }, -exports.create = function(req, res){ - downloadMod(req.body.name, function (err, mods) { - if (mods && !err) { - res.send(mods); - } else { - res.send(500, err); - } - }); -}; + create: function(req, res){ + modsManager.download(req.body.name, function (err, mods) { + if (err || !mods) { + res.send(500, err); + } else { + res.send(mods); + } + }); + }, -exports.show = function(req, res){ - walk(config.path + "/" + req.params.mod).then(function(files) { - res.json(files); - }).otherwise(function(error) { - console.error(error.stack || error); - res.send({success: false}); - }); -}; + show: function(req, res){ + modsManager.traverse(req.params.mod, function (err, files) { + if (err || !files) { + console.error(err.stack || err); + res.send(500, err); + } else { + res.json(files); + } + }); + }, -exports.update = function(req, res){ - downloadMod(req.params.mod, function (err, mods) { - if (mods && !err) { - res.send(mods); - } else { - res.send(500, err); - } - }); -}; + update: function(req, res){ + modsManager.download(req.params.mod, function (err, mods) { + if (err || !mods) { + res.send(500, err); + } else { + res.send(mods); + } + }); + }, -exports.destroy = function(req, res){ - res.send('destroy mod ' + req.params.mod); + destroy: function(req, res){ + res.send('destroy mod ' + req.params.mod); + }, + }; };