diff --git a/index.js b/index.js index 88ac8b7a..f2c2bf97 100755 --- a/index.js +++ b/index.js @@ -1,121 +1,3 @@ #!/usr/bin/env node process.chdir(__dirname); - -var config = require("./config.json"); -var ClientManager= new require("./src/clientManager"); -var program = require("commander"); -var shout = require("./src/server.js"); -var fs = require("fs"); - -program - .option("-p, --port "); - -program - .command("start") - .description("Start the server") - .action(function() { - var users = new ClientManager().getUsers(); - if (!config.public && !users.length) { - console.log(""); - console.log("No users found!"); - console.log("Create a new user with 'shout add-user '.") - console.log(""); - } else { - var port = program.port || config.port; - shout(port, config.public); - } - }); - -program - .command("list") - .description("List all existing users") - .action(function() { - var users = new ClientManager().getUsers(); - if (!users.length) { - console.log(""); - console.log("No users found!"); - console.log(""); - } else { - console.log(""); - console.log("Users:"); - for (var i = 0; i < users.length; i++) { - console.log(" " + (i + 1) + ". " + users[i]); - } - console.log(""); - } - }); - -program - .command("add ") - .description("Add a new user") - .action(function(name) { - try { - var path = __dirname + "/users"; - var test = path + "/.test"; - fs.mkdirSync(test); - fs.rmdirSync(test); - } catch (e) { - console.log(""); - console.log("You have no permissions to write to " + path); - console.log("Try running the command as sudo."); - console.log(""); - return; - } - var manager = new ClientManager(); - var users = manager.getUsers(); - if (users.indexOf(name) !== -1) { - console.log(""); - console.log("User '" + name + "' already exists."); - console.log(""); - return; - } - require("read")({ - prompt: "Password: " - }, function(err, password) { - console.log(""); - if (err) { - return; - } - var success = manager.addUser( - name, - password - ); - console.log("Added '" + name + "'."); - console.log(""); - }); - }); - -program - .command("remove ") - .description("Remove an existing user") - .action(function(name) { - try { - var path = __dirname + "/users"; - var test = path + "/.test"; - fs.mkdirSync(test); - fs.rmdirSync(test); - } catch (e) { - console.log(""); - console.log("You have no permissions to delete from " + path); - console.log("Try running the command as sudo."); - console.log(""); - return; - } - var manager = new ClientManager(); - var success = manager.removeUser(name); - if (success) { - console.log(""); - console.log("Removed '" + name + "'."); - console.log(""); - } else { - console.log(""); - console.log("User '" + name + "' doesn't exist."); - console.log(""); - } - }); - -program.parse(process.argv) - -if (!program.args.length) { - program.parse(process.argv.concat("start")); -} +var cli = require("./src/cli"); diff --git a/package.json b/package.json index 278bb8ba..abf8ea97 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "shout", "description": "A web IRC client", - "version": "0.10.3", + "version": "0.10.4", "author": "Mattias Erming", "preferGlobal": true, "bin": { diff --git a/src/cli/add.js b/src/cli/add.js new file mode 100644 index 00000000..27978a0c --- /dev/null +++ b/src/cli/add.js @@ -0,0 +1,43 @@ +var ClientManager = new require("../clientManager"); +var fs = require("fs"); +var program = require("commander"); + +program + .command("add ") + .description("Add a new user") + .action(function(name) { + try { + var path = process.cwd() + "/users"; + var test = path + "/.test"; + fs.mkdirSync(test); + fs.rmdirSync(test); + } catch (e) { + console.log(""); + console.log("You have no permissions to write to " + path); + console.log("Try running the command as sudo."); + console.log(""); + return; + } + var manager = new ClientManager(); + var users = manager.getUsers(); + if (users.indexOf(name) !== -1) { + console.log(""); + console.log("User '" + name + "' already exists."); + console.log(""); + return; + } + require("read")({ + prompt: "Password: " + }, function(err, password) { + console.log(""); + if (err) { + return; + } + var success = manager.addUser( + name, + password + ); + console.log("Added '" + name + "'."); + console.log(""); + }); + }); diff --git a/src/cli/index.js b/src/cli/index.js new file mode 100644 index 00000000..325b6aa0 --- /dev/null +++ b/src/cli/index.js @@ -0,0 +1,14 @@ +var program = require("commander"); + +require("./start"); +require("./list"); +require("./add"); +require("./remove"); + +program + .option("-p, --port ") + .parse(process.argv); + +if (!program.args.length) { + program.parse(process.argv.concat("start")); +} diff --git a/src/cli/list.js b/src/cli/list.js new file mode 100644 index 00000000..d83e3518 --- /dev/null +++ b/src/cli/list.js @@ -0,0 +1,21 @@ +var ClientManager = new require("../clientManager"); +var program = require("commander"); + +program + .command("list") + .description("List all existing users") + .action(function() { + var users = new ClientManager().getUsers(); + if (!users.length) { + console.log(""); + console.log("No users found!"); + console.log(""); + } else { + console.log(""); + console.log("Users:"); + for (var i = 0; i < users.length; i++) { + console.log(" " + (i + 1) + ". " + users[i]); + } + console.log(""); + } + }); diff --git a/src/cli/remove.js b/src/cli/remove.js new file mode 100644 index 00000000..54bc3771 --- /dev/null +++ b/src/cli/remove.js @@ -0,0 +1,32 @@ +var ClientManager = new require("../clientManager"); +var fs = require("fs"); +var program = require("commander"); + +program + .command("remove ") + .description("Remove an existing user") + .action(function(name) { + try { + var path = process.cwd() + "/users"; + var test = path + "/.test"; + fs.mkdirSync(test); + fs.rmdirSync(test); + } catch (e) { + console.log(""); + console.log("You have no permissions to delete from " + path); + console.log("Try running the command as sudo."); + console.log(""); + return; + } + var manager = new ClientManager(); + var success = manager.removeUser(name); + if (success) { + console.log(""); + console.log("Removed '" + name + "'."); + console.log(""); + } else { + console.log(""); + console.log("User '" + name + "' doesn't exist."); + console.log(""); + } + }); diff --git a/src/cli/start.js b/src/cli/start.js new file mode 100644 index 00000000..d14a732b --- /dev/null +++ b/src/cli/start.js @@ -0,0 +1,20 @@ +var config = require("../../config.json"); +var ClientManager = new require("../clientManager"); +var program = require("commander"); +var shout = require("../server"); + +program + .command("start") + .description("Start the server") + .action(function() { + var users = new ClientManager().getUsers(); + if (!config.public && !users.length) { + console.log(""); + console.log("No users found!"); + console.log("Create a new user with 'shout add-user '.") + console.log(""); + } else { + var port = program.port || config.port; + shout(port, config.public); + } + });