From 2ed7d6364c6c84fe43b7ca93c33d81b3d2abf6c8 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Fri, 3 Oct 2014 16:33:44 -0700 Subject: [PATCH] Added '--home ' option --- config.js | 9 --------- src/client.js | 1 - src/clientManager.js | 8 +++----- src/command-line/add.js | 4 ++-- src/command-line/config.js | 14 ++------------ src/command-line/edit.js | 2 +- src/command-line/index.js | 15 +++++++++++++++ src/command-line/list.js | 2 +- src/command-line/remove.js | 2 +- src/command-line/reset.js | 2 +- src/command-line/start.js | 2 +- src/helper.js | 34 ++++++---------------------------- src/log.js | 4 ++-- src/server.js | 6 +++--- 14 files changed, 38 insertions(+), 67 deletions(-) diff --git a/config.js b/config.js index 21681a81..4e41abb6 100644 --- a/config.js +++ b/config.js @@ -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 // diff --git a/src/client.js b/src/client.js index 077a8cc4..55ef0996 100644 --- a/src/client.js +++ b/src/client.js @@ -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; diff --git a/src/clientManager.js b/src/clientManager.js index 8197d700..23da0706 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -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) { diff --git a/src/command-line/add.js b/src/command-line/add.js index 08b4ba86..01db39d5 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -9,7 +9,7 @@ program .command("add []") .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(""); } diff --git a/src/command-line/config.js b/src/command-line/config.js index c77d0d26..2385ef87 100644 --- a/src/command-line/config.js +++ b/src/command-line/config.js @@ -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"} ); }); diff --git a/src/command-line/edit.js b/src/command-line/edit.js index 1d2c5829..8bde2656 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -5,7 +5,7 @@ var Helper = require("../helper"); program .command("edit ") - .description("Edit user: '" + Helper.resolveHomePath("users", "", "user.json") + "'") + .description("Edit user: '" + Helper.HOME + "/users//user.json'") .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { diff --git a/src/command-line/index.js b/src/command-line/index.js index 5001f30a..9fe93095 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -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 ", "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")); } diff --git a/src/command-line/list.js b/src/command-line/list.js index d950bb95..f94e20b4 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -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) { diff --git a/src/command-line/remove.js b/src/command-line/remove.js index b16aa9e8..bdab70c4 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -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); diff --git a/src/command-line/reset.js b/src/command-line/reset.js index b3861751..5f373c4b 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -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: ", diff --git a/src/command-line/start.js b/src/command-line/start.js index dd8425a4..8f5d9b92 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -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 ", "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; diff --git a/src/helper.js b/src/helper.js index 4069d0db..f1d43a4e 100644 --- a/src/helper.js +++ b/src/helper.js @@ -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"); +}; diff --git a/src/log.js b/src/log.js index 60af9623..c0ec4f3c 100644 --- a/src/log.js +++ b/src/log.js @@ -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"; diff --git a/src/server.js b/src/server.js index d5798d69..59780cfd 100644 --- a/src/server.js +++ b/src/server.js @@ -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 || {};