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 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'));

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')
, 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);
},
};
};