Skip to content

Instantly share code, notes, and snippets.

@albertogalca
Created January 6, 2020 10:42
Show Gist options
  • Save albertogalca/8b017c3bb7c7a7f3acf619dc66ab1586 to your computer and use it in GitHub Desktop.
Save albertogalca/8b017c3bb7c7a7f3acf619dc66ab1586 to your computer and use it in GitHub Desktop.
Rename React Native Fonts
const fs = require('fs');
const path = require('path');
const opentype = require('opentype.js');
const fontFormats = ['.ttf', '.otf'];
const fontsPath = path.join(__dirname, 'assets', 'fonts');
const filenames = fs.readdirSync(fontsPath);
filenames.forEach(filename => {
const extension = path.extname(filename);
if (!fontFormats.includes(extension)) {
return;
}
const fontPath = path.join(fontsPath, filename);
const font = opentype.loadSync(fontPath);
const newFilename = font.names.postScriptName.en + extension;
const newFontPath = path.join(fontsPath, newFilename);
const relativeFontPath = path.relative(__dirname, fontPath);
const relativeNewFontPath = path.relative(__dirname, newFontPath);
if (filename === newFilename) {
console.log(`${relativeFontPath} is already named correctly.`);
} else {
fs.renameSync(fontPath, newFontPath);
console.log(`Renamed ${relativeFontPath} to ${relativeNewFontPath}.`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment