diff --git a/_tools/generateFontData.js b/_tools/generateFontData.js index 400f220..42fe8b5 100644 --- a/_tools/generateFontData.js +++ b/_tools/generateFontData.js @@ -18,14 +18,15 @@ const path = require("path"); const { parseFontFile, buildStylesheet, - buildFontJs + buildFontJs, + getSelector } = require("specimen-skeleton-support"); const srcDirectory = path.resolve(__dirname, "../", "src"); const fontsDirectory = path.resolve(srcDirectory, "fonts"); -const dataDirectory = path.resolve(srcDirectory, "_data"); -const fontsStylesheetPath = path.resolve(srcDirectory, "css", "font.css"); -const fontJsPath = path.resolve(srcDirectory, "js", "font.js"); +const dataDirectory = path.resolve(srcDirectory, "_data/fonts"); +const fontsStylesheetPath = path.resolve(srcDirectory, "css", "fonts.css"); +const fontJsPath = path.resolve(srcDirectory, "js", "fonts.js"); const assert = (condition, message) => { if (!condition) { @@ -33,22 +34,27 @@ const assert = (condition, message) => { } }; +const _appendFile = util.promisify(fs.appendFile); const _writeFile = util.promisify(fs.writeFile); -const writeFile = (path, contents) => { +const writeFile = (path, contents, append) => { console.info("Writing", path); + if (append) { + return _appendFile(path, contents); + } return _writeFile(path, contents); }; -const writeDataFile = async (filename, data) => { - const dataFilePath = path.join(dataDirectory, filename); - const fileContents = JSON.stringify(data, null, 4); - - return writeFile(dataFilePath, fileContents); +const writeDataFile = async (filename, fontName, data) => { + fs.mkdir(path.join(dataDirectory, fontName), { recursive: true }, () => { + const dataFilePath = path.join(dataDirectory, fontName, filename); + const fileContents = JSON.stringify(data, null, 4); + return writeFile(dataFilePath, fileContents); + }); }; const writeDataFiles = async fontData => { - const promises = Object.entries(fontData).map(([type, data]) => { - return writeDataFile(`${type}.json`, data); + const promises = Object.entries(fontData.data).map(([type, data]) => { + return writeDataFile(`${type}.json`, getSelector(fontData, true), data); }); return Promise.all(promises); @@ -59,46 +65,58 @@ const writeStylesheet = async (fontData, fontFilePath) => { path.dirname(fontsStylesheetPath), fontFilePath ); - const stylesheet = buildStylesheet(fontData, fontUrl).toString(); - return writeFile(fontsStylesheetPath, stylesheet); + let stylesheet = buildStylesheet(fontData, fontUrl).toString(); + stylesheet += "\n\n"; + return writeFile(fontsStylesheetPath, stylesheet, true); }; const writeFontJs = async fontData => { const js = buildFontJs(fontData); - return writeFile(fontJsPath, js); + return writeFile(fontJsPath, js, true); }; -const findFirstFontFile = async directory => { +const findFontFile = async directory => { const fontFiles = (await util.promisify(fs.readdir)(directory)).filter( f => path.extname(f) == ".woff2" ); assert( fontFiles.length > 0, - `No font file found. Place your font in ${path.relative( + `No WOFF2 font files found. Place your WOFF2 fonts in ${path.relative( process.cwd(), directory )}.` ); - assert( - fontFiles.length == 1, - "Multiple font files found. Please specify the path to your font file explicitly." - ); + const paths = fontFiles.map(fontFile => ({ + name: path.basename(fontFile, path.extname(fontFile)), + path: path.resolve(fontsDirectory, fontFile) + })); - return path.resolve(fontsDirectory, fontFiles[0]); + return paths; }; const main = async () => { - const fontFilePath = - process.argv[2] || (await findFirstFontFile(fontsDirectory)); - const fontData = await parseFontFile(fontFilePath); - - await Promise.all([ - writeDataFiles(fontData.data), - writeStylesheet(fontData, fontFilePath), - writeFontJs(fontData) - ]); + const fontFiles = process.argv[2] || (await findFontFile(fontsDirectory)); + + // Initialise files + writeFile( + fontsStylesheetPath, + `/* Generated by the Specimen Skeleton */\n` + ); + writeFile( + fontJsPath, + `/* Generated by the Specimen Skeleton */\nexport const fontNames = [];\n` + ); + + for (const fontFile of fontFiles) { + const fontData = await parseFontFile(fontFile.path); + await Promise.all([ + writeDataFiles(fontData, fontFile.name), + writeStylesheet(fontData, fontFile.path), + writeFontJs(fontData, fontFile.name) + ]); + } }; main().catch(e => { diff --git a/src/_includes/character-grid.html b/src/_includes/character-grid.html index 394cfc4..a081b6d 100644 --- a/src/_includes/character-grid.html +++ b/src/_includes/character-grid.html @@ -1,5 +1,7 @@ -