Add TypeScript integrations package
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { createDefaultIntegrationRegistry } from '../index.js';
|
||||
import { ConsoleLogger, DiscoveryEngine } from '../core/index.js';
|
||||
import { commandDiscover } from './commands.discover.js';
|
||||
import { commandInspect } from './commands.inspect.js';
|
||||
import { commandList } from './commands.list.js';
|
||||
import { commandSetup } from './commands.setup.js';
|
||||
|
||||
export class CliRuntime {
|
||||
public integrationRegistry = createDefaultIntegrationRegistry();
|
||||
public discoveryEngine = new DiscoveryEngine(this.integrationRegistry);
|
||||
public logger = new ConsoleLogger();
|
||||
|
||||
public async list(): Promise<string> {
|
||||
return commandList(this.integrationRegistry);
|
||||
}
|
||||
|
||||
public async inspect(domainArg: string): Promise<string> {
|
||||
return commandInspect(this.integrationRegistry, domainArg);
|
||||
}
|
||||
|
||||
public async discover() {
|
||||
return commandDiscover(this.discoveryEngine, {
|
||||
logger: this.logger,
|
||||
});
|
||||
}
|
||||
|
||||
public async setup(domainArg: string) {
|
||||
return commandSetup(this.integrationRegistry, domainArg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import type { DiscoveryEngine, IDiscoveryCandidate, IDiscoveryContext } from '../core/index.js';
|
||||
|
||||
export const commandDiscover = async (
|
||||
discoveryEngineArg: DiscoveryEngine,
|
||||
contextArg: IDiscoveryContext = {}
|
||||
): Promise<IDiscoveryCandidate[]> => {
|
||||
return discoveryEngineArg.runActiveDiscovery(contextArg);
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { IntegrationRegistry } from '../core/index.js';
|
||||
|
||||
export const commandInspect = (registryArg: IntegrationRegistry, domainArg: string): string => {
|
||||
const integration = registryArg.get(domainArg);
|
||||
if (!integration) {
|
||||
throw new Error(`Integration not found: ${domainArg}`);
|
||||
}
|
||||
|
||||
const descriptor = integration.discoveryDescriptor;
|
||||
const probes = descriptor.getProbes();
|
||||
const matchers = descriptor.getMatchers();
|
||||
const validators = descriptor.getValidators();
|
||||
|
||||
return [
|
||||
`Domain: ${integration.domain}`,
|
||||
`Name: ${integration.displayName}`,
|
||||
`Status: ${integration.status}`,
|
||||
'',
|
||||
'Discovery:',
|
||||
` probes: ${probes.length ? probes.map((probeArg) => probeArg.id).join(', ') : 'none'}`,
|
||||
` matchers: ${matchers.length ? matchers.map((matcherArg) => matcherArg.id).join(', ') : 'none'}`,
|
||||
` validators: ${validators.length ? validators.map((validatorArg) => validatorArg.id).join(', ') : 'none'}`,
|
||||
].join('\n');
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
import type { IntegrationRegistry } from '../core/index.js';
|
||||
|
||||
export const commandList = (registryArg: IntegrationRegistry): string => {
|
||||
return registryArg
|
||||
.list()
|
||||
.map((integrationArg) => `${integrationArg.domain}\t${integrationArg.displayName}\t${integrationArg.status}`)
|
||||
.join('\n');
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { IntegrationRegistry } from '../core/index.js';
|
||||
|
||||
export const commandSetup = async (registryArg: IntegrationRegistry, domainArg: string) => {
|
||||
const integration = registryArg.get(domainArg) as any;
|
||||
if (!integration) {
|
||||
throw new Error(`Integration not found: ${domainArg}`);
|
||||
}
|
||||
if (!integration.configFlow) {
|
||||
return {
|
||||
domain: domainArg,
|
||||
status: 'no-config-flow',
|
||||
};
|
||||
}
|
||||
const step = await integration.configFlow.start(
|
||||
{
|
||||
source: 'manual',
|
||||
integrationDomain: domainArg,
|
||||
id: `${domainArg}:manual`,
|
||||
},
|
||||
{}
|
||||
);
|
||||
return {
|
||||
domain: domainArg,
|
||||
step: {
|
||||
kind: step.kind,
|
||||
title: step.title,
|
||||
description: step.description,
|
||||
fields: step.fields,
|
||||
error: step.error,
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
import { CliRuntime } from './classes.cliruntime.js';
|
||||
|
||||
export * from './classes.cliruntime.js';
|
||||
export * from './commands.discover.js';
|
||||
export * from './commands.inspect.js';
|
||||
export * from './commands.list.js';
|
||||
export * from './commands.setup.js';
|
||||
|
||||
export const runCli = async (argvArg = process.argv.slice(2)) => {
|
||||
const runtime = new CliRuntime();
|
||||
const [commandArg = 'list', integrationArg] = argvArg;
|
||||
|
||||
if (commandArg === 'list') {
|
||||
console.log(await runtime.list());
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandArg === 'inspect') {
|
||||
console.log(await runtime.inspect(integrationArg || 'hue'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandArg === 'discover') {
|
||||
console.log(JSON.stringify(await runtime.discover(), null, 2));
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandArg === 'setup') {
|
||||
console.log(JSON.stringify(await runtime.setup(integrationArg || 'hue'), null, 2));
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(`Unknown integrations CLI command: ${commandArg}`);
|
||||
};
|
||||
|
||||
if (process.argv[1]?.endsWith('/cli.js') || process.argv[1]?.endsWith('/cli.ts.js')) {
|
||||
runCli().catch((errorArg) => {
|
||||
console.error(errorArg.message);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user