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
//
// 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
// @default {}

View File

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

View File

@ -1,6 +1,5 @@
var ClientManager = new require("../clientManager");
var bcrypt = require("bcrypt-nodejs");
var fs = require("fs");
var program = require("commander");
var mkdirp = require("mkdirp");
var Helper = require("../helper");
@ -9,21 +8,10 @@ program
.command("add <name>")
.description("Add a new user")
.action(function(name/* , password */) {
var path = Helper.HOME + "/users";
try {
mkdirp.sync(path);
mkdirp.sync(Helper.USERS_PATH);
} catch (e) {
log.error("Could not create", 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.");
log.error("Could not create", Helper.USERS_PATH);
return;
}
var manager = new ClientManager();
@ -50,5 +38,5 @@ function add(manager, name, password) {
hash
);
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
.command("config")
.description("Edit config: '" + Helper.HOME + "/config.js'")
.description("Edit config: " + Helper.CONFIG_PATH)
.action(function() {
child.spawn(
process.env.EDITOR || "vi",
[Helper.HOME + "/config.js"],
[Helper.CONFIG_PATH],
{stdio: "inherit"}
);
});

View File

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

View File

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

View File

@ -1,22 +1,10 @@
var ClientManager = new require("../clientManager");
var fs = require("fs");
var program = require("commander");
var Helper = require("../helper");
program
.command("remove <name>")
.description("Remove an existing user")
.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();
if (manager.removeUser(name)) {
log.info("Removed user '" + name + "'.");

View File

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

View File

@ -1,17 +1,35 @@
var path = require("path");
var os = require("os");
module.exports = {
HOME: (process.env.HOME || process.env.USERPROFILE) + "/.lounge",
getConfig: getConfig,
var Helper = {
expandHome: expandHome,
getConfig: getConfig,
getUserConfigPath: getUserConfigPath,
getUserLogsPath: getUserLogsPath,
setHome: setHome,
};
function getConfig() {
return require(path.resolve(this.HOME) + "/config");
module.exports = Helper;
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;
if (os.homedir) {
@ -24,6 +42,5 @@ function expandHome(path) {
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) {
try {
var path = Helper.HOME + "/logs/" + user + "/" + network;
var path = Helper.getUserLogsPath(user, network);
mkdirp.sync(path);
} catch (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);