perf(cli): cache devices runtime import

This commit is contained in:
Vincent Koc
2026-05-29 21:26:12 +02:00
parent 1ff95ff3e6
commit bdd9c70787

View File

@@ -17,6 +17,14 @@ type DevicesRpcOpts = {
const DEFAULT_DEVICES_TIMEOUT_MS = 10_000;
type DevicesRuntimeModule = typeof import("./devices-cli.runtime.js");
let devicesRuntimePromise: Promise<DevicesRuntimeModule> | undefined;
function loadDevicesRuntime(): Promise<DevicesRuntimeModule> {
return (devicesRuntimePromise ??= import("./devices-cli.runtime.js"));
}
const devicesCallOpts = (cmd: Command, defaults?: { timeoutMs?: number }) =>
cmd
.option("--url <url>", "Gateway WebSocket URL (defaults to gateway.remote.url when configured)")
@@ -37,7 +45,7 @@ export function registerDevicesCli(program: Command) {
.command("list")
.description("List pending and paired devices")
.action(async (opts: DevicesRpcOpts) => {
const { runDevicesListCommand } = await import("./devices-cli.runtime.js");
const { runDevicesListCommand } = await loadDevicesRuntime();
await runDevicesListCommand(opts);
}),
);
@@ -48,7 +56,7 @@ export function registerDevicesCli(program: Command) {
.description("Remove a paired device entry")
.argument("<deviceId>", "Paired device id")
.action(async (deviceId: string, opts: DevicesRpcOpts) => {
const { runDevicesRemoveCommand } = await import("./devices-cli.runtime.js");
const { runDevicesRemoveCommand } = await loadDevicesRuntime();
await runDevicesRemoveCommand(deviceId, opts);
}),
);
@@ -60,7 +68,7 @@ export function registerDevicesCli(program: Command) {
.option("--pending", "Also reject all pending pairing requests", false)
.option("--yes", "Confirm destructive clear", false)
.action(async (opts: DevicesRpcOpts) => {
const { runDevicesClearCommand } = await import("./devices-cli.runtime.js");
const { runDevicesClearCommand } = await loadDevicesRuntime();
await runDevicesClearCommand(opts);
}),
);
@@ -72,7 +80,7 @@ export function registerDevicesCli(program: Command) {
.argument("[requestId]", "Pending request id")
.option("--latest", "Show the most recent pending request to approve explicitly", false)
.action(async (requestId: string | undefined, opts: DevicesRpcOpts) => {
const { runDevicesApproveCommand } = await import("./devices-cli.runtime.js");
const { runDevicesApproveCommand } = await loadDevicesRuntime();
await runDevicesApproveCommand(requestId, opts);
}),
);
@@ -83,7 +91,7 @@ export function registerDevicesCli(program: Command) {
.description("Reject a pending device pairing request")
.argument("<requestId>", "Pending request id")
.action(async (requestId: string, opts: DevicesRpcOpts) => {
const { runDevicesRejectCommand } = await import("./devices-cli.runtime.js");
const { runDevicesRejectCommand } = await loadDevicesRuntime();
await runDevicesRejectCommand(requestId, opts);
}),
);
@@ -96,7 +104,7 @@ export function registerDevicesCli(program: Command) {
.requiredOption("--role <role>", "Role name")
.option("--scope <scope...>", "Scopes to attach to the token (repeatable)")
.action(async (opts: DevicesRpcOpts) => {
const { runDevicesRotateCommand } = await import("./devices-cli.runtime.js");
const { runDevicesRotateCommand } = await loadDevicesRuntime();
await runDevicesRotateCommand(opts);
}),
);
@@ -108,7 +116,7 @@ export function registerDevicesCli(program: Command) {
.requiredOption("--device <id>", "Device id")
.requiredOption("--role <role>", "Role name")
.action(async (opts: DevicesRpcOpts) => {
const { runDevicesRevokeCommand } = await import("./devices-cli.runtime.js");
const { runDevicesRevokeCommand } = await loadDevicesRuntime();
await runDevicesRevokeCommand(opts);
}),
);