forked from mongodb/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withConfig.ts
119 lines (108 loc) · 2.87 KB
/
withConfig.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
import yargs from "yargs";
import Path from "path";
import { logger } from "mongodb-rag-core";
import { EvalConfig } from "./EvalConfig";
export type LoadConfigArgs = {
config?: string;
};
export const loadConfig = async ({
config: configPathIn,
}: LoadConfigArgs): Promise<EvalConfig> => {
const path = Path.resolve(
configPathIn === undefined ? "eval.config.cjs" : configPathIn
);
const partialConfigConstructor = (await import(path))
.default as () => Promise<Partial<EvalConfig>>;
const partialConfig = await partialConfigConstructor();
const missingProperties: string[] = [];
const config: EvalConfig = {
...partialConfig,
metadataStore: checkRequiredProperty(
partialConfig,
"metadataStore",
missingProperties
),
generatedDataStore: checkRequiredProperty(
partialConfig,
"generatedDataStore",
missingProperties
),
evaluationStore: checkRequiredProperty(
partialConfig,
"evaluationStore",
missingProperties
),
reportStore: checkRequiredProperty(
partialConfig,
"reportStore",
missingProperties
),
commands: checkRequiredProperty(
partialConfig,
"commands",
missingProperties
),
};
if (missingProperties.length !== 0) {
throw new Error(
`Config is missing the following properties: ${missingProperties.join(
", "
)}`
);
}
return config;
};
export const withConfig = async <T>(
action: (config: EvalConfig, args: T) => Promise<void>,
args: LoadConfigArgs & T
) => {
const config = await loadConfig(args);
try {
return await action(config, args);
} catch (error) {
logger.error(`Action failed: ${(error as Error).message}`);
throw error;
} finally {
// Close all closable resources
await Promise.all(
Object.values(config)
.filter((v) => v.close !== undefined)
.map((v) => v.close as () => Promise<void>)
);
// Run afterAll if it exists
await config.afterAll?.();
process.exit(0);
}
};
/**
Apply config options to CLI command.
*/
export const withConfigOptions = <T>(
args: yargs.Argv<T>
): yargs.Argv<T & LoadConfigArgs> => {
return args.option("config", {
string: true,
description: "Path to config JS file.",
});
};
/**
Asserts that the given property is defined in the given object and returns
that value as a definitely not undefined type.
*/
function checkRequiredProperty<T, K extends keyof T>(
object: T,
k: K,
missingProperties: string[]
): Exclude<T[K], undefined> {
const value = object[k];
if (value === undefined) {
missingProperties.push(k.toString());
// Hack: this is an invalid value. The caller MUST check the errors
return undefined as Exclude<T[K], undefined>;
}
return value as Exclude<T[K], undefined>;
}
type Closeable = {
close?(): Promise<void>;
};
type CleanupFunc = () => Promise<void>;