Files
ccs/src/docker/docker-bootstrap.ts
T
Tam Nhu Tran 1c72b4b8d5 refactor(cliproxy): flatten module structure and colocate tests (#1135)
- Split types.ts (331 LOC) into 4 concern-based files under types/:
  platform-types, binary-types, provider-types, config-types
- Preserve backward compat via barrel re-export (types.ts → types/index)
- Reorganize 53 root-level files into 8 subdirectories:
  accounts/, ai-providers/, auth/, binary/, config/, executor/,
  management/, proxy/, quota/, routing/, services/, sync/
- Reduce src/cliproxy/ root from 65 to 8 files (target: ≤10)
- Colocate 87 unit tests from tests/unit/cliproxy/ into
  src/cliproxy/*/__tests__/ (13 colocated test directories)
- Update import paths across 40+ consumer files
- Add TDD backward-compat test for types split

Refs #1135
2026-04-29 17:06:39 -04:00

83 lines
2.6 KiB
TypeScript

import { spawn } from 'child_process';
import { ensureCLIProxyBinary } from '../cliproxy/binary-manager';
import {
configExists,
configNeedsRegeneration,
generateConfig,
getCliproxyWritablePath,
regenerateConfig,
} from '../cliproxy/config/config-generator';
import { CLIPROXY_DEFAULT_PORT } from '../cliproxy/config/port-manager';
import { getCliproxyConfigPath } from '../cliproxy/config/path-resolver';
import { registerSession, unregisterSession } from '../cliproxy/session-tracker';
import { getInstalledCliproxyVersion } from '../cliproxy/binary-manager';
async function prepareIntegratedRuntime(): Promise<{ binaryPath: string; configPath: string }> {
const binaryPath = await ensureCLIProxyBinary(false);
const configPath = !configExists(CLIPROXY_DEFAULT_PORT)
? generateConfig('gemini', CLIPROXY_DEFAULT_PORT)
: configNeedsRegeneration()
? regenerateConfig(CLIPROXY_DEFAULT_PORT)
: getCliproxyConfigPath();
return { binaryPath, configPath };
}
async function runCliproxy(): Promise<number> {
const { binaryPath, configPath } = await prepareIntegratedRuntime();
return new Promise<number>((resolve, reject) => {
const child = spawn(binaryPath, ['--config', configPath], {
stdio: 'inherit',
env: {
...process.env,
WRITABLE_PATH: getCliproxyWritablePath(),
},
});
// Register session lock so dashboard can detect the running proxy
let sessionId: string | undefined;
child.on('spawn', () => {
if (!child.pid) return;
try {
const version = getInstalledCliproxyVersion();
sessionId = registerSession(CLIPROXY_DEFAULT_PORT, child.pid, version, 'plus');
} catch (err) {
console.error(
`[cliproxy] Failed to register session lock: ${err instanceof Error ? err.message : String(err)}`
);
}
});
child.on('error', reject);
child.on('close', (code) => {
if (sessionId) {
unregisterSession(sessionId, CLIPROXY_DEFAULT_PORT);
}
resolve(code ?? 1);
});
});
}
async function main(): Promise<number> {
const command = process.argv[2];
if (command !== 'run-cliproxy') {
console.error('[X] Usage: node dist/docker/docker-bootstrap.js run-cliproxy');
return 1;
}
return runCliproxy();
}
if (require.main === module) {
void main()
.then((exitCode) => {
process.exitCode = exitCode;
})
.catch((error) => {
console.error(
`[X] Failed to prepare Docker runtime: ${error instanceof Error ? error.message : String(error)}`
);
process.exitCode = 1;
});
}