diff --git a/src/clientManager.js b/src/clientManager.js index c076494f..6211fb46 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -41,7 +41,7 @@ ClientManager.prototype.autoloadUsers = function() { process.exit(1); } - if (!users.length) { + if (users.length === 0) { log.info(noUsersWarning); } @@ -51,7 +51,7 @@ ClientManager.prototype.autoloadUsers = function() { const loaded = this.clients.map((c) => c.name); const updatedUsers = this.getUsers(); - if (!updatedUsers.length) { + if (updatedUsers.length === 0) { log.info(noUsersWarning); } diff --git a/src/command-line/add.js b/src/command-line/add.js index c88833d5..894e5f88 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -1,9 +1,9 @@ "use strict"; -var ClientManager = new require("../clientManager"); -var colors = require("colors/safe"); -var program = require("commander"); -var Helper = require("../helper"); +const colors = require("colors/safe"); +const program = require("commander"); +const fs = require("fs"); +const Helper = require("../helper"); const Utils = require("./utils"); program @@ -11,6 +11,13 @@ program .description("Add a new user") .on("--help", Utils.extraHelp) .action(function(name) { + if (!fs.existsSync(Helper.USERS_PATH)) { + log.error(`${Helper.USERS_PATH} does not exist.`); + return; + } + + const ClientManager = require("../clientManager"); + if (Helper.config.public) { log.warn(`Users have no effect in ${colors.bold("public")} mode.`); } diff --git a/src/command-line/config.js b/src/command-line/config.js index 908b3dcc..9b601dc8 100644 --- a/src/command-line/config.js +++ b/src/command-line/config.js @@ -1,9 +1,10 @@ "use strict"; -var program = require("commander"); -var child = require("child_process"); -var colors = require("colors/safe"); -var Helper = require("../helper"); +const program = require("commander"); +const child = require("child_process"); +const colors = require("colors/safe"); +const fs = require("fs"); +const Helper = require("../helper"); const Utils = require("./utils"); program @@ -11,6 +12,11 @@ program .description(`Edit configuration file located at ${colors.green(Helper.CONFIG_PATH)}.`) .on("--help", Utils.extraHelp) .action(function() { + if (!fs.existsSync(Helper.CONFIG_PATH)) { + log.error(`${Helper.CONFIG_PATH} does not exist.`); + return; + } + var child_spawn = child.spawn( process.env.EDITOR || "vi", [Helper.CONFIG_PATH], diff --git a/src/command-line/edit.js b/src/command-line/edit.js index b2da9450..2b552f6b 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -1,10 +1,10 @@ "use strict"; -var ClientManager = new require("../clientManager"); -var program = require("commander"); -var child = require("child_process"); -var colors = require("colors/safe"); -var Helper = require("../helper"); +const program = require("commander"); +const child = require("child_process"); +const colors = require("colors/safe"); +const fs = require("fs"); +const Helper = require("../helper"); const Utils = require("./utils"); program @@ -12,6 +12,13 @@ program .description(`Edit user file located at ${colors.green(Helper.getUserConfigPath(""))}.`) .on("--help", Utils.extraHelp) .action(function(name) { + if (!fs.existsSync(Helper.USERS_PATH)) { + log.error(`${Helper.USERS_PATH} does not exist.`); + return; + } + + const ClientManager = require("../clientManager"); + var users = new ClientManager().getUsers(); if (users === undefined) { // There was an error, already logged diff --git a/src/command-line/index.js b/src/command-line/index.js index 01cfc619..285d5fe2 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -2,12 +2,9 @@ global.log = require("../log.js"); -var program = require("commander"); -var colors = require("colors/safe"); -var fs = require("fs"); -var fsextra = require("fs-extra"); -var path = require("path"); -var Helper = require("../helper"); +const program = require("commander"); +const colors = require("colors/safe"); +const Helper = require("../helper"); const Utils = require("./utils"); program.version(Helper.getVersion(), "-v, --version") @@ -28,21 +25,6 @@ if (!home) { Helper.setHome(home); -if (!fs.existsSync(Helper.CONFIG_PATH)) { - fsextra.ensureDirSync(Helper.HOME); - fs.chmodSync(Helper.HOME, "0700"); - fsextra.copySync(path.resolve(path.join( - __dirname, - "..", - "..", - "defaults", - "config.js" - )), Helper.CONFIG_PATH); - log.info(`Configuration file created at ${colors.green(Helper.CONFIG_PATH)}.`); -} - -fsextra.ensureDirSync(Helper.USERS_PATH); - require("./start"); require("./config"); require("./list"); diff --git a/src/command-line/list.js b/src/command-line/list.js index e10a7375..d092ad8b 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -1,8 +1,8 @@ "use strict"; -var ClientManager = new require("../clientManager"); -var program = require("commander"); -var colors = require("colors/safe"); +const colors = require("colors/safe"); +const program = require("commander"); +const fs = require("fs"); const Helper = require("../helper"); const Utils = require("./utils"); @@ -11,6 +11,13 @@ program .description("List all users") .on("--help", Utils.extraHelp) .action(function() { + if (!fs.existsSync(Helper.USERS_PATH)) { + log.error(`${Helper.USERS_PATH} does not exist.`); + return; + } + + const ClientManager = require("../clientManager"); + if (Helper.config.public) { log.warn(`Users have no effect in ${colors.bold("public")} mode.`); } @@ -21,12 +28,12 @@ program return; } - if (!users.length) { - log.info(`There are currently no users. Create one with ${colors.bold("lounge add ")}.`); - } else { + if (users.length > 0) { log.info("Users:"); - for (var i = 0; i < users.length; i++) { - log.info(`${i + 1}. ${colors.bold(users[i])}`); - } + users.forEach((user, i) => { + log.info(`${i + 1}. ${colors.bold(user)}`); + }); + } else { + log.info(`There are currently no users. Create one with ${colors.bold("lounge add ")}.`); } }); diff --git a/src/command-line/remove.js b/src/command-line/remove.js index e15f13ff..beb3f3b1 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -1,8 +1,9 @@ "use strict"; -var ClientManager = new require("../clientManager"); -var program = require("commander"); -var colors = require("colors/safe"); +const colors = require("colors/safe"); +const program = require("commander"); +const fs = require("fs"); +const Helper = require("../helper"); const Utils = require("./utils"); program @@ -10,6 +11,12 @@ program .description("Remove an existing user") .on("--help", Utils.extraHelp) .action(function(name) { + if (!fs.existsSync(Helper.USERS_PATH)) { + log.error(`${Helper.USERS_PATH} does not exist.`); + return; + } + + const ClientManager = require("../clientManager"); const manager = new ClientManager(); try { diff --git a/src/command-line/reset.js b/src/command-line/reset.js index 6721ff4b..a5f4da43 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -1,10 +1,9 @@ "use strict"; -var ClientManager = new require("../clientManager"); -var fs = require("fs"); -var program = require("commander"); -var colors = require("colors/safe"); -var Helper = require("../helper"); +const colors = require("colors/safe"); +const program = require("commander"); +const fs = require("fs"); +const Helper = require("../helper"); const Utils = require("./utils"); program @@ -12,6 +11,13 @@ program .description("Reset user password") .on("--help", Utils.extraHelp) .action(function(name) { + if (!fs.existsSync(Helper.USERS_PATH)) { + log.error(`${Helper.USERS_PATH} does not exist.`); + return; + } + + const ClientManager = require("../clientManager"); + var users = new ClientManager().getUsers(); if (users === undefined) { // There was an error, already logged diff --git a/src/command-line/start.js b/src/command-line/start.js index db33e127..fbec3f17 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -1,8 +1,11 @@ "use strict"; -var program = require("commander"); -var server = require("../server"); -var Helper = require("../helper"); +const colors = require("colors/safe"); +const fs = require("fs"); +const fsextra = require("fs-extra"); +const path = require("path"); +const program = require("commander"); +const Helper = require("../helper"); const Utils = require("./utils"); program @@ -15,6 +18,10 @@ program .description("Start the server") .on("--help", Utils.extraHelp) .action(function(options) { + initalizeConfig(); + + const server = require("../server"); + var mode = Helper.config.public; if (options.public) { mode = true; @@ -29,3 +36,20 @@ program server(); }); + +function initalizeConfig() { + if (!fs.existsSync(Helper.CONFIG_PATH)) { + fsextra.ensureDirSync(Helper.HOME); + fs.chmodSync(Helper.HOME, "0700"); + fsextra.copySync(path.resolve(path.join( + __dirname, + "..", + "..", + "defaults", + "config.js" + )), Helper.CONFIG_PATH); + log.info(`Configuration file created at ${colors.green(Helper.CONFIG_PATH)}.`); + } + + fsextra.ensureDirSync(Helper.USERS_PATH); +}