downloadAllIcon function

Future<void> downloadAllIcon(
  1. String pathSource,
  2. CoreGenModel modelGen,
  3. FigmaGen config,
  4. FigmaFile figmaFile,
  5. ImageWriter writer,
)

Implementation

Future<void> downloadAllIcon(
  String pathSource,
  CoreGenModel modelGen,
  FigmaGen config,
  FigmaFile figmaFile,
  ImageWriter writer,
) async {
  final lstLog = <String>[];

  final List<({String? id, String? name})> iconIds = [
    for (final i in figmaFile.document.documents)
      if (i.type == 'BOOLEAN_OPERATION') (id: i.id, name: i.name),
  ].where((e) => e.id != null).toList();

  final token = modelGen.tokenFigma ?? config.figmaToken;

  if (token == null) {
    stdout.writeln('Token figma is null');
    return;
  }

  final iconUrls = await fetchIcons(
    config.figmaKey,
    token,
    iconIds.map((e) => e.id).where((e) => e != null).join(','),
    modelGen,
  );

  for (final img in iconUrls) {
    final path = iconIds.firstWhereOrNull((e) => e.id == img.imageId)?.name ??
        img.imageId;

    final pathDot =
        path.split('.').length > 1 ? path : '$path.${modelGen.format}';

    final check = checkIfExit(pathSource, pathDot);
    if (img.url == null) {
      stdout.writeln('icon $path from figma(${config.figmaKey}) not found');
    }
    if (check || img.url == null) continue;

    stdout.writeln('icon $path from figma(${config.figmaKey}) download...');
    final imageByte = await download(img.url!);

    lstLog.add('add icons: - $path');

    writer(imageByte, pathDot, '');
  }
  stdout
    ..writeln(lstLog.join('\n'))
    ..writeln('Download icons success!');
}