applyClientFonts method

Future<bool> applyClientFonts(
  1. String clientName,
  2. String clientAssetsPath
)

Swaps system fonts for client fonts and updates pubspec configuration. Returns true when fonts were changed and need restoring on cleanup.

Implementation

Future<bool> applyClientFonts(
    String clientName, String clientAssetsPath) async {
  final clientFontsDir = findClientFontsDir(clientName, clientAssetsPath);
  final targetFontsDir = Directory(p.join(projectDir, 'assets', 'fonts'));

  if (clientFontsDir == null) {
    Logger.info('No custom fonts found for this client. Skipping.');
    return false;
  }

  final hasFontFiles = clientFontsDir
      .listSync()
      .whereType<File>()
      .any((f) => !p.basename(f.path).startsWith('.'));
  if (!hasFontFiles) {
    Logger.info('Client fonts directory is empty. Skipping.');
    return false;
  }

  final backupDir = Directory('${targetFontsDir.path}.bak');
  if (targetFontsDir.existsSync() && !backupDir.existsSync()) {
    await targetFontsDir.rename(backupDir.path);
  }

  await targetFontsDir.create(recursive: true);
  await _copyDirectory(clientFontsDir, targetFontsDir);

  final fontsConfigFile = File(p.join(clientFontsDir.path, 'fonts.yaml'));
  if (fontsConfigFile.existsSync()) {
    Logger.info('Applying client font configuration to pubspec...');

    try {
      final fontsContent = await fontsConfigFile.readAsString();
      final fontsList = loadYaml(fontsContent);

      if (fontsList is YamlList || fontsList is List) {
        await config.updatePubspecFonts(fontsList);
        Logger.success('Client font configuration applied to pubspec.yaml');
      } else {
        throw BuildException(
          'Invalid format in fonts.yaml: must be a YAML list of font configurations.',
          fix:
              'Check "${fontsConfigFile.path}" and ensure it is structured as a YAML list.',
        );
      }
    } on BuildException {
      rethrow;
    } catch (e, s) {
      throw BuildException(
        'Failed to parse or apply font configuration from "${fontsConfigFile.path}"',
        fix: 'Check fonts.yaml syntax for formatting errors.',
        originalStackTrace: s,
      );
    }
  } else {
    Logger.warning(
        'Fonts copied but no fonts.yaml found in "${clientFontsDir.path}"; '
        'pubspec font families were not updated.');
  }
  return true;
}