Skip to content

Commit

Permalink
add --debug for logger.debug
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Apr 18, 2022
1 parent 530be1a commit 425768e
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 12 deletions.
13 changes: 7 additions & 6 deletions commons/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ export const DEFAULT_CONFIG_OPTIONS: IConfigOptions = {
min: '0',
max: '10000',

nonInteractive: false,
debug: false,
decreaseOnFail: false,
download: true,
hideNegativeHits: false,
onlyNewestMajor: false,
increaseOnFail: false,
inverse: false,
nonInteractive: false,
onlyNewestMajor: false,
quiet: false,
store: true,
download: true,
increaseOnFail: false,
decreaseOnFail: false,
unzip: false,
quiet: false,
}

export const DEFAULT_FULL_CONFIG: IChromeFullConfig = {
Expand Down
19 changes: 18 additions & 1 deletion config/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ComparableVersion } from '../commons/ComparableVersion'
import { DEFAULT_CONFIG_OPTIONS } from '../commons/constants'
import type { IChromeConfigWrapper, IStoreConfigWrapper, IExportConfigWrapper } from '../interfaces/interfaces'
import type { OS } from '../interfaces/os.interfaces'
import { Logger, logger } from '../log/logger'
import { Logger, logger, DebugMode } from '../log/logger'
import { createChromeFullConfig, createChromeOptions, createImportConfig, createExportConfig, createChromeSingleConfig } from '../test/test.utils'
import { readConfig } from './config'

Expand Down Expand Up @@ -241,6 +241,23 @@ describe('config', () => {
expect(config).toEqual(expectedConfig)
})

it('should enable debug logging on --debug', () => {
programMock.opts.mockReturnValue(createChromeOptions({
debug: true,
}))

const config = readConfig([''], 'linux')

const expectedConfig: IChromeConfigWrapper = {
action: 'loadChrome',
config: createChromeFullConfig()
}

expect(loggerMock.setDebugMode).toBeCalledTimes(1)
expect(loggerMock.setDebugMode).toBeCalledWith(DebugMode.DEBUG)
expect(config).toEqual(expectedConfig)
})

