From 5475cb6401ce66fab54249f5e128750f137a6625 Mon Sep 17 00:00:00 2001 From: Adriano Cahete Date: Tue, 18 Jun 2019 16:13:16 -0300 Subject: [PATCH] Update naming --- bin/webfont-generator-cli | 152 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 bin/webfont-generator-cli diff --git a/bin/webfont-generator-cli b/bin/webfont-generator-cli new file mode 100644 index 0000000..1f8d4c4 --- /dev/null +++ b/bin/webfont-generator-cli @@ -0,0 +1,152 @@ +#! /usr/bin/env node + +'strict mode' + +require('colors') + +const { removeUndefinedKeys } = require('../lib/utils') +const { FORMATS } = require('../lib/constants') +const fontGenerator = require('../lib/index') +const errors = require('../lib/errors') +const minimist = require('minimist') +const glob = require('glob') + +/** + * Initialise script and options + * + * @return {void} + */ +function init() { + const args = minimist(process.argv.slice(2)) + const types = (args.types || 'svg,ttf,woff,woff2,eot') + .split(',') + .map(ext => ext.trim()) + + const options = removeUndefinedKeys({ + paths : getPaths(args._), + outputDir : args.o || args.out, + fontName : args.n || args.name, + fontsPath : args.f || args.fontspath, + types : types, + css : args.c || args.css, + cssPath : args.csspath, + cssFontsUrl : args.cssfontsurl, + cssTemplate : args.csstp, + json : args.j || args.json, + jsonPath : args.jsonpath, + html : args.html, + htmlPath : args.htmlpath, + htmlTemplate : args.htmltp, + silent : args.s || args.silent || false, + startCodepoint : args.codepoint, + codepoints : args.codepoints, + classPrefix : args.p || args.prefix, + baseTag : args.t || args.tag, + baseSelector : args.selector, + normalize : args.normalize, + round : args.round, + descent : args.descent, + fixedWidth : args.mono, + fontHeight : args.height, + centerHorizontally : args.center, + timestamp : args.timestamp + }) + + const calledEmpty = Object.keys(args).length === 1 && args._.length === 0 + + // Parse Boolean values that default to true + FORMATS.forEach(key => { + if (typeof options[key] !== 'undefined') { + options[key] = options[key] === 'true' + } + }) + + // Show usage if missing any arguments or called with -h / --help + if (args.h || args.help || calledEmpty) { + showHelp() + return process.exit() + } + + run(options) +} + +/** + * Run icon font kit creation + * + * @param {Object} options + * @return {void} + */ +function run(options) { + fontGenerator.generate(options, err => err ? fail(err) : null).catch(err => fail(err)) +} + +/** + * Fail - Interrut process and log validation errors or throw unhandled exceptions + * + * @param {Error|ValidationError} err + * @return {void} + */ +function fail(err) { + if (typeof err === 'string' || err instanceof errors.ValidationError) { + console.error(err.red || err.message.red) + process.exit() + } + + if (err instanceof Error) { throw err } +} + +/** + * Display usage + * + * @return {void} + */ +function showHelp() { + console.log( + 'Usage :'.bold + ' webfont-generator-cli [ svg-icons-glob ] -o [ output-dir ] [ options ]\n' + + 'Example :'.bold + ' webfont-generator-cli src/*.svg -o dist\n\n' + + + 'Options:\n'.bold + + ' -o, --out '.bold + 'Output icon font set files to directory\n'.italic + + ' -n, --name '.bold + 'Name to use for generated fonts and files (Default: icons)\n' + + ' -s, --silent '.bold + 'Do not produce output logs other than errors (Default: false)\n' + + ' -f, --fontspath '.bold + 'Relative path to fonts directory to use in output files (Default: ./)\n' + + ' -c, --css '.bold + 'Generate CSS file if true (Default: true)\n' + + ' --types '.bold + 'Comma delimited list of font types (Defaults to svg,ttf,woff,eot)\n' + + ' --csspath '.bold + 'CSS output path (Defaults to /.css)\n' + + ' --cssfontsurl '.bold + 'CSS fonts directory url (Defaults to relative path)\n' + + ' --csstp '.bold + 'CSS handlebars template path (Optional)\n' + + ' --html '.bold + 'Generate HTML preview file if true (Default: true)\n' + + ' --htmlpath '.bold + 'HTML output path (Defaults to /.html)\n' + + ' --htmltp '.bold + 'HTML handlebars template path (Optional)\n' + + ' --codepoint '.bold + 'The starting character code to count up from (Optional)\n' + + ' --codepoints '.bold + 'Path to explicit character code mapping JSON file (Optional)\n' + + ' -j, --json '.bold + 'Generate JSON map file if true (Default: true)\n' + + ' --jsonpath '.bold + 'JSON output path (Defaults to /.json)\n' + + ' -p, --prefix '.bold + 'CSS classname prefix for icons (Default: icon)\n' + + ' -t, --tag '.bold + 'CSS base selector for icons (Default: i)\n' + + ' --selector '.bold + 'Use a selector instead of \'tag + prefix\' (Default: null)\n' + + ' --normalize '.bold + 'Normalize icons sizes (Default: false)\n' + + ' --round '.bold + 'Setup SVG rounding (Default: 10e12)\n' + + ' --descent '.bold + 'Offset applied to the baseline (Default: 0)\n' + + ' --mono '.bold + 'Make font monospace (Default: false)\n' + + ' --height '.bold + 'Fixed font height value\n' + + ' --center '.bold + 'Center font horizontally\n' + + ' --timestamp '.bold + 'Override font creation time (Unix time stamp)' + ) +} + +/** + * Resolve globs + * + * @param {String} globs + * @return {[String]} + */ +function getPaths(globs) { + var out = [] + + globs.forEach(str => out = out.concat(glob.sync(str))) + + return out +} + +init()