UNPKG

rn-ml

Version:

React Native cli tools

94 lines (93 loc) 3.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const sharp_1 = __importDefault(require("sharp")); const path_1 = __importDefault(require("path")); const logger_1 = require("../tools/logger"); const constants_1 = require("./constants"); const data_1 = require("./template/android/data"); const genMipmapIcon = async ({ coverSize, iconSize, borderRadius, sourceIcon, destIcon, padding, resDir, }) => { const svgImage = ` <svg> <rect x="0" y="0" width="${iconSize}" height="${iconSize}" rx="${borderRadius}" ry="${borderRadius}"/> </svg> `; const rect = Buffer.from(svgImage); const { data: rectImage } = await (0, sharp_1.default)(rect).toBuffer({ resolveWithObject: true, }); const { data: iconData } = await (0, sharp_1.default)(sourceIcon) .resize(iconSize, iconSize) .composite([{ input: rectImage, blend: "dest-in" }]) .toBuffer({ resolveWithObject: true }); const iconPath = path_1.default.resolve(resDir, destIcon); await (0, sharp_1.default)({ create: { width: coverSize, height: coverSize, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 }, }, }) .composite([ { input: iconData, top: padding, left: padding, }, ]) .toFile(iconPath) .then(() => { (0, logger_1.log)("✨ " + iconPath); }); }; const run = async (config) => { const { androidSource, flavor, platform, source } = config; const resDir = path_1.default.resolve(androidSource, (0, constants_1.resAndroidDir)(flavor)); for (const [key, icon] of Object.entries(data_1.iconSourceSet)) { const mipmapPath = path_1.default.resolve(resDir, key); fs_extra_1.default.ensureDirSync(mipmapPath); await genMipmapIcon({ coverSize: icon.coverSize, borderRadius: icon.iconCircleRadius, iconSize: icon.iconCircleSize, padding: icon.paddingCircle, sourceIcon: source, destIcon: `${constants_1.IC_LAUNCHER}_round.png`, resDir: mipmapPath, }); await genMipmapIcon({ coverSize: icon.forceGroundCoverSize, borderRadius: 0, iconSize: icon.forceGroundIconSize, padding: icon.paddingForceGround, sourceIcon: source, destIcon: `${constants_1.IC_LAUNCHER}_foreground.png`, resDir: mipmapPath, }); await genMipmapIcon({ coverSize: icon.coverSize, borderRadius: icon.iconSquareRadius, iconSize: icon.iconSquareSize, padding: icon.paddingSquare, sourceIcon: source, destIcon: `${constants_1.IC_LAUNCHER}.png`, resDir: mipmapPath, }); } const flavorDir = path_1.default.resolve(androidSource, (0, constants_1.resAndroidDir)(flavor), "../"); await genMipmapIcon({ coverSize: constants_1.ANDROID_PLAY_STORE_SIZE, borderRadius: 0, iconSize: constants_1.ANDROID_PLAY_STORE_SIZE, padding: 0, sourceIcon: source, destIcon: `${constants_1.IC_LAUNCHER}-playstore.png`, resDir: flavorDir, }); }; exports.run = run;