Added '--home <path>' option

This commit is contained in:
Mattias Erming 2014-10-03 16:33:44 -07:00
parent 83f4c5dd69
commit 2ed7d6364c
14 changed files with 38 additions and 67 deletions

View File

@ -34,15 +34,6 @@ module.exports = {
//
theme: "themes/example.css",
//
// Override home directory.
// Leaving this field empty will default to '~/.shout/'.
//
// @type string
// @default ""
//
home: "",
//
// Autoload users
//

View File

@ -8,7 +8,6 @@ var Network = require("./models/network");
var slate = require("slate-irc");
var tls = require("tls");
var Helper = require("./helper");
var config = Helper.getConfig();
module.exports = Client;

View File

@ -31,7 +31,7 @@ ClientManager.prototype.loadUsers = function() {
ClientManager.prototype.loadUser = function(name) {
try {
var json = fs.readFileSync(
Helper.resolveHomePath("users", name, "user.json"),
Helper.HOME + "/users/" + name + "/user.json",
"utf-8"
);
json = JSON.parse(json);
@ -56,10 +56,8 @@ ClientManager.prototype.loadUser = function(name) {
ClientManager.prototype.getUsers = function() {
var users = [];
var path = Helper.resolveHomePath("users");
var path = Helper.HOME + "/users";
mkdirp.sync(path);
try {
users = fs.readdirSync(path);
} catch(e) {
@ -103,7 +101,7 @@ ClientManager.prototype.removeUser = function(name) {
return false;
}
try {
var path = Helper.resolveHomePath("users", name);
var path = Helper.HOME + "/users/" + name;
fs.unlinkSync(path + "/user.json");
fs.rmdirSync(path);
} catch(e) {

View File

@ -9,7 +9,7 @@ program
.command("add <name> [<password>]")
.description("Add a new user")
.action(function(name, password) {
var path = Helper.resolveHomePath("users");
var path = Helper.HOME + "/users";
try {
mkdirp.sync(path);
} catch (e) {
@ -59,6 +59,6 @@ function add(manager, name, password) {
hash
);
console.log("User '" + name + "' created:");
console.log(Helper.resolveHomePath("users", name, "user.json"));
console.log(Helper.HOME + "/users/" + name + "/user.json");
console.log("");
}

View File

@ -5,23 +5,13 @@ var mkdirp = require("mkdirp");
var child = require("child_process");
var Helper = require("../helper");
var CONFIG_PATH = process.env.SHOUT_CONFIG;
if (!CONFIG_PATH) {
CONFIG_PATH = Helper.resolveHomePath("config.js");
}
if (!fs.existsSync(CONFIG_PATH)) {
mkdirp.sync(Helper.getHomeDirectory());
var configFile = fs.readFileSync(path.resolve(__dirname, "..", "..", "config.js"));
fs.writeFileSync(CONFIG_PATH, configFile);
}
program
.command("config")
.description("Edit config: '" + CONFIG_PATH + "'")
.description("Edit config: '" + Helper.HOME + "/config.js'")
.action(function() {
child.spawn(
"vi",
[CONFIG_PATH],
[Helper.HOME + "/config.js"],
{stdio: "inherit"}
);
});

View File

@ -5,7 +5,7 @@ var Helper = require("../helper");
program
.command("edit <name>")
.description("Edit user: '" + Helper.resolveHomePath("users", "<name>", "user.json") + "'")
.description("Edit user: '" + Helper.HOME + "/users/<name>/user.json'")
.action(function(name) {
var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) {

View File

@ -1,7 +1,12 @@
var program = require("commander");
var pkg = require("../../package.json");
var fs = require("fs");
var mkdirp = require("mkdirp");
var Helper = require("../helper");
program.version(pkg.version, "-v, --version");
program.option("");
program.option(" --home <path>", "home path");
require("./start");
require("./config");
@ -13,6 +18,16 @@ require("./edit");
program.parse(process.argv);
if (program.home) {
Helper.HOME = program.home;
} else {
var config = Helper.HOME + "/config.js";
if (!fs.existsSync(config)) {
mkdirp.sync(Helper.HOME);
fs.writeFileSync(config, fs.readFileSync(__dirname + "/../../config.js"));
}
}
if (!program.args.length) {
program.parse(process.argv.concat("start"));
}

View File

@ -3,7 +3,7 @@ var program = require("commander");
program
.command("list")
.description("List all existing users")
.description("List all users")
.action(function() {
var users = new ClientManager().getUsers();
if (!users.length) {

View File

@ -8,7 +8,7 @@ program
.description("Remove an existing user")
.action(function(name) {
try {
var path = Helper.resolveHomePath("users");
var path = Helper.HOME + "/users";
var test = path + "/.test";
fs.mkdirSync(test);
fs.rmdirSync(test);

View File

@ -15,7 +15,7 @@ program
console.log("");
return;
}
var file = Helper.resolveHomePath("users/" + name + "/user.json");
var file = Helper.HOME + "/users/" + name + "/user.json";
var user = require(file);
require("read")({
prompt: "Password: ",

View File

@ -2,7 +2,6 @@ var ClientManager = new require("../clientManager");
var program = require("commander");
var shout = require("../server");
var Helper = require("../helper");
var config = Helper.getConfig();
program
.option("-H, --host <ip>", "host")
@ -13,6 +12,7 @@ program
.description("Start the server")
.action(function() {
var users = new ClientManager().getUsers();
var config = Helper.getConfig();
var mode = config.public;
if (program.public) {
mode = true;

View File

@ -1,30 +1,8 @@
var fs = require("fs");
var path = require("path");
var Helper = module.exports = {
getConfig: function () {
var filename = process.env.SHOUT_CONFIG;
if(!filename || !fs.existsSync(filename)) {
filename = this.resolveHomePath("config.js");
if(!fs.existsSync(filename)) {
filename = path.resolve(__dirname, "..", "config");
}
}
return require(filename);
},
getHomeDirectory: function () {
return (
(process.env.SHOUT_CONFIG && fs.existsSync(process.env.SHOUT_CONFIG) && 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);
}
module.exports = {
HOME: process.env.HOME + "/.shout",
getConfig: getConfig
};
Helper.HOME = Helper.getHomeDirectory()
function getConfig() {
return require(this.HOME + "/config.js");
};

View File

@ -2,17 +2,17 @@ var fs = require("fs");
var mkdirp = require("mkdirp");
var moment = require("moment");
var Helper = require("./helper");
var config = Helper.getConfig();
module.exports = {
write: function(user, network, chan, msg) {
var path = Helper.resolveHomePath("users", user, "logs", network);
var path = Helper.HOME + "/users/" + user + "/logs/" + network;
try {
mkdirp.sync(path);
} catch(e) {
return;
}
var config = Helper.getConfig();
var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss";
var tz = (config.logs || {}).timezone || "UTC+00:00";

View File

@ -6,20 +6,20 @@ var express = require("express");
var fs = require("fs");
var io = require("socket.io");
var Helper = require("./helper");
var config = Helper.getConfig();
var config = {};
var sockets = null;
var manager = new ClientManager();
module.exports = function(port, host, isPublic) {
config = Helper.getConfig();
config.port = port;
config.host = host;
config.public = isPublic;
var app = express()
.use(index)
.use(express.static("client"))
.use(express.static(Helper.resolveHomePath("cache")));
.use(express.static("client"));
var server = null;
var https = config.https || {};