General Actions to handlers Refacto
This commit is contained in:
+38
-5
@@ -2,12 +2,40 @@ import yargs from 'yargs/yargs'
|
||||
import { hideBin } from 'yargs/helpers'
|
||||
import { pathToFileURL } from 'url'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { dirname, join } from 'path'
|
||||
import { dirname, join, resolve } from 'path'
|
||||
import { RedisConnexion } from '../redisConnexion.js'
|
||||
import { configHelper } from '../configHelper.js'
|
||||
import { MySQLClient } from '@p42/p42modules'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
|
||||
const UUID_DASHED_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
const UUID_HEX32_RE = /^[0-9a-f]{32}$/i
|
||||
|
||||
export function normalizeUuid(value) {
|
||||
if(typeof(value) !== 'string') {
|
||||
throw(new Error('UUID must be a string'))
|
||||
}
|
||||
|
||||
const trimmed = value.trim()
|
||||
if(!trimmed) throw(new Error('UUID must be a non-empty string'))
|
||||
|
||||
if(UUID_DASHED_RE.test(trimmed)) {
|
||||
return(trimmed.toLowerCase())
|
||||
}
|
||||
|
||||
let hex = trimmed
|
||||
if(/^0x/i.test(hex)) hex = hex.slice(2)
|
||||
if(!UUID_HEX32_RE.test(hex)) {
|
||||
throw(new Error(
|
||||
`Invalid UUID: ${value} (expected dashed form or 32-char hex, optionally prefixed with 0x)`
|
||||
))
|
||||
}
|
||||
|
||||
hex = hex.toLowerCase()
|
||||
return(`${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`)
|
||||
}
|
||||
|
||||
const LOG_COLORS = {
|
||||
action: '\x1b[37m',
|
||||
@@ -145,6 +173,7 @@ async function main() {
|
||||
systemCnx,
|
||||
arenaCnx,
|
||||
log,
|
||||
normalizeUuid,
|
||||
onArenaMessage(handler) {
|
||||
arenaHandlers.add(handler)
|
||||
},
|
||||
@@ -173,7 +202,11 @@ async function main() {
|
||||
process.exit(exitCode)
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error(`${LOG_COLORS.error}${err.message ?? err}${LOG_COLORS.reset}`)
|
||||
process.exit(1)
|
||||
})
|
||||
const isMain = process.argv[1] && resolve(__filename) === resolve(process.argv[1])
|
||||
|
||||
if(isMain) {
|
||||
main().catch(err => {
|
||||
console.error(`${LOG_COLORS.error}${err.message ?? err}${LOG_COLORS.reset}`)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user