/ compose-icon.js
compose-icon.js
1 const fs = require('fs'); 2 const {promisify} = require('util'); 3 const execa = require('execa'); 4 const tempy = require('tempy'); 5 const gm = require('gm').subClass({imageMagick: true}); 6 const icns = require('icns-lib'); 7 8 const readFile = promisify(fs.readFile); 9 const writeFile = promisify(fs.writeFile); 10 11 const filterMap = (map, filterFn) => Object.entries(map).filter(filterFn).reduce((out, [key, item]) => ({...out, [key]: item}), {}); 12 13 // Drive icon from `/System/Library/Extensions/IOStorageFamily.kext/Contents/Resources/Removable.icns`` 14 const baseDiskIconPath = `${__dirname}/disk-icon.icns`; 15 16 async function composeIcon(type, appIcon, mountIcon, composedIcon) { 17 mountIcon = gm(mountIcon); 18 appIcon = gm(appIcon); 19 const appIconSize = await promisify(appIcon.size.bind(appIcon))(); 20 const mountIconSize = appIconSize; 21 22 // Change the perspective of the app icon to match the mount drive icon 23 appIcon = appIcon.out('-matte').out('-virtual-pixel', 'transparent').out('-distort', 'Perspective', `1,1 ${appIconSize.width * 0.08},1 ${appIconSize.width},1 ${appIconSize.width * 0.92},1 1,${appIconSize.height} 1,${appIconSize.height} ${appIconSize.width},${appIconSize.height} ${appIconSize.width},${appIconSize.height}`); 24 25 // Resize the app icon to fit it inside the mount icon, aspect ration should not be kept to create the perspective illution 26 appIcon = appIcon.resize(appIconSize.width / 1.7, appIconSize.height / 1.78, '!'); 27 28 const tempAppIconPath = tempy.file({extension: 'png'}); 29 await promisify(appIcon.write.bind(appIcon))(tempAppIconPath); 30 31 // Compose the two icons 32 const iconGravityFactor = mountIconSize.height * 0.155; 33 mountIcon = mountIcon.composite(tempAppIconPath).gravity('Center').geometry(`+0-${iconGravityFactor}`); 34 35 composedIcon[type] = await promisify(mountIcon.toBuffer.bind(mountIcon))(); 36 } 37 38 const hasGm = async () => { 39 try { 40 await execa('gm', ['-version']); 41 return true; 42 } catch (error) { 43 if (error.code === 'ENOENT') { 44 return false; 45 } 46 47 throw error; 48 } 49 }; 50 51 module.exports = async appIconPath => { 52 if (!await hasGm()) { 53 return baseDiskIconPath; 54 } 55 56 const baseDiskIcons = filterMap(icns.parse(await readFile(baseDiskIconPath)), ([key]) => icns.isImageType(key)); 57 const appIcon = filterMap(icns.parse(await readFile(appIconPath)), ([key]) => icns.isImageType(key)); 58 59 const composedIcon = {}; 60 await Promise.all(Object.entries(appIcon).map(async ([type, icon]) => { 61 if (baseDiskIcons[type]) { 62 return composeIcon(type, icon, baseDiskIcons[type], composedIcon); 63 } 64 65 console.warn('There is no base image for this type', type); 66 })); 67 68 const tempComposedIcon = tempy.file({extension: 'icns'}); 69 70 await writeFile(tempComposedIcon, icns.format(composedIcon)); 71 72 return tempComposedIcon; 73 };