Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
[refact] move functions from index.ts to other places
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzeng committed Jan 26, 2023
1 parent b0e68ba commit 055d27c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/exit_code.ts
Original file line number Diff line number Diff line change
@@ -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
32 changes: 9 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 + '.')
Expand Down
30 changes: 30 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 055d27c

Please sign in to comment.