From 252dca3d3fce691d93e65b57bf095488c4e3e6e8 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 18 Jul 2017 12:36:24 +0300 Subject: [PATCH] Only create config folder in 'start' command Fixes #520. Fixes #1288. --- src/command-line/add.js | 15 +++++++++++---- src/command-line/config.js | 14 ++++++++++---- src/command-line/edit.js | 17 ++++++++++++----- src/command-line/index.js | 24 +++--------------------- src/command-line/list.js | 13 ++++++++++--- src/command-line/remove.js | 13 ++++++++++--- src/command-line/reset.js | 16 +++++++++++----- src/command-line/start.js | 30 +++++++++++++++++++++++++++--- 8 files changed, 94 insertions(+), 48 deletions(-) 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..c6dc732f 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.`); } 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); +}