createWebFavicon function

void createWebFavicon({
  1. required String imagePath,
  2. String faviconOutputExtension = '.png',
})

Start create web favicon

Implementation

void createWebFavicon({
  required String imagePath,
  String faviconOutputExtension = '.png',
}) {
  final image = Icon.loadFile(imagePath);
  if (image == null) {
    CliLogger.error('The file $imagePath could not be read.', level: .two);
    exit(1);
  }

  final fileName = faviconOutputExtension.endsWith('ico')
      ? 'favicon.ico'
      : 'favicon.png';

  if (faviconOutputExtension.endsWith('ico')) {
    final templates = [
      WebIconTemplate(name: fileName, size: 16),
      WebIconTemplate(name: fileName, size: 32),
      WebIconTemplate(name: fileName, size: 48),
    ];
    _saveFaviconIcoWeb(templates, fileName, image);
    CliLogger.success('Generated favicon ico image', level: .two);
  } else {
    final template = WebIconTemplate(name: fileName, size: 48);
    _saveFaviconPngWeb(template, image);
    CliLogger.success('Generated favicon png image', level: .two);
  }

  // Remove the old favicon if it exists
  final oldFileName = faviconOutputExtension.endsWith('ico')
      ? 'favicon.png'
      : 'favicon.ico';
  final oldFaviconFile = File('$WEB_DEFAULT_FAVICON_DIR$oldFileName');
  if (oldFaviconFile.existsSync()) {
    oldFaviconFile.deleteSync();
  }

  // Update index.html
  final indexHtmlFile = File('${WEB_DEFAULT_FAVICON_DIR}index.html');
  if (indexHtmlFile.existsSync()) {
    var content = indexHtmlFile.readAsStringSync();
    final newType = faviconOutputExtension.endsWith('ico')
        ? 'image/x-icon'
        : 'image/png';

    final regex = RegExp(r'<link rel="icon"([^>]+)>', multiLine: true);
    content = content.replaceAllMapped(regex, (match) {
      var tagInfo = match.group(1)!;
      tagInfo = tagInfo.replaceAll(
        RegExp(r'href="[^"]*"|href=\x27[^\x27]*\x27'),
        'href="$fileName"',
      );
      if (tagInfo.contains('type=')) {
        tagInfo = tagInfo.replaceAll(
          RegExp(r'type="[^"]*"|type=\x27[^\x27]*\x27'),
          'type="$newType"',
        );
      } else {
        if (tagInfo.endsWith('/')) {
          tagInfo =
              '${tagInfo.substring(0, tagInfo.length - 1)} type="$newType"/';
        } else {
          tagInfo += ' type="$newType"';
        }
      }
      return '<link rel="icon"$tagInfo>';
    });

    indexHtmlFile.writeAsStringSync(content);
  }

  CliLogger.success('Updated index.html', level: .two);
}