Skip to content

Commit b779f3e

Browse files
add ability for library options to be passed via cli
1 parent 72a4bc8 commit b779f3e

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

src/cli.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
#!/usr/bin/env node
22
import { createDB } from "./index";
3+
import { OPTION_TYPE_CHECKS } from "./constants";
34

45
async function main() {
6+
const definedOptions = process.argv.filter((option) => option.startsWith('--'))
7+
const options = {}
8+
for (const opt of definedOptions) {
9+
const index = process.argv.indexOf(opt)
10+
const optionValue = process.argv[index + 1]
11+
12+
if (optionValue === undefined) {
13+
throw `Option ${opt} must have a value.`
14+
}
15+
16+
const optionName = opt.slice(2)
17+
const optionType = OPTION_TYPE_CHECKS[optionName].definedType;
18+
19+
//Try to convert the options to their correct types.
20+
//We do not need to do any proper type validation here as the library will make sure everything is correct.
21+
//Like for example, if a string is passed to a number option, it'll be converted to NaN here, but the library
22+
//will throw an error for it not being an actual number.
23+
if (optionType === 'boolean') {
24+
if (optionValue === 'true') {
25+
options[optionName] = true
26+
} else if (optionValue === 'false') {
27+
options[optionName] = false
28+
} else {
29+
options[optionName] = optionValue
30+
}
31+
} else if (optionType === 'number') {
32+
options[optionName] = parseInt(optionValue)
33+
} else {
34+
options[opt.slice(2)] = optionValue
35+
}
36+
}
537
console.log('Creating ephemeral MySQL database...')
6-
const db = await createDB();
7-
console.log(`A MySQL databases has been successfully created with the following parameters.\n\nUsername: ${db.username} \nDatabase Name: ${db.dbName} \nPort: ${db.port} \nX Plugin Port: ${db.xPort} \nSocket: ${db.socket} \nX Plugin Socket: ${db.xSocket}\n`)
38+
const db = await createDB(options);
39+
console.log(`A MySQL databases has been successfully created with the following parameters:\n\nUsername: ${db.username} \nDatabase Name: ${db.dbName} \nPort: ${db.port} \nX Plugin Port: ${db.xPort} \nSocket: ${db.socket} \nX Plugin Socket: ${db.xSocket}\n`)
840
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}`)
941
}
1042

src/constants.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,66 +39,82 @@ export const INTERNAL_OPTIONS = ['_DO_NOT_USE_deleteDBAfterStopped', '_DO_NOT_US
3939
export const OPTION_TYPE_CHECKS: OptionTypeChecks = {
4040
version: {
4141
check: (opt: any) => opt === undefined || typeof opt === 'string' && validSemver(opt) !== null,
42-
errorMessage: 'Option version must be either undefined or a valid semver string.'
42+
errorMessage: 'Option version must be either undefined or a valid semver string.',
43+
definedType: 'string'
4344
},
4445
dbName: {
4546
check: (opt: any) => opt === undefined || typeof opt === 'string' && opt.length <= 64,
46-
errorMessage: 'Option dbName must be either undefined or a string that is not longer than 64 characters.'
47+
errorMessage: 'Option dbName must be either undefined or a string that is not longer than 64 characters.',
48+
definedType: 'string'
4749
},
4850
logLevel: {
4951
check: (opt: any) => opt === undefined || Object.keys(LOG_LEVELS).includes(opt),
50-
errorMessage: 'Option logLevel must be either undefined or "LOG", "WARN", or "ERROR".'
52+
errorMessage: 'Option logLevel must be either undefined or "LOG", "WARN", or "ERROR".',
53+
definedType: 'string'
5154
},
5255
portRetries: {
5356
check: (opt: any) => opt === undefined || typeof opt === 'number' && opt >= 0,
54-
errorMessage: 'Option portRetries must be either undefined, a positive number, or 0.'
57+
errorMessage: 'Option portRetries must be either undefined, a positive number, or 0.',
58+
definedType: 'number'
5559
},
5660
downloadBinaryOnce: {
5761
check: (opt: any) => opt === undefined || typeof opt === 'boolean',
58-
errorMessage: 'Option downloadBinaryOnce must be either undefined or a boolean.'
62+
errorMessage: 'Option downloadBinaryOnce must be either undefined or a boolean.',
63+
definedType: 'boolean'
5964
},
6065
lockRetries: {
6166
check: (opt: any) => opt === undefined || typeof opt === 'number' && opt >= 0,
62-
errorMessage: 'Option lockRetries must be either undefined, a positive number, or 0.'
67+
errorMessage: 'Option lockRetries must be either undefined, a positive number, or 0.',
68+
definedType: 'number'
6369
},
6470
lockRetryWait: {
6571
check: (opt: any) => opt === undefined || typeof opt === 'number' && opt >= 0,
66-
errorMessage: 'Option lockRetryWait must be either undefined, a positive number, or 0.'
72+
errorMessage: 'Option lockRetryWait must be either undefined, a positive number, or 0.',
73+
definedType: 'number'
6774
},
6875
username: {
6976
check: (opt: any) => opt === undefined || typeof opt === 'string' && opt.length <= 32,
70-
errorMessage: 'Option username must be either undefined or a string that is not longer than 32 characters.'
77+
errorMessage: 'Option username must be either undefined or a string that is not longer than 32 characters.',
78+
definedType: 'string'
7179
},
7280
ignoreUnsupportedSystemVersion: {
7381
check: (opt: any) => opt === undefined || typeof opt === 'boolean',
74-
errorMessage: 'Option ignoreUnsupportedSystemVersion must be either undefined or a boolean.'
82+
errorMessage: 'Option ignoreUnsupportedSystemVersion must be either undefined or a boolean.',
83+
definedType: 'boolean'
7584
},
7685
port: {
7786
check: (opt: any) => opt === undefined || typeof opt === 'number' && opt >= 0 && opt <= 65535,
78-
errorMessage: 'Option port must be either undefined or a number that is between 0 and 65535 inclusive.'
87+
errorMessage: 'Option port must be either undefined or a number that is between 0 and 65535 inclusive.',
88+
definedType: 'number'
7989
},
8090
xPort: {
8191
check: (opt: any) => opt === undefined || typeof opt === 'number' && opt >= 0 && opt <= 65535,
82-
errorMessage: 'Option xPort must be either undefined or a number that is between 0 and 65535 inclusive.'
92+
errorMessage: 'Option xPort must be either undefined or a number that is between 0 and 65535 inclusive.',
93+
definedType: 'number'
8394
},
8495
downloadRetries: {
8596
check: (opt: any) => opt === undefined || typeof opt === 'number' && opt >= 0,
86-
errorMessage: 'Option downloadRetries must be either undefined, a positive number, or 0.'
97+
errorMessage: 'Option downloadRetries must be either undefined, a positive number, or 0.',
98+
definedType: 'number'
8799
},
88100
initSQLString: {
89101
check: (opt: any) => opt === undefined || typeof opt === 'string',
90-
errorMessage: 'Option initSQLString must be either undefined or a string.'
102+
errorMessage: 'Option initSQLString must be either undefined or a string.',
103+
definedType: 'string'
91104
},
92105
_DO_NOT_USE_deleteDBAfterStopped: {
93106
check: (opt: any) => opt === undefined || typeof opt === 'boolean',
94-
errorMessage: 'Option _DO_NOT_USE_deleteDBAfterStopped must be either undefined or a boolean.'
107+
errorMessage: 'Option _DO_NOT_USE_deleteDBAfterStopped must be either undefined or a boolean.',
108+
definedType: 'boolean'
95109
},
96110
_DO_NOT_USE_dbPath: {
97111
check: (opt: any) => opt === undefined || typeof opt === 'string',
98-
errorMessage: 'Option _DO_NOT_USE_dbPath must be either undefined or a string.'
112+
errorMessage: 'Option _DO_NOT_USE_dbPath must be either undefined or a string.',
113+
definedType: 'string'
99114
},
100115
_DO_NOT_USE_binaryDirectoryPath: {
101116
check: (opt: any) => opt === undefined || typeof opt === 'string',
102-
errorMessage: 'Option _DO_NOT_USE_binaryDirectoryPath must be either undefined or a string.'
117+
errorMessage: 'Option _DO_NOT_USE_binaryDirectoryPath must be either undefined or a string.',
118+
definedType: 'string'
103119
}
104120
} as const;

types/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export type BinaryInfo = {
8181
export type OptionTypeChecks = {
8282
[key in keyof Required<ServerOptions>]: {
8383
check: (opt: any) => boolean,
84-
errorMessage: string
84+
errorMessage: string,
85+
definedType: "string" | "boolean" | "number"
8586
}
8687
}

0 commit comments

Comments
 (0)