resolveOrmProject function

OrmProjectContext resolveOrmProject({
  1. String? configPath,
})

Resolves the ORM project context, locating the ormed.yaml config file.

If configPath is provided, it attempts to resolve that specific file. Otherwise, it searches for ormed.yaml in the current or parent directories.

Implementation

OrmProjectContext resolveOrmProject({String? configPath}) {
  if (configPath != null && configPath.trim().isNotEmpty) {
    final normalized = p.normalize(
      p.isAbsolute(configPath)
          ? configPath
          : p.join(Directory.current.path, configPath),
    );
    final file = File(normalized);
    if (!file.existsSync()) {
      throw StateError('Config file $normalized not found.');
    }
    final root = findProjectRoot(file.parent);
    return OrmProjectContext(root: root, configFile: file);
  }

  // Use findOrmConfigFile from ormed package to locate config
  final configFile = findOrmConfigFile();
  if (configFile != null) {
    final root = findProjectRoot(configFile.parent);
    return OrmProjectContext(root: root, configFile: configFile);
  }

  throw StateError(
    'Missing ormed.yaml. Run `ormed init` or provide --config path.',
  );
}