Skip to content

Instantly share code, notes, and snippets.

@jamesporter
Created July 10, 2018 08:25
Show Gist options
  • Save jamesporter/fa562c027b01d908457c68e751e9c44c to your computer and use it in GitHub Desktop.
Save jamesporter/fa562c027b01d908457c68e751e9c44c to your computer and use it in GitHub Desktop.
Rename Google Web fonts as React Native Android font assets for cross platform naming compatibility
// Rename Font files in Android React Native assets folder so can use easily on iOS and Android
// see https://blog.bam.tech/developper-news/add-a-custom-font-to-your-react-native-app
const fs = require("fs"), path = require("path");
const directory = path.join(".", "android", "app", "src", "main", "assets", "fonts");
const files = fs.readdirSync(directory);
const newName = name => {
const [baseName, ext] = name.split(".");
const [font, variant] = baseName.split("-");
const newFontName = font.replace(/([A-Z])/g, " $1").trim();
const newVariantName = variant.replace(/([A-Z])/g, "_$1").toLowerCase();
return newFontName + newVariantName + "." + ext;
};
files.forEach(fn => {
const newPath = path.join(directory, newName(fn));
fs.renameSync(path.join(directory, fn), newPath);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment