Created
January 6, 2020 10:42
-
-
Save albertogalca/8b017c3bb7c7a7f3acf619dc66ab1586 to your computer and use it in GitHub Desktop.
Rename React Native Fonts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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