@open-wa/cli 5.0.0-alpha.8 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +2 -2
- package/README.md +1 -1
- package/dist/bin.cjs +7 -7
- package/dist/bin.d.cts +1 -1
- package/dist/{event-projection-store-BM9eOQXx.cjs → event-projection-store-DRXPBnNc.cjs} +23 -15
- package/dist/index.cjs +1 -2
- package/dist/index.d.cts +13 -13
- package/dist/index.d.cts.map +1 -1
- package/package.json +17 -11
- package/.open-wa/chrome-executable-path.json +0 -4
- package/.turbo/turbo-build.log +0 -16
- package/CHANGELOG.md +0 -79
- package/src/__tests__/cli-termination.test.ts +0 -50
- package/src/__tests__/event-projection-store.test.ts +0 -64
- package/src/__tests__/interactive-session.test.ts +0 -84
- package/src/__tests__/interactive-terminal.test.ts +0 -80
- package/src/__tests__/output-broker.test.ts +0 -66
- package/src/__tests__/output-mode.test.ts +0 -68
- package/src/__tests__/shutdown-controller.test.ts +0 -73
- package/src/bin.ts +0 -115
- package/src/config/cli-flags-schema.ts +0 -53
- package/src/index.ts +0 -7
- package/src/presenter/interactive-terminal.ts +0 -178
- package/src/runtime/cli-termination.ts +0 -66
- package/src/runtime/interactive-session.ts +0 -64
- package/src/runtime/output-broker.ts +0 -193
- package/src/runtime/output-mode.ts +0 -73
- package/src/runtime/shutdown-controller.ts +0 -86
- package/src/state/event-projection-store.ts +0 -166
- package/src/tunnel-client.ts +0 -169
- package/tsconfig.json +0 -23
- package/tsdown.config.ts +0 -8
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
|
|
3
|
-
describe('InteractiveTerminalPresenter', () => {
|
|
4
|
-
it('subscribes externally and rerenders Ink without React hooks in the presenter', async () => {
|
|
5
|
-
const rerender = vi.fn();
|
|
6
|
-
const clear = vi.fn();
|
|
7
|
-
const unmount = vi.fn();
|
|
8
|
-
const render = vi.fn(() => ({ rerender, clear, unmount }));
|
|
9
|
-
const createElement = vi.fn((type: unknown, props?: Record<string, unknown>, ...children: unknown[]) => ({
|
|
10
|
-
type,
|
|
11
|
-
props: { ...(props ?? {}), children },
|
|
12
|
-
}));
|
|
13
|
-
const subscribe = vi.fn((listener: (snapshot: unknown) => void) => {
|
|
14
|
-
subscribe.listener = listener;
|
|
15
|
-
listener({
|
|
16
|
-
phase: 'auth.qr',
|
|
17
|
-
sessionId: 'demo',
|
|
18
|
-
qr: { sessionId: 'demo', qr: 'qr-token' },
|
|
19
|
-
recentLogs: [],
|
|
20
|
-
messageCount: 0,
|
|
21
|
-
ackCount: 0,
|
|
22
|
-
ready: false,
|
|
23
|
-
});
|
|
24
|
-
return () => undefined;
|
|
25
|
-
}) as ReturnType<typeof vi.fn> & { listener?: () => void };
|
|
26
|
-
const getSnapshot = vi
|
|
27
|
-
.fn()
|
|
28
|
-
.mockReturnValueOnce({ phase: 'boot', recentLogs: [], messageCount: 0, ackCount: 0, ready: false })
|
|
29
|
-
.mockReturnValue({
|
|
30
|
-
phase: 'auth.qr',
|
|
31
|
-
sessionId: 'demo',
|
|
32
|
-
qr: { sessionId: 'demo', qr: 'qr-token' },
|
|
33
|
-
recentLogs: [],
|
|
34
|
-
messageCount: 0,
|
|
35
|
-
ackCount: 0,
|
|
36
|
-
ready: false,
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
vi.doMock('react', () => ({ createElement }));
|
|
40
|
-
vi.doMock('ink', () => ({
|
|
41
|
-
render,
|
|
42
|
-
Box: 'Box',
|
|
43
|
-
Text: 'Text',
|
|
44
|
-
}));
|
|
45
|
-
vi.doMock('qrcode', () => ({
|
|
46
|
-
default: {
|
|
47
|
-
toString: vi.fn(async () => 'QR ASCII'),
|
|
48
|
-
},
|
|
49
|
-
}));
|
|
50
|
-
|
|
51
|
-
const { InteractiveTerminalPresenter } = await import('../presenter/interactive-terminal');
|
|
52
|
-
const presenter = new InteractiveTerminalPresenter({
|
|
53
|
-
store: {
|
|
54
|
-
subscribe,
|
|
55
|
-
getSnapshot,
|
|
56
|
-
} as any,
|
|
57
|
-
stdout: { columns: 200 } as any,
|
|
58
|
-
stderr: {} as any,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
await presenter.start();
|
|
62
|
-
await Promise.resolve();
|
|
63
|
-
|
|
64
|
-
expect(render).toHaveBeenCalledTimes(1);
|
|
65
|
-
expect(rerender).toHaveBeenCalled();
|
|
66
|
-
|
|
67
|
-
const rerenderedTree = rerender.mock.calls.at(-1)?.[0] as { props?: { qrAscii?: string } } | undefined;
|
|
68
|
-
expect(rerenderedTree?.props?.qrAscii).toBe('QR ASCII');
|
|
69
|
-
expect(
|
|
70
|
-
createElement.mock.calls.some((call) => call.includes('Terminal window too small to render QR Code visually. Please expand.'))
|
|
71
|
-
).toBe(false);
|
|
72
|
-
|
|
73
|
-
presenter.stop();
|
|
74
|
-
|
|
75
|
-
vi.resetModules();
|
|
76
|
-
vi.doUnmock('react');
|
|
77
|
-
vi.doUnmock('ink');
|
|
78
|
-
vi.doUnmock('qrcode');
|
|
79
|
-
});
|
|
80
|
-
});
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { createOutputBroker } from '../runtime/output-broker';
|
|
3
|
-
|
|
4
|
-
describe('OutputBroker', () => {
|
|
5
|
-
afterEach(() => {
|
|
6
|
-
vi.restoreAllMocks();
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('intercepts console output and keeps a bounded buffer', () => {
|
|
10
|
-
const broker = createOutputBroker({ interactive: true, maxEntries: 2, passthrough: false });
|
|
11
|
-
broker.install();
|
|
12
|
-
const interceptedLog = console.log;
|
|
13
|
-
|
|
14
|
-
console.log('alpha');
|
|
15
|
-
console.warn('beta');
|
|
16
|
-
console.error('gamma');
|
|
17
|
-
|
|
18
|
-
const entries = broker.getEntries();
|
|
19
|
-
expect(entries).toHaveLength(2);
|
|
20
|
-
expect(entries.map((entry) => entry.message)).toEqual(['beta', 'gamma']);
|
|
21
|
-
|
|
22
|
-
broker.dispose();
|
|
23
|
-
expect(console.log).not.toBe(interceptedLog);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('passes through to the original console in plain mode', () => {
|
|
27
|
-
const forwarded: string[] = [];
|
|
28
|
-
const originalLog = console.log;
|
|
29
|
-
console.log = ((...args: unknown[]) => {
|
|
30
|
-
forwarded.push(args.join(' '));
|
|
31
|
-
}) as typeof console.log;
|
|
32
|
-
|
|
33
|
-
const broker = createOutputBroker({ interactive: false, maxEntries: 5, passthrough: true });
|
|
34
|
-
broker.install();
|
|
35
|
-
|
|
36
|
-
console.log('plain-text');
|
|
37
|
-
|
|
38
|
-
broker.dispose();
|
|
39
|
-
console.log = originalLog;
|
|
40
|
-
|
|
41
|
-
expect(forwarded).toEqual(['plain-text']);
|
|
42
|
-
expect(broker.getEntries()).toHaveLength(1);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('restores stdout writes after cleanup', () => {
|
|
46
|
-
const broker = createOutputBroker({ interactive: true, passthrough: false });
|
|
47
|
-
broker.install();
|
|
48
|
-
const interceptedWrite = process.stdout.write;
|
|
49
|
-
broker.dispose();
|
|
50
|
-
|
|
51
|
-
expect(process.stdout.write).not.toBe(interceptedWrite);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('can leave stdout and stderr untouched for Ink rendering compatibility', () => {
|
|
55
|
-
const originalStdoutWrite = process.stdout.write;
|
|
56
|
-
const originalStderrWrite = process.stderr.write;
|
|
57
|
-
const broker = createOutputBroker({ interactive: true, passthrough: false, interceptStreams: false });
|
|
58
|
-
|
|
59
|
-
broker.install();
|
|
60
|
-
|
|
61
|
-
expect(process.stdout.write).toBe(originalStdoutWrite);
|
|
62
|
-
expect(process.stderr.write).toBe(originalStderrWrite);
|
|
63
|
-
|
|
64
|
-
broker.dispose();
|
|
65
|
-
});
|
|
66
|
-
});
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { detectOutputMode } from '../runtime/output-mode';
|
|
3
|
-
|
|
4
|
-
describe('detectOutputMode', () => {
|
|
5
|
-
it('selects interactive mode when stdio is fully tty-capable', () => {
|
|
6
|
-
const result = detectOutputMode({
|
|
7
|
-
env: {},
|
|
8
|
-
stdin: { isTTY: true },
|
|
9
|
-
stdout: { isTTY: true },
|
|
10
|
-
stderr: { isTTY: true },
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
expect(result).toEqual({
|
|
14
|
-
kind: 'interactive',
|
|
15
|
-
interactive: true,
|
|
16
|
-
reason: 'tty-ready',
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('refuses interactive mode when stdout is not a tty', () => {
|
|
21
|
-
const result = detectOutputMode({
|
|
22
|
-
env: {},
|
|
23
|
-
stdin: { isTTY: true },
|
|
24
|
-
stdout: { isTTY: false },
|
|
25
|
-
stderr: { isTTY: true },
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
expect(result.reason).toBe('stdout-not-tty');
|
|
29
|
-
expect(result.interactive).toBe(false);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('refuses interactive mode when TERM is dumb', () => {
|
|
33
|
-
const result = detectOutputMode({
|
|
34
|
-
env: { TERM: 'dumb' },
|
|
35
|
-
stdin: { isTTY: true },
|
|
36
|
-
stdout: { isTTY: true },
|
|
37
|
-
stderr: { isTTY: true },
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
expect(result.reason).toBe('term-dumb');
|
|
41
|
-
expect(result.kind).toBe('plain');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('honors explicit non-interactive overrides', () => {
|
|
45
|
-
const result = detectOutputMode({
|
|
46
|
-
env: { OPEN_WA_NON_INTERACTIVE: '1' },
|
|
47
|
-
stdin: { isTTY: true },
|
|
48
|
-
stdout: { isTTY: true },
|
|
49
|
-
stderr: { isTTY: true },
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
expect(result.reason).toBe('env.non-interactive');
|
|
53
|
-
expect(result.kind).toBe('plain');
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('honors explicit interactive flag when tty checks pass', () => {
|
|
57
|
-
const result = detectOutputMode({
|
|
58
|
-
flags: { outputMode: 'interactive' },
|
|
59
|
-
env: {},
|
|
60
|
-
stdin: { isTTY: true },
|
|
61
|
-
stdout: { isTTY: true },
|
|
62
|
-
stderr: { isTTY: true },
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
expect(result.reason).toBe('flag.interactive');
|
|
66
|
-
expect(result.kind).toBe('interactive');
|
|
67
|
-
});
|
|
68
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { createShutdownController } from '../runtime/shutdown-controller';
|
|
3
|
-
|
|
4
|
-
describe('ShutdownController', () => {
|
|
5
|
-
it('runs graceful shutdown before cleanup on the first signal', async () => {
|
|
6
|
-
const order: string[] = [];
|
|
7
|
-
const controller = createShutdownController({
|
|
8
|
-
gracefulShutdown: async (signal) => {
|
|
9
|
-
order.push(`graceful:${signal}`);
|
|
10
|
-
},
|
|
11
|
-
cleanup: async () => {
|
|
12
|
-
order.push('cleanup');
|
|
13
|
-
},
|
|
14
|
-
exit: (code) => {
|
|
15
|
-
order.push(`exit:${code}`);
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
await controller.handleSignal('SIGINT');
|
|
20
|
-
|
|
21
|
-
expect(order).toEqual(['graceful:SIGINT', 'cleanup', 'exit:0']);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('escalates to a failing exit when graceful shutdown times out', async () => {
|
|
25
|
-
vi.useFakeTimers();
|
|
26
|
-
const order: string[] = [];
|
|
27
|
-
const controller = createShutdownController({
|
|
28
|
-
timeoutMs: 25,
|
|
29
|
-
gracefulShutdown: async () => {
|
|
30
|
-
await new Promise(() => undefined);
|
|
31
|
-
},
|
|
32
|
-
cleanup: async () => {
|
|
33
|
-
order.push('cleanup');
|
|
34
|
-
},
|
|
35
|
-
exit: (code) => {
|
|
36
|
-
order.push(`exit:${code}`);
|
|
37
|
-
},
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const promise = controller.handleSignal('SIGTERM');
|
|
41
|
-
await vi.advanceTimersByTimeAsync(25);
|
|
42
|
-
await promise;
|
|
43
|
-
|
|
44
|
-
expect(order).toEqual(['cleanup', 'exit:1']);
|
|
45
|
-
vi.useRealTimers();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('forces immediate exit on a second signal', async () => {
|
|
49
|
-
vi.useFakeTimers();
|
|
50
|
-
const order: string[] = [];
|
|
51
|
-
const controller = createShutdownController({
|
|
52
|
-
timeoutMs: 1_000,
|
|
53
|
-
gracefulShutdown: async () => {
|
|
54
|
-
order.push('graceful');
|
|
55
|
-
await new Promise(() => undefined);
|
|
56
|
-
},
|
|
57
|
-
cleanup: async () => {
|
|
58
|
-
order.push('cleanup');
|
|
59
|
-
},
|
|
60
|
-
exit: (code) => {
|
|
61
|
-
order.push(`exit:${code}`);
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const first = controller.handleSignal('SIGINT');
|
|
66
|
-
await controller.handleSignal('SIGINT');
|
|
67
|
-
|
|
68
|
-
expect(order).toEqual(['graceful', 'cleanup', 'exit:1']);
|
|
69
|
-
vi.runOnlyPendingTimers();
|
|
70
|
-
vi.useRealTimers();
|
|
71
|
-
void first;
|
|
72
|
-
});
|
|
73
|
-
});
|
package/src/bin.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { resetCliOutputSink, runCli, setCliOutputSink, type CliRuntimeResult } from '@open-wa/wa-automate';
|
|
3
|
-
import { TunnelClient } from './tunnel-client';
|
|
4
|
-
import { parseCliLocalFlags } from './config/cli-flags-schema';
|
|
5
|
-
import { performGracefulShutdown, writeVisibleFatalError } from './runtime/cli-termination';
|
|
6
|
-
import type { OutputBroker } from './runtime/output-broker';
|
|
7
|
-
import { detectOutputMode } from './runtime/output-mode';
|
|
8
|
-
import { createInteractiveCliSession } from './runtime/interactive-session';
|
|
9
|
-
import { createShutdownController } from './runtime/shutdown-controller';
|
|
10
|
-
import type { EventProjectionStore } from './state/event-projection-store';
|
|
11
|
-
|
|
12
|
-
async function main() {
|
|
13
|
-
const parsedLocalFlags = parseCliLocalFlags(process.argv.slice(2));
|
|
14
|
-
const outputMode = detectOutputMode({ flags: parsedLocalFlags.flags });
|
|
15
|
-
|
|
16
|
-
let broker: OutputBroker | undefined;
|
|
17
|
-
let store: EventProjectionStore | undefined;
|
|
18
|
-
let detachEvents: (() => void) | undefined;
|
|
19
|
-
let interactiveCleanup: ((options?: { clearPresenter?: boolean }) => void) | undefined;
|
|
20
|
-
|
|
21
|
-
const cleanupCliResources = (options: { clearPresenter?: boolean } = {}) => {
|
|
22
|
-
const clearPresenter = options.clearPresenter ?? true;
|
|
23
|
-
detachEvents?.();
|
|
24
|
-
detachEvents = undefined;
|
|
25
|
-
resetCliOutputSink();
|
|
26
|
-
interactiveCleanup?.({ clearPresenter });
|
|
27
|
-
interactiveCleanup = undefined;
|
|
28
|
-
broker = undefined;
|
|
29
|
-
store = undefined;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
if (outputMode.interactive) {
|
|
33
|
-
const session = await createInteractiveCliSession();
|
|
34
|
-
broker = session.broker;
|
|
35
|
-
store = session.store;
|
|
36
|
-
interactiveCleanup = session.cleanup;
|
|
37
|
-
setCliOutputSink(session.sink);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let runtime: CliRuntimeResult | undefined;
|
|
41
|
-
let tunnelClient: TunnelClient | undefined;
|
|
42
|
-
|
|
43
|
-
const shutdownController = createShutdownController({
|
|
44
|
-
timeoutMs: 10_000,
|
|
45
|
-
gracefulShutdown: async (signal) => {
|
|
46
|
-
await performGracefulShutdown({ runtime, tunnelClient, store }, signal);
|
|
47
|
-
},
|
|
48
|
-
cleanup: async () => {
|
|
49
|
-
cleanupCliResources();
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
shutdownController.install();
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
const result = await runCli(parsedLocalFlags.forwardedArgs);
|
|
57
|
-
runtime = result as CliRuntimeResult | undefined;
|
|
58
|
-
|
|
59
|
-
if (!runtime) {
|
|
60
|
-
shutdownController.dispose();
|
|
61
|
-
cleanupCliResources();
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (store) {
|
|
66
|
-
detachEvents = store.attachEmitter(runtime.events);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// If pm2 is used, result is void
|
|
70
|
-
if (runtime?.config.proxyHost && runtime.config.proxyToken) {
|
|
71
|
-
const log = (level: 'info' | 'warn' | 'error', message: string) => {
|
|
72
|
-
if (broker) {
|
|
73
|
-
broker.write(level, message);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (level === 'warn') {
|
|
78
|
-
console.warn(message);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (level === 'error') {
|
|
83
|
-
console.error(message);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
console.log(message);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
log('info', `[CLI] Starting TunnelClient connecting to ${runtime.config.proxyHost}...`);
|
|
91
|
-
tunnelClient = new TunnelClient({
|
|
92
|
-
proxyHost: runtime.config.proxyHost,
|
|
93
|
-
proxyToken: runtime.config.proxyToken,
|
|
94
|
-
sessionId: runtime.config.sessionId,
|
|
95
|
-
localSessionPort: runtime.config.port,
|
|
96
|
-
log,
|
|
97
|
-
});
|
|
98
|
-
tunnelClient.connect();
|
|
99
|
-
}
|
|
100
|
-
} catch (err: unknown) {
|
|
101
|
-
const message = err instanceof Error ? `${err.message}${err.stack ? `\n${err.stack}` : ''}` : String(err);
|
|
102
|
-
store?.projectStatus({ phase: 'error', detail: message });
|
|
103
|
-
shutdownController.dispose();
|
|
104
|
-
if (broker) {
|
|
105
|
-
broker.write('error', `Fatal error: ${message}`);
|
|
106
|
-
writeVisibleFatalError(message, (rawMessage) => broker?.writeRawStderr(rawMessage));
|
|
107
|
-
cleanupCliResources({ clearPresenter: false });
|
|
108
|
-
} else {
|
|
109
|
-
console.error('Fatal error:', err);
|
|
110
|
-
}
|
|
111
|
-
process.exit(1);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
main();
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
const OutputModeOverrideSchema = z.enum(['interactive', 'plain']);
|
|
4
|
-
|
|
5
|
-
export const CliLocalFlagsSchema = z.object({
|
|
6
|
-
outputMode: OutputModeOverrideSchema.optional(),
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
export type CliLocalFlags = z.infer<typeof CliLocalFlagsSchema>;
|
|
10
|
-
|
|
11
|
-
export interface ParsedCliLocalFlags {
|
|
12
|
-
flags: CliLocalFlags;
|
|
13
|
-
forwardedArgs: string[];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function parseCliLocalFlags(argv: string[]): ParsedCliLocalFlags {
|
|
17
|
-
const forwardedArgs: string[] = [];
|
|
18
|
-
let outputMode: CliLocalFlags['outputMode'];
|
|
19
|
-
|
|
20
|
-
for (let index = 0; index < argv.length; index += 1) {
|
|
21
|
-
const arg = argv[index];
|
|
22
|
-
|
|
23
|
-
if (arg === '--interactive') {
|
|
24
|
-
outputMode = 'interactive';
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (arg === '--non-interactive') {
|
|
29
|
-
outputMode = 'plain';
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (arg === '--output-mode') {
|
|
34
|
-
const value = argv[index + 1];
|
|
35
|
-
outputMode = OutputModeOverrideSchema.parse(value);
|
|
36
|
-
index += 1;
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const match = arg.match(/^--output-mode=(interactive|plain)$/);
|
|
41
|
-
if (match) {
|
|
42
|
-
outputMode = OutputModeOverrideSchema.parse(match[1]);
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
forwardedArgs.push(arg);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
flags: CliLocalFlagsSchema.parse({ outputMode }),
|
|
51
|
-
forwardedArgs,
|
|
52
|
-
};
|
|
53
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { runCli as main, startCli as start, parseCliArgs } from '@open-wa/wa-automate';
|
|
2
|
-
export { parseCliLocalFlags } from './config/cli-flags-schema';
|
|
3
|
-
export { createOutputBroker, OutputBroker } from './runtime/output-broker';
|
|
4
|
-
export { CliShutdownError, performGracefulShutdown, writeVisibleFatalError } from './runtime/cli-termination';
|
|
5
|
-
export { detectOutputMode } from './runtime/output-mode';
|
|
6
|
-
export { createShutdownController, ShutdownController } from './runtime/shutdown-controller';
|
|
7
|
-
export { createEventProjectionStore, EventProjectionStore } from './state/event-projection-store';
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import QRCode from 'qrcode';
|
|
2
|
-
import type { EventProjectionStore, ProjectionSnapshot } from '../state/event-projection-store';
|
|
3
|
-
|
|
4
|
-
export interface InteractiveTerminalPresenterOptions {
|
|
5
|
-
store: EventProjectionStore;
|
|
6
|
-
stdout?: NodeJS.WriteStream;
|
|
7
|
-
stderr?: NodeJS.WriteStream;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type InkInstance = {
|
|
11
|
-
clear?: () => void;
|
|
12
|
-
rerender?: (tree: unknown) => void;
|
|
13
|
-
unmount: () => void;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export class InteractiveTerminalPresenter {
|
|
17
|
-
private readonly store: EventProjectionStore;
|
|
18
|
-
private readonly stdout: NodeJS.WriteStream;
|
|
19
|
-
private readonly stderr: NodeJS.WriteStream;
|
|
20
|
-
private ink?: InkInstance;
|
|
21
|
-
private unsubscribe?: () => void;
|
|
22
|
-
private started = false;
|
|
23
|
-
private stopped = false;
|
|
24
|
-
private snapshot: ProjectionSnapshot;
|
|
25
|
-
private qrAscii?: string;
|
|
26
|
-
private qrValue?: string;
|
|
27
|
-
private renderTree?: () => unknown;
|
|
28
|
-
private qrSequence = 0;
|
|
29
|
-
|
|
30
|
-
constructor(options: InteractiveTerminalPresenterOptions) {
|
|
31
|
-
this.store = options.store;
|
|
32
|
-
this.stdout = options.stdout ?? process.stdout;
|
|
33
|
-
this.stderr = options.stderr ?? process.stderr;
|
|
34
|
-
this.snapshot = this.store.getSnapshot();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async start(): Promise<void> {
|
|
38
|
-
if (this.started) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
this.started = true;
|
|
43
|
-
|
|
44
|
-
const React = await import('react');
|
|
45
|
-
const ink = await import('ink');
|
|
46
|
-
|
|
47
|
-
await this.updateQrAscii(this.snapshot);
|
|
48
|
-
|
|
49
|
-
const LogStream = ({ snapshot }: { snapshot: ProjectionSnapshot }) => {
|
|
50
|
-
if (snapshot.recentLogs.length === 0) {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return React.createElement(
|
|
55
|
-
ink.Box,
|
|
56
|
-
{ flexDirection: 'column', marginTop: 1 },
|
|
57
|
-
React.createElement(ink.Text, { color: 'cyan' }, 'Recent Logs'),
|
|
58
|
-
...snapshot.recentLogs.slice(-12).map((entry, index) =>
|
|
59
|
-
React.createElement(
|
|
60
|
-
ink.Text,
|
|
61
|
-
{
|
|
62
|
-
key: `${entry.timestamp}-${index}`,
|
|
63
|
-
color: entry.level === 'error' ? 'red' : entry.level === 'warn' ? 'yellow' : 'white',
|
|
64
|
-
},
|
|
65
|
-
`[${entry.level}] ${entry.message}`
|
|
66
|
-
)
|
|
67
|
-
)
|
|
68
|
-
);
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const QrBlock = ({ snapshot, qrAscii }: { snapshot: ProjectionSnapshot; qrAscii?: string }) => {
|
|
72
|
-
if (!snapshot.qr) {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (!qrAscii) {
|
|
77
|
-
return React.createElement(ink.Text, { color: 'yellow' }, 'Generating QR view...');
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const qrLines = qrAscii.split('\n');
|
|
81
|
-
|
|
82
|
-
return React.createElement(
|
|
83
|
-
ink.Box,
|
|
84
|
-
{ flexDirection: 'column', marginTop: 1 },
|
|
85
|
-
React.createElement(ink.Text, { color: 'green' }, 'QR Code'),
|
|
86
|
-
...qrLines.map((line, index) => React.createElement(ink.Text, { key: `qr-${index}` }, line))
|
|
87
|
-
);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const App = ({ snapshot, qrAscii }: { snapshot: ProjectionSnapshot; qrAscii?: string }) =>
|
|
91
|
-
React.createElement(
|
|
92
|
-
ink.Box,
|
|
93
|
-
{ flexDirection: 'column', paddingX: 1 },
|
|
94
|
-
React.createElement(ink.Text, { color: 'greenBright' }, '[VT-OS] OPEN-WA CLI'),
|
|
95
|
-
React.createElement(ink.Text, null, 'Mode: ink-interactive'),
|
|
96
|
-
React.createElement(ink.Text, null, `Phase: ${snapshot.phase}`),
|
|
97
|
-
React.createElement(ink.Text, null, `Session: ${snapshot.sessionId ?? 'pending'}`),
|
|
98
|
-
React.createElement(ink.Text, null, `Ready: ${snapshot.ready ? 'yes' : 'no'}`),
|
|
99
|
-
React.createElement(ink.Text, null, `Messages: ${snapshot.messageCount} | Ack updates: ${snapshot.ackCount}`),
|
|
100
|
-
snapshot.detail
|
|
101
|
-
? React.createElement(ink.Text, { color: snapshot.phase === 'error' ? 'red' : 'yellow' }, `Detail: ${snapshot.detail}`)
|
|
102
|
-
: null,
|
|
103
|
-
React.createElement(QrBlock, { snapshot, qrAscii }),
|
|
104
|
-
React.createElement(LogStream, { snapshot })
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
this.renderTree = () => React.createElement(App, { snapshot: this.snapshot, qrAscii: this.qrAscii });
|
|
108
|
-
|
|
109
|
-
this.ink = ink.render(this.renderTree(), {
|
|
110
|
-
stdout: this.stdout,
|
|
111
|
-
stderr: this.stderr,
|
|
112
|
-
patchConsole: false,
|
|
113
|
-
exitOnCtrlC: false,
|
|
114
|
-
}) as InkInstance;
|
|
115
|
-
|
|
116
|
-
this.unsubscribe = this.store.subscribe((snapshot) => {
|
|
117
|
-
void this.handleSnapshot(snapshot);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
stop(options: { clear?: boolean } = {}): void {
|
|
122
|
-
if (this.stopped) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
this.stopped = true;
|
|
127
|
-
const clear = options.clear ?? true;
|
|
128
|
-
this.unsubscribe?.();
|
|
129
|
-
this.unsubscribe = undefined;
|
|
130
|
-
|
|
131
|
-
if (clear) {
|
|
132
|
-
this.ink?.clear?.();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
this.ink?.unmount();
|
|
136
|
-
this.ink = undefined;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private async handleSnapshot(snapshot: ProjectionSnapshot): Promise<void> {
|
|
140
|
-
if (this.stopped) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
this.snapshot = snapshot;
|
|
145
|
-
await this.updateQrAscii(snapshot);
|
|
146
|
-
this.rerender();
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
private rerender(): void {
|
|
150
|
-
if (this.stopped || !this.renderTree) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
this.ink?.rerender?.(this.renderTree());
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private async updateQrAscii(snapshot: ProjectionSnapshot): Promise<void> {
|
|
158
|
-
if (!snapshot.qr?.qr) {
|
|
159
|
-
this.qrValue = undefined;
|
|
160
|
-
this.qrAscii = undefined;
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (snapshot.qr.qr === this.qrValue && this.qrAscii) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
this.qrValue = snapshot.qr.qr;
|
|
169
|
-
const sequence = ++this.qrSequence;
|
|
170
|
-
const qrAscii = await QRCode.toString(snapshot.qr.qr, { type: 'terminal', small: true });
|
|
171
|
-
|
|
172
|
-
if (this.stopped || sequence !== this.qrSequence) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
this.qrAscii = qrAscii;
|
|
177
|
-
}
|
|
178
|
-
}
|