resolve method

Future<String> resolve({
  1. required String category,
  2. String? subPath,
  3. String? workingDirectory,
  4. String? packageRootOverride,
})

Implementation

Future<String> resolve({
  required String category,
  String? subPath,
  String? workingDirectory,
  String? packageRootOverride,
}) async {
  final segments = <String>[category, ?subPath];

  final candidates = <String>[];

  final home = homeDirectoryOverride ?? _homeDirectory();
  if (home != null) {
    candidates.add(p.joinAll([home, '.rekeens', 'templates', ...segments]));
  }

  final cwd = workingDirectory ?? Directory.current.path;
  candidates.add(p.joinAll([cwd, '.rekeens', 'templates', ...segments]));

  String? root;
  try {
    root = packageRootOverride ?? await getPackageRoot();
  } catch (e) {
    logger.warn('Could not resolve package root: $e');
    root = null;
  }
  if (root != null) {
    candidates.add(p.joinAll([root, 'templates', ...segments]));
  }

  for (final candidate in candidates) {
    if (Directory(candidate).existsSync()) return candidate;
  }

  final location = subPath == null ? category : '$category/$subPath';
  throw StateError(
    'Template "$location" not found. Searched:\n'
    '${candidates.map((c) => '  - $c').join('\n')}',
  );
}