generate method

Future<bool> generate()

Generates icon assets and prepares files for font generation.

This method:

  1. Creates necessary directories
  2. Fetches icon components from Figma
  3. Downloads icon assets
  4. Organizes files for font generation

Returns true if generation was successful, false otherwise.

Implementation

Future<bool> generate() async {
  try {
    // Create output directories
    final directories = [
      Directory(path.join(_outputDir, 'svg')),
      Directory(path.join(_outputDir, 'pdf')),
    ];

    for (final dir in directories) {
      if (!dir.existsSync()) {
        dir.createSync(recursive: true);
      }
    }

    // Fetch file data
    final fileData = await _client.getFile();
    final document = fileData['document'];

    // Find icon components
    final components = _findComponents(document);
    if (components.isEmpty) {
      Logger.error('No icon components found in node $_nodeId');
      return false;
    }

    Logger.info('Found ${components.length} icon components');
    if (_verbose) {
      for (final component in components) {
        Logger.debug('Icon: ${component['name']}');
      }
    }

    // Get image URLs
    final componentIds = components.map((c) => c['id'] as String).toList();
    final format = _directSvg ? 'svg' : 'pdf';
    final imageUrls =
        await _client.getImageUrls(componentIds, format: format);

    // Download images
    for (final component in components) {
      final id = component['id'] as String;
      final name = component['name'] as String;
      final url = imageUrls[id] as String;

      final extension = _directSvg ? 'svg' : 'pdf';
      final outputPath = path.join(
        _outputDir,
        extension,
        '$name.$extension',
      );

      final imageData = await _client.downloadImage(url);
      File(outputPath).writeAsBytesSync(imageData);

      if (_verbose) {
        Logger.debug('Downloaded: $outputPath');
      }
    }

    Logger.success('Icon generation completed successfully');
    return true;
  } catch (e) {
    Logger.error('Failed to generate icons: $e');
    return false;
  }
}