run method

  1. @override
FutureOr? run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
FutureOr? run() async {
  final args = argResults;
  if (args == null) {
    return;
  }

  final fileId = args['fileId'] as String;
  final token = args['token'] as String;
  final outputFolder = args['outputFolder'] as String;
  final imageFormat = args['format'] as String;
  final scales = args['scale'] as String;
  final forceDownload = args['force'] as bool;

  List<String> sectionsToDownload = [];
  if ((args['section'] as String) != 'APP_ASSET_') {
    sectionsToDownload =
        (args['section'] as String).split(',').map((e) => e.trim()).toList();
  }

  List<double> scalesList = handleScales(imageFormat, scales);

  logger.log('\nπŸ“₯ Fetching Figma file...');
  final fileData =
      await figmaApi.fetchFigmaFile(fileId: fileId, token: token);

  logger.log("πŸ“‚ Identifying 'APP_ASSET_' sections...");
  final assets = figmaApi.findAppImages(fileData, sectionsToDownload);

  if (assets.isEmpty) {
    logger.log("❌ No 'APP_ASSET_' sections found.");
    return;
  }

  for (var sectionName in assets.keys) {
    logger.log('\nπŸ“₯ Processing images for section: $sectionName');

    List<Map<String, String>> assetList =
        assets[sectionName]!; // Full asset list (id + name)
    List<String> nodeIds =
        assetList.map((e) => e['id']!).toList(); // Extract only IDs

    // Loop over the scales list, fetch and download images for each scale
    for (var scale in scalesList) {
      logger.log('  πŸ”„ Fetching ${scale}x images...');

      final imageUrls = await figmaApi.fetchImageUrls(
        fileId: fileId,
        token: token,
        nodeIds: nodeIds,
        imageFormat: imageFormat,
        scale: scale,
      );

      await figmaApi.downloadImages(
          parentDirectory: outputFolder,
          imageUrls: imageUrls,
          sectionName: sectionName,
          imageFormat: imageFormat,
          scale: scale,
          assets: assetList,
          forceDownload: forceDownload);
    }
  }

  logger.log('\nπŸŽ‰ All images downloaded successfully!');
}