|
| 1 | +import "dotenv/config"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import type { ConfigInterface } from "../index.js"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Load the Parse Server configuration. |
| 8 | + * @param configPath The path to the config file. |
| 9 | + * @param options Options for loading the config. |
| 10 | + * @returns The loaded config. |
| 11 | + */ |
| 12 | +export async function loadConfig( |
| 13 | + configPath?: string, |
| 14 | + options?: { operation: string } |
| 15 | +): Promise<ConfigInterface> { |
| 16 | + const { |
| 17 | + PARSE_SERVER_APPLICATION_ID, |
| 18 | + PARSE_SERVER_MASTER_KEY, |
| 19 | + PARSE_PUBLIC_SERVER_URL, |
| 20 | + PARSE_SERVER_URL, |
| 21 | + PARSE_SERVER_DOWN_SCHEMA_SERVER_URL, |
| 22 | + PARSE_SERVER_DOWN_SCHEMA_APPID, |
| 23 | + PARSE_SERVER_DOWN_SCHEMA_MASTERKEY, |
| 24 | + PARSE_SERVER_UP_SCHEMA_SERVER_URL, |
| 25 | + PARSE_SERVER_UP_SCHEMA_APPID, |
| 26 | + PARSE_SERVER_UP_SCHEMA_MASTERKEY, |
| 27 | + } = process.env; |
| 28 | + |
| 29 | + //Set url to the fetch schema server url if it is set and options.operation is "down" |
| 30 | + if ( |
| 31 | + options?.operation === "down" && |
| 32 | + PARSE_SERVER_DOWN_SCHEMA_SERVER_URL && |
| 33 | + PARSE_SERVER_DOWN_SCHEMA_APPID && |
| 34 | + PARSE_SERVER_DOWN_SCHEMA_MASTERKEY |
| 35 | + ) { |
| 36 | + console.log( |
| 37 | + "[@openinc/parse-server-schema] Using config from process.env with PARSE_SERVER_DOWN_SCHEMA_SERVER_URL: " + |
| 38 | + PARSE_SERVER_DOWN_SCHEMA_SERVER_URL + |
| 39 | + " with APPID: " + |
| 40 | + PARSE_SERVER_DOWN_SCHEMA_APPID + |
| 41 | + " and MASTERKEY: " + |
| 42 | + PARSE_SERVER_DOWN_SCHEMA_MASTERKEY |
| 43 | + ); |
| 44 | + |
| 45 | + return { |
| 46 | + publicServerURL: PARSE_SERVER_DOWN_SCHEMA_SERVER_URL, |
| 47 | + appId: PARSE_SERVER_DOWN_SCHEMA_APPID, |
| 48 | + masterKey: PARSE_SERVER_DOWN_SCHEMA_MASTERKEY, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + //Set url to the push schema server url if it is set and options.operation is "up" |
| 53 | + if ( |
| 54 | + options?.operation === "up" && |
| 55 | + PARSE_SERVER_UP_SCHEMA_SERVER_URL && |
| 56 | + PARSE_SERVER_UP_SCHEMA_APPID && |
| 57 | + PARSE_SERVER_UP_SCHEMA_MASTERKEY |
| 58 | + ) { |
| 59 | + console.log( |
| 60 | + "[@openinc/parse-server-schema] Using config from process.env with PARSE_SERVER_UP_SCHEMA_SERVER_URL: " + |
| 61 | + PARSE_SERVER_UP_SCHEMA_SERVER_URL + |
| 62 | + " with APPID: " + |
| 63 | + PARSE_SERVER_UP_SCHEMA_APPID + |
| 64 | + " and MASTERKEY: " + |
| 65 | + PARSE_SERVER_UP_SCHEMA_MASTERKEY |
| 66 | + ); |
| 67 | + |
| 68 | + return { |
| 69 | + publicServerURL: PARSE_SERVER_UP_SCHEMA_SERVER_URL, |
| 70 | + appId: PARSE_SERVER_UP_SCHEMA_APPID, |
| 71 | + masterKey: PARSE_SERVER_UP_SCHEMA_MASTERKEY, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + //Use default env variables |
| 76 | + const url = PARSE_SERVER_URL || PARSE_PUBLIC_SERVER_URL; |
| 77 | + if (PARSE_SERVER_APPLICATION_ID && PARSE_SERVER_MASTER_KEY && url) { |
| 78 | + console.log( |
| 79 | + "[@openinc/parse-server-schema] Using config from process.env with PARSE_SERVER_URL: " + |
| 80 | + url + |
| 81 | + " with APPID: " + |
| 82 | + PARSE_SERVER_APPLICATION_ID + |
| 83 | + " and MASTERKEY: " + |
| 84 | + PARSE_SERVER_MASTER_KEY |
| 85 | + ); |
| 86 | + |
| 87 | + return { |
| 88 | + publicServerURL: url, |
| 89 | + appId: PARSE_SERVER_APPLICATION_ID, |
| 90 | + masterKey: PARSE_SERVER_MASTER_KEY, |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + configPath = path.resolve(configPath || "config/parse-server.config.json"); |
| 95 | + |
| 96 | + if (!fs.existsSync(configPath)) { |
| 97 | + console.error( |
| 98 | + `[@openinc/parse-server-schema] No config at '${configPath}'` |
| 99 | + ); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | + |
| 103 | + let config; |
| 104 | + |
| 105 | + if (configPath.endsWith(".js")) { |
| 106 | + config = require(configPath); |
| 107 | + } |
| 108 | + |
| 109 | + if (configPath.endsWith(".json")) { |
| 110 | + config = JSON.parse(fs.readFileSync(configPath, "utf-8")); |
| 111 | + } |
| 112 | + |
| 113 | + if (!config) { |
| 114 | + console.error(`[@openinc/parse-server-schema] Invalid config file type`); |
| 115 | + process.exit(1); |
| 116 | + } |
| 117 | + |
| 118 | + if (!config.publicServerURL) { |
| 119 | + console.error( |
| 120 | + `[@openinc/parse-server-schema] Invalid config: Missing key 'publicServerURL'.` |
| 121 | + ); |
| 122 | + process.exit(1); |
| 123 | + } |
| 124 | + |
| 125 | + if (!config.appId) { |
| 126 | + console.error( |
| 127 | + `[@openinc/parse-server-schema] Invalid config: Missing key 'appId'.` |
| 128 | + ); |
| 129 | + process.exit(1); |
| 130 | + } |
| 131 | + |
| 132 | + if (!config.masterKey) { |
| 133 | + console.error( |
| 134 | + `[@openinc/parse-server-schema] Invalid config: Missing key 'masterKey'.` |
| 135 | + ); |
| 136 | + process.exit(1); |
| 137 | + } |
| 138 | + |
| 139 | + return config; |
| 140 | +} |
0 commit comments