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 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

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

@ -8,12 +8,11 @@ program
.description("Remove an existing user") .description("Remove an existing user")
.action(function(name) { .action(function(name) {
try { try {
var path = Helper.HOME + "/users"; var test = require("path").join(Helper.USERS_PATH, ".test");
var test = path + "/.test";
fs.mkdirSync(test); fs.mkdirSync(test);
fs.rmdirSync(test); fs.rmdirSync(test);
} catch (e) { } 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."); log.info("Try running the command as sudo.");
return; return;
} }

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