This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
forked from Workshape/icon-font-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
766fe60
commit 5475cb6
Showing
1 changed file
with
152 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <out> 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 <out>/<name>.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 <out>/<name>.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 <out>/<name>.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() |