Skip to content

Commit

Permalink
Remove .js extension from requires (#2725)
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor1791 authored and Zarel committed Aug 30, 2016
1 parent 26d67ee commit 1a1f64f
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 100 deletions.
41 changes: 21 additions & 20 deletions app.js
Expand Up @@ -71,7 +71,7 @@ try {
*********************************************************/

try {
require.resolve('./config/config.js');
require.resolve('./config/config');
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err; // should never happen

Expand All @@ -81,15 +81,16 @@ try {
fs.readFileSync(path.resolve(__dirname, 'config/config-example.js'))
);
} finally {
global.Config = require('./config/config.js');
global.Config = require('./config/config');
}

if (Config.watchconfig) {
fs.watchFile(path.resolve(__dirname, 'config/config.js'), (curr, prev) => {
let configPath = require.resolve(__dirname, 'config/config');

This comment has been minimized.

Copy link
@Zarel

Zarel Sep 10, 2016

Member

Well, this is a hard-to-find bug.

require.resolve does something completely different from path.resolve. require.resolve only accepts one argument, so this is actually equivalent to

 let configPath = require.resolve(__dirname);

Which is the path to app.js, not the path to config/config.js as intended.

This comment has been minimized.

Copy link
@Zarel

Zarel Sep 10, 2016

Member

@taylor1791, not blaming you, but I'm highlighting you so you know for if you ever do this sort of thing again.

This comment has been minimized.

Copy link
@taylor1791

taylor1791 Sep 10, 2016

Author Contributor

Thank you. I appreciate you taking the time and letting me know.

fs.watchFile(configPath, (curr, prev) => {
if (curr.mtime <= prev.mtime) return;
try {
delete require.cache[require.resolve('./config/config.js')];
global.Config = require('./config/config.js');
delete require.cache[configPath];
global.Config = require('./config/config');
if (global.Users) Users.cacheGroupData();
console.log('Reloaded config/config.js');
} catch (e) {
Expand All @@ -102,38 +103,38 @@ if (Config.watchconfig) {
* Set up most of our globals
*********************************************************/

global.Monitor = require('./monitor.js');
global.Monitor = require('./monitor');

global.Tools = require('./tools.js');
global.Tools = require('./tools');
global.toId = Tools.getId;

global.LoginServer = require('./loginserver.js');
global.LoginServer = require('./loginserver');

global.Ladders = require(Config.remoteladder ? './ladders-remote.js' : './ladders.js');
global.Ladders = require(Config.remoteladder ? './ladders-remote' : './ladders');

global.Users = require('./users.js');
global.Users = require('./users');

global.Punishments = require('./punishments.js');
global.Punishments = require('./punishments');

global.Rooms = require('./rooms.js');
global.Rooms = require('./rooms');

delete process.send; // in case we're a child process
global.Verifier = require('./verifier.js');
global.Verifier = require('./verifier');
Verifier.PM.spawn();

global.CommandParser = require('./command-parser.js');
global.CommandParser = require('./command-parser');

global.Simulator = require('./simulator.js');
global.Simulator = require('./simulator');

global.Tournaments = require('./tournaments');

global.Dnsbl = require('./dnsbl.js');
global.Dnsbl = require('./dnsbl');
Dnsbl.loadDatacenters();

if (Config.crashguard) {
// graceful crash - allow current battles to finish before restarting
process.on('uncaughtException', err => {
let crashMessage = require('./crashlogger.js')(err, 'The main process');
let crashMessage = require('./crashlogger')(err, 'The main process');
if (crashMessage !== 'lockdown') return;
let stack = Tools.escapeHTML(err.stack).split("\n").slice(0, 2).join("<br />");
if (Rooms.lobby) {
Expand All @@ -152,7 +153,7 @@ if (Config.crashguard) {
* Start networking processes to be connected to
*********************************************************/

global.Sockets = require('./sockets.js');
global.Sockets = require('./sockets');

exports.listen = function (port, bindAddress, workerCount) {
Sockets.listen(port, bindAddress, workerCount);
Expand All @@ -176,11 +177,11 @@ if (require.main === module) {
Tools.includeFormats();
Rooms.global.formatListText = Rooms.global.getFormatListText();

global.TeamValidator = require('./team-validator.js');
global.TeamValidator = require('./team-validator');
TeamValidator.PM.spawn();

/*********************************************************
* Start up the REPL server
*********************************************************/

require('./repl.js').start('app', cmd => eval(cmd));
require('./repl').start('app', cmd => eval(cmd));
2 changes: 1 addition & 1 deletion battle-engine.js
Expand Up @@ -4938,7 +4938,7 @@ Battle = (() => {
let side = this[slot];
if (!side) {
console.log('**** ' + slot + ' tried to leave before it was possible in ' + this.id);
require('./crashlogger.js')(new Error('**** ' + slot + ' tried to leave before it was possible in ' + this.id), 'A simulator process');
require('./crashlogger')(new Error('**** ' + slot + ' tried to leave before it was possible in ' + this.id), 'A simulator process');
return;
}

Expand Down
12 changes: 6 additions & 6 deletions chat-plugins/datasearch.js
Expand Up @@ -61,7 +61,7 @@ const PM = exports.PM = new ProcessManager({
result = null;
}
} catch (err) {
require('./../crashlogger.js')(err, 'A search query', data);
require('./../crashlogger')(err, 'A search query', data);
result = {error: "Sorry! Our search engine crashed on your query. We've been automatically notified and will fix this crash."};
}
return result;
Expand Down Expand Up @@ -242,24 +242,24 @@ exports.commands = {
if (process.send && module === process.mainModule) {
// This is a child process!

global.Config = require('../config/config.js');
global.Config = require('../config/config');

if (Config.crashguard) {
process.on('uncaughtException', err => {
require('../crashlogger.js')(err, 'A dexsearch process', true);
require('../crashlogger')(err, 'A dexsearch process', true);
});
}

global.Tools = require('../tools.js');
global.Tools = require('../tools');
global.toId = Tools.getId;
Tools.includeData();
Tools.includeMods();
global.TeamValidator = require('../team-validator.js');
global.TeamValidator = require('../team-validator');

process.on('message', message => PM.onMessageDownstream(message));
process.on('disconnect', () => process.exit());

require('../repl.js').start('dexsearch', cmd => eval(cmd));
require('../repl').start('dexsearch', cmd => eval(cmd));
} else if (!PM.maxProcesses) {
process.nextTick(() => Tools.includeMods());
}
Expand Down
2 changes: 1 addition & 1 deletion chat-plugins/mafia.js
Expand Up @@ -3,7 +3,7 @@

'use strict';

let MafiaData = require('./mafia-data.js');
let MafiaData = require('./mafia-data');

const deadImage = '<img width="75" height="75" src="//play.pokemonshowdown.com/fx/mafia-dead.png" />';
const meetingMsg = {town: 'The town has lynched a suspect!', mafia: 'The mafia strikes again!'};
Expand Down
6 changes: 3 additions & 3 deletions command-parser.js
Expand Up @@ -60,13 +60,13 @@ exports.multiLinePattern = {
* Load command files
*********************************************************/

let baseCommands = exports.baseCommands = require('./commands.js').commands;
let baseCommands = exports.baseCommands = require('./commands').commands;
let commands = exports.commands = Object.assign({}, baseCommands);

// Install plug-in commands

// info always goes first so other plugins can shadow it
Object.assign(commands, require('./chat-plugins/info.js').commands);
Object.assign(commands, require('./chat-plugins/info').commands);

for (let file of fs.readdirSync(path.resolve(__dirname, 'chat-plugins'))) {
if (file.substr(-3) !== '.js' || file === 'info.js') continue;
Expand Down Expand Up @@ -283,7 +283,7 @@ class CommandContext {
try {
result = commandHandler.call(this, this.target, this.room, this.user, this.connection, this.cmd, this.message);
} catch (err) {
if (require('./crashlogger.js')(err, 'A chat command', {
if (require('./crashlogger')(err, 'A chat command', {
user: this.user.name,
room: this.room.id,
message: this.message,
Expand Down
20 changes: 10 additions & 10 deletions commands.js
Expand Up @@ -2501,10 +2501,10 @@ exports.commands = {
}
}

CommandParser.uncacheTree('./command-parser.js');
delete require.cache[require.resolve('./commands.js')];
delete require.cache[require.resolve('./chat-plugins/info.js')];
global.CommandParser = require('./command-parser.js');
CommandParser.uncacheTree('./command-parser');
delete require.cache[require.resolve('./commands')];
delete require.cache[require.resolve('./chat-plugins/info')];
global.CommandParser = require('./command-parser');

let runningTournaments = Tournaments.tournaments;
CommandParser.uncacheTree('./tournaments');
Expand All @@ -2524,9 +2524,9 @@ exports.commands = {
} else if (target === 'formats') {
let toolsLoaded = !!Tools.isLoaded;
// uncache the tools.js dependency tree
CommandParser.uncacheTree('./tools.js');
CommandParser.uncacheTree('./tools');
// reload tools.js
global.Tools = require('./tools.js')[toolsLoaded ? 'includeData' : 'includeFormats'](); // note: this will lock up the server for a few seconds
global.Tools = require('./tools')[toolsLoaded ? 'includeData' : 'includeFormats'](); // note: this will lock up the server for a few seconds
// rebuild the formats list
Rooms.global.formatListText = Rooms.global.getFormatListText();
// respawn validator processes
Expand All @@ -2539,15 +2539,15 @@ exports.commands = {
return this.sendReply("Formats have been hotpatched.");
} else if (target === 'loginserver') {
fs.unwatchFile('./config/custom.css');
CommandParser.uncacheTree('./loginserver.js');
global.LoginServer = require('./loginserver.js');
CommandParser.uncacheTree('./loginserver');
global.LoginServer = require('./loginserver');
return this.sendReply("The login server has been hotpatched. New login server requests will use the new code.");
} else if (target === 'learnsets' || target === 'validator') {
TeamValidator.PM.respawn();
return this.sendReply("The team validator has been hotpatched. Any battles started after now will have teams be validated according to the new code.");
} else if (target === 'punishments') {
delete require.cache[require.resolve('./punishments.js')];
global.Punishments = require('./punishments.js');
delete require.cache[require.resolve('./punishments')];
global.Punishments = require('./punishments');
return this.sendReply("Punishments have been hotpatched.");
} else if (target === 'dnsbl') {
Dnsbl.loadDatacenters();
Expand Down
4 changes: 2 additions & 2 deletions data/scripts.js
Expand Up @@ -1159,7 +1159,7 @@ exports.BattleScripts = {
template = this.getTemplate('unown');

let err = new Error('Template incompatible with random battles: ' + species);
require('../crashlogger.js')(err, 'The randbat set generator');
require('../crashlogger')(err, 'The randbat set generator');
}

if (typeof teamDetails !== 'object') teamDetails = {megaCount: teamDetails};
Expand Down Expand Up @@ -2433,7 +2433,7 @@ exports.BattleScripts = {
template = this.getTemplate('unown');

let err = new Error('Template incompatible with random battles: ' + species);
require('../crashlogger.js')(err, 'The doubles randbat set generator');
require('../crashlogger')(err, 'The doubles randbat set generator');
}

if (typeof teamDetails !== 'object') teamDetails = {megaCount: teamDetails};
Expand Down
2 changes: 1 addition & 1 deletion mods/gen5/scripts.js
Expand Up @@ -11,7 +11,7 @@ exports.BattleScripts = {
template = this.getTemplate('unown');

let err = new Error('Template incompatible with random battles: ' + name);
require('./../../crashlogger.js')(err, 'The randbat set generator');
require('./../../crashlogger')(err, 'The randbat set generator');
}

if (template.battleOnly) name = template.baseSpecies;
Expand Down
2 changes: 1 addition & 1 deletion pokemon-showdown
Expand Up @@ -18,4 +18,4 @@ let port;
if (process.argv[2]) {
port = parseInt(process.argv[2]); // eslint-disable-line radix
}
require('./app.js').listen(port);
require('./app').listen(port);
4 changes: 2 additions & 2 deletions repl.js
Expand Up @@ -75,7 +75,7 @@ exports.start = function (prefix, suffix, evalFunction) {
if (e.code === "EADDRINUSE") {
fs.unlink(name, e => {
if (e && e.code !== "ENOENT") {
require('./crashlogger.js')(e, 'REPL: ' + name);
require('./crashlogger')(e, 'REPL: ' + name);
return;
}

Expand All @@ -84,6 +84,6 @@ exports.start = function (prefix, suffix, evalFunction) {
return;
}

require('./crashlogger.js')(e, 'REPL: ' + name);
require('./crashlogger')(e, 'REPL: ' + name);
});
};
6 changes: 3 additions & 3 deletions rooms.js
Expand Up @@ -586,7 +586,7 @@ let GlobalRoom = (() => {
};
GlobalRoom.prototype.matchmakingOK = function (search1, search2, user1, user2, formatid) {
// This should never happen.
if (!user1 || !user2) return void require('./crashlogger.js')(new Error("Matched user " + (user1 ? search2.userid : search1.userid) + " not found"), "The main process");
if (!user1 || !user2) return void require('./crashlogger')(new Error("Matched user " + (user1 ? search2.userid : search1.userid) + " not found"), "The main process");

// users must be different
if (user1 === user2) return false;
Expand Down Expand Up @@ -1699,8 +1699,8 @@ Rooms.GlobalRoom = GlobalRoom;
Rooms.BattleRoom = BattleRoom;
Rooms.ChatRoom = ChatRoom;

Rooms.RoomGame = require('./room-game.js').RoomGame;
Rooms.RoomGamePlayer = require('./room-game.js').RoomGamePlayer;
Rooms.RoomGame = require('./room-game').RoomGame;
Rooms.RoomGamePlayer = require('./room-game').RoomGamePlayer;

// initialize

Expand Down
16 changes: 8 additions & 8 deletions simulator.js
Expand Up @@ -13,14 +13,14 @@

'use strict';

global.Config = require('./config/config.js');
global.Config = require('./config/config');

const ProcessManager = require('./process-manager');
const BattleEngine = require('./battle-engine').Battle;

const SimulatorProcess = new ProcessManager({
maxProcesses: Config.simulatorprocesses,
execFile: 'simulator.js',
execFile: 'simulator',
onMessageUpstream: function (message) {
let lines = message.split('\n');
let battle = this.pendingTasks.get(lines[0]);
Expand Down Expand Up @@ -450,17 +450,17 @@ exports.create = function (id, format, rated, room) {
if (process.send && module === process.mainModule) {
// This is a child process!

global.Tools = require('./tools.js').includeMods();
global.Tools = require('./tools').includeMods();
global.toId = Tools.getId;

if (Config.crashguard) {
// graceful crash - allow current battles to finish before restarting
process.on('uncaughtException', err => {
require('./crashlogger.js')(err, 'A simulator process');
require('./crashlogger')(err, 'A simulator process');
});
}

require('./repl.js').start('battle-engine-', process.pid, cmd => eval(cmd));
require('./repl').start('battle-engine-', process.pid, cmd => eval(cmd));

let Battles = new Map();

Expand All @@ -481,7 +481,7 @@ if (process.send && module === process.mainModule) {
try {
Battles.set(id, BattleEngine.construct(id, data[2], data[3], sendBattleMessage));
} catch (err) {
if (require('./crashlogger.js')(err, 'A battle', {
if (require('./crashlogger')(err, 'A battle', {
message: message,
}) === 'lockdown') {
let ministack = Tools.escapeHTML(err.stack).split("\n").slice(0, 2).join("<br />");
Expand All @@ -499,7 +499,7 @@ if (process.send && module === process.mainModule) {
// remove from battle list
Battles.delete(id);
} else {
require('./crashlogger.js')(new Error("Invalid dealloc"), 'A battle', {
require('./crashlogger')(new Error("Invalid dealloc"), 'A battle', {
message: message,
});
}
Expand All @@ -511,7 +511,7 @@ if (process.send && module === process.mainModule) {
try {
battle.receive(data, more);
} catch (err) {
require('./crashlogger.js')(err, 'A battle', {
require('./crashlogger')(err, 'A battle', {
message: message,
currentRequest: prevRequest,
log: '\n' + battle.log.join('\n').replace(/\n\|split\n[^\n]*\n[^\n]*\n[^\n]*\n/g, '\n'),
Expand Down

0 comments on commit 1a1f64f

Please sign in to comment.