From 055d27c8baa648d95df2afd80c5a13e98396b77f Mon Sep 17 00:00:00 2001 From: Hyperbola Date: Fri, 27 Jan 2023 02:46:35 +0800 Subject: [PATCH] [refact] move functions from index.ts to other places --- src/exit_code.ts | 4 ++++ src/index.ts | 32 +++++++++----------------------- src/util.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 src/exit_code.ts diff --git a/src/exit_code.ts b/src/exit_code.ts new file mode 100644 index 0000000..35e0cd2 --- /dev/null +++ b/src/exit_code.ts @@ -0,0 +1,4 @@ +export const EXIT_TASK_FAILED = 1 +export const EXIT_LOGIN_FAILED = 69 +export const EXIT_CODE_INVALID_ARGUMENT = 87 +export const EXIT_CODE_UNKNOWN_ERROR = 255 diff --git a/src/index.ts b/src/index.ts index 6abd360..6aa4383 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,13 @@ import path from 'path' import log from 'loglevel' // cspell: ignore loglevel import { program } from 'commander' import Bot from './pinkoi-bot' - -const EXIT_TASK_FAILED = 1 -const EXIT_LOGIN_FAILED = 69 -const EXIT_CODE_INVALID_ARGUMENT = 87 -const EXIT_CODE_UNKNOWN_ERROR = 255 +import { setupLogging, validateArgs } from './util' +import { + EXIT_CODE_INVALID_ARGUMENT, + EXIT_CODE_UNKNOWN_ERROR, + EXIT_LOGIN_FAILED, + EXIT_TASK_FAILED +} from './exit_code' const version = '1.3.0' const majorVersion = version.split('.')[0] @@ -30,25 +32,9 @@ program ) const args = program.parse(process.argv).opts() +validateArgs(args) -if (process.env['DEBUG']) { - log.setDefaultLevel('debug') - if (args.quiet) { - log.warn('Option `--quiet` is ignored in debug mode.') - } -} else if (args.quiet) { - log.setDefaultLevel('warn') -} else { - log.setDefaultLevel('info') -} - -// Argument validation. -if (args.checkin === args.solveWeeklyMission) { - log.error( - 'You should run exactly one of --checkin or --solve-weekly-mission.' - ) - process.exit(EXIT_CODE_INVALID_ARGUMENT) -} +setupLogging(args) async function main() { log.info('Start pinkoi coins bot v' + version + '.') diff --git a/src/util.ts b/src/util.ts index 9725e2f..e1f3f2b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,33 @@ +import log from 'loglevel' // cspell: ignore loglevel +import { OptionValues } from 'commander' +import { EXIT_CODE_INVALID_ARGUMENT } from './exit_code' + export function sleep() { return new Promise((res) => setTimeout(res, 500)) } + +export function setupLogging(args: OptionValues) { + if (process.env['DEBUG']) { + log.setDefaultLevel('debug') + if (args.quiet) { + log.warn('Option `--quiet` is ignored in debug mode.') + } + } else if (args.quiet) { + log.setDefaultLevel('warn') + } else { + log.setDefaultLevel('info') + } +} + +function requireCheckinOrSolveWeeklyMission(args: OptionValues) { + if (args.checkin === args.solveWeeklyMission) { + log.error( + 'You should run exactly one of --checkin or --solve-weekly-mission.' + ) + process.exit(EXIT_CODE_INVALID_ARGUMENT) + } +} + +export function validateArgs(args: OptionValues) { + requireCheckinOrSolveWeeklyMission(args) +}