From 41e5090fb0ea22e3d39096527c02b240bafe4ae8 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 22 Oct 2019 15:00:05 +0300 Subject: [PATCH] Do not crash on first run due to config.js not existing --- src/command-line/index.js | 61 +++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/command-line/index.js b/src/command-line/index.js index 2d71f5a7..734e1e84 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -29,31 +29,11 @@ const argvWithoutOptions = program.parseOptions(process.argv); Helper.setHome(process.env.THELOUNGE_HOME || Utils.defaultHome()); // Check config file owner and warn if we're running under a different user -if (process.getuid) { - const uid = process.getuid(); - - if (uid === 0) { - log.warn( - `You are currently running The Lounge as root. ${colors.bold.red( - "We highly discourage running as root!" - )}` - ); - } - - const configStat = fs.statSync(path.join(Helper.getHomePath(), "config.js")); - - if (configStat && configStat.uid !== uid) { - log.warn( - "Config file owner does not match the user you are currently running The Lounge as." - ); - log.warn( - "To prevent any issues, please run thelounge commands " + - "as the correct user that owns the config folder." - ); - log.warn( - "See https://thelounge.chat/docs/usage#using-the-correct-system-user for more information." - ); - } +try { + verifyFileOwner(); +} catch (e) { + // We do not care about failures of these checks + // fs.statSync will throw if config.js does not exist (e.g. first run) } Utils.checkOldHome(); @@ -83,3 +63,34 @@ program.parse(argvWithoutOptions.args.concat(argvWithoutOptions.unknown)); if (!program.args.length) { program.help(); } + +function verifyFileOwner() { + if (!process.getuid) { + return; + } + + const uid = process.getuid(); + + if (uid === 0) { + log.warn( + `You are currently running The Lounge as root. ${colors.bold.red( + "We highly discourage running as root!" + )}` + ); + } + + const configStat = fs.statSync(path.join(Helper.getHomePath(), "config.js")); + + if (configStat && configStat.uid !== uid) { + log.warn( + "Config file owner does not match the user you are currently running The Lounge as." + ); + log.warn( + "To prevent any issues, please run thelounge commands " + + "as the correct user that owns the config folder." + ); + log.warn( + "See https://thelounge.chat/docs/usage#using-the-correct-system-user for more information." + ); + } +}