Replace all concatenated paths with Node's path.join

This commit is contained in:
Jérémie Astori 2016-05-08 02:21:31 -04:00
parent e6990e0fc7
commit fe85873f1e
9 changed files with 50 additions and 43 deletions

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

@ -9,21 +9,21 @@ 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.error("Could not create", Helper.USERS_PATH);
log.info("Try running the command as sudo.");
return;
}
try {
var test = path + "/.test";
var test = require("path").join(Helper.USERS_PATH, ".test");
fs.mkdirSync(test);
fs.rmdirSync(test);
} catch (e) {
log.error("You have no permissions to write to", path);
log.error("You have no permissions to write to", Helper.USERS_PATH);
log.info("Try running the command as sudo.");
log.error("Could not create", Helper.USERS_PATH);
return;
}
var manager = new ClientManager();
@ -50,5 +50,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");
@ -12,17 +13,22 @@ program.option(" --home <path>" , "home path");
var argv = program.parseOptions(process.argv);
if (program.home) {
Helper.HOME = program.home;
Helper.HOME = path.resolve(program.home);
}
var config = Helper.HOME + "/config.js";
if (!fs.existsSync(config)) {
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

@ -8,12 +8,11 @@ program
.description("Remove an existing user")
.action(function(name) {
try {
var path = Helper.HOME + "/users";
var test = path + "/.test";
var test = require("path").join(Helper.USERS_PATH, ".test");
fs.mkdirSync(test);
fs.rmdirSync(test);
} catch (e) {
log.warn("You have no permissions to delete from " + path);
log.warn("You have no permissions to delete from " + Helper.USERS_PATH);
log.info("Try running the command as sudo.");
return;
}

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,26 @@
var path = require("path");
var os = require("os");
var HOME = expandHome("~/.lounge");
module.exports = {
HOME: (process.env.HOME || process.env.USERPROFILE) + "/.lounge",
getConfig: getConfig,
HOME: HOME,
CONFIG_PATH: path.join(HOME, "config.js"),
USERS_PATH: path.join(HOME, "users"),
expandHome: expandHome,
getConfig: getConfig,
getUserConfigPath: getUserConfigPath,
};
function getConfig() {
return require(path.resolve(this.HOME) + "/config");
function getUserConfigPath(name) {
return path.join(this.USERS_PATH, name + ".json");
}
function expandHome(path) {
function getConfig() {
return require(this.CONFIG_PATH);
}
function expandHome(shortenedPath) {
var home;
if (os.homedir) {
@ -24,6 +33,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 = require("path").join(Helper.HOME, "logs", user, network);
mkdirp.sync(path);
} catch (e) {
log.error("Unabled to create logs directory", e);