Skip to content

Instantly share code, notes, and snippets.

@mochki
Created July 6, 2020 20:09
Show Gist options
  • Save mochki/c6f36bdf7fb62e35c0bf546be1eac41f to your computer and use it in GitHub Desktop.
Save mochki/c6f36bdf7fb62e35c0bf546be1eac41f to your computer and use it in GitHub Desktop.
// For testing new svgs against old svgs
// yarn global add blink-diff
// brew install imagemagick
import { promises as fs } from 'fs';
import { remove as rm } from 'fs-extra';
import { execSync } from 'child_process';
import { remove } from 'lodash';
const iconsDir = 'some_dir';
const testIconDir = `${iconsDir}/testIcons`;
const oldIconDir = `${iconsDir}/oldIcons`;
const testIconPNGDir = `${iconsDir}/testIconsPNG`;
const oldIconPNGDir = `${iconsDir}/oldIconsPNG`;
const diffDir = `${iconsDir}/diffs`;
const scrubSVGExtension = (fileName: string) => fileName.replace('.svg', '');
const isSmall = (iconName: string) => (iconName.includes('-sm') ? 18 : 24);
(async () => {
/* Prep */
console.log('Cleaning up');
await rm(`${testIconDir}/.DS_Store`);
await rm(testIconPNGDir);
await rm(oldIconPNGDir);
await rm(diffDir);
/* Create PNGS */
console.log('generating PNGs');
await fs.mkdir(testIconPNGDir);
const testIconsSVGs = await fs.readdir(testIconDir);
testIconsSVGs.forEach((iconSVG) => {
execSync(
`rsvg-convert -h ${isSmall(iconSVG)} ${testIconDir}/${iconSVG} > ${testIconPNGDir}/${scrubSVGExtension(
iconSVG
)}.png`
);
});
await fs.mkdir(oldIconPNGDir);
const oldIconsSVGs = await fs.readdir(oldIconDir);
oldIconsSVGs.forEach((iconSVG) => {
execSync(
`rsvg-convert -h ${isSmall(iconSVG)} ${oldIconDir}/${iconSVG} > ${oldIconPNGDir}/${scrubSVGExtension(
iconSVG
)}.png`
);
});
/* Visual Diffing */
console.log('Visual Diffing');
await fs.mkdir(diffDir);
const netNewIcons: string[] = [];
const testIconPNGs = await fs.readdir(testIconPNGDir);
const oldIconPNGs = await fs.readdir(oldIconPNGDir);
testIconPNGs.forEach((iconPNG) => {
if (oldIconPNGs.includes(iconPNG)) {
remove(oldIconPNGs, (curr) => curr === iconPNG);
execSync(`blink-diff --output ${diffDir}/${iconPNG} ${oldIconPNGDir}/${iconPNG} ${testIconPNGDir}/${iconPNG}`);
} else {
netNewIcons.push(iconPNG);
}
});
const report = {
netNewIcons,
removedIcons: oldIconPNGs,
};
console.log(report.netNewIcons.join(','));
console.log(report.removedIcons.join(','));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment