Load home directory from helper and make it configurable.

This commit is contained in:
Sascha Depold 2014-09-13 14:23:17 +02:00
parent ca08753d3b
commit aa2406eb4b
9 changed files with 53 additions and 25 deletions

View File

@ -106,6 +106,14 @@ Pretty simple, huh?
If you want to edit users manually, see `users/example/user.json`.
## Custom paths
You can define Shout's file storage path via the following options:
- Add an entry called `home` to the `config.json` file.
- Set the environment variable `SHOUT_HOME` before calling the shout executable.
- Use the default of `$HOME/.shout`.
## Commands
These are the commands currently implemented:

View File

@ -2,8 +2,7 @@ var _ = require("lodash");
var fs = require("fs");
var Client = require("./client");
var mkdirp = require("mkdirp");
const HOME = process.env.HOME + "/.shout";
var Helper = require("./helper");
module.exports = ClientManager;
@ -41,7 +40,7 @@ ClientManager.prototype.loadUsers = function(sockets) {
ClientManager.prototype.loadUser = function(name) {
try {
var json = fs.readFileSync(
HOME + "/users/" + name + "/user.json",
Helper.resolveHomePath("users", name, "user.json"),
"utf-8"
);
json = JSON.parse(json);
@ -54,8 +53,10 @@ ClientManager.prototype.loadUser = function(name) {
ClientManager.prototype.getUsers = function() {
var users = [];
var path = HOME + "/users/";
var path = Helper.resolveHomePath("users");
mkdirp.sync(path);
try {
users = fs.readdirSync(path);
} catch(e) {
@ -75,7 +76,7 @@ ClientManager.prototype.addUser = function(name, password) {
return false;
}
try {
var path = HOME + "/users/" + name;
var path = Helper.resolveHomePath("users", name);
var user = {
user: name,
password: password || "",
@ -99,7 +100,7 @@ ClientManager.prototype.removeUser = function(name) {
return false;
}
try {
var path = HOME + "/users/" + name;
var path = Helper.resolveHomePath("users", name);
fs.unlinkSync(path + "/user.json");
fs.rmdirSync(path);
} catch(e) {

View File

@ -3,15 +3,14 @@ var bcrypt = require("bcrypt");
var fs = require("fs");
var program = require("commander");
var mkdirp = require("mkdirp");
const HOME = process.env.HOME + "/.shout";
var Helper = require("../helper");
program
.command("add <name>")
.description("Add a new user")
.action(function(name) {
try {
var path = HOME + "/users";
var path = Helper.resolveHomePath("users");
mkdirp.sync(path);
} catch (e) {
console.log("");
@ -21,7 +20,7 @@ program
return;
}
try {
var path = HOME + "/users";
var path = Helper.resolveHomePath("users");
var test = path + "/.test";
fs.mkdirSync(test);
fs.rmdirSync(test);

View File

@ -1,12 +1,11 @@
var ClientManager = new require("../clientManager");
var program = require("commander");
var child = require("child_process");
const HOME = process.env.HOME + "/.shout";
var Helper = require("../helper");
program
.command("edit <name>")
.description("Edit user: '" + HOME + "/users/<name>/user.json'")
.description("Edit user: '" + Helper.resolveHomePath("users", "<name>", "user.json") + "'")
.action(function(name) {
var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) {
@ -15,10 +14,9 @@ program
console.log("");
return;
}
var path = HOME + "/users/";
child.spawn(
"vi",
[path + name + "/user.json"],
[Helper.resolveHomePath("users", name, "user.json")],
{stdio: "inherit"}
);
});

View File

@ -1,15 +1,14 @@
var ClientManager = new require("../clientManager");
var fs = require("fs");
var program = require("commander");
const HOME = process.env.HOME + "/.shout";
var Helper = require("../helper");
program
.command("remove <name>")
.description("Remove an existing user")
.action(function(name) {
try {
var path = HOME + "/users";
var path = Helper.resolveHomePath("users");
var test = path + "/.test";
fs.mkdirSync(test);
fs.rmdirSync(test);

View File

@ -2,8 +2,7 @@ var bcrypt = require("bcrypt");
var ClientManager = new require("../clientManager");
var fs = require("fs");
var program = require("commander");
const HOME = process.env.HOME + "/.shout";
var Helper = require("../helper");
program
.command("reset <name>")
@ -16,7 +15,7 @@ program
console.log("");
return;
}
var path = HOME + "/users/";
var path = Helper.resolveHomePath("users");
var file = path + name + "/user.json";
var user = require(file);
require("read")({

22
src/helper.js Normal file
View File

@ -0,0 +1,22 @@
var path = require("path");
var Helper = module.exports = {
getConfig: function () {
return require(path.resolve(__dirname, "..", "config.json"));
},
getHomeDirectory: function () {
return (
this.getConfig().home ||
process.env.SHOUT_HOME ||
path.resolve(process.env.HOME, ".shout")
);
},
resolveHomePath: function () {
var fragments = [ Helper.HOME ].concat([].slice.apply(arguments));
return path.resolve.apply(path, fragments);
}
};
Helper.HOME = Helper.getHomeDirectory()

View File

@ -4,6 +4,7 @@ var config = require("../../../config.json");
var fs = require("fs");
var mkdirp = require("mkdirp");
var request = require("superagent");
var Helper = require("../../helper");
module.exports = function(irc, network) {
var client = this;
@ -40,7 +41,7 @@ module.exports = function(irc, network) {
};
function fetchImage(url, callback) {
var path = process.env.HOME + "/.shout/cache/thumbs";
var path = Helper.resolveHomePath("cache", "thumbs");
var name = new Date().getTime().toString();
mkdirp(path, function(e) {
if (e) {

View File

@ -6,6 +6,7 @@ var config = require("../config.json");
var fs = require("fs");
var http = require("connect");
var io = require("socket.io");
var Helper = require("./helper");
var sockets = null;
var manager = new ClientManager();
@ -18,7 +19,7 @@ module.exports = function(port, host, isPublic) {
var app = http()
.use(index)
.use(http.static("client"))
.use(http.static(process.env.HOME + "/.shout/cache"))
.use(http.static(Helper.resolveHomePath("cache")))
.listen(config.port, config.host);
sockets = io(app);