From 59a154919fff1304229a03244719e82924eacc10 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Tue, 13 Jun 2017 07:01:58 -0300 Subject: [PATCH] App: create default config file in launch script --- app.js | 16 +++++----------- pokemon-showdown | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index 228a37448629..1f90c6ec767b 100644 --- a/app.js +++ b/app.js @@ -43,13 +43,12 @@ 'use strict'; const fs = require('fs'); -const path = require('path'); // Check for dependencies try { require.resolve('sockjs'); } catch (e) { - throw new Error("Dependencies unmet; run npm install"); + throw new Error('Dependencies are unmet; run node pokemon-showdown before launching Pokemon Showdown again.'); } /********************************************************* @@ -60,16 +59,11 @@ try { require.resolve('./config/config'); } catch (err) { if (err.code !== 'MODULE_NOT_FOUND') throw err; // should never happen - - // Copy it over synchronously from config-example.js since it's needed before we can start the server - console.log("config.js doesn't exist - creating one with default settings..."); - fs.writeFileSync(path.resolve(__dirname, 'config/config.js'), - fs.readFileSync(path.resolve(__dirname, 'config/config-example.js')) - ); -} finally { - global.Config = require('./config/config'); + throw new Error('config.js does not exist; run node pokemon-showdown to set up the default config file before launching Pokemon Showdown again.'); } +global.Config = require('./config/config'); + if (Config.watchconfig) { let configPath = require.resolve('./config/config'); fs.watchFile(configPath, (curr, prev) => { @@ -80,7 +74,7 @@ if (Config.watchconfig) { if (global.Users) Users.cacheGroupData(); console.log('Reloaded config/config.js'); } catch (e) { - console.log('Error reloading config/config.js: ' + e.stack); + console.error(`Error reloading config/config.js: ${e.stack}`); } }); } diff --git a/pokemon-showdown b/pokemon-showdown index 553b327e0c5d..220fe557d6ca 100755 --- a/pokemon-showdown +++ b/pokemon-showdown @@ -2,6 +2,8 @@ 'use strict'; const child_process = require('child_process'); +const fs = require('fs'); +const path = require('path'); // Make sure we're Node 6+ @@ -9,7 +11,7 @@ try { eval('{ let [a] = [1]; }'); } catch (e) { console.log("We require Node.js v6 or later; you're using " + process.version); - process.exit(); + process.exit(1); } // Make sure our dependencies are available, and install them if they @@ -22,6 +24,21 @@ try { child_process.execSync('npm install --production', {stdio: 'inherit'}); } +// Make sure config.js exists. If not, copy it over synchronously from +// config-example.js, since it's needed before we can start the server + +try { + require.resolve('./config/config'); +} catch (err) { + if (err.code !== 'MODULE_NOT_FOUND') throw err; // should never happen + + console.log('config.js does not exist. Creating one with default settings...'); + fs.writeFileSync( + path.resolve(__dirname, 'config/config.js'), + fs.readFileSync(path.resolve(__dirname, 'config/config-example.js')) + ); +} + // Start the server. We manually load app.js so it can be configured to run as // the main module, rather than this file being considered the main module. // This ensures any dependencies that were just installed can be found when