-
Notifications
You must be signed in to change notification settings - Fork 31
/
ConfigFileSettingsLoader.ts
65 lines (55 loc) · 1.43 KB
/
ConfigFileSettingsLoader.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
import path from "path";
import { ConfluenceSettings, DEFAULT_SETTINGS } from "../Settings";
import { SettingsLoader } from "./SettingsLoader";
import fs from "fs";
import yargs from "yargs";
export class ConfigFileSettingsLoader extends SettingsLoader {
private configPath: string = path.join(
process.cwd() ?? "",
".markdown-confluence.json"
);
constructor(configPath?: string) {
super();
if (configPath) {
this.configPath = configPath;
return;
}
if (
"CONFLUENCE_CONFIG_FILE" in process.env &&
process.env["CONFLUENCE_CONFIG_FILE"]
) {
this.configPath = process.env["CONFLUENCE_CONFIG_FILE"];
}
const options = yargs(process.argv)
.option("config", {
alias: "c",
describe: "Path to the config file",
type: "string",
default: this.configPath,
demandOption: false,
})
.parseSync();
this.configPath = options.config;
}
loadPartial(): Partial<ConfluenceSettings> {
try {
const configData = fs.readFileSync(this.configPath, {
encoding: "utf-8",
});
const config = JSON.parse(configData);
const result: Partial<ConfluenceSettings> = {};
for (const key in DEFAULT_SETTINGS) {
if (Object.prototype.hasOwnProperty.call(config, key)) {
const propertyKey = key as keyof ConfluenceSettings;
const element = config[propertyKey];
if (element) {
result[propertyKey] = element;
}
}
}
return result;
} catch {
return {};
}
}
}