-
Notifications
You must be signed in to change notification settings - Fork 31
/
Settings.ts
256 lines (223 loc) · 6.14 KB
/
Settings.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import fs from "fs";
import path from "path";
import yargs from "yargs";
export class ConfluenceSettings {
confluenceBaseUrl: string;
confluenceParentId: string;
atlassianUserName: string;
atlassianApiToken: string;
folderToPublish: string;
contentRoot: string;
}
export const DEFAULT_SETTINGS = new ConfluenceSettings();
DEFAULT_SETTINGS.confluenceBaseUrl = "";
DEFAULT_SETTINGS.confluenceParentId = "";
DEFAULT_SETTINGS.atlassianUserName = "";
DEFAULT_SETTINGS.atlassianApiToken = "";
DEFAULT_SETTINGS.folderToPublish = "Confluence Pages";
DEFAULT_SETTINGS.contentRoot = process.cwd();
export abstract class SettingsLoader {
abstract loadPartial(): Partial<ConfluenceSettings>;
load(): ConfluenceSettings {
const initialSettings = this.loadPartial();
const settings = this.validateSettings(initialSettings);
return settings;
}
protected validateSettings(
settings: Partial<ConfluenceSettings>
): ConfluenceSettings {
if (!settings.confluenceBaseUrl) {
throw new Error("Confluence base URL is required");
}
if (!settings.confluenceParentId) {
throw new Error("Confluence parent ID is required");
}
if (!settings.atlassianUserName) {
throw new Error("Atlassian user name is required");
}
if (!settings.atlassianApiToken) {
throw new Error("Atlassian API token is required");
}
if (!settings.folderToPublish) {
throw new Error("Folder to publish is required");
}
if (!settings.contentRoot) {
throw new Error("Content root is required");
} else {
if (!settings.contentRoot.endsWith("/")) {
settings.contentRoot += "/";
}
}
return settings as ConfluenceSettings;
}
}
export class DefaultSettingsLoader extends SettingsLoader {
loadPartial(): Partial<ConfluenceSettings> {
return DEFAULT_SETTINGS;
}
}
export class StaticSettingsLoader extends SettingsLoader {
constructor(private settings: Partial<ConfluenceSettings>) {
super();
}
loadPartial(): Partial<ConfluenceSettings> {
return this.settings;
}
}
export class EnvironmentVariableSettingsLoader extends SettingsLoader {
loadPartial(): Partial<ConfluenceSettings> {
return {
confluenceBaseUrl: process.env.CONFLUENCE_BASE_URL,
confluenceParentId: process.env.CONFLUENCE_PARENT_ID,
atlassianUserName: process.env.ATLASSIAN_USERNAME,
atlassianApiToken: process.env.ATLASSIAN_API_TOKEN,
folderToPublish: process.env.FOLDER_TO_PUBLISH,
contentRoot: process.env.CONFLUENCE_CONTENT_ROOT,
};
}
}
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 element = config[key as keyof ConfluenceSettings];
if (element) {
result[key as keyof ConfluenceSettings] = element;
}
}
}
return result;
} catch {
return {};
}
}
}
export class CommandLineArgumentSettingsLoader extends SettingsLoader {
loadPartial(): Partial<ConfluenceSettings> {
const options = yargs(process.argv)
.usage("Usage: $0 [options]")
.option("config", {
alias: "c",
describe: "Path to the config file",
type: "string",
default: path.join(
process.env.HOME ?? "",
".mermaid-confluence.json"
),
demandOption: false,
})
.option("baseUrl", {
alias: "b",
describe: "Confluence base URL",
type: "string",
demandOption: false,
})
.option("parentId", {
alias: "p",
describe: "Confluence parent ID",
type: "string",
demandOption: false,
})
.option("userName", {
alias: "u",
describe: "Atlassian user name",
type: "string",
demandOption: false,
})
.option("apiToken", {
describe: "Atlassian API token",
type: "string",
demandOption: false,
})
.option("enableFolder", {
alias: "f",
describe: "Folder enable to publish",
type: "string",
demandOption: false,
})
.option("contentRoot", {
alias: "cr",
describe:
"Root to search for files to publish. All files must be part of this directory.",
type: "string",
demandOption: false,
})
.parseSync();
return {
confluenceBaseUrl: options.baseUrl,
confluenceParentId: options.parentId,
atlassianUserName: options.userName,
atlassianApiToken: options.apiToken,
folderToPublish: options.enableFolder,
contentRoot: options.contentRoot,
};
}
}
export class AutoSettingsLoader extends SettingsLoader {
constructor(private loaders: SettingsLoader[] = []) {
super();
if (loaders.length === 0) {
this.loaders.push(new DefaultSettingsLoader());
this.loaders.push(new ConfigFileSettingsLoader());
this.loaders.push(new EnvironmentVariableSettingsLoader());
this.loaders.push(new CommandLineArgumentSettingsLoader());
}
}
private combineSettings(): ConfluenceSettings {
let settings: Partial<ConfluenceSettings> = {};
for (const loader of this.loaders) {
const partialSettings = loader.loadPartial();
for (const key in partialSettings) {
if (
Object.prototype.hasOwnProperty.call(partialSettings, key)
) {
const element =
partialSettings[key as keyof ConfluenceSettings];
if (element) {
settings = {
...settings,
[key as keyof ConfluenceSettings]: element,
};
}
}
}
}
return settings as ConfluenceSettings;
}
loadPartial(): Partial<ConfluenceSettings> {
return this.combineSettings();
}
}