findPackageConfig function

Future<PackageConfig?> findPackageConfig(
  1. Directory directory, {
  2. bool recurse = true,
  3. void onError(
    1. Object error
    )?,
  4. int minVersion = 1,
})

Finds a package configuration relative to directory.

If directory contains a package configuration, either a .dart_tool/package_config.json file or, if not, a .packages, then that file is loaded.

If no file is found in the current directory, then the parent directories are checked recursively, all the way to the root directory, to check if those contains a package configuration. If recurse is set to false, this parent directory check is not performed.

If onError is provided, the configuration file parsing will report errors by calling that function, and then try to recover. The returned package configuration is a best effort attempt to create a valid configuration from the invalid configuration file. If no onError is provided, errors are thrown immediately.

If minVersion is set to something greater than its default, any lower-version configuration files are ignored in the search.

Returns null if no configuration file is found.

Implementation

Future<PackageConfig?> findPackageConfig(Directory directory,
    {bool recurse = true,
    void Function(Object error)? onError,
    int minVersion = 1}) {
  if (minVersion > PackageConfig.maxVersion) {
    throw ArgumentError.value(minVersion, 'minVersion',
        'Maximum known version is ${PackageConfig.maxVersion}');
  }
  return discover.findPackageConfig(
      directory, minVersion, recurse, onError ?? throwError);
}