mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-03-21 17:25:10 -05:00
CLI: Update to use util.parseArgs (#11245)
Some checks are pending
Node.js CI / build (18.x) (push) Waiting to run
Some checks are pending
Node.js CI / build (18.x) (push) Waiting to run
--------- Co-authored-by: Guangcong Luo <guangcongluo@gmail.com>
This commit is contained in:
parent
38b0d9475e
commit
19d279c9b5
15
build
15
build
|
|
@ -2,10 +2,12 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// technically this was introduced in Node 17, but we'll ask for the most recent LTS with it to be safe
|
// fetch was introduced in Node 18, which is EOL,
|
||||||
structuredClone({});
|
// so we'll ask for the most recent "Active LTS" with it to be safe
|
||||||
|
// https://nodejs.org/en/about/previous-releases
|
||||||
|
fetch;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("We require Node.js version 18 or later; you're using " + process.version);
|
console.error("We require Node.js version 22 or later; you're using " + process.version);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,13 +24,10 @@ function shell(cmd) {
|
||||||
|
|
||||||
// Check to make sure the most recently added or updated dependency is installed at the correct version
|
// Check to make sure the most recently added or updated dependency is installed at the correct version
|
||||||
try {
|
try {
|
||||||
var version = require('esbuild').version.split('.');
|
require.resolve('ts-chacha20');
|
||||||
if (parseInt(version[1]) < 16) {
|
|
||||||
throw new Error("esbuild version too old");
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Installing dependencies...');
|
console.log('Installing dependencies...');
|
||||||
shell('npm install');
|
shell('npm ci');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure config.js exists. If not, copy it over synchronously from
|
// Make sure config.js exists. If not, copy it over synchronously from
|
||||||
|
|
|
||||||
198
pokemon-showdown
198
pokemon-showdown
|
|
@ -22,8 +22,11 @@
|
||||||
// on the side of caution and run `node build` to ensure we're always running
|
// on the side of caution and run `node build` to ensure we're always running
|
||||||
// with the latest code.
|
// with the latest code.
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
var util = require('util');
|
||||||
|
var cli = null;
|
||||||
|
|
||||||
function build() {
|
function build() {
|
||||||
if (process.argv.includes('--skip-build')) return;
|
if (cli.values['skip-build']) return;
|
||||||
require('child_process').execSync('node build', {stdio: 'inherit', cwd: __dirname});
|
require('child_process').execSync('node build', {stdio: 'inherit', cwd: __dirname});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,65 +43,116 @@ function ensureBuilt() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
|
function showHelp() {
|
||||||
|
console.log('pokemon-showdown start [--skip-build] [PORT]');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Starts a PS server on the specified port');
|
||||||
|
console.log(' (Defaults to the port setting in config/config.js)');
|
||||||
|
console.log(' (The port setting in config/config.js defaults to 8000)');
|
||||||
|
console.log('');
|
||||||
|
console.log('pokemon-showdown generate-team [FORMAT-ID] [RANDOM-SEED]');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Generates a random team, and writes it to stdout in packed team format');
|
||||||
|
console.log(' (Format defaults to the latest Random Battles format)');
|
||||||
|
console.log('');
|
||||||
|
console.log('pokemon-showdown validate-team [FORMAT-ID]');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Reads a team from stdin, and validates it');
|
||||||
|
console.log(' If valid: exits with code 0');
|
||||||
|
console.log(' If invalid: writes errors to stderr, exits with code 1');
|
||||||
|
console.log('');
|
||||||
|
console.log('pokemon-showdown simulate-battle');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Simulates a battle, taking input to stdin and writing output to stdout');
|
||||||
|
console.log(' Protocol is documented in ./dist/sim/README.md');
|
||||||
|
console.log('');
|
||||||
|
console.log('pokemon-showdown json-team');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Reads a team in any format from stdin, writes the unpacked JSON to stdout');
|
||||||
|
console.log('');
|
||||||
|
console.log('pokemon-showdown pack-team');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Reads a team in any format from stdin, writes the packed team to stdout');
|
||||||
|
console.log('');
|
||||||
|
console.log('pokemon-showdown export-team');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Reads a team in any format from stdin, writes the exported (human-readable) team to stdout');
|
||||||
|
console.log('');
|
||||||
|
console.log('pokemon-showdown help');
|
||||||
|
console.log('');
|
||||||
|
console.log(' Displays this reference');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fetch;
|
||||||
|
} catch (e) {
|
||||||
|
// fetch was introduced in Node 18, which is EOL,
|
||||||
|
// so we'll ask for the most recent "Active LTS" with it to be safe
|
||||||
|
// https://nodejs.org/en/about/previous-releases
|
||||||
|
console.error("We require Node.js version 22 or later; you're using " + process.version);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var cliOpts = {
|
||||||
|
help: {
|
||||||
|
type: 'boolean',
|
||||||
|
short: 'h',
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
'skip-build': {
|
||||||
|
type: 'boolean',
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
debug: {
|
||||||
|
type: 'boolean',
|
||||||
|
short: 'D',
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
replay: {
|
||||||
|
type: 'boolean',
|
||||||
|
short: 'R',
|
||||||
|
default: false
|
||||||
|
},
|
||||||
|
spectate: {
|
||||||
|
type: 'boolean',
|
||||||
|
short: 'S',
|
||||||
|
default: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var parserOpts = {
|
||||||
|
options: cliOpts,
|
||||||
|
strict: false,
|
||||||
|
allowPositionals: true,
|
||||||
|
allowNegative: false
|
||||||
|
};
|
||||||
|
|
||||||
|
cli = util.parseArgs(parserOpts);
|
||||||
|
|
||||||
|
if (cli.values['help']) {
|
||||||
|
showHelp();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cli.positionals.length || /^[0-9]+$/.test(cli.positionals[0])) {
|
||||||
// Start the server.
|
// Start the server.
|
||||||
//
|
//
|
||||||
// The port the server should host on can be passed using the second argument
|
// The port the server should host on can be passed as an argument, e.g. to
|
||||||
// when launching with this file the same way app.js normally allows, e.g. to
|
|
||||||
// host on port 9000:
|
// host on port 9000:
|
||||||
// $ ./pokemon-showdown 9000
|
// $ ./pokemon-showdown 9000
|
||||||
build();
|
build();
|
||||||
require('module')._load('./dist/server/index.js', module, true);
|
require('module')._load('./dist/server/index.js', module, true);
|
||||||
} else {
|
} else {
|
||||||
switch (process.argv[2]) {
|
switch (cli.positionals[0]) {
|
||||||
case 'help':
|
case 'help':
|
||||||
case 'h':
|
case 'h':
|
||||||
case '?':
|
case '?':
|
||||||
case '-h':
|
|
||||||
case '--help':
|
|
||||||
case '-?':
|
|
||||||
console.log('pokemon-showdown start [--skip-build] [PORT]');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Starts a PS server on the specified port');
|
|
||||||
console.log(' (Defaults to the port setting in config/config.js)');
|
|
||||||
console.log(' (The port setting in config/config.js defaults to 8000)');
|
|
||||||
console.log('');
|
|
||||||
console.log('pokemon-showdown generate-team [FORMAT-ID [RANDOM-SEED]]');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Generates a random team, and writes it to stdout in packed team format');
|
|
||||||
console.log(' (Format defaults to the latest Random Battles format)');
|
|
||||||
console.log('');
|
|
||||||
console.log('pokemon-showdown validate-team [FORMAT-ID]');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Reads a team from stdin, and validates it');
|
|
||||||
console.log(' If valid: exits with code 0');
|
|
||||||
console.log(' If invalid: writes errors to stderr, exits with code 1');
|
|
||||||
console.log('');
|
|
||||||
console.log('pokemon-showdown simulate-battle');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Simulates a battle, taking input to stdin and writing output to stdout');
|
|
||||||
console.log(' Protocol is documented in ./dist/sim/README.md');
|
|
||||||
console.log('');
|
|
||||||
console.log('pokemon-showdown json-team');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Reads a team in any format from stdin, writes the unpacked JSON to stdout');
|
|
||||||
console.log('');
|
|
||||||
console.log('pokemon-showdown pack-team');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Reads a team in any format from stdin, writes the packed team to stdout');
|
|
||||||
console.log('');
|
|
||||||
console.log('pokemon-showdown export-team');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Reads a team in any format from stdin, writes the exported (human-readable) team to stdout');
|
|
||||||
console.log('');
|
|
||||||
console.log('pokemon-showdown help');
|
|
||||||
console.log('');
|
|
||||||
console.log(' Displays this reference');
|
|
||||||
break;
|
|
||||||
case 'start':
|
|
||||||
case '--skip-build':
|
|
||||||
{
|
{
|
||||||
process.argv.splice(2, 1);
|
showHelp();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'start':
|
||||||
|
{
|
||||||
build();
|
build();
|
||||||
require('module')._load('./dist/server/index.js', module, true);
|
require('module')._load('./dist/server/index.js', module, true);
|
||||||
break;
|
break;
|
||||||
|
|
@ -107,8 +161,8 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
|
||||||
{
|
{
|
||||||
ensureBuilt();
|
ensureBuilt();
|
||||||
var Teams = require('./dist/sim/teams.js').Teams;
|
var Teams = require('./dist/sim/teams.js').Teams;
|
||||||
var seed = process.argv[4] || undefined;
|
var seed = cli.positionals[2] || undefined;
|
||||||
console.log(Teams.pack(Teams.generate(process.argv[3], {seed})));
|
console.log(Teams.pack(Teams.generate(cli.positionals[1], {seed})));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'validate-team':
|
case 'validate-team':
|
||||||
|
|
@ -117,7 +171,7 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
|
||||||
require('source-map-support/register');
|
require('source-map-support/register');
|
||||||
var Teams = require('./dist/sim/teams.js').Teams;
|
var Teams = require('./dist/sim/teams.js').Teams;
|
||||||
var TeamValidator = require('./dist/sim/team-validator.js').TeamValidator;
|
var TeamValidator = require('./dist/sim/team-validator.js').TeamValidator;
|
||||||
var validator = TeamValidator.get(process.argv[3]);
|
var validator = TeamValidator.get(cli.positionals[1]);
|
||||||
var Streams = require('./dist/lib/streams.js');
|
var Streams = require('./dist/lib/streams.js');
|
||||||
var stdin = Streams.stdin();
|
var stdin = Streams.stdin();
|
||||||
|
|
||||||
|
|
@ -145,44 +199,10 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
|
||||||
var stdin = Streams.stdin();
|
var stdin = Streams.stdin();
|
||||||
var stdout = Streams.stdout();
|
var stdout = Streams.stdout();
|
||||||
|
|
||||||
var args = process.argv.slice(3);
|
|
||||||
|
|
||||||
var options = args.flatMap(function (arg) {
|
|
||||||
if (arg.charAt(0) !== '-') {
|
|
||||||
if (arg) console.error("Invalid parameter: " + arg);
|
|
||||||
return [];
|
|
||||||
} else if (arg.charAt(1) === '-') {
|
|
||||||
return arg.slice(2);
|
|
||||||
} else {
|
|
||||||
return Array.from(arg.slice(1));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var debug = false;
|
|
||||||
var replay = false;
|
|
||||||
var spectate = false;
|
|
||||||
for (var i = 0; i < options.length; i++) {
|
|
||||||
switch (options[i]) {
|
|
||||||
case 'debug': case 'D':
|
|
||||||
debug = true;
|
|
||||||
break;
|
|
||||||
case 'replay': case 'R':
|
|
||||||
replay = true;
|
|
||||||
break;
|
|
||||||
case 'spectate': case 'spectator': case 'S':
|
|
||||||
replay = true;
|
|
||||||
spectate = true;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
console.error("Invalid option: " + options[i]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var battleStream = new BattleTextStream({
|
var battleStream = new BattleTextStream({
|
||||||
noCatch: true,
|
noCatch: true,
|
||||||
debug: debug,
|
debug: cli.values['debug'],
|
||||||
replay: spectate ? 'spectator' : replay,
|
replay: cli.values['spectate'] ? 'spectator' : cli.values['replay']
|
||||||
});
|
});
|
||||||
stdin.pipeTo(battleStream);
|
stdin.pipeTo(battleStream);
|
||||||
battleStream.pipeTo(stdout);
|
battleStream.pipeTo(stdout);
|
||||||
|
|
@ -247,7 +267,7 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('Unrecognized command: ' + process.argv[2]);
|
console.error('Unrecognized command: ' + cli.positionals[0]);
|
||||||
console.error('Use `pokemon-showdown help` for help');
|
console.error('Use `pokemon-showdown help` for help');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,12 +50,23 @@ try {
|
||||||
}
|
}
|
||||||
// NOTE: This file intentionally doesn't use too many modern JavaScript
|
// NOTE: This file intentionally doesn't use too many modern JavaScript
|
||||||
// features, so that it doesn't crash old versions of Node.js, so we
|
// features, so that it doesn't crash old versions of Node.js, so we
|
||||||
// can successfully print the "We require Node.js 18+" message.
|
// can successfully print the "We require Node.js 22+" message.
|
||||||
|
|
||||||
// Check for version
|
// I've gotten enough reports by people who don't use the launch
|
||||||
const nodeVersion = parseInt(process.versions.node);
|
// script that this is worth repeating here
|
||||||
if (isNaN(nodeVersion) || nodeVersion < 18) {
|
try {
|
||||||
throw new Error("We require Node.js version 18 or later; you're using " + process.version);
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||||
|
fetch;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error("We require Node.js version 22 or later; you're using " + process.version);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
require.resolve('ts-chacha20');
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error("Dependencies are unmet; run `npm ci` before launching Pokemon Showdown again.");
|
||||||
}
|
}
|
||||||
|
|
||||||
import { FS, Repl } from '../lib';
|
import { FS, Repl } from '../lib';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user