From 6ce73f402e4c257a5781476c7bb7c76cc44a186b Mon Sep 17 00:00:00 2001 From: Masoud Ghorbani Date: Thu, 2 Jan 2025 16:48:21 +0100 Subject: [PATCH] refactor: implement flagsInterceptor for command flag handling across multiple commands --- src/commands/config/init.ts | 3 +- src/commands/plugins/install.ts | 8 +++-- src/commands/plugins/prune.ts | 8 +++-- src/commands/plugins/uninstall.ts | 8 +++-- src/commands/reports/stats.ts | 8 +++-- src/commands/vaults/run.ts | 8 +++-- src/providers/command.ts | 55 ------------------------------- src/utils/command.ts | 12 ++++++- src/utils/logger.ts | 20 ++++++++++- 9 files changed, 62 insertions(+), 68 deletions(-) diff --git a/src/commands/config/init.ts b/src/commands/config/init.ts index f60ae26..fa7a58f 100644 --- a/src/commands/config/init.ts +++ b/src/commands/config/init.ts @@ -11,6 +11,7 @@ import { InitCommandCallback, InitFlags, } from '../../types/commands' +import { flagsInterceptor } from '../../utils/command' /** * Init command configure an ovm.json config file in user's home dir. @@ -31,7 +32,7 @@ export default class Init extends FactoryCommand { public async run() { try { const { args, flags } = await this.parse(Init) - return action(args, this.flagsInterceptor(flags)) + return action(args, flagsInterceptor>(flags)) } catch (error) { this.handleError(error) } finally { diff --git a/src/commands/plugins/install.ts b/src/commands/plugins/install.ts index 5e382e5..ca385a9 100644 --- a/src/commands/plugins/install.ts +++ b/src/commands/plugins/install.ts @@ -1,7 +1,8 @@ import { Args, Flags, flush } from '@oclif/core' import { FactoryCommandWithVaults } from '../../providers/command' import installService from '../../services/install' -import { InstallFlags } from '../../types/commands' +import { FactoryFlagsWithVaults, InstallFlags } from '../../types/commands' +import { flagsInterceptor } from '../../utils/command' const { action } = installService @@ -43,7 +44,10 @@ export default class Install extends FactoryCommandWithVaults { public async run(): Promise { try { const { args, flags } = await this.parse(Install) - return action(args, this.flagsInterceptor(flags)) + return action( + args, + flagsInterceptor>(flags), + ) } catch (error) { this.handleError(error) throw error diff --git a/src/commands/plugins/prune.ts b/src/commands/plugins/prune.ts index b227580..122c5da 100644 --- a/src/commands/plugins/prune.ts +++ b/src/commands/plugins/prune.ts @@ -1,7 +1,8 @@ import { flush } from '@oclif/core' import { FactoryCommandWithVaults } from '../../providers/command' import pruneService from '../../services/prune' -import { PruneFlags } from '../../types/commands' +import { FactoryFlagsWithVaults, PruneFlags } from '../../types/commands' +import { flagsInterceptor } from '../../utils/command' const { action } = pruneService @@ -28,7 +29,10 @@ export default class Prune extends FactoryCommandWithVaults { public async run() { try { const { args, flags } = await this.parse(Prune) - await action(args, this.flagsInterceptor(flags)) + await action( + args, + flagsInterceptor>(flags), + ) } catch (error) { this.handleError(error) } finally { diff --git a/src/commands/plugins/uninstall.ts b/src/commands/plugins/uninstall.ts index c421730..5c3bc4a 100644 --- a/src/commands/plugins/uninstall.ts +++ b/src/commands/plugins/uninstall.ts @@ -1,7 +1,8 @@ import { Args, flush } from '@oclif/core' import { FactoryCommandWithVaults } from '../../providers/command' import uninstallService from '../../services/uninstall' -import { UninstallFlags } from '../../types/commands' +import { FactoryFlagsWithVaults, UninstallFlags } from '../../types/commands' +import { flagsInterceptor } from '../../utils/command' const { action } = uninstallService @@ -35,7 +36,10 @@ export default class Uninstall extends FactoryCommandWithVaults { public async run() { try { const { args, flags } = await this.parse(Uninstall) - await action(args, this.flagsInterceptor(flags)) + await action( + args, + flagsInterceptor>(flags), + ) } catch (error) { this.handleError(error) } finally { diff --git a/src/commands/reports/stats.ts b/src/commands/reports/stats.ts index 3bb9428..bddc1b7 100644 --- a/src/commands/reports/stats.ts +++ b/src/commands/reports/stats.ts @@ -1,7 +1,8 @@ import { Flags, flush } from '@oclif/core' import { FactoryCommandWithVaults } from '../../providers/command' import statsService from '../../services/stats' -import { StatsFlags } from '../../types/commands' +import { FactoryFlagsWithVaults, StatsFlags } from '../../types/commands' +import { flagsInterceptor } from '../../utils/command' const { action } = statsService @@ -34,7 +35,10 @@ export default class Stats extends FactoryCommandWithVaults { public async run(): Promise { try { const { args, flags } = await this.parse(Stats) - return await action(args, this.flagsInterceptor(flags)) + return await action( + args, + flagsInterceptor>(flags), + ) } catch (error) { this.handleError(error) throw error diff --git a/src/commands/vaults/run.ts b/src/commands/vaults/run.ts index fd8281b..3ba7dd5 100644 --- a/src/commands/vaults/run.ts +++ b/src/commands/vaults/run.ts @@ -1,7 +1,8 @@ import { Args, Flags, flush } from '@oclif/core' import { FactoryCommandWithVaults } from '../../providers/command' import runService from '../../services/run' -import { RunFlags } from '../../types/commands' +import { FactoryFlagsWithVaults, RunFlags } from '../../types/commands' +import { flagsInterceptor } from '../../utils/command' const { action } = runService @@ -66,7 +67,10 @@ export default class Run extends FactoryCommandWithVaults { public async run(): Promise { try { const { args, flags } = await this.parse(Run) - await action(args, this.flagsInterceptor(flags)) + await action( + args, + flagsInterceptor>(flags), + ) } catch (error) { this.handleError(error) } finally { diff --git a/src/providers/command.ts b/src/providers/command.ts index 472c019..1410653 100644 --- a/src/providers/command.ts +++ b/src/providers/command.ts @@ -1,17 +1,14 @@ import { Command, Flags } from '@oclif/core' -import { ParserInput } from '@oclif/core/lib/interfaces/parser' import { exec } from 'child_process' import { Vault } from 'obsidian-utils' import { homedir } from 'os' import path from 'path' -import { FactoryFlags, FactoryFlagsWithVaults } from '../types/commands' import { handlerCommandError } from '../utils/command' import { OVM_CONFIG_FILENAME, RESERVED_VARIABLES, VAULTS_PATH_FLAG_DESCRIPTION, } from '../utils/constants' -import { logger } from '../utils/logger' const DEFAULT_CONFIG_PATH = path.join(homedir(), OVM_CONFIG_FILENAME) @@ -41,33 +38,6 @@ class FactoryCommand extends Command { throw new Error('Method not implemented.') } - public enableLoggingTimestamp(timestamp: boolean): void { - process.env.OVM_ENABLE_LOG_TIMESTAMP = timestamp ? '0' : '1' - } - - public enableDebugLogLevel( - debug: boolean, - flags: ParserInput['flags'], - ): void { - if (debug) { - logger.level = 'debug' - logger.debug(`Command called`, { flags }) - } - } - - public flagsInterceptor(flags: FactoryFlags): FactoryFlags { - const { debug, timestamp } = flags - - this.enableLoggingTimestamp(timestamp) - - if (debug) { - logger.level = 'debug' - logger.debug(`Command called`, { flags }) - } - - return flags - } - public handleError(error: unknown) { handlerCommandError(error) } @@ -89,31 +59,6 @@ class FactoryCommandWithVaults extends Command { throw new Error('Method not implemented.') } - public enableLoggingTimestamp(timestamp: boolean): void { - process.env.OVM_ENABLE_LOG_TIMESTAMP = timestamp ? '0' : '1' - } - - public enableDebugLogLevel( - debug: boolean, - flags: ParserInput['flags'], - ): void { - if (debug) { - logger.level = 'debug' - logger.debug(`Command called`, { flags }) - } - } - - public flagsInterceptor( - flags: FactoryFlagsWithVaults, - ): FactoryFlagsWithVaults { - const { debug, timestamp } = flags - - this.enableLoggingTimestamp(timestamp) - this.enableDebugLogLevel(debug, flags as ParserInput['flags']) - - return flags - } - public handleError(error: unknown) { handlerCommandError(error) } diff --git a/src/utils/command.ts b/src/utils/command.ts index 23e438e..e511de9 100644 --- a/src/utils/command.ts +++ b/src/utils/command.ts @@ -1,6 +1,16 @@ import { ExitPromptError } from '@inquirer/core' import { handle } from '@oclif/core' -import { logger } from './logger' +import { CommonFlags } from '../types/commands' +import { enableDebugLogLevel, enableLoggingTimestamp, logger } from './logger' + +export const flagsInterceptor = (flags: T): T => { + const { debug, timestamp } = flags + + enableLoggingTimestamp(timestamp) + enableDebugLogLevel(debug, flags) + + return flags +} export const handlerCommandError = (error: unknown) => { if (process.env.CI || process.env.CI === 'true') { diff --git a/src/utils/logger.ts b/src/utils/logger.ts index e94c1ac..9fe58d6 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -1,7 +1,11 @@ import { tmpdir } from 'os' import { join } from 'path' import { createLogger, format, transports } from 'winston' -import { FactoryFlags, FactoryFlagsWithVaults } from '../types/commands' +import { + CommonFlags, + FactoryFlags, + FactoryFlagsWithVaults, +} from '../types/commands' export const CUSTOM_COMMAND_LOGGER_FILE = join( tmpdir(), @@ -42,3 +46,17 @@ export const customCommandLogger = createLogger({ export const silentCheck = ( flags?: FactoryFlags | FactoryFlagsWithVaults, ) => flags && 'silent' in flags && flags.silent + +export const enableLoggingTimestamp = (timestamp: boolean) => { + process.env.OVM_ENABLE_LOG_TIMESTAMP = timestamp ? 'true' : 'false' +} + +export const enableDebugLogLevel = ( + debug: boolean, + flags: CommonFlags | FactoryFlags | FactoryFlagsWithVaults, +) => { + if (debug) { + logger.level = 'debug' + logger.debug(`Command called`, { flags }) + } +}