checkAssets method

Future<bool> checkAssets(
  1. List<ConstructYaml> constructs,
  2. Directory root
)

Implementation

Future<bool> checkAssets(
  List<ConstructYaml> constructs,
  Directory root,
) async {
  final assetsFile =
      await root.getInternalRevaliFile(ConstructEntrypointHandler.assetsFile);

  Future<void> saveAssets() async {
    logger.detail('Saving assets file');
    final json = constructs.map((e) => e.toJson()).toList();

    if (!await assetsFile.exists()) {
      await assetsFile.create(recursive: true);
    }

    await assetsFile.writeAsString(jsonEncode(json));
  }

  if (!await assetsFile.exists()) {
    await saveAssets();
    return true;
  }

  List<ConstructYaml> existingConstructs;
  try {
    final existingAssets =
        jsonDecode(await assetsFile.readAsString()) as List;

    existingConstructs =
        existingAssets.map((e) => ConstructYaml.fromJson(e as Map)).toList();
  } catch (e) {
    await saveAssets();
    return true;
  }

  const deepEquality = DeepCollectionEquality();

  if (deepEquality.equals(constructs, existingConstructs)) {
    logger.detail('Assets are up to date');
    return false;
  }

  await saveAssets();
  return true;
}