resolvePackageExecutable function
Resolves the absolute path to an executable in bin/ given a packageUri
and an optional executableName.
If executableName is omitted, defaults to the package name (the first path
segment of packageUri).
Throws a StateError if the executable file does not exist.
Implementation
Future<String> resolvePackageExecutable(
String packageUri, [
String? executableName,
]) async {
final pkgDir = await resolvePackageDirectory(packageUri);
final exec = executableName ?? Uri.parse(packageUri).pathSegments.first;
final binFile = File(p.join(pkgDir, 'bin', '$exec.dart'));
if (!binFile.existsSync()) {
throw StateError(
'Executable not found at "${binFile.path}". '
'Ensure bin/$exec.dart exists in package "$packageUri".',
);
}
return p.normalize(binFile.path);
}