mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
Replace inquirer 8 with the @inquirer prompt packages in create
inquirer 8 pulls in 50 packages and 7.4 MB (rxjs, old chalk) for every user running npm init @clusterio. The standalone select, input, confirm and checkbox packages from the same project cover all prompts used and cut the create install from 70 packages / 8.1 MB to 31 / 0.94 MB. The Windows rl.close workaround now patches readline.Interface instead of inquirer's Prompt class, since the new prompts create their own readline interface. Ctrl+C at a prompt exits with code 130 instead of 0, and closed stdin no longer crashes with ERR_USE_AFTER_CLOSE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3abcba5991
commit
d2a4e9a4a1
2 files changed
+111
-162
No files matched your search
+107
-161
@@ -1,10 +1,14 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
import checkbox from "@inquirer/checkbox";
|
||||||
|
import confirm from "@inquirer/confirm";
|
||||||
|
import input from "@inquirer/input";
|
||||||
|
import select from "@inquirer/select";
|
||||||
import child_process from "node:child_process";
|
import child_process from "node:child_process";
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import inquirer from "inquirer";
|
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
import readline from "node:readline";
|
||||||
import stream from "node:stream";
|
import stream from "node:stream";
|
||||||
import util from "node:util";
|
import util from "node:util";
|
||||||
import yargs from "yargs";
|
import yargs from "yargs";
|
||||||
@@ -16,15 +20,13 @@ const scriptExt = process.platform === "win32" ? ".cmd" : "";
|
|||||||
import { escapeArg } from "./escape_arg.js";
|
import { escapeArg } from "./escape_arg.js";
|
||||||
|
|
||||||
// I hate this, there is a bug with rl.close on windows that was meant to have been fixed in Node14.4
|
// I hate this, there is a bug with rl.close on windows that was meant to have been fixed in Node14.4
|
||||||
// See nodejs/node#21771 and nodejs/node#30701 for the apparent fix that was applied
|
// See nodejs/node#21771, nodejs/node#30701 and nodejs/node#31762
|
||||||
// One option is to not call rl.close: SBoudrias/Inquirer.js#767
|
|
||||||
// But this comes with its own issues: SBoudrias/Inquirer.js#899
|
|
||||||
// The solution below was adapted from aws-amplify/amplify-cli#12347
|
// The solution below was adapted from aws-amplify/amplify-cli#12347
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
const _inquirer_close = inquirer.ui.Prompt.prototype.close;
|
const close = readline.Interface.prototype.close;
|
||||||
inquirer.ui.Prompt.prototype.close = function() {
|
readline.Interface.prototype.close = function() {
|
||||||
this.rl.terminal = false;
|
this.terminal = false;
|
||||||
_inquirer_close.call(this);
|
close.call(this);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,23 +166,23 @@ async function execCtl(args) {
|
|||||||
// Same characters as the controller refuses in host names.
|
// Same characters as the controller refuses in host names.
|
||||||
const hostNameControlCharacters = /[\p{Cc}\u202a-\u202e\u2066-\u2069]/u;
|
const hostNameControlCharacters = /[\p{Cc}\u202a-\u202e\u2066-\u2069]/u;
|
||||||
|
|
||||||
function validateHostName(input) {
|
function validateHostName(value) {
|
||||||
if (!input || !input.trim()) {
|
if (!value || !value.trim()) {
|
||||||
return "Host name may not be empty";
|
return "Host name may not be empty";
|
||||||
}
|
}
|
||||||
if (input.trim().length > 100) {
|
if (value.trim().length > 100) {
|
||||||
return "Host name may be at most 100 characters";
|
return "Host name may be at most 100 characters";
|
||||||
}
|
}
|
||||||
if (hostNameControlCharacters.test(input)) {
|
if (hostNameControlCharacters.test(value)) {
|
||||||
return "Host name may not contain control characters";
|
return "Host name may not contain control characters";
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateControllerUrl(input) {
|
function validateControllerUrl(value) {
|
||||||
let url;
|
let url;
|
||||||
try {
|
try {
|
||||||
url = new URL(input);
|
url = new URL(value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return "Controller URL must be a URL like https://example.com/";
|
return "Controller URL must be a URL like https://example.com/";
|
||||||
}
|
}
|
||||||
@@ -191,8 +193,8 @@ function validateControllerUrl(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The host requires the controller URL to end with a slash.
|
// The host requires the controller URL to end with a slash.
|
||||||
function normaliseControllerUrl(input) {
|
function normaliseControllerUrl(value) {
|
||||||
const url = new URL(input);
|
const url = new URL(value);
|
||||||
if (!url.pathname.endsWith("/")) {
|
if (!url.pathname.endsWith("/")) {
|
||||||
url.pathname += "/";
|
url.pathname += "/";
|
||||||
}
|
}
|
||||||
@@ -537,61 +539,49 @@ WantedBy=multi-user.target
|
|||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line complexity
|
// eslint-disable-next-line complexity
|
||||||
async function inquirerMissingArgs(args) {
|
async function promptMissingArgs(args) {
|
||||||
let answers = {};
|
let answers = {};
|
||||||
if (args.mode) { answers.mode = args.mode; }
|
if (args.mode) { answers.mode = args.mode; }
|
||||||
if (args["plugin-template"]) { answers.mode = "plugin-template"; }
|
if (args["plugin-template"]) { answers.mode = "plugin-template"; }
|
||||||
answers = await inquirer.prompt([
|
answers.mode ??= await select({
|
||||||
{
|
message: "Operating mode to install",
|
||||||
type: "list",
|
default: "standalone",
|
||||||
name: "mode",
|
choices: [
|
||||||
message: "Operating mode to install",
|
{ name: "Standalone (install both controller and host on this computer)", value: "standalone" },
|
||||||
default: "standalone",
|
{ name: "Controller only", value: "controller" },
|
||||||
choices: [
|
{ name: "Host only", value: "host" },
|
||||||
{ name: "Standalone (install both controller and host on this computer)", value: "standalone" },
|
{ name: "Ctl only", value: "ctl" },
|
||||||
{ name: "Controller only", value: "controller" },
|
{ name: "Plugins only", value: "plugins" },
|
||||||
{ name: "Host only", value: "host" },
|
{ name: "Plugin template", value: "plugin-template" },
|
||||||
{ name: "Ctl only", value: "ctl" },
|
],
|
||||||
{ name: "Plugins only", value: "plugins" },
|
});
|
||||||
{ name: "Plugin template", value: "plugin-template" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
|
|
||||||
if (["standalone", "controller"].includes(answers.mode)) {
|
if (["standalone", "controller"].includes(answers.mode)) {
|
||||||
if (args.admin) { answers.admin = args.admin; }
|
if (args.admin) { answers.admin = args.admin; }
|
||||||
answers = await inquirer.prompt([
|
answers.admin ??= await input({
|
||||||
{
|
message: "Admin account name",
|
||||||
type: "input",
|
validate: value => {
|
||||||
name: "admin",
|
if (!value) {
|
||||||
message: "Admin account name",
|
return "May not be empty";
|
||||||
validate: input => {
|
}
|
||||||
if (!input) {
|
return true;
|
||||||
return "May not be empty";
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
], answers);
|
});
|
||||||
|
|
||||||
if (args.httpPort) { answers.httpPort = args.httpPort; }
|
if (args.httpPort) { answers.httpPort = args.httpPort; }
|
||||||
answers = await inquirer.prompt([
|
answers.httpPort ??= await input({
|
||||||
{
|
message: "HTTP port to listen on",
|
||||||
type: "input",
|
default: "8080",
|
||||||
name: "httpPort",
|
validate: value => {
|
||||||
message: "HTTP port to listen on",
|
if (!value) {
|
||||||
default: 8080,
|
return "May not be empty";
|
||||||
validate: input => {
|
}
|
||||||
if (!input) {
|
if (!Number.isInteger(Number(value))) {
|
||||||
return "May not be empty";
|
return "Must be a number";
|
||||||
}
|
}
|
||||||
if (!Number.isInteger(Number(input))) {
|
return true;
|
||||||
return "Must be a number";
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
], answers);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (answers.mode === "standalone") {
|
if (answers.mode === "standalone") {
|
||||||
@@ -605,23 +595,17 @@ async function inquirerMissingArgs(args) {
|
|||||||
if (answers.mode === "host") {
|
if (answers.mode === "host") {
|
||||||
if (args.hostName) { answers.hostName = args.hostName; }
|
if (args.hostName) { answers.hostName = args.hostName; }
|
||||||
if (args.controllerUrl) { answers.controllerUrl = args.controllerUrl; }
|
if (args.controllerUrl) { answers.controllerUrl = args.controllerUrl; }
|
||||||
answers = await inquirer.prompt([
|
answers.hostName ??= await input({
|
||||||
{
|
message: "Name of host",
|
||||||
type: "input",
|
default: os.hostname(),
|
||||||
name: "hostName",
|
validate: validateHostName,
|
||||||
message: "Name of host",
|
});
|
||||||
default: os.hostname(),
|
answers.controllerUrl ??= await input({
|
||||||
validate: validateHostName,
|
message: "Controller URL",
|
||||||
},
|
validate: validateControllerUrl,
|
||||||
{
|
});
|
||||||
type: "input",
|
|
||||||
name: "controllerUrl",
|
|
||||||
message: "Controller URL",
|
|
||||||
validate: validateControllerUrl,
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
|
|
||||||
// Inquirer does not validate answers given as arguments
|
// The prompts do not validate answers given as arguments
|
||||||
for (let result of [validateHostName(answers.hostName), validateControllerUrl(answers.controllerUrl)]) {
|
for (let result of [validateHostName(answers.hostName), validateControllerUrl(answers.controllerUrl)]) {
|
||||||
if (result !== true) {
|
if (result !== true) {
|
||||||
throw new InstallError(result);
|
throw new InstallError(result);
|
||||||
@@ -632,22 +616,10 @@ async function inquirerMissingArgs(args) {
|
|||||||
|
|
||||||
if (answers.mode === "ctl") {
|
if (answers.mode === "ctl") {
|
||||||
if (args.controllerUrl) { answers.controllerUrl = args.controllerUrl; }
|
if (args.controllerUrl) { answers.controllerUrl = args.controllerUrl; }
|
||||||
answers = await inquirer.prompt([
|
answers.controllerUrl ??= await input({ message: "Controller URL" });
|
||||||
{
|
|
||||||
type: "input",
|
|
||||||
name: "controllerUrl",
|
|
||||||
message: "Controller URL",
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
|
|
||||||
if (args.controllerToken) { answers.controllerToken = args.controllerToken; }
|
if (args.controllerToken) { answers.controllerToken = args.controllerToken; }
|
||||||
answers = await inquirer.prompt([
|
answers.controllerToken ??= await input({ message: "Controller authentication Token" });
|
||||||
{
|
|
||||||
type: "input",
|
|
||||||
name: "controllerToken",
|
|
||||||
message: "Controller authentication Token",
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (["standalone", "host"].includes(answers.mode)) {
|
if (["standalone", "host"].includes(answers.mode)) {
|
||||||
@@ -660,14 +632,10 @@ async function inquirerMissingArgs(args) {
|
|||||||
myIp = await result.text();
|
myIp = await result.text();
|
||||||
} catch (err) { /* ignore */ }
|
} catch (err) { /* ignore */ }
|
||||||
}
|
}
|
||||||
answers = await inquirer.prompt([
|
answers.publicAddress ??= await input({
|
||||||
{
|
message: "Public DNS/IP address of this server",
|
||||||
type: "input",
|
default: myIp,
|
||||||
name: "publicAddress",
|
});
|
||||||
message: "Public DNS/IP address of this server",
|
|
||||||
default: myIp,
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
|
|
||||||
if (args.factorioDir) { answers.factorioDir = args.factorioDir; }
|
if (args.factorioDir) { answers.factorioDir = args.factorioDir; }
|
||||||
let locations = factorioLocations[process.platform] || [];
|
let locations = factorioLocations[process.platform] || [];
|
||||||
@@ -678,28 +646,17 @@ async function inquirerMissingArgs(args) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
answers = await inquirer.prompt([
|
answers.factorioDir ??= await select({
|
||||||
{
|
message: "Path to Factorio installation",
|
||||||
type: "list",
|
choices: [
|
||||||
name: "factorioDir",
|
...foundLocations.map(location => ({ name: `${location} (auto detected)`, value: location })),
|
||||||
message: "Path to Factorio installation",
|
{ name: "Use local factorio directory, you must copy an installation to it", value: "factorio" },
|
||||||
choices: [
|
{ name: "Provide path manually", value: null },
|
||||||
...foundLocations.map(location => ({ name: `${location} (auto detected)`, value: location })),
|
],
|
||||||
{ name: "Use local factorio directory, you must copy an installation to it", value: "factorio" },
|
});
|
||||||
{ name: "Provide path manually", value: null },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
|
|
||||||
if (answers.factorioDir === null) {
|
if (answers.factorioDir === null) {
|
||||||
answers = await inquirer.prompt([
|
answers.factorioDir = await input({ message: "Path to Factorio installation" });
|
||||||
{
|
|
||||||
type: "input",
|
|
||||||
name: "factorioDir",
|
|
||||||
message: "Path to Factorio installation",
|
|
||||||
askAnswered: true,
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
} else if (answers.factorioDir === "factorio") {
|
} else if (answers.factorioDir === "factorio") {
|
||||||
await fs.mkdir("factorio", { recursive: true });
|
await fs.mkdir("factorio", { recursive: true });
|
||||||
}
|
}
|
||||||
@@ -707,14 +664,10 @@ async function inquirerMissingArgs(args) {
|
|||||||
|
|
||||||
if (["standalone", "controller", "host"].includes(answers.mode)) {
|
if (["standalone", "controller", "host"].includes(answers.mode)) {
|
||||||
if (args.hasOwnProperty("remoteNpm")) { answers.remoteNpm = args.remoteNpm; }
|
if (args.hasOwnProperty("remoteNpm")) { answers.remoteNpm = args.remoteNpm; }
|
||||||
answers = await inquirer.prompt([
|
answers.remoteNpm ??= await confirm({
|
||||||
{
|
message: "Allow remote updates and plugin installs from the web interface?",
|
||||||
type: "confirm",
|
default: true,
|
||||||
name: "remoteNpm",
|
});
|
||||||
message: "Allow remote updates and plugin installs from the web interface?",
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (answers.mode === "plugin-template") {
|
if (answers.mode === "plugin-template") {
|
||||||
@@ -725,29 +678,23 @@ async function inquirerMissingArgs(args) {
|
|||||||
answers.pluginName = args["plugin-name"];
|
answers.pluginName = args["plugin-name"];
|
||||||
}
|
}
|
||||||
answers.plugins = [];
|
answers.plugins = [];
|
||||||
answers = await inquirer.prompt([
|
answers.pluginTemplate ??= await checkbox({
|
||||||
{
|
message: "Plugin templates to use",
|
||||||
type: "checkbox",
|
choices: [
|
||||||
name: "pluginTemplate",
|
{ name: "Controller", value: "controller" },
|
||||||
message: "Plugin templates to use",
|
{ name: "Host", value: "host" },
|
||||||
choices: [
|
{ name: "Instance", value: "instance" },
|
||||||
{ name: "Controller", value: "controller" },
|
{ name: "Lua Module", value: "module" },
|
||||||
{ name: "Host", value: "host" },
|
{ name: "Command Line", value: "ctl" },
|
||||||
{ name: "Instance", value: "instance" },
|
{ name: "Web UI", value: "web" },
|
||||||
{ name: "Lua Module", value: "module" },
|
{ name: "No Typescript", value: "js" },
|
||||||
{ name: "Command Line", value: "ctl" },
|
{ name: "No Config", value: "no_config" },
|
||||||
{ name: "Web UI", value: "web" },
|
],
|
||||||
{ name: "No Typescript", value: "js" },
|
});
|
||||||
{ name: "No Config", value: "no_config" },
|
answers.pluginName ??= await input({
|
||||||
],
|
message: "Name of your new plugin",
|
||||||
},
|
default: path.basename(path.resolve()),
|
||||||
{
|
});
|
||||||
type: "input",
|
|
||||||
name: "pluginName",
|
|
||||||
message: "Name of your new plugin",
|
|
||||||
default: path.basename(path.resolve()),
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dev) {
|
if (dev) {
|
||||||
@@ -755,15 +702,11 @@ async function inquirerMissingArgs(args) {
|
|||||||
} else if (args.plugins) {
|
} else if (args.plugins) {
|
||||||
answers.plugins = args.plugins;
|
answers.plugins = args.plugins;
|
||||||
}
|
}
|
||||||
answers = await inquirer.prompt([
|
answers.plugins ??= await checkbox({
|
||||||
{
|
message: "Plugins to install",
|
||||||
type: "checkbox",
|
choices: availablePlugins,
|
||||||
name: "plugins",
|
pageSize: 20,
|
||||||
message: "Plugins to install",
|
});
|
||||||
choices: availablePlugins,
|
|
||||||
pageSize: 20,
|
|
||||||
},
|
|
||||||
], answers);
|
|
||||||
|
|
||||||
return answers;
|
return answers;
|
||||||
}
|
}
|
||||||
@@ -847,7 +790,7 @@ async function main() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let answers = await inquirerMissingArgs(args);
|
let answers = await promptMissingArgs(args);
|
||||||
logger.verbose(JSON.stringify(answers));
|
logger.verbose(JSON.stringify(answers));
|
||||||
|
|
||||||
if (answers.pluginTemplate) {
|
if (answers.pluginTemplate) {
|
||||||
@@ -911,7 +854,10 @@ async function main() {
|
|||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
main().catch(err => {
|
main().catch(err => {
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
if (err instanceof InstallError) {
|
if (err.name === "ExitPromptError") {
|
||||||
|
// Ctrl+C at a prompt
|
||||||
|
process.exitCode = 130;
|
||||||
|
} else if (err instanceof InstallError) {
|
||||||
logger.error(err.message);
|
logger.error(err.message);
|
||||||
} else {
|
} else {
|
||||||
logger.fatal(`
|
logger.fatal(`
|
||||||
|
|||||||
@@ -32,8 +32,11 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@inquirer/checkbox": "^5.2.8",
|
||||||
|
"@inquirer/confirm": "^6.3.5",
|
||||||
|
"@inquirer/input": "^5.1.9",
|
||||||
|
"@inquirer/select": "^5.2.8",
|
||||||
"chalk": "catalog:",
|
"chalk": "catalog:",
|
||||||
"inquirer": "^8.2.7",
|
|
||||||
"yargs": "catalog:"
|
"yargs": "catalog:"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user