Skip to content

Commit 196b8b1

Browse files
Refactor: Decouple component logging from local storage flag
1 parent 13ed32f commit 196b8b1

5 files changed

Lines changed: 32 additions & 15 deletions

File tree

src/cli/facade/dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
204204
hotReloading,
205205
liveReloadPort: liveReload.port,
206206
local: true,
207-
enableComponentConsoleOutput: true,
207+
componentConsole: console,
208208
postRequestPayloadSize,
209209
components: opts.components,
210210
path: path.resolve(componentsDir),

src/registry/domain/options-sanitiser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import zlib from 'node:zlib';
22
import { compileSync } from 'oc-client-browser';
33
import settings from '../../resources/settings';
44
import type { Config } from '../../types';
5+
import createNoopConsole from '../../utils/noop-console';
56
import * as auth from './authentication';
67

78
const DEFAULT_NODE_KEEPALIVE_MS = 5000;
@@ -174,8 +175,8 @@ export default function optionsSanitiser(input: RegistryOptions): Config {
174175
options.tarExtractMode = 766;
175176
}
176177

177-
if (typeof options.enableComponentConsoleOutput === 'undefined') {
178-
options.enableComponentConsoleOutput = false;
178+
if (!options.componentConsole) {
179+
options.componentConsole = createNoopConsole();
179180
}
180181

181182
if (

src/registry/routes/helpers/get-component.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ export interface GetComponentResult {
7070
}
7171

7272
export const stream = Symbol('stream');
73-
const noop = () => {};
74-
const noopConsole = Object.fromEntries(
75-
Object.keys(console).map((key) => [key, noop])
76-
);
7773

7874
/**
7975
* Converts the plugins to a function that returns a record of plugins with the context applied
@@ -676,9 +672,7 @@ export default function getComponent(conf: Config, repository: Repository) {
676672
exports: {} as Record<string, (...args: any[]) => any>
677673
},
678674
exports: {} as Record<string, (...args: any[]) => any>,
679-
console: conf.enableComponentConsoleOutput
680-
? console
681-
: noopConsole,
675+
console: conf.componentConsole,
682676
setTimeout,
683677
Buffer,
684678
AbortController: globalThis?.AbortController,

src/types.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,23 @@ export interface Config<T = any> {
290290
*/
291291
local: boolean;
292292
/**
293-
* Enables component console output (console.log, console.error, etc.)
294-
* during component execution. Useful for debugging in development and
295-
* lower environments.
293+
* Console interface provided to components during execution.
294+
* Allows for flexible logging strategies: pass the real console, a custom
295+
* implementation that sends logs to a monitoring provider, or a no-op console.
296296
*
297-
* @default false
297+
* @example
298+
* // Log to console
299+
* componentConsole: console
300+
* @example
301+
* // Log to monitoring provider
302+
* componentConsole: createCustomConsole(monitoringClient)
303+
* @example
304+
* // Disable component logs
305+
* componentConsole: createNoopConsole()
306+
*
307+
* @default createNoopConsole() - a no-op console that discards all logs
298308
*/
299-
enableComponentConsoleOutput: boolean;
309+
componentConsole: Partial<Console>;
300310
/**
301311
* File and directory mode (octal) applied when extracting tarballs during
302312
* publishing.

src/utils/noop-console.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Creates a no-op console object that silently discards all logging calls.
3+
* Useful as a default console implementation when component logging is disabled.
4+
*
5+
* @returns Console object with all methods mapped to no-op functions
6+
*/
7+
export function createNoopConsole(): Partial<Console> {
8+
const noop = () => {};
9+
return Object.fromEntries(Object.keys(console).map((key) => [key, noop]));
10+
}
11+
12+
export default createNoopConsole;

0 commit comments

Comments
 (0)