load static method

CliConfig load(
  1. Directory dir
)

Implementation

static CliConfig load(Directory dir) {
  final file = File(p.join(dir.path, fileName));
  if (!file.existsSync()) {
    throw CliException(
      'No $fileName in ${dir.path}. Create one; see the hotpatch README.',
    );
  }
  final Object? doc;
  try {
    doc = loadYaml(file.readAsStringSync());
  } on YamlException catch (error) {
    throw CliException('$fileName is not valid YAML: ${error.message}');
  }
  if (doc is! YamlMap) throw CliException('$fileName must be a YAML map.');
  final YamlMap map = doc;

  String required(String key) {
    final value = map[key];
    if (value is! String || value.trim().isEmpty) {
      throw CliException('$fileName: "$key" is required.');
    }
    return value.trim();
  }

  String optional(String key, String fallback) {
    final value = map[key];
    return value is String && value.trim().isNotEmpty
        ? value.trim()
        : fallback;
  }

  final rawArgs = map['build_args'];
  final List<String> buildArgs;
  if (rawArgs == null) {
    buildArgs = const [];
  } else if (rawArgs is YamlList) {
    buildArgs = [for (final arg in rawArgs) '$arg'];
  } else {
    throw CliException('$fileName: "build_args" must be a list.');
  }

  return CliConfig(
    projectDir: dir,
    appId: required('app_id'),
    store: required('store'),
    keyFile: resolvePath(required('key_file'), dir),
    flutter:
        Platform.environment['FLUTTER'] ?? optional('flutter', 'flutter'),
    aws: Platform.environment['HOTPATCH_AWS'] ?? optional('aws', 'aws'),
    buildArgs: buildArgs,
  );
}