(settingsFilename: string, isSettings: boolean)
| 91 | * The isSettings variable only controls the error logging. |
| 92 | */ |
| 93 | const parseSettings = (settingsFilename: string, isSettings: boolean) => { |
| 94 | let settingsStr = ''; |
| 95 | |
| 96 | let settingsType, notFoundMessage, notFoundFunction; |
| 97 | |
| 98 | if (isSettings) { |
| 99 | settingsType = 'settings'; |
| 100 | notFoundMessage = 'Continuing using defaults!'; |
| 101 | notFoundFunction = logger.warn.bind(logger); |
| 102 | } else { |
| 103 | settingsType = 'credentials'; |
| 104 | notFoundMessage = 'Ignoring.'; |
| 105 | notFoundFunction = logger.info.bind(logger); |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | // read the settings file |
| 110 | settingsStr = fs.readFileSync(settingsFilename).toString(); |
| 111 | } catch (e) { |
| 112 | notFoundFunction(`No ${settingsType} file found in ${settingsFilename}. ${notFoundMessage}`); |
| 113 | |
| 114 | // or maybe undefined! |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | try { |
| 119 | settingsStr = jsonminify(settingsStr).replace(',]', ']').replace(',}', '}'); |
| 120 | |
| 121 | const settings = JSON.parse(settingsStr); |
| 122 | |
| 123 | logger.info(`${settingsType} loaded from: ${settingsFilename}`); |
| 124 | |
| 125 | return lookupEnvironmentVariables(settings); |
| 126 | } catch (e: any) { |
| 127 | logger.error(`There was an error processing your ${settingsType} ` + |
| 128 | `file from ${settingsFilename}: ${e.message}`); |
| 129 | |
| 130 | process.exit(1); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | // Provide git version if available |
no test coverage detected