@homelib/terminal 0.0.0-wip.01265bf1cc90
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 +19 -0
- package/README.md +11 -0
- package/bld/library/@bootstrap-arguments.d.ts +1 -0
- package/bld/library/@bootstrap-arguments.js +12 -0
- package/bld/library/@bootstrap-arguments.js.map +1 -0
- package/bld/library/@log-format.d.ts +3 -0
- package/bld/library/@log-format.js +8 -0
- package/bld/library/@log-format.js.map +1 -0
- package/bld/library/@tui/bindings-page.d.ts +90 -0
- package/bld/library/@tui/bindings-page.js +414 -0
- package/bld/library/@tui/bindings-page.js.map +1 -0
- package/bld/library/@tui/provider-page.d.ts +10 -0
- package/bld/library/@tui/provider-page.js +6 -0
- package/bld/library/@tui/provider-page.js.map +1 -0
- package/bld/library/@tui/providers-page.d.ts +14 -0
- package/bld/library/@tui/providers-page.js +31 -0
- package/bld/library/@tui/providers-page.js.map +1 -0
- package/bld/library/@tui/startup-page.d.ts +14 -0
- package/bld/library/@tui/startup-page.js +39 -0
- package/bld/library/@tui/startup-page.js.map +1 -0
- package/bld/library/@tui/startup.d.ts +9 -0
- package/bld/library/@tui/startup.js +371 -0
- package/bld/library/@tui/startup.js.map +1 -0
- package/bld/library/index.d.ts +2 -0
- package/bld/library/index.js +22 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/log.d.ts +2 -0
- package/bld/library/log.js +74 -0
- package/bld/library/log.js.map +1 -0
- package/bld/library/tui.d.ts +30 -0
- package/bld/library/tui.js +72 -0
- package/bld/library/tui.js.map +1 -0
- package/package.json +25 -0
- package/src/library/@bootstrap-arguments.ts +13 -0
- package/src/library/@log-format.ts +19 -0
- package/src/library/@tui/bindings-page.tsx +987 -0
- package/src/library/@tui/provider-page.tsx +36 -0
- package/src/library/@tui/providers-page.tsx +97 -0
- package/src/library/@tui/startup-page.tsx +113 -0
- package/src/library/@tui/startup.tsx +679 -0
- package/src/library/index.ts +30 -0
- package/src/library/log.ts +127 -0
- package/src/library/tui.tsx +201 -0
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BindingFile,
|
|
3
|
+
type BootstrapBindingDevice,
|
|
4
|
+
type BootstrapBindingEndpoint,
|
|
5
|
+
type BootstrapBindingScope,
|
|
6
|
+
type BootstrapContext,
|
|
7
|
+
type BootstrapProvider,
|
|
8
|
+
type EndpointBinding,
|
|
9
|
+
type EndpointPath,
|
|
10
|
+
type ProviderBindingDevice,
|
|
11
|
+
applyProviderBindingRequests,
|
|
12
|
+
getEndpointPathKey,
|
|
13
|
+
removeEndpointBinding,
|
|
14
|
+
} from '@homelib/core';
|
|
15
|
+
import {render, useApp, useInput} from 'ink';
|
|
16
|
+
import {useRef, useState} from 'react';
|
|
17
|
+
|
|
18
|
+
import {ProviderBindingOutlet, ProviderDetailsOutlet} from '../tui.js';
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
type BindingDeviceItem,
|
|
22
|
+
BindingDevicePage,
|
|
23
|
+
BindingProviderPage,
|
|
24
|
+
type BindingScopeItem,
|
|
25
|
+
BindingScopePage,
|
|
26
|
+
BindingsPage,
|
|
27
|
+
type StaleBindingItem,
|
|
28
|
+
StaleBindingsPage,
|
|
29
|
+
getBindingSummary,
|
|
30
|
+
} from './bindings-page.js';
|
|
31
|
+
import {ProviderPage} from './provider-page.js';
|
|
32
|
+
import {ProvidersPage} from './providers-page.js';
|
|
33
|
+
import {StartupPage, type StartupPageSelection} from './startup-page.js';
|
|
34
|
+
|
|
35
|
+
export type StartupTuiModel = {
|
|
36
|
+
readonly scriptName: string;
|
|
37
|
+
readonly providers: readonly BootstrapProvider[];
|
|
38
|
+
readonly bindingScopes: readonly BootstrapBindingScope[];
|
|
39
|
+
readonly bindingFile: BindingFile;
|
|
40
|
+
readonly updateBindingFile: BootstrapContext['updateBindingFile'];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export async function presentStartup(
|
|
44
|
+
context: BootstrapContext,
|
|
45
|
+
scriptName: string,
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
if (!isInteractiveTerminal()) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const model: StartupTuiModel = {
|
|
52
|
+
scriptName,
|
|
53
|
+
providers: context.providers,
|
|
54
|
+
bindingScopes: context.bindingScopes,
|
|
55
|
+
bindingFile: context.initialBindingFile,
|
|
56
|
+
updateBindingFile: context.updateBindingFile,
|
|
57
|
+
};
|
|
58
|
+
const instance = render(<Startup model={model} />, {alternateScreen: true});
|
|
59
|
+
let result: unknown;
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
result = await instance.waitUntilExit();
|
|
63
|
+
} finally {
|
|
64
|
+
instance.cleanup();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (result === 'run') {
|
|
68
|
+
return;
|
|
69
|
+
} else if (result === undefined) {
|
|
70
|
+
await interruptProcess();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
throw new TypeError('Unexpected startup page result.');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type StartupProps = {
|
|
77
|
+
readonly model: StartupTuiModel;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
export function Startup({model}: StartupProps): React.JSX.Element {
|
|
82
|
+
const {exit} = useApp();
|
|
83
|
+
const [page, setPage] = useState<StartupTuiPage>({type: 'startup'});
|
|
84
|
+
const [bindingFile, setBindingFile] = useState(model.bindingFile);
|
|
85
|
+
const bindingMutationQueueReference = useRef<Promise<void>>(
|
|
86
|
+
Promise.resolve(),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const bindingScopeItems = createBindingScopeItems(
|
|
90
|
+
model.bindingScopes,
|
|
91
|
+
bindingFile,
|
|
92
|
+
);
|
|
93
|
+
const staleBindingItems = createStaleBindingItems(
|
|
94
|
+
model.bindingScopes,
|
|
95
|
+
bindingFile,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
useInput(input => {
|
|
99
|
+
if (input === 'q' && page.type !== 'startup') {
|
|
100
|
+
setPage({type: 'startup'});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const handleSelect = (selection: StartupPageSelection): void => {
|
|
105
|
+
if (selection === 'run') {
|
|
106
|
+
void bindingMutationQueueReference.current.then(() => {
|
|
107
|
+
exit(selection);
|
|
108
|
+
});
|
|
109
|
+
} else if (selection === 'providers') {
|
|
110
|
+
setPage({type: 'providers'});
|
|
111
|
+
} else {
|
|
112
|
+
setPage({type: 'bindings'});
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const mutateBindingFile = (
|
|
117
|
+
createNextBindingFile: (currentBindingFile: BindingFile) => BindingFile,
|
|
118
|
+
): Promise<void> => {
|
|
119
|
+
const operation = bindingMutationQueueReference.current.then(async () => {
|
|
120
|
+
const nextBindingFile = await model.updateBindingFile(
|
|
121
|
+
createNextBindingFile,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
setBindingFile(nextBindingFile);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
bindingMutationQueueReference.current = operation.catch(() => undefined);
|
|
128
|
+
return operation;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
if (page.type === 'bindings') {
|
|
132
|
+
return (
|
|
133
|
+
<BindingsPage
|
|
134
|
+
model={{
|
|
135
|
+
scriptName: model.scriptName,
|
|
136
|
+
scopes: bindingScopeItems,
|
|
137
|
+
staleBindingCount: staleBindingItems.length,
|
|
138
|
+
}}
|
|
139
|
+
onBack={() => {
|
|
140
|
+
setPage({type: 'startup'});
|
|
141
|
+
}}
|
|
142
|
+
onSelect={scope => {
|
|
143
|
+
setPage({type: 'binding-scope', scopePath: scope.path});
|
|
144
|
+
}}
|
|
145
|
+
onSelectStaleBindings={() => {
|
|
146
|
+
setPage({type: 'stale-bindings'});
|
|
147
|
+
}}
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
} else if (page.type === 'stale-bindings') {
|
|
151
|
+
return (
|
|
152
|
+
<StaleBindingsPage
|
|
153
|
+
model={{
|
|
154
|
+
scriptName: model.scriptName,
|
|
155
|
+
bindings: staleBindingItems,
|
|
156
|
+
}}
|
|
157
|
+
onBack={() => {
|
|
158
|
+
setPage({type: 'bindings'});
|
|
159
|
+
}}
|
|
160
|
+
onRemove={async binding => {
|
|
161
|
+
await mutateBindingFile(currentBindingFile => {
|
|
162
|
+
const pathKey = getEndpointPathKey(binding.path);
|
|
163
|
+
const bindingStillExists = currentBindingFile.bindings.some(
|
|
164
|
+
item => getEndpointPathKey(item.endpoint) === pathKey,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
if (!bindingStillExists) {
|
|
168
|
+
throw new Error('stale binding no longer exists.');
|
|
169
|
+
} else if (
|
|
170
|
+
findStartupBindingEndpoint(model.bindingScopes, binding.path) !==
|
|
171
|
+
undefined
|
|
172
|
+
) {
|
|
173
|
+
throw new Error('endpoint binding is no longer stale.');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return removeEndpointBinding(currentBindingFile, binding.path);
|
|
177
|
+
});
|
|
178
|
+
}}
|
|
179
|
+
/>
|
|
180
|
+
);
|
|
181
|
+
} else if (page.type === 'binding-scope') {
|
|
182
|
+
const scope = findBindingScopeItem(bindingScopeItems, page.scopePath);
|
|
183
|
+
|
|
184
|
+
if (scope === undefined) {
|
|
185
|
+
throw new TypeError('Unknown binding scope selected.');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return (
|
|
189
|
+
<BindingScopePage
|
|
190
|
+
key={JSON.stringify(scope.path)}
|
|
191
|
+
model={{scriptName: model.scriptName, scope}}
|
|
192
|
+
onBack={() => {
|
|
193
|
+
const parent = findParentBindingScopeItem(
|
|
194
|
+
bindingScopeItems,
|
|
195
|
+
scope.path,
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
if (parent === undefined) {
|
|
199
|
+
setPage({type: 'bindings'});
|
|
200
|
+
} else {
|
|
201
|
+
setPage({type: 'binding-scope', scopePath: parent.path});
|
|
202
|
+
}
|
|
203
|
+
}}
|
|
204
|
+
onSelectScope={selectedScope => {
|
|
205
|
+
setPage({type: 'binding-scope', scopePath: selectedScope.path});
|
|
206
|
+
}}
|
|
207
|
+
onSelectDevice={device => {
|
|
208
|
+
setPage({
|
|
209
|
+
type: 'binding-device',
|
|
210
|
+
scopePath: scope.path,
|
|
211
|
+
deviceName: device.name,
|
|
212
|
+
});
|
|
213
|
+
}}
|
|
214
|
+
/>
|
|
215
|
+
);
|
|
216
|
+
} else if (page.type === 'binding-device') {
|
|
217
|
+
const device = findBindingDeviceItem(
|
|
218
|
+
bindingScopeItems,
|
|
219
|
+
page.scopePath,
|
|
220
|
+
page.deviceName,
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
if (device === undefined) {
|
|
224
|
+
throw new TypeError('Unknown binding device selected.');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return (
|
|
228
|
+
<BindingDevicePage
|
|
229
|
+
model={{
|
|
230
|
+
scriptName: model.scriptName,
|
|
231
|
+
scopePath: page.scopePath,
|
|
232
|
+
device,
|
|
233
|
+
providers: model.providers.map(({namespace, provider}) => ({
|
|
234
|
+
namespace,
|
|
235
|
+
name: provider.name,
|
|
236
|
+
boundEndpointCount: getProviderBoundEndpointCount(
|
|
237
|
+
page.scopePath,
|
|
238
|
+
device,
|
|
239
|
+
bindingFile,
|
|
240
|
+
namespace,
|
|
241
|
+
provider.name,
|
|
242
|
+
),
|
|
243
|
+
})),
|
|
244
|
+
}}
|
|
245
|
+
onBack={() => {
|
|
246
|
+
setPage({type: 'binding-scope', scopePath: page.scopePath});
|
|
247
|
+
}}
|
|
248
|
+
onSelectProvider={provider => {
|
|
249
|
+
const providerItem = findProvider(
|
|
250
|
+
model.providers,
|
|
251
|
+
provider.namespace,
|
|
252
|
+
provider.name,
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
if (providerItem === undefined) {
|
|
256
|
+
throw new TypeError('Unknown binding provider selected.');
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
setPage({
|
|
260
|
+
type: 'binding-provider',
|
|
261
|
+
scopePath: page.scopePath,
|
|
262
|
+
deviceName: page.deviceName,
|
|
263
|
+
provider: providerItem,
|
|
264
|
+
});
|
|
265
|
+
}}
|
|
266
|
+
onUnbind={async endpoint => {
|
|
267
|
+
await mutateBindingFile(currentBindingFile => {
|
|
268
|
+
const currentBinding = findEndpointBinding(
|
|
269
|
+
currentBindingFile,
|
|
270
|
+
endpoint.path,
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
if (currentBinding === undefined) {
|
|
274
|
+
throw new Error('endpoint binding no longer exists.');
|
|
275
|
+
} else if (
|
|
276
|
+
currentBinding.provider.namespace !==
|
|
277
|
+
endpoint.provider.namespace ||
|
|
278
|
+
currentBinding.provider.name !== endpoint.provider.name
|
|
279
|
+
) {
|
|
280
|
+
throw new Error('endpoint binding changed; go back and retry.');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return removeEndpointBinding(currentBindingFile, endpoint.path);
|
|
284
|
+
});
|
|
285
|
+
}}
|
|
286
|
+
/>
|
|
287
|
+
);
|
|
288
|
+
} else if (page.type === 'binding-provider') {
|
|
289
|
+
const device = findStartupBindingDevice(
|
|
290
|
+
model.bindingScopes,
|
|
291
|
+
page.scopePath,
|
|
292
|
+
page.deviceName,
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
if (device === undefined) {
|
|
296
|
+
throw new TypeError('Unknown provider binding device selected.');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const providerReference = {
|
|
300
|
+
namespace: page.provider.namespace,
|
|
301
|
+
name: page.provider.provider.name,
|
|
302
|
+
};
|
|
303
|
+
const providerBindingDevice = createProviderBindingDevice(
|
|
304
|
+
device,
|
|
305
|
+
bindingFile,
|
|
306
|
+
);
|
|
307
|
+
const activeBindings = getActiveEndpointBindings(
|
|
308
|
+
model.bindingScopes,
|
|
309
|
+
bindingFile,
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
return (
|
|
313
|
+
<BindingProviderPage
|
|
314
|
+
model={{
|
|
315
|
+
scriptName: model.scriptName,
|
|
316
|
+
scopePath: page.scopePath,
|
|
317
|
+
deviceName: page.deviceName,
|
|
318
|
+
provider: providerReference,
|
|
319
|
+
}}
|
|
320
|
+
>
|
|
321
|
+
<ProviderBindingOutlet
|
|
322
|
+
bindings={activeBindings}
|
|
323
|
+
device={providerBindingDevice}
|
|
324
|
+
provider={page.provider.provider}
|
|
325
|
+
providerReference={providerReference}
|
|
326
|
+
onBack={() => {
|
|
327
|
+
setPage({
|
|
328
|
+
type: 'binding-device',
|
|
329
|
+
scopePath: page.scopePath,
|
|
330
|
+
deviceName: page.deviceName,
|
|
331
|
+
});
|
|
332
|
+
}}
|
|
333
|
+
onComplete={() => {
|
|
334
|
+
setPage({type: 'binding-scope', scopePath: page.scopePath});
|
|
335
|
+
}}
|
|
336
|
+
onBind={async requests => {
|
|
337
|
+
await mutateBindingFile(currentBindingFile => {
|
|
338
|
+
return applyProviderBindingRequests(
|
|
339
|
+
currentBindingFile,
|
|
340
|
+
requests,
|
|
341
|
+
providerReference,
|
|
342
|
+
page.provider.provider,
|
|
343
|
+
providerBindingDevice,
|
|
344
|
+
model.bindingScopes,
|
|
345
|
+
);
|
|
346
|
+
});
|
|
347
|
+
}}
|
|
348
|
+
/>
|
|
349
|
+
</BindingProviderPage>
|
|
350
|
+
);
|
|
351
|
+
} else if (page.type === 'providers') {
|
|
352
|
+
return (
|
|
353
|
+
<ProvidersPage
|
|
354
|
+
model={{
|
|
355
|
+
scriptName: model.scriptName,
|
|
356
|
+
providers: model.providers.map(({namespace, provider}) => ({
|
|
357
|
+
namespace,
|
|
358
|
+
name: provider.name,
|
|
359
|
+
})),
|
|
360
|
+
}}
|
|
361
|
+
onBack={() => {
|
|
362
|
+
setPage({type: 'startup'});
|
|
363
|
+
}}
|
|
364
|
+
onSelect={provider => {
|
|
365
|
+
const providerItem = findProvider(
|
|
366
|
+
model.providers,
|
|
367
|
+
provider.namespace,
|
|
368
|
+
provider.name,
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
if (providerItem === undefined) {
|
|
372
|
+
throw new TypeError('Unknown provider selected.');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
setPage({type: 'provider', provider: providerItem});
|
|
376
|
+
}}
|
|
377
|
+
/>
|
|
378
|
+
);
|
|
379
|
+
} else if (page.type === 'provider') {
|
|
380
|
+
const onBack = (): void => {
|
|
381
|
+
setPage({type: 'providers'});
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
return (
|
|
385
|
+
<ProviderPage
|
|
386
|
+
model={{
|
|
387
|
+
scriptName: model.scriptName,
|
|
388
|
+
provider: {
|
|
389
|
+
namespace: page.provider.namespace,
|
|
390
|
+
name: page.provider.provider.name,
|
|
391
|
+
},
|
|
392
|
+
}}
|
|
393
|
+
>
|
|
394
|
+
<ProviderDetailsOutlet
|
|
395
|
+
provider={page.provider.provider}
|
|
396
|
+
onBack={onBack}
|
|
397
|
+
/>
|
|
398
|
+
</ProviderPage>
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const bindingSummary = getBindingSummary(bindingScopeItems);
|
|
403
|
+
|
|
404
|
+
return (
|
|
405
|
+
<StartupPage
|
|
406
|
+
model={{
|
|
407
|
+
scriptName: model.scriptName,
|
|
408
|
+
providerCount: model.providers.length,
|
|
409
|
+
endpoints: {
|
|
410
|
+
boundCount: bindingSummary.configuredEndpointCount,
|
|
411
|
+
unboundCount: bindingSummary.unconfiguredEndpointCount,
|
|
412
|
+
},
|
|
413
|
+
}}
|
|
414
|
+
onSelect={handleSelect}
|
|
415
|
+
/>
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
type StartupTuiPage =
|
|
420
|
+
| {readonly type: 'startup'}
|
|
421
|
+
| {readonly type: 'bindings'}
|
|
422
|
+
| {readonly type: 'stale-bindings'}
|
|
423
|
+
| {readonly type: 'binding-scope'; readonly scopePath: readonly string[]}
|
|
424
|
+
| {
|
|
425
|
+
readonly type: 'binding-device';
|
|
426
|
+
readonly scopePath: readonly string[];
|
|
427
|
+
readonly deviceName: string;
|
|
428
|
+
}
|
|
429
|
+
| {
|
|
430
|
+
readonly type: 'binding-provider';
|
|
431
|
+
readonly scopePath: readonly string[];
|
|
432
|
+
readonly deviceName: string;
|
|
433
|
+
readonly provider: BootstrapProvider;
|
|
434
|
+
}
|
|
435
|
+
| {readonly type: 'providers'}
|
|
436
|
+
| {readonly type: 'provider'; readonly provider: BootstrapProvider};
|
|
437
|
+
|
|
438
|
+
function createBindingScopeItems(
|
|
439
|
+
scopes: readonly BootstrapBindingScope[],
|
|
440
|
+
bindingFile: BindingFile,
|
|
441
|
+
): readonly BindingScopeItem[] {
|
|
442
|
+
return scopes.map(scope => createBindingScopeItem(scope, bindingFile));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function createBindingScopeItem(
|
|
446
|
+
scope: BootstrapBindingScope,
|
|
447
|
+
bindingFile: BindingFile,
|
|
448
|
+
): BindingScopeItem {
|
|
449
|
+
return {
|
|
450
|
+
path: scope.path,
|
|
451
|
+
scopes: scope.scopes.map(childScope =>
|
|
452
|
+
createBindingScopeItem(childScope, bindingFile),
|
|
453
|
+
),
|
|
454
|
+
devices: scope.devices.map(device => ({
|
|
455
|
+
name: device.name,
|
|
456
|
+
endpoints: device.endpoints.map(endpoint => {
|
|
457
|
+
const binding = findEndpointBinding(bindingFile, endpoint.path);
|
|
458
|
+
|
|
459
|
+
return {
|
|
460
|
+
path: endpoint.path,
|
|
461
|
+
name: endpoint.endpoint.name,
|
|
462
|
+
provider: binding?.provider,
|
|
463
|
+
};
|
|
464
|
+
}),
|
|
465
|
+
})),
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function createStaleBindingItems(
|
|
470
|
+
scopes: readonly BootstrapBindingScope[],
|
|
471
|
+
bindingFile: BindingFile,
|
|
472
|
+
): readonly StaleBindingItem[] {
|
|
473
|
+
return bindingFile.bindings.flatMap(binding =>
|
|
474
|
+
findStartupBindingEndpoint(scopes, binding.endpoint) === undefined
|
|
475
|
+
? [{path: binding.endpoint, provider: binding.provider}]
|
|
476
|
+
: [],
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** @internal */
|
|
481
|
+
export function getActiveEndpointBindings(
|
|
482
|
+
scopes: readonly BootstrapBindingScope[],
|
|
483
|
+
bindingFile: BindingFile,
|
|
484
|
+
): readonly EndpointBinding[] {
|
|
485
|
+
return bindingFile.bindings.filter(
|
|
486
|
+
binding =>
|
|
487
|
+
findStartupBindingEndpoint(scopes, binding.endpoint) !== undefined,
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** @internal */
|
|
492
|
+
export function createProviderBindingDevice(
|
|
493
|
+
device: BootstrapBindingDevice,
|
|
494
|
+
bindingFile: BindingFile,
|
|
495
|
+
): ProviderBindingDevice {
|
|
496
|
+
const bindingMap = new Map(
|
|
497
|
+
bindingFile.bindings.map(binding => [
|
|
498
|
+
getEndpointPathKey(binding.endpoint),
|
|
499
|
+
binding,
|
|
500
|
+
]),
|
|
501
|
+
);
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
name: device.name,
|
|
505
|
+
deviceConstructors: device.deviceConstructors,
|
|
506
|
+
endpoints: device.endpoints.map(endpoint => ({
|
|
507
|
+
path: endpoint.path,
|
|
508
|
+
endpoint: endpoint.endpoint,
|
|
509
|
+
binding: bindingMap.get(getEndpointPathKey(endpoint.path)),
|
|
510
|
+
})),
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function findBindingScopeItem(
|
|
515
|
+
scopes: readonly BindingScopeItem[],
|
|
516
|
+
path: readonly string[],
|
|
517
|
+
): BindingScopeItem | undefined {
|
|
518
|
+
for (const scope of scopes) {
|
|
519
|
+
if (pathsEqual(scope.path, path)) {
|
|
520
|
+
return scope;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const child = findBindingScopeItem(scope.scopes, path);
|
|
524
|
+
|
|
525
|
+
if (child !== undefined) {
|
|
526
|
+
return child;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
return undefined;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function findParentBindingScopeItem(
|
|
534
|
+
scopes: readonly BindingScopeItem[],
|
|
535
|
+
path: readonly string[],
|
|
536
|
+
): BindingScopeItem | undefined {
|
|
537
|
+
for (const scope of scopes) {
|
|
538
|
+
if (scope.scopes.some(child => pathsEqual(child.path, path))) {
|
|
539
|
+
return scope;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const parent = findParentBindingScopeItem(scope.scopes, path);
|
|
543
|
+
|
|
544
|
+
if (parent !== undefined) {
|
|
545
|
+
return parent;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return undefined;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function findBindingDeviceItem(
|
|
553
|
+
scopes: readonly BindingScopeItem[],
|
|
554
|
+
scopePath: readonly string[],
|
|
555
|
+
deviceName: string,
|
|
556
|
+
): BindingDeviceItem | undefined {
|
|
557
|
+
return findBindingScopeItem(scopes, scopePath)?.devices.find(
|
|
558
|
+
device => device.name === deviceName,
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function findStartupBindingDevice(
|
|
563
|
+
scopes: readonly BootstrapBindingScope[],
|
|
564
|
+
scopePath: readonly string[],
|
|
565
|
+
deviceName: string,
|
|
566
|
+
): BootstrapBindingDevice | undefined {
|
|
567
|
+
for (const scope of scopes) {
|
|
568
|
+
if (pathsEqual(scope.path, scopePath)) {
|
|
569
|
+
return scope.devices.find(device => device.name === deviceName);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const device = findStartupBindingDevice(
|
|
573
|
+
scope.scopes,
|
|
574
|
+
scopePath,
|
|
575
|
+
deviceName,
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
if (device !== undefined) {
|
|
579
|
+
return device;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
return undefined;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function findProvider(
|
|
587
|
+
providers: readonly BootstrapProvider[],
|
|
588
|
+
namespace: string,
|
|
589
|
+
name: string,
|
|
590
|
+
): BootstrapProvider | undefined {
|
|
591
|
+
return providers.find(
|
|
592
|
+
item => item.namespace === namespace && item.provider.name === name,
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function findEndpointBinding(
|
|
597
|
+
bindingFile: BindingFile,
|
|
598
|
+
path: EndpointPath,
|
|
599
|
+
): EndpointBinding | undefined {
|
|
600
|
+
const pathKey = getEndpointPathKey(path);
|
|
601
|
+
|
|
602
|
+
return bindingFile.bindings.find(
|
|
603
|
+
binding => getEndpointPathKey(binding.endpoint) === pathKey,
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function findStartupBindingEndpoint(
|
|
608
|
+
scopes: readonly BootstrapBindingScope[],
|
|
609
|
+
path: EndpointPath,
|
|
610
|
+
): BootstrapBindingEndpoint | undefined {
|
|
611
|
+
const pathKey = getEndpointPathKey(path);
|
|
612
|
+
|
|
613
|
+
for (const scope of scopes) {
|
|
614
|
+
for (const device of scope.devices) {
|
|
615
|
+
const endpoint = device.endpoints.find(
|
|
616
|
+
item => getEndpointPathKey(item.path) === pathKey,
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
if (endpoint !== undefined) {
|
|
620
|
+
return endpoint;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const endpoint = findStartupBindingEndpoint(scope.scopes, path);
|
|
625
|
+
|
|
626
|
+
if (endpoint !== undefined) {
|
|
627
|
+
return endpoint;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
return undefined;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function getProviderBoundEndpointCount(
|
|
635
|
+
scopePath: readonly string[],
|
|
636
|
+
device: BindingDeviceItem,
|
|
637
|
+
bindingFile: BindingFile,
|
|
638
|
+
namespace: string,
|
|
639
|
+
providerName: string,
|
|
640
|
+
): number {
|
|
641
|
+
const deviceEndpointNameSet = new Set(
|
|
642
|
+
device.endpoints.map(endpoint => endpoint.name),
|
|
643
|
+
);
|
|
644
|
+
|
|
645
|
+
return bindingFile.bindings.filter(
|
|
646
|
+
binding =>
|
|
647
|
+
binding.provider.namespace === namespace &&
|
|
648
|
+
binding.provider.name === providerName &&
|
|
649
|
+
pathsEqual(binding.endpoint.scopePath, scopePath) &&
|
|
650
|
+
binding.endpoint.deviceName === device.name &&
|
|
651
|
+
deviceEndpointNameSet.has(binding.endpoint.endpointName),
|
|
652
|
+
).length;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function pathsEqual(
|
|
656
|
+
left: readonly string[],
|
|
657
|
+
right: readonly string[],
|
|
658
|
+
): boolean {
|
|
659
|
+
return (
|
|
660
|
+
left.length === right.length &&
|
|
661
|
+
left.every((segment, index) => segment === right[index])
|
|
662
|
+
);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function isInteractiveTerminal(): boolean {
|
|
666
|
+
const stdinIsTty = process.stdin.isTTY;
|
|
667
|
+
const stdoutIsTty = process.stdout.isTTY;
|
|
668
|
+
|
|
669
|
+
if (stdinIsTty === undefined || stdoutIsTty === undefined) {
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
return stdinIsTty && stdoutIsTty;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function interruptProcess(): Promise<never> {
|
|
677
|
+
process.kill(process.pid, 'SIGINT');
|
|
678
|
+
return new Promise<never>(() => undefined);
|
|
679
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {basename} from 'node:path';
|
|
2
|
+
|
|
3
|
+
import {addLogListener, registerBootstrapFrontend} from '@homelib/core';
|
|
4
|
+
|
|
5
|
+
import {isRunRequested} from './@bootstrap-arguments.js';
|
|
6
|
+
import {presentStartup} from './@tui/startup.js';
|
|
7
|
+
import {writeLogEvent} from './log.js';
|
|
8
|
+
|
|
9
|
+
registerBootstrapFrontend(context => {
|
|
10
|
+
if (isRunRequested(process.argv)) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return presentStartup(context, getScriptName());
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
addLogListener(writeLogEvent);
|
|
18
|
+
|
|
19
|
+
export * from './log.js';
|
|
20
|
+
export * from './tui.js';
|
|
21
|
+
|
|
22
|
+
function getScriptName(): string {
|
|
23
|
+
const scriptPath = process.argv.at(1);
|
|
24
|
+
|
|
25
|
+
if (scriptPath === undefined) {
|
|
26
|
+
return 'automation';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return basename(scriptPath);
|
|
30
|
+
}
|