refactor: Improve logging initialization and refactor CLI options

- 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 <noreply@anthropic.com>
This commit is contained in:
Matt Godbolt
2025-05-13 16:04:14 -05:00
co-authored by Claude
parent 7158c5583f
commit 1007eed3eb
7 changed files with 45 additions and 32 deletions
+2 -2
View File
@@ -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;
};
+21 -10
View File
@@ -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}`);
+14 -3
View File
@@ -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) {
+5 -14
View File
@@ -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',
});
});
});
+1 -1
View File
@@ -32,7 +32,6 @@ function createMockAppArgs(overrides: Partial<AppArguments> = {}): AppArguments
port: 10240,
hostname: 'localhost',
env: ['prod'],
suppressConsoleLog: false,
gitReleaseName: '',
releaseBuildNumber: '',
rootDir: '/test/root',
@@ -46,6 +45,7 @@ function createMockAppArgs(overrides: Partial<AppArguments> = {}): AppArguments
metricsPort: undefined,
useLocalProps: true,
propDebug: false,
tmpDir: undefined,
...overrides,
};
}
+1 -1
View File
@@ -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 = {
+1 -1
View File
@@ -30,7 +30,6 @@ function createMockAppArgs(overrides: Partial<AppArguments> = {}): AppArguments
port: 10240,
hostname: 'localhost',
env: ['test'],
suppressConsoleLog: false,
gitReleaseName: '',
releaseBuildNumber: '',
rootDir: '/test/root',
@@ -44,6 +43,7 @@ function createMockAppArgs(overrides: Partial<AppArguments> = {}): AppArguments
metricsPort: undefined,
useLocalProps: true,
propDebug: false,
tmpDir: undefined,
...overrides,
};
}