fromFile static method

SupabaseGenConfig fromFile(
  1. String filePath, {
  2. String dotEnvPath = '.env',
})

Loads configuration from a YAML file and environment variables.

filePath: Path to the YAML configuration file. dotEnvPath: Path to the .env file. Defaults to '.env' in the current directory.

Implementation

static SupabaseGenConfig fromFile(
  String filePath, {
  String dotEnvPath = '.env',
}) {
  final file = File(filePath);
  if (!file.existsSync()) {
    throw FileSystemException('Configuration file not found', filePath);
  }

  final yamlContent = file.readAsStringSync();
  final yamlMap = loadYaml(yamlContent);

  if (yamlMap is! Map) {
    throw FormatException('YAML content does not represent a Map', filePath);
  }
  final configMap = _convertYamlToMap(yamlMap);

  // Load .env file.
  // Create a DotEnv instance, allow it to also read platform environment variables as fallback.
  final envLoader = dotenv.DotEnv(includePlatformEnvironment: true);
  // Check if the .env file exists before trying to load it to prevent errors if it's optional
  // or if platform env vars are the primary source.
  if (File(dotEnvPath).existsSync()) {
    envLoader.load([dotEnvPath]);
  } else {
    print(
      "Info: .env file not found at '$dotEnvPath'. Relying on platform environment variables if any.",
    );
  }

  // envLoader.map will contain variables from .env file (if loaded) and platform environment.
  // Variables from .env file take precedence over platform environment variables with the same name.
  final envVars = envLoader.map;

  return SupabaseGenConfig.fromYaml(configMap, envVars);
}