From 33c7689d9f9435ef21628f5af0029b741a12fa48 Mon Sep 17 00:00:00 2001 From: Sebastian-Webster <84299475+Sebastian-Webster@users.noreply.github.com> Date: Wed, 15 Jan 2025 01:24:20 +0400 Subject: [PATCH 1/3] gracefully fail if invalid option is passed to cli --- src/cli.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index 3e807cf3..c4a67a40 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node import { createDB } from "./index"; -import { OPTION_TYPE_CHECKS } from "./constants"; +import { DEFAULT_OPTIONS_KEYS, OPTION_TYPE_CHECKS } from "./constants"; import { ServerOptions } from "../types"; async function main() { @@ -9,6 +9,10 @@ async function main() { _DO_NOT_USE_cli: true } for (const opt of definedOptions) { + if (!DEFAULT_OPTIONS_KEYS.includes(opt)) { + throw `Option ${opt} is not a valid option.` + } + const index = process.argv.indexOf(opt) const optionValue = process.argv[index + 1] From 0b6f59771f0f9c8604972d3c5f6568eb18ecffd2 Mon Sep 17 00:00:00 2001 From: Sebastian-Webster <84299475+Sebastian-Webster@users.noreply.github.com> Date: Wed, 15 Jan 2025 01:28:10 +0400 Subject: [PATCH 2/3] change throws to console.error in cli --- src/cli.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index c4a67a40..318bc3a2 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -10,14 +10,16 @@ async function main() { } for (const opt of definedOptions) { if (!DEFAULT_OPTIONS_KEYS.includes(opt)) { - throw `Option ${opt} is not a valid option.` + console.error(`Option ${opt} is not a valid option.`) + return } const index = process.argv.indexOf(opt) const optionValue = process.argv[index + 1] if (optionValue === undefined) { - throw `Option ${opt} must have a value.` + console.error(`Option ${opt} must have a value.`) + return } const optionName = opt.slice(2) From 3daf254c9b65c36f233e71593a6c73861cce1003 Mon Sep 17 00:00:00 2001 From: Sebastian-Webster <84299475+Sebastian-Webster@users.noreply.github.com> Date: Wed, 15 Jan 2025 01:32:07 +0400 Subject: [PATCH 3/3] chore: change async main function in cli to sync function --- src/cli.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 318bc3a2..2c092d52 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,7 +3,7 @@ import { createDB } from "./index"; import { DEFAULT_OPTIONS_KEYS, OPTION_TYPE_CHECKS } from "./constants"; import { ServerOptions } from "../types"; -async function main() { +function main() { const definedOptions = process.argv.filter((option) => option.startsWith('--')) const options: ServerOptions = { _DO_NOT_USE_cli: true @@ -44,17 +44,18 @@ async function main() { } } console.log('Creating ephemeral MySQL database...') - const db = await createDB(options); - console.log(`A MySQL database has been successfully created with the following parameters:\n\nMySQL Version: ${db.mysql.version} (${db.mysql.versionIsInstalledOnSystem ? 'installed on this system' : 'not installed on this system - downloaded from the MySQL CDN'}) \nUsername: ${db.username} \nDatabase Name: ${db.dbName} \nPort: ${db.port} \nX Plugin Port: ${db.xPort} \nSocket: ${db.socket} \nX Plugin Socket: ${db.xSocket}\n`) - if (process.platform === 'win32') { - //The connection information logs will be different for Windows compared to other platforms. - //Windows uses mysqlsh instead of mysql to invoke the client shell, needs a --sql flag to be put into SQL mode, and also does not have a protocol flag. - //Also according to https://bugs.mysql.com/bug.php?id=106852, you cannot log into a MySQL database with a named pipe for the first connection so a socket connection suggestion - //should only be displayed for non-Windows platforms. - console.log(`If you want to use the MySQL CLI client to connect to the database, you can use the following command: \nmysqlsh --sql -u ${db.username} -P ${db.port}\nIf prompted for a password, leave the password field blank. The database does not have a password set.`) - } else { - console.log(`If you want to use the MySQL CLI client to connect to the database, you can use either commands: \nmysql -u ${db.username} -P ${db.port} --protocol tcp \nOR\nmysql -u ${db.username} --socket ${db.socket}\nIf prompted for a password, leave the password field blank. The database does not have a password set.`) - } + createDB(options).then(db => { + console.log(`A MySQL database has been successfully created with the following parameters:\n\nMySQL Version: ${db.mysql.version} (${db.mysql.versionIsInstalledOnSystem ? 'installed on this system' : 'not installed on this system - downloaded from the MySQL CDN'}) \nUsername: ${db.username} \nDatabase Name: ${db.dbName} \nPort: ${db.port} \nX Plugin Port: ${db.xPort} \nSocket: ${db.socket} \nX Plugin Socket: ${db.xSocket}\n`) + if (process.platform === 'win32') { + //The connection information logs will be different for Windows compared to other platforms. + //Windows uses mysqlsh instead of mysql to invoke the client shell, needs a --sql flag to be put into SQL mode, and also does not have a protocol flag. + //Also according to https://bugs.mysql.com/bug.php?id=106852, you cannot log into a MySQL database with a named pipe for the first connection so a socket connection suggestion + //should only be displayed for non-Windows platforms. + console.log(`If you want to use the MySQL CLI client to connect to the database, you can use the following command: \nmysqlsh --sql -u ${db.username} -P ${db.port}\nIf prompted for a password, leave the password field blank. The database does not have a password set.`) + } else { + console.log(`If you want to use the MySQL CLI client to connect to the database, you can use either commands: \nmysql -u ${db.username} -P ${db.port} --protocol tcp \nOR\nmysql -u ${db.username} --socket ${db.socket}\nIf prompted for a password, leave the password field blank. The database does not have a password set.`) + } + }) } main()