Merge pull request #307 from thelounge/astorije/path-join-everywhere

Replace all concatenated paths with Node's path.join
This commit is contained in:
Max-P 2016-05-21 22:22:28 -04:00
commit fca941e962
11 changed files with 59 additions and 66 deletions

View File

@ -129,7 +129,7 @@ module.exports = {
// Log settings // Log settings
// //
// Logging has to be enabled per user. If enabled, logs will be stored in // Logging has to be enabled per user. If enabled, logs will be stored in
// the '/users/<user>/logs/' folder. // the 'logs/<user>/<network>/' folder.
// //
// @type object // @type object
// @default {} // @default {}

View File

@ -3,7 +3,6 @@ var fs = require("fs");
var Client = require("./client"); var Client = require("./client");
var mkdirp = require("mkdirp"); var mkdirp = require("mkdirp");
var Helper = require("./helper"); var Helper = require("./helper");
var path = require("path");
module.exports = ClientManager; module.exports = ClientManager;
@ -46,10 +45,9 @@ ClientManager.prototype.loadUser = function(name) {
ClientManager.prototype.getUsers = function() { ClientManager.prototype.getUsers = function() {
var users = []; var users = [];
var path = Helper.HOME + "/users"; mkdirp.sync(Helper.USERS_PATH);
mkdirp.sync(path);
try { try {
var files = fs.readdirSync(path); var files = fs.readdirSync(Helper.USERS_PATH);
files.forEach(function(file) { files.forEach(function(file) {
if (file.indexOf(".json") !== -1) { if (file.indexOf(".json") !== -1) {
users.push(file.replace(".json", "")); users.push(file.replace(".json", ""));
@ -68,10 +66,9 @@ ClientManager.prototype.addUser = function(name, password) {
return false; return false;
} }
try { try {
var usersPath = path.join(Helper.HOME, "users"); mkdirp.sync(Helper.USERS_PATH);
mkdirp.sync(usersPath);
if (path.basename(name) !== name) { if (require("path").basename(name) !== name) {
throw new Error(name + " is an invalid username."); throw new Error(name + " is an invalid username.");
} }
@ -82,7 +79,7 @@ ClientManager.prototype.addUser = function(name, password) {
networks: [] networks: []
}; };
fs.writeFileSync( fs.writeFileSync(
path.join(usersPath, name + ".json"), Helper.getUserConfigPath(name),
JSON.stringify(user, null, " ") JSON.stringify(user, null, " ")
); );
} catch (e) { } catch (e) {
@ -100,14 +97,13 @@ ClientManager.prototype.updateUser = function(name, opts) {
if (typeof opts === "undefined") { if (typeof opts === "undefined") {
return false; return false;
} }
var path = Helper.HOME + "/users/" + name + ".json";
var user = {};
var user = {};
try { try {
user = this.readUserConfig(name); user = this.readUserConfig(name);
_.assign(user, opts); _.assign(user, opts);
fs.writeFileSync( fs.writeFileSync(
path, Helper.getUserConfigPath(name),
JSON.stringify(user, null, " ") JSON.stringify(user, null, " ")
); );
} catch (e) { } catch (e) {
@ -122,9 +118,8 @@ ClientManager.prototype.readUserConfig = function(name) {
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
return false; return false;
} }
var path = Helper.HOME + "/users/" + name + ".json";
var user = {}; var user = {};
var data = fs.readFileSync(path, "utf-8"); var data = fs.readFileSync(Helper.getUserConfigPath(name), "utf-8");
user = JSON.parse(data); user = JSON.parse(data);
return user; return user;
}; };
@ -135,8 +130,7 @@ ClientManager.prototype.removeUser = function(name) {
return false; return false;
} }
try { try {
var path = Helper.HOME + "/users/" + name + ".json"; fs.unlinkSync(Helper.getUserConfigPath(name));
fs.unlinkSync(path);
} catch (e) { } catch (e) {
throw e; throw e;
} }

View File

@ -1,6 +1,5 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var bcrypt = require("bcrypt-nodejs"); var bcrypt = require("bcrypt-nodejs");
var fs = require("fs");
var program = require("commander"); var program = require("commander");
var mkdirp = require("mkdirp"); var mkdirp = require("mkdirp");
var Helper = require("../helper"); var Helper = require("../helper");
@ -9,21 +8,10 @@ program
.command("add <name>") .command("add <name>")
.description("Add a new user") .description("Add a new user")
.action(function(name/* , password */) { .action(function(name/* , password */) {
var path = Helper.HOME + "/users";
try { try {
mkdirp.sync(path); mkdirp.sync(Helper.USERS_PATH);
} catch (e) { } catch (e) {
log.error("Could not create", path); log.error("Could not create", Helper.USERS_PATH);
log.info("Try running the command as sudo.");
return;
}
try {
var test = path + "/.test";
fs.mkdirSync(test);
fs.rmdirSync(test);
} catch (e) {
log.error("You have no permissions to write to", path);
log.info("Try running the command as sudo.");
return; return;
} }
var manager = new ClientManager(); var manager = new ClientManager();
@ -50,5 +38,5 @@ function add(manager, name, password) {
hash hash
); );
log.info("User '" + name + "' created:"); log.info("User '" + name + "' created:");
log.info(Helper.HOME + "/users/" + name + ".json"); log.info(Helper.getUserConfigPath(name));
} }

View File

@ -4,11 +4,11 @@ var Helper = require("../helper");
program program
.command("config") .command("config")
.description("Edit config: '" + Helper.HOME + "/config.js'") .description("Edit config: " + Helper.CONFIG_PATH)
.action(function() { .action(function() {
child.spawn( child.spawn(
process.env.EDITOR || "vi", process.env.EDITOR || "vi",
[Helper.HOME + "/config.js"], [Helper.CONFIG_PATH],
{stdio: "inherit"} {stdio: "inherit"}
); );
}); });

View File

@ -5,7 +5,7 @@ var Helper = require("../helper");
program program
.command("edit <name>") .command("edit <name>")
.description("Edit user: '" + Helper.HOME + "/users/<name>.json'") .description("Edit user: " + Helper.getUserConfigPath("<name>"))
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
@ -14,7 +14,7 @@ program
} }
child.spawn( child.spawn(
process.env.EDITOR || "vi", process.env.EDITOR || "vi",
[require("path").join(Helper.HOME, "users", name + ".json")], [Helper.getUserConfigPath(name)],
{stdio: "inherit"} {stdio: "inherit"}
); );
}); });

View File

@ -4,6 +4,7 @@ var program = require("commander");
var pkg = require("../../package.json"); var pkg = require("../../package.json");
var fs = require("fs"); var fs = require("fs");
var mkdirp = require("mkdirp"); var mkdirp = require("mkdirp");
var path = require("path");
var Helper = require("../helper"); var Helper = require("../helper");
program.version(pkg.version, "-v, --version"); program.version(pkg.version, "-v, --version");
@ -11,18 +12,22 @@ program.option("");
program.option(" --home <path>" , "home path"); program.option(" --home <path>" , "home path");
var argv = program.parseOptions(process.argv); var argv = program.parseOptions(process.argv);
if (program.home) {
Helper.HOME = program.home;
}
var config = Helper.HOME + "/config.js"; Helper.setHome(program.home);
if (!fs.existsSync(config)) {
if (!fs.existsSync(Helper.CONFIG_PATH)) {
mkdirp.sync(Helper.HOME, {mode: "0700"}); mkdirp.sync(Helper.HOME, {mode: "0700"});
fs.writeFileSync( fs.writeFileSync(
config, Helper.CONFIG_PATH,
fs.readFileSync(__dirname + "/../../defaults/config.js") fs.readFileSync(path.resolve(path.join(
__dirname,
"..",
"..",
"defaults",
"config.js"
)))
); );
log.info("Config created:", config); log.info("Config created:", Helper.CONFIG_PATH);
} }
require("./start"); require("./start");

View File

@ -1,22 +1,10 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var fs = require("fs");
var program = require("commander"); var program = require("commander");
var Helper = require("../helper");
program program
.command("remove <name>") .command("remove <name>")
.description("Remove an existing user") .description("Remove an existing user")
.action(function(name) { .action(function(name) {
try {
var path = Helper.HOME + "/users";
var test = path + "/.test";
fs.mkdirSync(test);
fs.rmdirSync(test);
} catch (e) {
log.warn("You have no permissions to delete from " + path);
log.info("Try running the command as sudo.");
return;
}
var manager = new ClientManager(); var manager = new ClientManager();
if (manager.removeUser(name)) { if (manager.removeUser(name)) {
log.info("Removed user '" + name + "'."); log.info("Removed user '" + name + "'.");

View File

@ -13,7 +13,7 @@ program
log.error("User '" + name + "' doesn't exist."); log.error("User '" + name + "' doesn't exist.");
return; return;
} }
var file = Helper.HOME + "/users/" + name + ".json"; var file = Helper.getUserConfigPath(name);
var user = require(file); var user = require(file);
require("read")({ require("read")({
prompt: "[thelounge] New password: ", prompt: "[thelounge] New password: ",

View File

@ -1,17 +1,35 @@
var path = require("path"); var path = require("path");
var os = require("os"); var os = require("os");
module.exports = { var Helper = {
HOME: (process.env.HOME || process.env.USERPROFILE) + "/.lounge",
getConfig: getConfig,
expandHome: expandHome, expandHome: expandHome,
getConfig: getConfig,
getUserConfigPath: getUserConfigPath,
getUserLogsPath: getUserLogsPath,
setHome: setHome,
}; };
function getConfig() { module.exports = Helper;
return require(path.resolve(this.HOME) + "/config");
function setHome(homePath) {
this.HOME = expandHome(homePath || "~/.lounge");
this.CONFIG_PATH = path.join(this.HOME, "config.js");
this.USERS_PATH = path.join(this.HOME, "users");
} }
function expandHome(path) { function getUserConfigPath(name) {
return path.join(this.USERS_PATH, name + ".json");
}
function getUserLogsPath(name, network) {
return path.join(this.HOME, "logs", name, network);
}
function getConfig() {
return require(this.CONFIG_PATH);
}
function expandHome(shortenedPath) {
var home; var home;
if (os.homedir) { if (os.homedir) {
@ -24,6 +42,5 @@ function expandHome(path) {
home = home.replace("$", "$$$$"); home = home.replace("$", "$$$$");
return path.replace(/^~($|\/|\\)/, home + "$1"); return path.resolve(shortenedPath.replace(/^~($|\/|\\)/, home + "$1"));
} }

View File

@ -5,7 +5,7 @@ var Helper = require("./helper");
module.exports.write = function(user, network, chan, msg) { module.exports.write = function(user, network, chan, msg) {
try { try {
var path = Helper.HOME + "/logs/" + user + "/" + network; var path = Helper.getUserLogsPath(user, network);
mkdirp.sync(path); mkdirp.sync(path);
} catch (e) { } catch (e) {
log.error("Unabled to create logs directory", e); log.error("Unabled to create logs directory", e);

View File

@ -1 +1,2 @@
process.env.HOME = "test/fixtures"; var home = require("path").join(__dirname, ".lounge");
require("../../src/helper").setHome(home);