loadStandaloneConfiguration function

Future<Map> loadStandaloneConfiguration(
  1. FileSystem fileSystem, {
  2. String directoryPath = './config',
  3. String? overrideEnvironmentName,
  4. String? envPath,
  5. void onWarning(
    1. String message
    )?,
})

Loads configuration, and returns a Map.

You can override onWarning; otherwise, configuration errors will throw.

Implementation

Future<Map> loadStandaloneConfiguration(FileSystem fileSystem,
    {String directoryPath = './config',
    String? overrideEnvironmentName,
    String? envPath,
    void Function(String message)? onWarning}) async {
  var sourceDirectory = fileSystem.directory(directoryPath);
  var env = dotenv.env;
  var envFile = sourceDirectory.childFile(envPath ?? '.env');

  if (await envFile.exists()) {
    dotenv.load(envFile.absolute.uri.toFilePath());
  }

  var environmentName = env['ANGEL_ENV'] ?? 'development';

  if (overrideEnvironmentName != null) {
    environmentName = overrideEnvironmentName;
  }

  onWarning ??= (String message) => throw StateError(message);
  var out = {};

  var defaultYaml = sourceDirectory.childFile('default.yaml');
  await _loadYamlFile(out, defaultYaml, env, onWarning);

  var configFilePath = '$environmentName.yaml';
  var configFile = sourceDirectory.childFile(configFilePath);

  await _loadYamlFile(out, configFile, env, onWarning);

  return out;
}