From cc4412f0ff1f811bb7d38b5da75e128c270d4e6b Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Fri, 19 Apr 2019 18:04:31 +0300 Subject: [PATCH] refactor: use async/await (#182) --- README.md | 2 +- package-lock.json | 12 +- package.json | 2 +- src/__tests__/standalone.test.js | 8 +- src/cli.js | 9 +- src/standalone.js | 283 +++++++++++++------------------ 6 files changed, 136 insertions(+), 180 deletions(-) diff --git a/README.md b/README.md index 7688adfc..81a2d68d 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,7 @@ The CLI can exit the process with the following exit codes: - [svg2ttf](https://github.com/fontello/svg2ttf) - Converts SVG fonts to TTF format. - [ttf2eot](https://github.com/fontello/ttf2eot) - Converts TTF fonts to EOT format. - [ttf2woff](https://github.com/fontello/ttf2woff) - Converts TTF fonts to WOFF format. -- [ttf2woff2](https://github.com/nfroidure/ttf2woff2) - Converts TTF fonts to WOFF2. +- [wawoff2](https://github.com/fontello/wawoff2) - Converts TTF fonts to WOFF2 and versa vice. ## Roadmap diff --git a/package-lock.json b/package-lock.json index 7974fe53..5139b337 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1841,9 +1841,9 @@ } }, "caniuse-lite": { - "version": "1.0.30000960", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000960.tgz", - "integrity": "sha512-7nK5qs17icQaX6V3/RYrJkOsZyRNnroA4+ZwxaKJzIKy+crIy0Mz5CBlLySd2SNV+4nbUZeqeNfiaEieUBu3aA==", + "version": "1.0.30000962", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000962.tgz", + "integrity": "sha512-WXYsW38HK+6eaj5IZR16Rn91TGhU3OhbwjKZvJ4HN/XBIABLKfbij9Mnd3pM0VEwZSlltWjoWg3I8FQ0DGgNOA==", "dev": true }, "capture-exit": { @@ -10620,9 +10620,9 @@ "dev": true }, "uglify-js": { - "version": "3.5.4", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.5.4.tgz", - "integrity": "sha512-GpKo28q/7Bm5BcX9vOu4S46FwisbPbAmkkqPnGIpKvKTM96I85N6XHQV+k4I6FA2wxgLhcsSyHoNhzucwCflvA==", + "version": "3.5.5", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.5.5.tgz", + "integrity": "sha512-e58FqZzPwaLODQetDQKlvErZaGkh1UmzP8YwU0aG65NLourKNtwVyDG8tkIyUU0vqWzxaikSvTaxrCSscmvqvQ==", "dev": true, "optional": true, "requires": { diff --git a/package.json b/package.json index 4a3abc2f..9d12807c 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "eslint-plugin-jest": "^22.4.1", "eslint-plugin-jsx-a11y": "^6.0.0", "eslint-plugin-lodash": "^5.1.0", - "eslint-plugin-markdown": "^1.0.0-rc.1", + "eslint-plugin-markdown": "^1.0.0", "eslint-plugin-node": "^8.0.1", "eslint-plugin-promise": "^4.1.1", "eslint-plugin-react": "^7.1.0", diff --git a/src/__tests__/standalone.test.js b/src/__tests__/standalone.test.js index 4498ead9..872aeaa4 100644 --- a/src/__tests__/standalone.test.js +++ b/src/__tests__/standalone.test.js @@ -11,9 +11,11 @@ const fixturesGlob = "src/__tests__/fixtures"; describe("standalone", () => { it("should throw error if `files` not passed", async () => { - await expect(() => standalone()).toThrow( - "You must pass webfont a `files` glob" - ); + try { + await standalone(); + } catch (error) { + expect(error.message).toMatch("You must pass webfont a `files` glob"); + } }); it("should throw error `files glob patterns specified did not match any files` if not found files", async () => { diff --git a/src/cli.js b/src/cli.js index b0688a4d..1a9601d1 100644 --- a/src/cli.js +++ b/src/cli.js @@ -342,12 +342,11 @@ if (cli.flags.version || cli.flags.v) { } Promise.resolve() - .then(() => - Object.assign({}, optionsBase, { + .then(() => { + const options = Object.assign({}, optionsBase, { files: cli.input - }) - ) - .then(options => { + }); + if (options.files.length === 0) { cli.showHelp(); } diff --git a/src/standalone.js b/src/standalone.js index 0a1939ba..703ad16b 100644 --- a/src/standalone.js +++ b/src/standalone.js @@ -15,6 +15,27 @@ import ttf2woff from "ttf2woff"; import wawoff2 from "wawoff2"; import xml2js from "xml2js"; +async function buildConfig(options) { + let searchPath = process.cwd(); + let configPath = null; + + if (options.configFile) { + searchPath = null; + configPath = path.resolve(process.cwd(), options.configFile); + } + + const configExplorer = cosmiconfig("webfont"); + const config = await (configPath + ? configExplorer.load(configPath) + : configExplorer.search(searchPath)); + + if (!config) { + return {}; + } + + return config; +} + function getGlyphsData(files, options) { const metadataProvider = options.metadataProvider || @@ -87,7 +108,7 @@ function getGlyphsData(files, options) { }); } -function svgIcons2svgFont(glyphsData, options) { +function toSvg(glyphsData, options) { let result = ""; return new Promise((resolve, reject) => { @@ -128,30 +149,23 @@ function svgIcons2svgFont(glyphsData, options) { }); } -function buildConfig(options) { - let searchPath = process.cwd(); - let configPath = null; - - if (options.configFile) { - searchPath = null; - configPath = path.resolve(process.cwd(), options.configFile); - } +function toTtf(buffer, options) { + return Buffer.from(svg2ttf(buffer, options).buffer); +} - const configExplorer = cosmiconfig("webfont"); - const searchForConfig = configPath - ? configExplorer.load(configPath) - : configExplorer.search(searchPath); +function toEot(buffer) { + return Buffer.from(ttf2eot(buffer).buffer); +} - return searchForConfig.then(result => { - if (!result) { - return {}; - } +function toWoff(buffer, options) { + return Buffer.from(ttf2woff(buffer, options).buffer); +} - return result; - }); +function toWoff2(buffer) { + return wawoff2.compress(buffer); } -export default function(initialOptions) { +export default async function(initialOptions) { if (!initialOptions || !initialOptions.files) { throw new Error("You must pass webfont a `files` glob"); } @@ -196,162 +210,103 @@ export default function(initialOptions) { initialOptions ); - let glyphsData = []; - - return buildConfig({ + const config = await buildConfig({ configFile: options.configFile - }).then(loadedConfig => { - if (Object.keys(loadedConfig).length > 0) { - options = deepmerge(options, loadedConfig.config); - options.filePath = loadedConfig.filepath; - } + }); - return ( - globby([].concat(options.files)) - .then(foundFiles => { - const filteredFiles = foundFiles.filter( - foundFile => path.extname(foundFile) === ".svg" - ); - - if (filteredFiles.length === 0) { - throw new Error( - "Files glob patterns specified did not match any files" - ); - } + if (Object.keys(config).length > 0) { + options = deepmerge(options, config.config); + options.filePath = config.filepath; + } - return filteredFiles; - }) - .then(files => - Promise.resolve() - .then(() => getGlyphsData(files, options)) - .then(generatedDataInternal => { - glyphsData = generatedDataInternal; - - return svgIcons2svgFont(generatedDataInternal, options); - }) - ) - // Maybe add ttfautohint - .then(svgFont => { - const result = {}; - - result.svg = svgFont; - result.glyphsData = glyphsData; - - result.ttf = Buffer.from( - svg2ttf( - result.svg.toString(), - options.formatsOptions && options.formatsOptions.ttf - ? options.formatsOptions.ttf - : {} - ).buffer - ); - - if (options.formats.includes("eot")) { - result.eot = Buffer.from(ttf2eot(result.ttf).buffer); - } + const foundFiles = await globby([].concat(options.files)); + const filteredFiles = foundFiles.filter( + foundFile => path.extname(foundFile) === ".svg" + ); - if (options.formats.includes("woff")) { - result.woff = Buffer.from( - ttf2woff(result.ttf, { - metadata: options.metadata - }).buffer - ); - } + if (filteredFiles.length === 0) { + throw new Error("Files glob patterns specified did not match any files"); + } - return Promise.resolve() - .then(() => { - if (options.formats.includes("woff2")) { - return wawoff2.compress(result.ttf).then(woff2 => { - result.woff2 = woff2; + const result = {}; - return Promise.resolve(); - }); - } + result.glyphsData = await getGlyphsData(filteredFiles, options); + result.svg = await toSvg(result.glyphsData, options); + result.ttf = toTtf( + result.svg, + options.formatsOptions && options.formatsOptions.ttf + ? options.formatsOptions.ttf + : {} + ); - return Promise.resolve(); - }) - .then(() => result); - }) - .then(result => { - if (!options.template) { - return result; - } + if (options.formats.includes("eot")) { + result.eot = toEot(result.ttf); + } - const buildInTemplateDirectory = path.join(__dirname, "../templates"); - const buildInTemplates = { - css: { - path: path.join(buildInTemplateDirectory, "template.css.njk") - }, - html: { - path: path.join( - buildInTemplateDirectory, - "template.preview-html.njk" - ) - }, - scss: { - path: path.join(buildInTemplateDirectory, "template.scss.njk") - } - }; - - let templateFilePath = null; - - if (Object.keys(buildInTemplates).includes(options.template)) { - result.usedBuildInTemplate = true; - - nunjucks.configure(path.join(__dirname, "../")); - - templateFilePath = `${buildInTemplateDirectory}/template.${ - options.template - }.njk`; - } else { - const resolvedTemplateFilePath = path.resolve(options.template); - - nunjucks.configure(path.dirname(resolvedTemplateFilePath)); - - templateFilePath = path.resolve(resolvedTemplateFilePath); - } + if (options.formats.includes("woff")) { + result.woff = toWoff(result.ttf, { metadata: options.metadata }); + } - const nunjucksOptions = deepmerge.all([ - { - glyphs: glyphsData.map(glyphData => { - if (typeof options.glyphTransformFn === "function") { - glyphData.metadata = options.glyphTransformFn( - glyphData.metadata - ); - } + if (options.formats.includes("woff2")) { + result.woff2 = await toWoff2(result.ttf); + } - return glyphData.metadata; - }) - }, - options, - { - className: options.templateClassName - ? options.templateClassName - : options.fontName, - fontName: options.templateFontName - ? options.templateFontName - : options.fontName, - fontPath: options.templateFontPath.replace(/\/?$/, "/") - } - ]); - - result.template = nunjucks.render(templateFilePath, nunjucksOptions); - - return result; - }) - .then(result => { - if (!options.formats.includes("svg")) { - delete result.svg; - } + if (options.template) { + const templateDirectory = path.resolve(__dirname, "../templates"); + const buildInTemplates = { + css: { path: path.join(templateDirectory, "template.css.njk") }, + html: { path: path.join(templateDirectory, "template.html.njk") }, + scss: { path: path.join(templateDirectory, "template.scss.njk") } + }; - if (!options.formats.includes("ttf")) { - delete result.ttf; - } + let templateFilePath = null; - result.config = options; + if (Object.keys(buildInTemplates).includes(options.template)) { + result.usedBuildInTemplate = true; - return result; + nunjucks.configure(path.resolve(__dirname, "../")); + + templateFilePath = `${templateDirectory}/template.${ + options.template + }.njk`; + } else { + const resolvedTemplateFilePath = path.resolve(options.template); + + nunjucks.configure(path.dirname(resolvedTemplateFilePath)); + + templateFilePath = path.resolve(resolvedTemplateFilePath); + } + + const nunjucksOptions = deepmerge.all([ + { + glyphs: result.glyphsData.map(glyphData => { + if (typeof options.glyphTransformFn === "function") { + glyphData.metadata = options.glyphTransformFn(glyphData.metadata); + } + + return glyphData.metadata; }) - ); - }); + }, + options, + { + className: options.templateClassName || options.fontName, + fontName: options.templateFontName || options.fontName, + fontPath: options.templateFontPath.replace(/\/?$/, "/") + } + ]); + + result.template = nunjucks.render(templateFilePath, nunjucksOptions); + } + + if (!options.formats.includes("svg")) { + delete result.svg; + } + + if (!options.formats.includes("ttf")) { + delete result.ttf; + } + + result.config = options; + + return result; }