Skip to content

Commit

Permalink
App: create default config file in launch script
Browse files Browse the repository at this point in the history
  • Loading branch information
Morfent committed Jun 16, 2017
1 parent 846d250 commit 84d8915
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
20 changes: 6 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 before launching Pokemon Showdown again.');
}

/*********************************************************
Expand All @@ -60,27 +57,22 @@ 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) => {
require('fs').watchFile(configPath, (curr, prev) => {
if (curr.mtime <= prev.mtime) return;
try {
delete require.cache[configPath];
global.Config = require('./config/config');
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}`);
}
});
}
Expand Down
19 changes: 18 additions & 1 deletion pokemon-showdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
'use strict';

const child_process = require('child_process');
const fs = require('fs');
const path = require('path');

// Make sure we're Node 6+

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(128 + 69); // EX_UNAVAILABLE
}

// Make sure our dependencies are available, and install them if they
Expand All @@ -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
Expand Down

0 comments on commit 84d8915

Please sign in to comment.