-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Setup.ts
122 lines (114 loc) 路 3.93 KB
/
Setup.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { IActionInit, IActorOutputInit } from '@comunica/bus-init';
import { ComponentsManager } from 'componentsjs';
import type {
IComponentsManagerBuilderOptions,
IConstructionStrategy,
ComponentRegistry,
ConfigRegistry,
IModuleState,
LogLevel,
} from 'componentsjs';
import type { Runner } from './Runner';
const process: NodeJS.Process = require('process/');
/**
* Helper functions to setup instances from a given comunica config file.
* This config file must be understandable by the Components.js framework.
*
* @link https://www.npmjs.com/package/lsd-components
*/
/**
* Instantiate the given component.
*
* @param {string} configResourceUrl The URL or local path to a Components.js config file.
* @param {string} instanceUri A URI identifying the component to instantiate.
* @param {ISetupProperties} properties Properties to pass to the Components.js manager.
* @return {Promise<any>} A promise that resolves to the instance.
*/
export async function instantiateComponent(
configResourceUrl: string,
instanceUri: string,
properties?: ISetupProperties,
): Promise<any> {
// Handle optional arguments
if (!properties) {
properties = {};
}
const propertiesActual: IComponentsManagerBuilderOptions<any> = { mainModulePath: process.cwd(), ...properties };
// Instantiate the given config file
const manager = await ComponentsManager.build(propertiesActual);
await manager.configRegistry.register(configResourceUrl);
return await manager.instantiate(instanceUri);
}
/**
* Run the given config file.
* This will initialize the runner, and deinitialize it once it has finished
*
* @param {string} configResourceUrl The URL or local path to a Components.js config file.
* @param {any[]} action The action to pass to the runner.
* @param {string} runnerUri An optional URI identifying the runner.
* @param {ISetupProperties} properties Properties to pass to the Components.js loader.
* @return {Promise<any>} A promise that resolves when the runner has been initialized.
*/
export async function run(
configResourceUrl: string,
action: IActionInit,
runnerUri?: string,
properties?: ISetupProperties,
): Promise<any> {
if (!runnerUri) {
runnerUri = 'urn:comunica:default:Runner';
}
const runner: Runner = await instantiateComponent(configResourceUrl, runnerUri, properties);
await runner.initialize();
let output: IActorOutputInit[];
try {
output = await runner.run(action);
} catch (error: unknown) {
await runner.deinitialize();
throw error;
}
await runner.deinitialize();
return output;
}
/**
* A copy of {@link IComponentsManagerBuilderOptions}, with all fields optional.
*/
export interface ISetupProperties {
/**
* Absolute path to the package root from which module resolution should start.
*/
mainModulePath?: string;
/**
* Callback for registering components and modules.
* Defaults to an invocation of {@link ComponentRegistry.registerAvailableModules}.
* @param registry A registry that accept component and module registrations.
*/
moduleLoader?: (registry: ComponentRegistry) => Promise<void>;
/**
* Callback for registering configurations.
* Defaults to no config registrations.
* @param registry A registry that accepts configuration registrations.
*/
configLoader?: (registry: ConfigRegistry) => Promise<void>;
/**
* A strategy for constructing instances.
* Defaults to {@link ConstructionStrategyCommonJs}.
*/
constructionStrategy?: IConstructionStrategy<any>;
/**
* If the error state should be dumped into `componentsjs-error-state.json`
* after failed instantiations.
* Defaults to `false`.
*/
dumpErrorState?: boolean;
/**
* The logging level.
* Defaults to `'warn'`.
*/
logLevel?: LogLevel;
/**
* The module state.
* Defaults to a newly created instances on the {@link mainModulePath}.
*/
moduleState?: IModuleState;
}