Skip to content

Instantly share code, notes, and snippets.

@quarrant
Last active December 8, 2019 20:04
Show Gist options
  • Save quarrant/b687549fef81545e7b6c88e3e59fbe30 to your computer and use it in GitHub Desktop.
Save quarrant/b687549fef81545e7b6c88e3e59fbe30 to your computer and use it in GitHub Desktop.
react-native-generate-imagesets.js
const path = require('path');
const fs = require('fs');
const appJson = require('../app.json');
const rootDirectory = process.cwd();
const resourcesImagesPath = path.resolve(rootDirectory, 'resources/images');
const imagesXcassetsPath = path.resolve(rootDirectory, 'ios', appJson.name, 'Images.xcassets');
const androidResPath = path.resolve(rootDirectory, 'android/app/src/main/res');
const iosContentObject = {
images: [
{ idiom: 'universal', scale: '1x' },
{ idiom: 'universal', scale: '2x' },
{ idiom: 'universal', scale: '3x' },
],
info: {
version: 1,
author: 'xcode',
},
};
const images = fs.readdirSync(resourcesImagesPath);
const groupedImages = images.reduce((grouped, filename) => {
const [shortname] = filename.split(/[@.]/);
if (!grouped[shortname]) return { ...grouped, [shortname]: [filename] };
grouped[shortname].push(filename);
return grouped;
}, {});
Object.keys(groupedImages).forEach((shortname) => {
const iosImagesetPath = path.resolve(imagesXcassetsPath, shortname + '.imageset');
const androidDrawablePath = path.resolve(androidResPath, 'drawable');
[iosImagesetPath, androidDrawablePath].forEach((dir) => {
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
});
groupedImages[shortname].forEach((filename) => {
const [fullname, extension] = filename.split('.');
const [shortname] = fullname.split('@');
iosContentObject.images = iosContentObject.images.map((image) => {
if (filename.includes(image.scale)) {
return { ...image, filename };
}
return image;
});
fs.writeFileSync(path.resolve(iosImagesetPath, 'Contents.json'), JSON.stringify(iosContentObject, null, 2));
fs.copyFileSync(path.resolve(resourcesImagesPath, filename), path.resolve(iosImagesetPath, filename));
fs.copyFileSync(
path.resolve(resourcesImagesPath, filename),
path.resolve(androidDrawablePath, shortname + '.' + extension),
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment