/
load-bootstrap-options.function.ts
57 lines (51 loc) · 2.06 KB
/
load-bootstrap-options.function.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
import {
SakuliBootstrapDefaults,
SakuliBootstrapOptions,
} from "./bootstrap-options.interface";
import { ifPresent, Maybe } from "@sakuli/commons";
import { getNodeModulesPaths } from "./get-node-modules-paths.function";
import { findSakuliPresets } from "./find-sakuli-presets.function";
import { cwd } from "process";
import { getPresetDeclarationFromFile } from "./get-preset-declaration-from-file.function";
import { join } from "path";
/**
* Reads sakuli bootconfiguration from a json file. The configuration is considered under a sakuli wich must implement the {@see SakuliBootstrapOptions} interface.*
*
* Per default it assumes the package.json in the current directory
*
* If no there is neither a readable file nor the corresponding key in the file the function will return an empty default config.
*
*/
export async function loadBootstrapOptions(): Promise<SakuliBootstrapOptions> {
let conf: Maybe<SakuliBootstrapOptions>;
try {
let sakuliBootstrapOptions: SakuliBootstrapOptions = { presetProvider: [] };
const nodeModulesPaths = await getNodeModulesPaths();
sakuliBootstrapOptions.presetProvider = findSakuliPresets(nodeModulesPaths);
const packageJsonPath = join(cwd(), "package.json");
const packagePresets = await getPresetDeclarationFromFile(packageJsonPath);
packagePresets.length
? console.debug(
`Manual configuration in package.json found: ${packagePresets}`
)
: console.debug("No sakuli preset configuration in package.json found");
for (const packagePreset of packagePresets) {
if (!sakuliBootstrapOptions.presetProvider.includes(packagePreset)) {
sakuliBootstrapOptions.presetProvider.push(packagePreset);
}
}
if (sakuliBootstrapOptions.presetProvider.length) {
console.info(
`Loading Sakuli with ${sakuliBootstrapOptions.presetProvider}`
);
}
conf = sakuliBootstrapOptions;
} catch (e) {
console.warn(`Could not load bootstrap options, because: ${e}`);
}
return ifPresent(
conf,
(c) => c,
() => SakuliBootstrapDefaults
);
}