Skip to content

Commit 0aad36a

Browse files
Copilothotlong
andcommitted
refactor: extract isHostConfig into shared plugin-detection utility
Address code review feedback: move host config detection logic into a dedicated, dependency-free utility module so serve.ts and tests share the same implementation. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent c3aeae9 commit 0aad36a

3 files changed

Lines changed: 19 additions & 11 deletions

File tree

packages/cli/src/commands/serve.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import net from 'net';
77
import chalk from 'chalk';
88
import { bundleRequire } from 'bundle-require';
99
import { loadConfig } from '../utils/config.js';
10+
import { isHostConfig } from '../utils/plugin-detection.js';
1011
import {
1112
printHeader,
1213
printKV,
@@ -193,10 +194,7 @@ export default class Serve extends Command {
193194
// Skip if config is a host/aggregator config that already contains
194195
// instantiated plugins — wrapping it would cause duplicate registration
195196
// and startup failures (e.g. plugin.app.dev-workspace).
196-
const isHostConfig = Array.isArray(config.plugins) &&
197-
config.plugins.some((p: any) => typeof p?.init === 'function');
198-
199-
if (!isHostConfig && (config.objects || config.manifest || config.apps)) {
197+
if (!isHostConfig(config) && (config.objects || config.manifest || config.apps)) {
200198
try {
201199
const { AppPlugin } = await import('@objectstack/runtime');
202200
await kernel.use(new AppPlugin(config));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* Detect whether a loaded config is a host/aggregator configuration.
5+
*
6+
* A host config already contains a `plugins` array with instantiated Plugin
7+
* objects (i.e. objects that have an `init` method). Such configs must NOT
8+
* be wrapped again by AppPlugin in the CLI, as that would cause duplicate
9+
* plugin registration and startup failures.
10+
*/
11+
export function isHostConfig(config: any): boolean {
12+
return (
13+
Array.isArray(config.plugins) &&
14+
config.plugins.some((p: any) => typeof p?.init === 'function')
15+
);
16+
}

packages/cli/test/serve-host-config.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, it, expect } from 'vitest';
2+
import { isHostConfig } from '../src/utils/plugin-detection';
23

34
/**
45
* Tests for the host config detection logic used in serve.ts
@@ -9,13 +10,6 @@ import { describe, it, expect } from 'vitest';
910
* and a `plugin.app.<id> failed to start` error.
1011
*/
1112
describe('Host config detection', () => {
12-
// Mirrors the detection logic in serve.ts L196-197
13-
function isHostConfig(config: any): boolean {
14-
return (
15-
Array.isArray(config.plugins) &&
16-
config.plugins.some((p: any) => typeof p?.init === 'function')
17-
);
18-
}
1913

2014
it('should detect host config with instantiated plugins', () => {
2115
const config = {

0 commit comments

Comments
 (0)