TidyConfig.load constructor

TidyConfig.load(
  1. String currentPath,
  2. YamlMap pubspecYaml
)

Loads configuration, preferring tidy_imports.yaml over the tidy_imports: block in pubspec.yaml.

A standalone file that does not parse — or cannot be read — is an issue like any other, not an exception. Everything else about a broken config is reported as a sentence and then survived; the one shape that threw instead was the loudest of them, a five-line YAML stack trace for a missing colon.

Implementation

factory TidyConfig.load(String currentPath, YamlMap pubspecYaml) {
  final standaloneFile = File('$currentPath/tidy_imports.yaml');
  if (standaloneFile.existsSync()) {
    final dynamic parsed;
    try {
      parsed = loadYaml(standaloneFile.readAsStringSync());
    } on Object catch (error) {
      return TidyConfig.fromYaml(null, [
        'tidy_imports.yaml '
            '${error is FileSystemException ? 'could not be read' : 'is not valid YAML'}'
            ' — ${_briefly(error)}. Using the defaults.',
      ]);
    }
    return TidyConfig.fromStandalone(parsed);
  }
  return TidyConfig.fromYaml(pubspecYaml['tidy_imports']);
}