Skip to content

Commit

Permalink
[image-utils] Silence sharp related warnings by default (expo#4558)
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric committed Sep 29, 2022
1 parent ada3e96 commit c4da68c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 4 additions & 3 deletions packages/image-utils/src/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Cache from './Cache';
import * as Download from './Download';
import * as Ico from './Ico';
import { ImageOptions } from './Image.types';
import { env } from './env';
import * as Jimp from './jimp';
import * as Sharp from './sharp';

Expand Down Expand Up @@ -73,8 +74,8 @@ async function resizeAsync(imageOptions: ImageOptions): Promise<Buffer> {

if (imageOptions.borderRadius) {
const mask = Buffer.from(
`<svg><rect x="0" y="0" width="${width}" height="${height}"
rx="${imageOptions.borderRadius}" ry="${imageOptions.borderRadius}"
`<svg><rect x="0" y="0" width="${width}" height="${height}"
rx="${imageOptions.borderRadius}" ry="${imageOptions.borderRadius}"
fill="${
backgroundColor && backgroundColor !== 'transparent' ? backgroundColor : 'none'
}" /></svg>`
Expand Down Expand Up @@ -105,7 +106,7 @@ function getDimensionsId(imageOptions: Pick<ImageOptions, 'width' | 'height'>):

async function maybeWarnAboutInstallingSharpAsync() {
// Putting the warning here will prevent the warning from showing if all images were reused from the cache
if (!hasWarned && !(await Sharp.isAvailableAsync())) {
if (env.EXPO_IMAGE_UTILS_DEBUG && !hasWarned && !(await Sharp.isAvailableAsync())) {
hasWarned = true;
console.warn(
chalk.yellow(
Expand Down
15 changes: 15 additions & 0 deletions packages/image-utils/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { boolish } from 'getenv';

class Env {
/** Enable image utils related debugging messages */
get EXPO_IMAGE_UTILS_DEBUG() {
return boolish('EXPO_IMAGE_UTILS_DEBUG', false);
}

/** Disable all Sharp related functionality. */
get EXPO_IMAGE_UTILS_NO_SHARP() {
return boolish('EXPO_IMAGE_UTILS_NO_SHARP', false);
}
}

export const env = new Env();
8 changes: 3 additions & 5 deletions packages/image-utils/src/sharp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import spawnAsync from '@expo/spawn-async';
import { boolish } from 'getenv';
import path from 'path';
import resolveFrom from 'resolve-from';
import semver from 'semver';

import { env } from './env';
import { Options, SharpCommandOptions, SharpGlobalOptions } from './sharp.types';

const SHARP_HELP_PATTERN = /\n\nSpecify --help for available options/g;
Expand All @@ -28,14 +28,12 @@ export async function resizeBufferAsync(buffer: Buffer, sizes: number[]): Promis
return resizedBuffers;
}

const isSharpDisabled = boolish('EXPO_IMAGE_UTILS_NO_SHARP', false);

/**
* Returns `true` if a global sharp instance can be found.
* This functionality can be overridden with `process.env.EXPO_IMAGE_UTILS_NO_SHARP=1`.
*/
export async function isAvailableAsync(): Promise<boolean> {
if (isSharpDisabled) {
if (env.EXPO_IMAGE_UTILS_NO_SHARP) {
return false;
}
try {
Expand Down Expand Up @@ -144,7 +142,7 @@ async function findSharpBinAsync(): Promise<string> {
* This method will throw errors if the `sharp` instance cannot be found, these errors can be circumvented by ensuring `isAvailableAsync()` resolves to `true`.
*/
export async function findSharpInstanceAsync(): Promise<any | null> {
if (isSharpDisabled) {
if (env.EXPO_IMAGE_UTILS_NO_SHARP) {
throw new Error(
'Global instance of sharp-cli cannot be retrieved because sharp-cli has been disabled with the environment variable `EXPO_IMAGE_UTILS_NO_SHARP`'
);
Expand Down

0 comments on commit c4da68c

Please sign in to comment.