generateThemeFiles method

Future<void> generateThemeFiles(
  1. String outputDirectory
)

Generates theme files from Figma variables.

Parameters:

  • outputDirectory: The directory where theme files will be generated

This method:

  1. Fetches variables from Figma
  2. Processes and categorizes variables
  3. Generates theme files for each mode (e.g., light/dark)

Throws an exception if:

  • Variables cannot be fetched
  • The mapped collection is not found
  • Files cannot be written to the output directory

Implementation

Future<void> generateThemeFiles(String outputDirectory) async {
  try {
    final data = await fetchVariables();

    if (!data.containsKey('meta')) {
      throw Exception(FigmaConstants.invalidResponseError);
    }

    _meta = data['meta'];
    final collections = _meta['variableCollections'] as Map<String, dynamic>;
    final variables = _meta['variables'] as Map<String, dynamic>;

    // Find the mapped collection
    final mappedCollection = collections.values.firstWhere(
      (collection) =>
          collection['name'] == FigmaConstants.mappedCollectionName,
      orElse: () =>
          throw Exception(FigmaConstants.mappedCollectionNotFoundError),
    );

    print(
        'Found mapped collection with ${mappedCollection['variableIds'].length} variables');

    // Process each mode in the mapped collection
    for (var mode in mappedCollection['modes']) {
      await _generateThemeFile(
          mode, mappedCollection, variables, outputDirectory);
    }
  } catch (e) {
    print('Error generating theme files: $e');
    rethrow;
  }
}