describe('arch/os', () => {
it('should do nothing on arch without os', () => {
programMock.opts.mockReturnValue(createChromeOptions({
Expand Down
11 changes: 8 additions & 3 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ComparableVersion } from '../commons/ComparableVersion'
import { DEFAULT_CONFIG_OPTIONS } from '../commons/constants'
import type { IConfigOptions } from '../interfaces/config.interfaces'
import type { ConfigWrapper, IChromeSingleConfig } from '../interfaces/interfaces'
import { logger } from '../log/logger'
import { DebugMode, logger } from '../log/logger'
/* eslint-disable-next-line import/no-namespace */
import * as packageJson from '../package.json'
import { mapOS } from '../utils'
Expand All @@ -24,7 +24,7 @@ export function readConfig(args: string[], platform: NodeJS.Platform): ConfigWra
.option('-a, --arch <arch>', 'The architecture for what the binary should be downloaded. Valid values are "x86" and "x64". Only works when --os is also set')
.option('-d, --decrease-on-fail', 'If a binary does not exist, go to the next lower version number and try again (regarding --min, --max and --max-results)', false)
.option('-i, --increase-on-fail', 'If a binary does not exist, go to the next higher version number and try again (regarding --min, --max and --max-results), overwrites "--decrease-on-fail" if both set', false)
.option('-z, --unzip', 'Directly unzip the downloaded zip-file and delete the .zip afterwards', false)
.option('-z, --unzip', 'Directly unzip the downloaded zip-file and delete the .zip afterwards', DEFAULT_CONFIG_OPTIONS.unzip)
.option('-n, --non-interactive', 'Don\'t show the selection menu. Automatically select the newest version. Only works when --decrease-on-fail is also set.', false)
.option('-t, --no-store', 'Don\'t store negative hits in the local store file.', DEFAULT_CONFIG_OPTIONS.store)
.option('-l, --no-download', 'Don\'t download the binary. It also continues with the next version, if --decrease-on-fail or --increase-on-fail is set. Useful to build up the negative hit store', DEFAULT_CONFIG_OPTIONS.download)
Expand All @@ -35,7 +35,8 @@ export function readConfig(args: string[], platform: NodeJS.Platform): ConfigWra
.option('-O, --only-newest-major', 'Show only the newest major version in user selection', DEFAULT_CONFIG_OPTIONS.onlyNewestMajor)
.option('-v, --inverse', 'Sort the selectable versions ascending', DEFAULT_CONFIG_OPTIONS.inverse)
.option('-s, --single <version>', 'Download a specific version in non-interactive mode, even if the file is listed in the localstore.json. Several other flags have no effect.')
.option('-q, --quiet', 'Suppress any logging output', false)
.option('-q, --quiet', 'Suppress any logging output', DEFAULT_CONFIG_OPTIONS.quiet)
.option('--debug', 'Activates the debug mode (extended logging)', DEFAULT_CONFIG_OPTIONS.debug)
.parse(args)

const options = program.opts() as IConfigOptions
Expand All @@ -45,6 +46,10 @@ export function readConfig(args: string[], platform: NodeJS.Platform): ConfigWra

const os = mapOS(options.os || platform)

if(options.debug) {
logger.setDebugMode(DebugMode.DEBUG)
}

if (!options.os && options.arch) {
logger.warn('Setting "--arch" has no effect, when "--os" is not set!')
}
Expand Down
2 changes: 2 additions & 0 deletions interfaces/config.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ export interface IConfigOptions {
single?: string,
inverse: boolean,

debug: boolean,

quiet: boolean,
}
26 changes: 25 additions & 1 deletion log/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import type { MaybeMockedDeep } from 'ts-jest/dist/utils/testing'

import type { PrinterWriteStream } from '../interfaces/printer.interfaces'
import { createStdioMock } from '../test/test.utils'
import { Logger } from './logger'
import { DebugMode, Logger } from './logger'

jest.mock('chalk', () => ({
blue: (text: string) => `blue: ${text}`,
magenta: (text: string) => `magenta: ${text}`,
red: (text: string) => `red: ${text}`,
yellow: (text: string) => `yellow: ${text}`,
}))
Expand Down Expand Up @@ -67,6 +68,29 @@ describe('logger', () => {
expect(stdioMock.cursorTo).toBeCalledWith(0)
})

it('should not log debug on DebugMode.NONE', () => {
logger.debug('foo')

expect(stdioMock.write).toBeCalledTimes(0)
expect(stdioMock.clearLine).toBeCalledTimes(0)
expect(stdioMock.cursorTo).toBeCalledTimes(0)
})

it('should log debug on DebugMode.DEBUG', () => {
logger.setDebugMode(DebugMode.DEBUG)
logger.debug('foo')

expect(stdioMock.write).toBeCalledTimes(2)
expect(stdioMock.write.mock.calls).toEqual([
['magenta: ? foo'],
['\n'],
])
expect(stdioMock.clearLine).toBeCalledTimes(1)
expect(stdioMock.clearLine).toBeCalledWith(0)
expect(stdioMock.cursorTo).toBeCalledTimes(1)
expect(stdioMock.cursorTo).toBeCalledWith(0)
})

it('should silent all log output', () => {
logger.silent()

Expand Down
19 changes: 19 additions & 0 deletions log/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { PrinterWriteStream } from '../interfaces/printer.interfaces'
import { Printer } from './printer'

export enum DebugMode {
NONE,
DEBUG,
}

/**
* Just a simple logger which holds no state. Try to prevent this when giving feedback to the user.
* Rather try to use a stateful logging like Spinner, Progress or Status
Expand All @@ -10,10 +15,17 @@ export class Logger extends Printer<Logger> {
super(stdio)
}

private debugMode = DebugMode.NONE

protected self(): Logger {
return this
}

public setDebugMode(mode: DebugMode): Logger {
this.debugMode = mode
return this
}

/**
* No state, no stop
*/
Expand All @@ -38,7 +50,14 @@ export class Logger extends Printer<Logger> {
return this.clearLine()
.write(this.WARN_FN(text))
.newline()
}

public debug(text: string): Logger {
return this.debugMode !== DebugMode.NONE
? this.clearLine()
.write(this.DEBUG_FN(text))
.newline()
: this.self()
}
}

Expand Down
1 change: 1 addition & 0 deletions log/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export abstract class Printer<T extends Printer<T>> {
protected readonly ERROR_FN = (msg: string): string => chalk.red(`✘ ${msg}`)
protected readonly WARN_FN = (msg: string): string => chalk.yellow(`! ${msg}`)
protected readonly INFO_FN = (msg: string): string => chalk.blue(`➔ ${msg}`)
protected readonly DEBUG_FN = (msg: string): string => chalk.magenta(`? ${msg}`)

protected constructor(private stdio: PrinterWriteStream) {
}
Expand Down
5 changes: 4 additions & 1 deletion store/loadStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { promisify } from 'util'

import { LOCAL_STORE_FILE } from '../commons/constants'
import type { IListStore } from '../interfaces/store.interfaces'
import { logger } from '../log/logger'
import { Store } from './Store'

const STORE_FILE = pathJoin(__dirname, '..', LOCAL_STORE_FILE)
Expand All @@ -26,7 +27,9 @@ const EMPTY_STORE: IListStore = {
}

export async function loadStore(): Promise<Store> {
// FIXME: exists is deprecated, use existsSync

logger.debug(`using store file: ${STORE_FILE}`)

const currentStoreJson = existsSync(STORE_FILE)
? await readFile(STORE_FILE, 'utf8')
: JSON.stringify(EMPTY_STORE)
Expand Down

0 comments on commit 425768e

Please sign in to comment.