2022-01-07 01:03:43 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2022-01-07 23:59:28 -05:00
|
|
|
|
import fs from 'fs-extra';
|
2022-01-07 01:03:43 -05:00
|
|
|
|
import path from 'path';
|
2022-01-07 23:59:28 -05:00
|
|
|
|
import {fileURLToPath} from 'url';
|
2022-03-06 00:59:42 -05:00
|
|
|
|
import logger from '@docusaurus/logger';
|
2022-05-14 11:39:50 -04:00
|
|
|
|
import sharp from 'sharp';
|
|
|
|
|
|
import imageSize from 'image-size';
|
2022-01-07 01:03:43 -05:00
|
|
|
|
|
2022-01-07 01:53:45 -05:00
|
|
|
|
const allImages = (
|
2022-01-07 01:03:43 -05:00
|
|
|
|
await fs.readdir(new URL('../../website/src/data/showcase', import.meta.url))
|
|
|
|
|
|
).filter((file) => ['.png', 'jpg', '.jpeg'].includes(path.extname(file)));
|
|
|
|
|
|
|
2022-01-07 23:59:28 -05:00
|
|
|
|
const [, , ...selectedImages] = process.argv;
|
2022-01-07 01:53:45 -05:00
|
|
|
|
const images = selectedImages.length > 0 ? selectedImages : allImages;
|
|
|
|
|
|
|
2022-01-07 01:03:43 -05:00
|
|
|
|
await Promise.all(
|
|
|
|
|
|
images.map(async (img) => {
|
2022-01-07 23:59:28 -05:00
|
|
|
|
const imgPath = fileURLToPath(
|
|
|
|
|
|
new URL(`../../website/src/data/showcase/${img}`, import.meta.url),
|
|
|
|
|
|
);
|
2022-01-07 03:35:46 -05:00
|
|
|
|
const {width, height} = imageSize(imgPath);
|
2022-04-10 22:59:19 -04:00
|
|
|
|
if (width === 640 && height === 320 && imgPath.endsWith('.png')) {
|
2022-03-06 00:59:42 -05:00
|
|
|
|
// Do not emit if not resized. Important because we can't guarantee
|
|
|
|
|
|
// idempotency during resize -> optimization
|
2022-01-07 03:35:46 -05:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-03-06 00:59:42 -05:00
|
|
|
|
logger.info`Resized path=${imgPath}: Before number=${width}×number=${height}`;
|
2022-01-07 01:03:43 -05:00
|
|
|
|
const data = await sharp(imgPath)
|
|
|
|
|
|
.resize(640, 320, {fit: 'cover', position: 'top'})
|
|
|
|
|
|
.png()
|
|
|
|
|
|
.toBuffer();
|
|
|
|
|
|
await fs.writeFile(imgPath.replace(/jpe?g/, 'png'), data);
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2022-01-30 21:31:24 -05:00
|
|
|
|
// You should also run
|
|
|
|
|
|
// optimizt `find website/src/data/showcase -type f -name '*.png'`.
|
|
|
|
|
|
// This is not included here because @funboxteam/optimizt doesn't seem to play
|
|
|
|
|
|
// well with M1 so I had to run this in a Rosetta terminal.
|
2022-01-07 01:03:43 -05:00
|
|
|
|
// TODO integrate this as part of the script
|