Moved all mods logic in router into separate class

This commit is contained in:
Björn Dahlgren 2015-01-21 04:00:09 +01:00
parent 7c4df71a90
commit 5d46fd7fc4
4 changed files with 151 additions and 108 deletions

5
app.js
View File

@ -3,6 +3,7 @@ var Resource = require('express-resource');
var config = require('./config'); var config = require('./config');
var Manager = require('./lib/manager'); var Manager = require('./lib/manager');
var Mods = require('./lib/mods');
var app = express(); var app = express();
var server = require('http').Server(app); var server = require('http').Server(app);
@ -16,11 +17,13 @@ app.use(express.static(__dirname + '/public'));
var manager = new Manager(config); var manager = new Manager(config);
manager.load(); manager.load();
var mods = new Mods(config);
var servers = require('./routes/servers')(manager); var servers = require('./routes/servers')(manager);
app.resource('api/logs', require('./routes/logs')); app.resource('api/logs', require('./routes/logs'));
app.resource('api/missions', require('./routes/missions')); 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); var serversResource = app.resource('api/servers', servers);
app.resource('api/settings', require('./routes/settings')); app.resource('api/settings', require('./routes/settings'));

57
lib/mods.js Normal file
View File

@ -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;

49
lib/mods/traverse.js Normal file
View File

@ -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);
});
};

View File

@ -1,114 +1,48 @@
var fs = require('fs') module.exports = function (modsManager) {
, path = require('path') return {
, async = require('async') index: function(req, res){
, playwithsix = require('playwithsix') modsManager.getMods(function (err, mods) {
, when = require('when') if (err) {
, nodefn = require('when/node/function'); res.send(500, err);
} else {
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) {
res.send(mods); res.send(mods);
}); }
}); });
} },
});
};
exports.create = function(req, res){ create: function(req, res){
downloadMod(req.body.name, function (err, mods) { modsManager.download(req.body.name, function (err, mods) {
if (mods && !err) { if (err || !mods) {
res.send(mods); res.send(500, err);
} else { } else {
res.send(500, err); res.send(mods);
} }
}); });
}; },
exports.show = function(req, res){ show: function(req, res){
walk(config.path + "/" + req.params.mod).then(function(files) { modsManager.traverse(req.params.mod, function (err, files) {
res.json(files); if (err || !files) {
}).otherwise(function(error) { console.error(err.stack || err);
console.error(error.stack || error); res.send(500, err);
res.send({success: false}); } else {
}); res.json(files);
}; }
});
},
exports.update = function(req, res){ update: function(req, res){
downloadMod(req.params.mod, function (err, mods) { modsManager.download(req.params.mod, function (err, mods) {
if (mods && !err) { if (err || !mods) {
res.send(mods); res.send(500, err);
} else { } else {
res.send(500, err); res.send(mods);
} }
}); });
}; },
exports.destroy = function(req, res){ destroy: function(req, res){
res.send('destroy mod ' + req.params.mod); res.send('destroy mod ' + req.params.mod);
},
};
}; };