-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathconnect.ts
More file actions
86 lines (80 loc) · 2.19 KB
/
connect.ts
File metadata and controls
86 lines (80 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Command } from 'commander';
import { connect, setup } from 'gridplus-sdk';
import {
error,
getStoredClient,
hasSession,
info,
loadSession,
output,
setStoredClient,
success,
withSpinner,
} from '../lib/index.js';
export const connectCommand = new Command('connect')
.description('Connect to a previously configured Lattice device')
.option('-j, --json', 'Output in JSON format')
.action(async (options) => {
try {
// Check if we have a saved session
if (!hasSession()) {
error('No device configured. Run "gp setup" first.');
process.exit(1);
}
const session = loadSession();
if (!session) {
error('Failed to load session data.');
process.exit(1);
}
info(`Connecting to device: ${session.deviceId}`);
// Initialize the SDK with stored credentials
const isPaired = await withSpinner('Connecting...', async () => {
// First setup the SDK state handlers
await setup({
getStoredClient,
setStoredClient,
});
// Then connect to the device
return connect(session.deviceId);
});
if (isPaired) {
success('Connected and paired!');
if (options.json) {
output(
{
status: 'connected',
paired: true,
deviceId: session.deviceId,
baseUrl: session.baseUrl,
appName: session.name,
},
'json',
);
} else {
output({
deviceId: session.deviceId,
baseUrl: session.baseUrl,
appName: session.name,
status: 'paired',
});
}
} else {
success('Connected but not paired.');
info('Run "gp pair" to pair with the device.');
if (options.json) {
output(
{
status: 'connected',
paired: false,
deviceId: session.deviceId,
},
'json',
);
}
}
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
error(`Connection failed: ${message}`);
process.exit(1);
}
});