diff --git a/app.js b/app.js index 228a374486293..19c46e12fca93 100644 --- a/app.js +++ b/app.js @@ -42,14 +42,11 @@ '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 or npm install before launching Pokemon Showdown again.'); } /********************************************************* @@ -60,19 +57,14 @@ 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 or copy config/config-example.js to config/config.js before launching Pokemon Showdown again.'); } +global.Config = require('./config/config'); + if (Config.watchconfig) { let configPath = require.resolve('./config/config'); - fs.watchFile(configPath, (curr, prev) => { + require('fs').watchFile(configPath, (curr, prev) => { if (curr.mtime <= prev.mtime) return; try { delete require.cache[configPath]; @@ -80,7 +72,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 553b327e0c5d5..a617d7f11ad57 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(0); } // 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