expo / expo-cli

Tools for creating, running, and deploying universal Expo and React Native apps
https://docs.expo.io/workflow/expo-cli/
2.6k stars 477 forks source link

[image-utils] [ENG-9956] add a function to check whether a PNG is valid #4761

Closed kadikraman closed 9 months ago

kadikraman commented 9 months ago

Why

We want to check whether the app icon and splash screen images in the codebase are invalid before pre-building so we could show the user an error message and fail the build. ENG-9956.

How

Added an getPngInfo function returns the PNG data if a valid PNG is found, and throws otherwise.

Intended usage:

import { getPngInfo } from '@expo/image-utils';

function assertValidPng(filePath: string) {
  try {
    getPngInfo(filePath);
  } catch (error) {
    if (error.code === 'ENOENT') {
      throw new CommandError('IMAGE_NOT_FOUND', `There was no image at ${filePath}`);
    }

    if (error.message.includes('Invalid file signature')) {
      throw new CommandError('IMAGE_CORRUPTED', `The image at ${filePath} is corrupted: ${error.message}`);
    }

    // unknown error
    throw error;
  }
}

Test Plan

Added packages/image-utils/src/__tests__/image-test.ts to verify for different file types.

linear[bot] commented 9 months ago
ENG-9956 Discuss where to validate app icons files / splash screen files

We should validate any of the images that can be specified in Expo app config. Should we validate these images (that they are valid pngs, and whatever else) inside of doctor? Or is there another place that would make more sense? If in doctor, should we be running doctor before running a build? (or some subset of doctor checks?) [image (2).png](https://uploads.linear.app/4a409308-b1e3-45fc-94fd-9ef8703ee369/0e59982a-7226-477d-98fd-26e5a05eb897/2cd983a9-f174-43e8-b7b5-801a647f62e9)