* Load a file and add it to the configuration * * @param {string} fullFilename an absolute file path * @param {DataConvert=} convert * @returns {null}
(fullFilename, convert)
| 1124 | * @returns {null} |
| 1125 | */ |
| 1126 | loadFile(fullFilename, convert) { |
| 1127 | let configObject = null; |
| 1128 | let fileContent = null; |
| 1129 | |
| 1130 | // Note that all methods here are the Sync versions. This is appropriate during |
| 1131 | // module loading (which is a synchronous operation), but not thereafter. |
| 1132 | |
| 1133 | try { |
| 1134 | // Try loading the file. |
| 1135 | fileContent = FileSystem.readFileSync(fullFilename, 'utf-8'); |
| 1136 | fileContent = fileContent.replace(/^\uFEFF/, ''); |
| 1137 | } |
| 1138 | catch (e2) { |
| 1139 | if (e2.code !== 'ENOENT') { |
| 1140 | throw new Error('Config file ' + fullFilename + ' cannot be read. Error code is: '+e2.code |
| 1141 | +'. Error message is: '+e2.message); |
| 1142 | } |
| 1143 | return null; // file doesn't exists |
| 1144 | } |
| 1145 | |
| 1146 | // Parse the file based on extension |
| 1147 | try { |
| 1148 | |
| 1149 | // skip if it's a gitcrypt file and CONFIG_SKIP_GITCRYPT is true |
| 1150 | if (!this.options.gitCrypt) { |
| 1151 | if (GIT_CRYPT_REGEX.test(fileContent)) { |
| 1152 | console.error('WARNING: ' + fullFilename + ' is a git-crypt file and CONFIG_SKIP_GITCRYPT is set. skipping.'); |
| 1153 | return null; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | configObject = this.parser.parse(fullFilename, fileContent); |
| 1158 | } catch (e3) { |
| 1159 | if (GIT_CRYPT_REGEX.test(fileContent)) { |
| 1160 | console.error('ERROR: ' + fullFilename + ' is a git-crypt file and CONFIG_SKIP_GITCRYPT is not set.'); |
| 1161 | } |
| 1162 | throw new Error("Cannot parse config file: '" + fullFilename + "': " + e3); |
| 1163 | } |
| 1164 | |
| 1165 | if (typeof configObject == 'function') { |
| 1166 | /** @type bootstrapCallback */ |
| 1167 | let fn = configObject; |
| 1168 | |
| 1169 | configObject = fn({ defer: deferConfig, util: Util, raw: RawConfig.raw }); |
| 1170 | } |
| 1171 | |
| 1172 | if (convert) { |
| 1173 | configObject = convert(configObject); |
| 1174 | } |
| 1175 | |
| 1176 | this.addConfig(fullFilename, configObject, fileContent); |
| 1177 | |
| 1178 | return configObject; |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * load custom-environment-variables |
no test coverage detected