getPackageRoot function

Future<String> getPackageRoot()

Implementation

Future<String> getPackageRoot() async {
  final resolved = await Isolate.resolvePackageUri(
    Uri.parse('package:rekeens_flutter_cli/utils/project_paths.dart'),
  );
  if (resolved != null && resolved.scheme == 'file') {
    final file = File.fromUri(resolved);
    if (file.existsSync()) {
      final libDir = p.dirname(file.path);
      final root = p.dirname(libDir);
      if (_isPackageRoot(root)) return root;
    }
  }

  final fromScript = _findRootFromPath(Platform.script.toFilePath());
  if (fromScript != null) return fromScript;

  final fromExecutable = _findRootFromPath(Platform.resolvedExecutable);
  if (fromExecutable != null) return fromExecutable;

  throw StateError('Unable to locate rekeens_flutter_cli package root.');
}