Files
flipper/desktop/scripts/build-icons.tsx
Michel Weststrate a6dac1d8d9 make icon download more robust, and use assets_DO_NOT_HARDCODE urls instead of asset urls.
Summary:
Since ~yesterday all our builds started failing due to being unable to download static assets. Although those assets load correctly in the browser, and by using the browsers's generated `curl` command, which looks like:

```
curl 'https://facebook.com/assets/?name=face-unhappy&variant=outline&size=24&set=facebook_icons&density=1x' \
  -H 'authority: facebook.com' \
  -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
  -H 'accept-language: nl,en-GB;q=0.9,en;q=0.8,nl-NL;q=0.7,en-US;q=0.6' \
  -H 'cache-control: no-cache' \
  -H 'cookie: OMITTED' \
  -H 'pragma: no-cache' \
  -H 'sec-ch-ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: document' \
  -H 'sec-fetch-mode: navigate' \
  -H 'sec-fetch-site: none' \
  -H 'sec-fetch-user: ?1' \
  -H 'upgrade-insecure-requests: 1' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' \
  --compressed
```

Download fails as soon as the cookie header is dropped.

Changes the base url to `https://facebook.com/images/assets_DO_NOT_HARDCODE/facebook_icons/` fixes that problem, unless the resolution doesn't exist (addressed in next diff).

This also seems to remove the flakiness we were experiencing before, so dropped the retry mechanism for now.

Also made failing to download an icon a warning instead of a build failure.

Reviewed By: lblasa

Differential Revision: D46552986

fbshipit-source-id: f1382d6a8fc6669691f0c8da6b77834d24a373c5
2023-06-08 07:31:34 -07:00

86 lines
2.6 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import path from 'path';
import fs from 'fs-extra';
import fetch from '@adobe/node-fetch-retry';
// eslint-disable-next-line node/no-extraneous-import
import type {Icon} from 'flipper-ui-core';
export type Icons = {
[key: string]: Icon['size'][];
};
// Takes a string like 'star', or 'star-outline', and converts it to
// {trimmedName: 'star', variant: 'filled'} or {trimmedName: 'star', variant: 'outline'}
function getIconPartsFromName(icon: string): {
trimmedName: string;
variant: 'outline' | 'filled';
} {
const isOutlineVersion = icon.endsWith('-outline');
const trimmedName = isOutlineVersion ? icon.replace('-outline', '') : icon;
const variant = isOutlineVersion ? 'outline' : 'filled';
return {trimmedName: trimmedName, variant: variant};
}
export async function downloadIcons(buildFolder: string) {
const icons: Icons = JSON.parse(
await fs.promises.readFile(path.join(buildFolder, 'icons.json'), {
encoding: 'utf8',
}),
);
const iconURLs = Object.entries(icons).reduce<Icon[]>(
(acc, [entryName, sizes]) => {
const {trimmedName: name, variant} = getIconPartsFromName(entryName);
acc.push(
// get icons in @1x and @2x
...sizes.map((size) => ({name, variant, size, density: 1})),
...sizes.map((size) => ({name, variant, size, density: 2})),
);
return acc;
},
[],
);
await Promise.all(
iconURLs.map(async (icon) => {
const url = getPublicIconUrl(icon);
const res = await fetch(url);
if (res.status !== 200) {
console.warn(
// eslint-disable-next-line prettier/prettier
`Could not download the icon ${icon} from ${url}: got status ${res.status}`,
);
return;
}
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(
path.join(buildFolder, buildLocalIconPath(icon)),
);
res.body.pipe(fileStream);
res.body.on('error', reject);
fileStream.on('finish', resolve);
});
}),
);
}
// should match flipper-ui-core/src/utils/icons.tsx
export function getPublicIconUrl({name, variant, size, density}: Icon) {
return `https://facebook.com/images/assets_DO_NOT_HARDCODE/facebook_icons/${name}_${variant}_${size}_primary-icon.png`;
}
// should match app/src/utils/icons.tsx
function buildLocalIconPath(icon: Icon) {
return path.join(
'icons',
`${icon.name}-${icon.variant}-${icon.size}@${icon.density}x.png`,
);
}