From 1007eed3eb6ce2f132f43033bd9765aad263bbb7 Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Tue, 13 May 2025 16:04:14 -0500 Subject: [PATCH] refactor: Improve logging initialization and refactor CLI options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create dedicated LoggingOptions interface with only required fields - Make CompilerExplorerOptions interface private to cli.ts module - Refactor setupTempDir to take only needed parameters - Extract logging configuration from CLI options object - Update AppArguments to use required propDebug field and optional tmpDir - Update tests to match the new interfaces 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- lib/app.interfaces.ts | 4 ++-- lib/app/cli.ts | 31 +++++++++++++++++++++---------- lib/logger.ts | 17 ++++++++++++++--- test/app/cli-tests.ts | 19 +++++-------------- test/app/config-tests.ts | 2 +- test/app/main-tests.ts | 2 +- test/app/server-tests.ts | 2 +- 7 files changed, 45 insertions(+), 32 deletions(-) diff --git a/lib/app.interfaces.ts b/lib/app.interfaces.ts index ec09efdf0..c9e199d4f 100644 --- a/lib/app.interfaces.ts +++ b/lib/app.interfaces.ts @@ -33,11 +33,11 @@ export type AppArguments = { doCache: boolean; fetchCompilersFromRemote: boolean; ensureNoCompilerClash: boolean | undefined; - suppressConsoleLog: boolean; prediscovered?: string; discoveryOnly?: string; staticPath?: string; metricsPort?: number; useLocalProps: boolean; - propDebug?: boolean; + propDebug: boolean; + tmpDir?: string; }; diff --git a/lib/app/cli.ts b/lib/app/cli.ts index feb08eafc..7bea937b9 100644 --- a/lib/app/cli.ts +++ b/lib/app/cli.ts @@ -49,8 +49,9 @@ export function parsePortNumberForOptions(value: string): number { /** * Options parsed from command-line arguments + * @private */ -export interface CompilerExplorerOptions { +interface CompilerExplorerOptions { env: string[]; rootDir: string; host?: string; @@ -163,13 +164,13 @@ export function detectWsl(): boolean { /** * Set up temporary directory, especially for WSL environments */ -export function setupTempDir(options: CompilerExplorerOptions, isWsl: boolean): void { +export function setupTempDir(tmpDir: string | undefined, isWsl: boolean): void { // If a tempDir is supplied, use it - if (options.tmpDir) { + if (tmpDir) { if (isWsl) { - process.env.TEMP = options.tmpDir; // for Windows + process.env.TEMP = tmpDir; // for Windows } else { - process.env.TMP = options.tmpDir; // for Linux + process.env.TMP = tmpDir; // for Linux } } // If running under WSL without explicit tmpDir, try to use Windows %TEMP% @@ -183,6 +184,7 @@ export function setupTempDir(options: CompilerExplorerOptions, isWsl: boolean): logger.warn('Unable to invoke cmd.exe to get windows %TEMP% path.'); } } + logger.info(`Using temporary dir: ${process.env.TEMP || process.env.TMP}`); } /** @@ -204,13 +206,13 @@ export function convertOptionsToAppArguments( doCache: options.cache, fetchCompilersFromRemote: options.remoteFetch, ensureNoCompilerClash: options.ensureNoIdClash, - suppressConsoleLog: options.suppressConsoleLog, prediscovered: options.prediscovered, discoveryOnly: options.discoveryOnly, staticPath: options.static, metricsPort: options.metricsPort, useLocalProps: options.local, - propDebug: options.propDebug, + propDebug: options.propDebug || false, + tmpDir: options.tmpDir, }; } @@ -221,15 +223,24 @@ export function convertOptionsToAppArguments( */ export function initialiseOptionsFromCommandLine(argv: string[]): AppArguments { const options = parseCommandLine(argv); - initialiseLogging(options); + + // Logging initialised here so it's done as early as possible. + initialiseLogging({ + debug: options.debug || false, + logHost: options.logHost, + logPort: options.logPort, + hostnameForLogging: options.hostnameForLogging, + loki: options.loki, + suppressConsoleLog: options.suppressConsoleLog, + paperTrailIdentifier: options.env.join('.'), + }); const isWsl = detectWsl(); if (isWsl) { process.env.wsl = 'true'; } - setupTempDir(options, isWsl); - logger.info(`Using temporary dir: ${process.env.TEMP || process.env.TMP}`); + setupTempDir(options.tmpDir, isWsl); const distPath = utils.resolvePathFromAppRoot('.'); logger.debug(`Distpath=${distPath}`); diff --git a/lib/logger.ts b/lib/logger.ts index b68232f59..c0efaa443 100644 --- a/lib/logger.ts +++ b/lib/logger.ts @@ -32,7 +32,18 @@ import LokiTransport from 'winston-loki'; // @ts-ignore import {Papertrail} from 'winston-papertrail'; import TransportStream, {TransportStreamOptions} from 'winston-transport'; -import {CompilerExplorerOptions} from './app/cli.js'; +/** + * Options required for configuring logging + */ +export interface LoggingOptions { + debug: boolean; + logHost?: string; + logPort?: number; + hostnameForLogging?: string; + loki?: string; + suppressConsoleLog: boolean; + paperTrailIdentifier: string; +} const consoleTransportInstance = new winston.transports.Console(); export const logger = winston.createLogger({ @@ -151,13 +162,13 @@ class Blackhole extends TransportStream { } } -export function initialiseLogging(options: CompilerExplorerOptions) { +export function initialiseLogging(options: LoggingOptions) { if (options.debug) { logger.level = 'debug'; } if (options.logHost && options.logPort) { - logToPapertrail(options.logHost, options.logPort, options.env.join('.'), options.hostnameForLogging); + logToPapertrail(options.logHost, options.logPort, options.paperTrailIdentifier, options.hostnameForLogging); } if (options.loki) { diff --git a/test/app/cli-tests.ts b/test/app/cli-tests.ts index 5a5688617..b22f84fbc 100644 --- a/test/app/cli-tests.ts +++ b/test/app/cli-tests.ts @@ -230,11 +230,7 @@ describe('CLI Module', () => { // Skip on Windows as it has different environment variable defaults if (process.platform === 'win32') return; - const options = { - tmpDir: '/custom/tmp', - } as any; - - setupTempDir(options, false); + setupTempDir('/custom/tmp', false); expect(process.env.TMP).toEqual('/custom/tmp'); expect(process.env.TEMP).toBeUndefined(); @@ -244,11 +240,7 @@ describe('CLI Module', () => { // Skip on Windows as it has different environment variable defaults if (process.platform === 'win32') return; - const options = { - tmpDir: '/custom/tmp', - } as any; - - setupTempDir(options, true); + setupTempDir('/custom/tmp', true); expect(process.env.TEMP).toEqual('/custom/tmp'); expect(process.env.TMP).toEqual(originalEnv.TMP); @@ -258,10 +250,9 @@ describe('CLI Module', () => { // Skip on Windows due to path separator differences (we ironically test this from linux) if (process.platform === 'win32') return; - const options = {} as any; vi.mocked(child_process.execSync).mockReturnValue(Buffer.from('C:\\Users\\user\\AppData\\Local\\Temp\n')); - setupTempDir(options, true); + setupTempDir(undefined, true); expect(process.env.TEMP).toEqual('/mnt/c/Users/user/AppData/Local/Temp'); expect(child_process.execSync).toHaveBeenCalledWith('cmd.exe /c echo %TEMP%'); @@ -279,13 +270,13 @@ describe('CLI Module', () => { cache: true, remoteFetch: true, ensureNoIdClash: true, - suppressConsoleLog: false, prediscovered: './prediscovered.json', discoveryOnly: './discoveryOnly.json', static: './static', metricsPort: 8081, local: true, propDebug: true, + tmpDir: '/custom/tmp', extraField: 'should be ignored', } as any; @@ -305,13 +296,13 @@ describe('CLI Module', () => { doCache: true, fetchCompilersFromRemote: true, ensureNoCompilerClash: true, - suppressConsoleLog: false, prediscovered: './prediscovered.json', discoveryOnly: './discoveryOnly.json', staticPath: './static', metricsPort: 8081, useLocalProps: true, propDebug: true, + tmpDir: '/custom/tmp', }); }); }); diff --git a/test/app/config-tests.ts b/test/app/config-tests.ts index 705ffed7e..2882ffb9c 100644 --- a/test/app/config-tests.ts +++ b/test/app/config-tests.ts @@ -32,7 +32,6 @@ function createMockAppArgs(overrides: Partial = {}): AppArguments port: 10240, hostname: 'localhost', env: ['prod'], - suppressConsoleLog: false, gitReleaseName: '', releaseBuildNumber: '', rootDir: '/test/root', @@ -46,6 +45,7 @@ function createMockAppArgs(overrides: Partial = {}): AppArguments metricsPort: undefined, useLocalProps: true, propDebug: false, + tmpDir: undefined, ...overrides, }; } diff --git a/test/app/main-tests.ts b/test/app/main-tests.ts index 9e276629c..8f146c60a 100644 --- a/test/app/main-tests.ts +++ b/test/app/main-tests.ts @@ -117,13 +117,13 @@ describe('Main module', () => { doCache: true, fetchCompilersFromRemote: true, ensureNoCompilerClash: false, - suppressConsoleLog: false, prediscovered: undefined, discoveryOnly: undefined, staticPath: undefined, metricsPort: undefined, useLocalProps: true, propDebug: false, + tmpDir: undefined, }; const mockConfig = { diff --git a/test/app/server-tests.ts b/test/app/server-tests.ts index 275248e6e..4553d0728 100644 --- a/test/app/server-tests.ts +++ b/test/app/server-tests.ts @@ -30,7 +30,6 @@ function createMockAppArgs(overrides: Partial = {}): AppArguments port: 10240, hostname: 'localhost', env: ['test'], - suppressConsoleLog: false, gitReleaseName: '', releaseBuildNumber: '', rootDir: '/test/root', @@ -44,6 +43,7 @@ function createMockAppArgs(overrides: Partial = {}): AppArguments metricsPort: undefined, useLocalProps: true, propDebug: false, + tmpDir: undefined, ...overrides, }; }