|
1 | 1 | import * as path from "path"; |
2 | 2 | import * as fs from "fs"; |
3 | | -import { Config } from "./types/Config"; |
| 3 | +import { Config, ConfigKey, isBasicAuth, isTokenAuth } from "./types/Config"; |
4 | 4 | import * as inquirer from "inquirer"; |
5 | 5 | import signale from "signale"; |
6 | 6 |
|
| 7 | + |
| 8 | +function overwriteConfigKeyWithEnvVarIfPresent<T>(config: Partial<Config>, configKey: ConfigKey, envKey: string): Partial<Config> { |
| 9 | + if (configKey === "pages") { |
| 10 | + throw Error("Cannot override pages using environment variable"); |
| 11 | + } |
| 12 | + |
| 13 | + if (process.env[envKey] !== undefined) { |
| 14 | + return { ...config, [configKey]: process.env[envKey] }; |
| 15 | + } else { |
| 16 | + return config; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | + |
7 | 21 | export class ConfigLoader { |
8 | | - static async load(configPath: string | null): Promise<Config> { |
9 | | - return await ConfigLoader.promptUserAndPassIfNotSet( |
10 | | - ConfigLoader.overwriteAuthFromConfigWithEnvIfPresent(ConfigLoader.readConfigFromFile(configPath)), |
11 | | - ); |
| 22 | + static async load(configPath: string | null): Promise<Partial<Config>> { |
| 23 | + let config = ConfigLoader.readConfigFromFile(configPath); |
| 24 | + config = ConfigLoader.overwriteAuthFromConfigWithEnvIfPresent(config), |
| 25 | + config = await ConfigLoader.promptUserAndPassIfNoCredentialsSet(config); |
| 26 | + return config; |
12 | 27 | } |
13 | 28 |
|
14 | | - private static readConfigFromFile(configPath: string | null): Config { |
| 29 | + private static readConfigFromFile(configPath: string | null): Partial<Config> { |
15 | 30 | configPath = path.resolve(configPath || path.join("cosmere.json")); |
16 | 31 | if (!fs.existsSync(configPath!)) { |
17 | 32 | signale.fatal(`File "${configPath}" not found!`); |
18 | 33 | process.exit(1); |
19 | 34 | } |
20 | 35 |
|
21 | | - let config = JSON.parse(fs.readFileSync(configPath!, "utf8")) as Config; |
22 | | - for (const i in config.pages) { |
23 | | - config.pages[i].file = path.isAbsolute(config.pages[i].file) |
24 | | - ? config.pages[i].file |
25 | | - : path.resolve(path.dirname(configPath) + "/" + config.pages[i].file); |
| 36 | + let config: Partial<Config> = JSON.parse(fs.readFileSync(configPath!, "utf8")); |
| 37 | + if (config.pages !== undefined) { |
| 38 | + for (const i in config.pages) { |
| 39 | + config.pages[i].file = path.isAbsolute(config.pages[i].file) |
| 40 | + ? config.pages[i].file |
| 41 | + : path.resolve(path.dirname(configPath) + "/" + config.pages[i].file); |
| 42 | + } |
26 | 43 | } |
27 | 44 | config.configPath = configPath; |
28 | 45 | return config; |
29 | 46 | } |
30 | 47 |
|
31 | | - private static overwriteAuthFromConfigWithEnvIfPresent(config: Config): Config { |
32 | | - config.user = process.env.CONFLUENCE_USERNAME || config.user; |
33 | | - config.pass = process.env.CONFLUENCE_PASSWORD || config.pass; |
| 48 | + private static overwriteAuthFromConfigWithEnvIfPresent(config: Partial<Config>): Partial<Config> { |
| 49 | + config = overwriteConfigKeyWithEnvVarIfPresent(config, "user", "CONFLUENCE_USERNAME"); |
| 50 | + config = overwriteConfigKeyWithEnvVarIfPresent(config, "pass", "CONFLUENCE_PASSWORD"); |
| 51 | + config = overwriteConfigKeyWithEnvVarIfPresent(config, "authToken", "CONFLUENCE_AUTH_TOKEN"); |
34 | 52 | return config; |
35 | 53 | } |
36 | 54 |
|
37 | | - private static async promptUserAndPassIfNotSet(config: Config): Promise<Config> { |
38 | | - const prompts = []; |
39 | | - if (!config.user) { |
40 | | - prompts.push({ |
| 55 | + private static async promptUserAndPassIfNoCredentialsSet(config: Partial<Config>): Promise<Partial<Config>> { |
| 56 | + if (isTokenAuth(config)) { |
| 57 | + return config; |
| 58 | + } |
| 59 | + |
| 60 | + if (!("user" in config)) { |
| 61 | + const answers = await inquirer.prompt([{ |
41 | 62 | type: "input", |
42 | 63 | name: "user", |
43 | 64 | message: "Your Confluence username:", |
44 | | - }); |
| 65 | + }]); |
| 66 | + (config as any).user = answers.user; |
45 | 67 | } |
46 | 68 |
|
47 | | - if (!config.pass) { |
48 | | - prompts.push({ |
| 69 | + if (!("pass" in config)) { |
| 70 | + const answers = await inquirer.prompt([{ |
49 | 71 | type: "password", |
50 | 72 | name: "pass", |
51 | 73 | message: "Your Confluence password:", |
52 | | - }); |
| 74 | + }]); |
| 75 | + (config as any).pass = answers.pass; |
53 | 76 | } |
54 | 77 |
|
55 | | - const answers = await inquirer.prompt(prompts); |
56 | | - config.user = config.user || (answers.user as string); |
57 | | - config.pass = config.pass || (answers.pass as string); |
58 | | - |
59 | 78 | return config; |
60 | 79 | } |
61 | 80 | } |
0 commit comments