Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App: create default config file in launch script #3630

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

/*********************************************************
Expand All @@ -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) => {
Expand All @@ -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}`);
}
});
}
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(1);
}

// 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