webfont 11.2.26 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/NOTICE.md +153 -0
  2. package/README.md +135 -28
  3. package/dist/cli.mjs +628 -0
  4. package/dist/index.js +1 -507
  5. package/dist/src/index.d.ts +2 -2
  6. package/dist/src/lib/execCLI/index.d.ts +3 -4
  7. package/dist/src/lib/inputSource.d.ts +5 -0
  8. package/dist/src/lib/p-limit/index.d.ts +2 -0
  9. package/dist/src/lib/sfnt/flavor.d.ts +2 -0
  10. package/dist/src/lib/svgicons2svgfont/index.d.ts +9 -0
  11. package/dist/src/lib/ttf2eot/index.d.ts +2 -0
  12. package/dist/src/standalone/convertWebfontInput.d.ts +3 -0
  13. package/dist/src/standalone/fetchWebfontUrl.d.ts +1 -0
  14. package/dist/src/standalone/glyphsData.d.ts +2 -2
  15. package/dist/src/standalone/index.d.ts +3 -3
  16. package/dist/src/standalone/inputMode.d.ts +7 -0
  17. package/dist/src/standalone/options.d.ts +3 -3
  18. package/dist/src/standalone/templateFonts.d.ts +3 -0
  19. package/dist/src/standalone/toTtf.d.ts +4 -0
  20. package/dist/src/types/DecompressedFont.d.ts +5 -0
  21. package/dist/src/types/Format.d.ts +4 -4
  22. package/dist/src/types/GlyphData.d.ts +2 -2
  23. package/dist/src/types/GlyphMetadata.d.ts +1 -1
  24. package/dist/src/types/GlyphTransformFn.d.ts +2 -2
  25. package/dist/src/types/InitialOptions.d.ts +5 -4
  26. package/dist/src/types/MetadataProvider.d.ts +5 -0
  27. package/dist/src/types/OptionsBase.d.ts +3 -2
  28. package/dist/src/types/Result.d.ts +8 -5
  29. package/dist/src/types/ResultConfig.d.ts +4 -0
  30. package/dist/src/types/WebfontOptions.d.ts +29 -3
  31. package/dist/src/types/index.d.ts +7 -6
  32. package/package.json +52 -57
  33. package/dist/cli.js +0 -801
  34. package/dist/jest.config.d.ts +0 -3
  35. package/dist/src/cli/index.d.ts +0 -1
  36. package/dist/src/cli/index.test.d.ts +0 -1
  37. package/dist/src/cli/meow/index.d.ts +0 -104
  38. package/dist/src/index.test.d.ts +0 -1
  39. package/dist/src/standalone/index.test.d.ts +0 -1
package/dist/cli.js DELETED
@@ -1,801 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- 'use strict';
4
-
5
- var fs$1 = require('fs');
6
- var path$1 = require('path');
7
- var meow = require('meow');
8
- var resolveFrom = require('resolve-from');
9
- var stream = require('stream');
10
- var SVGIcons2SVGFontStream = require('svgicons2svgfont');
11
- var cosmiconfig = require('cosmiconfig');
12
- var crypto = require('crypto');
13
- var deepmerge = require('deepmerge');
14
- var pLimit = require('p-limit');
15
- var xml2js = require('xml2js');
16
- var globby = require('globby');
17
- var nunjucks = require('nunjucks');
18
- var svg2ttf = require('svg2ttf');
19
- var ttf2eot = require('ttf2eot');
20
- var ttf2woff = require('ttf2woff');
21
- var wawoff2 = require('wawoff2');
22
-
23
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
24
-
25
- function _interopNamespace(e) {
26
- if (e && e.__esModule) return e;
27
- var n = Object.create(null);
28
- if (e) {
29
- Object.keys(e).forEach(function (k) {
30
- if (k !== 'default') {
31
- var d = Object.getOwnPropertyDescriptor(e, k);
32
- Object.defineProperty(n, k, d.get ? d : {
33
- enumerable: true,
34
- get: function () {
35
- return e[k];
36
- }
37
- });
38
- }
39
- });
40
- }
41
- n['default'] = e;
42
- return Object.freeze(n);
43
- }
44
-
45
- var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs$1);
46
- var fs__namespace = /*#__PURE__*/_interopNamespace(fs$1);
47
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path$1);
48
- var path__namespace = /*#__PURE__*/_interopNamespace(path$1);
49
- var meow__default = /*#__PURE__*/_interopDefaultLegacy(meow);
50
- var resolveFrom__default = /*#__PURE__*/_interopDefaultLegacy(resolveFrom);
51
- var SVGIcons2SVGFontStream__default = /*#__PURE__*/_interopDefaultLegacy(SVGIcons2SVGFontStream);
52
- var cosmiconfig__default = /*#__PURE__*/_interopDefaultLegacy(cosmiconfig);
53
- var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto);
54
- var deepmerge__default = /*#__PURE__*/_interopDefaultLegacy(deepmerge);
55
- var pLimit__default = /*#__PURE__*/_interopDefaultLegacy(pLimit);
56
- var xml2js__default = /*#__PURE__*/_interopDefaultLegacy(xml2js);
57
- var globby__default = /*#__PURE__*/_interopDefaultLegacy(globby);
58
- var nunjucks__default = /*#__PURE__*/_interopDefaultLegacy(nunjucks);
59
- var svg2ttf__default = /*#__PURE__*/_interopDefaultLegacy(svg2ttf);
60
- var ttf2eot__default = /*#__PURE__*/_interopDefaultLegacy(ttf2eot);
61
- var ttf2woff__default = /*#__PURE__*/_interopDefaultLegacy(ttf2woff);
62
- var wawoff2__default = /*#__PURE__*/_interopDefaultLegacy(wawoff2);
63
-
64
- /*! *****************************************************************************
65
- Copyright (c) Microsoft Corporation.
66
-
67
- Permission to use, copy, modify, and/or distribute this software for any
68
- purpose with or without fee is hereby granted.
69
-
70
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
71
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
72
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
73
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
74
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
75
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
76
- PERFORMANCE OF THIS SOFTWARE.
77
- ***************************************************************************** */
78
-
79
- var __assign = function() {
80
- __assign = Object.assign || function __assign(t) {
81
- for (var s, i = 1, n = arguments.length; i < n; i++) {
82
- s = arguments[i];
83
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
84
- }
85
- return t;
86
- };
87
- return __assign.apply(this, arguments);
88
- };
89
-
90
- function __awaiter(thisArg, _arguments, P, generator) {
91
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
92
- return new (P || (P = Promise))(function (resolve, reject) {
93
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
94
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
95
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
96
- step((generator = generator.apply(thisArg, _arguments || [])).next());
97
- });
98
- }
99
-
100
- function __generator(thisArg, body) {
101
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
102
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
103
- function verb(n) { return function (v) { return step([n, v]); }; }
104
- function step(op) {
105
- if (f) throw new TypeError("Generator is already executing.");
106
- while (_) try {
107
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
108
- if (y = 0, t) op = [op[0] & 2, t.value];
109
- switch (op[0]) {
110
- case 0: case 1: t = op; break;
111
- case 4: _.label++; return { value: op[1], done: false };
112
- case 5: _.label++; y = op[1]; op = [0]; continue;
113
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
114
- default:
115
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
116
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
117
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
118
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
119
- if (t[2]) _.ops.pop();
120
- _.trys.pop(); continue;
121
- }
122
- op = body.call(thisArg, _);
123
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
124
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
125
- }
126
- }
127
-
128
- /* eslint-disable max-len, sort-keys */
129
- var meowCLI = meow__default['default']("\n Usage: webfont [input] [options]\n\n Input: File(s) or glob(s).\n\n If an input argument is wrapped in quotation marks, it will be passed to \"fast-glob\"\n for cross-platform glob support.\n\n Options:\n\n --config\n\n Path to a specific configuration file (JSON, YAML, or CommonJS)\n or the name of a module in `node_modules` that points to one.\n If no `--config` argument is provided, webfont will search for\n configuration files in the following places, in this order:\n - a `webfont` property in `package.json`\n - a `.webfontrc` file (with or without filename extension:\n `.json`, `.yaml`, and `.js` are available)\n - a `webfont.config.js` file exporting a JS object\n The search will begin in the working directory and move up the\n directory tree until a configuration file is found.\n\n -f, --fontName\n\n The font family name you want, default: \"webfont\".\n\n -h, --help\n\n Output usage information.\n\n -v, --version\n\n Output the version number.\n\n -r, --formats\n\n Only this formats generate.\n\n -d, --dest\n\n Destination for generated fonts.\n\n -m, --dest-create\n\n Create destination directory if it does not exist.\n\n -t, --template\n\n Type of template ('css', 'scss', 'styl') or path to custom template.\n\n -s, --destTemplate\n\n Destination for generated template. If not passed used `dest` argument value.\n\n -c, --templateClassName\n\n Class name in css template.\n\n -p, --templateFontPath\n\n Font path in css template.\n\n -n, --templateFontName\n\n Font name in css template.\n\n --templateCacheString\n\n Specify cache string in scss/css template.\n\n --no-sort\n\n Keeps the files in the same order of entry\n\n --verbose\n\n Tell me everything!.\n\n For \"svgicons2svgfont\":\n\n --fontId\n\n The font id you want, default as \"--fontName\".\n\n --fontStyle\n\n The font style you want.\n\n --fontWeight\n\n The font weight you want.\n\n --fixedWidth\n\n Creates a monospace font of the width of the largest input icon.\n\n --centerHorizontally\n\n Calculate the bounds of a glyph and center it horizontally.\n\n --normalize\n\n Normalize icons by scaling them to the height of the highest icon.\n\n --fontHeight\n\n The outputted font height [MAX(icons.height)].\n\n --round\n\n Setup the SVG path rounding [10e12].\n\n --descent\n\n The font descent [0].\n\n --ascent\n\n The font ascent [height - descent].\n\n --startUnicode\n\n The start unicode codepoint for files without prefix [0xEA01].\n\n --prependUnicode\n\n Prefix files with their automatically allocated unicode codepoint.\n\n --metadata\n\n Content of the metadata tag.\n\n --addHashInFontUrl\n\n Generated font url will be : [webfont].[ext]?v=[hash]\n", {
130
- autoHelp: false,
131
- autoVersion: false,
132
- flags: {
133
- ascent: {
134
- type: "string"
135
- },
136
- centerHorizontally: {
137
- type: "boolean"
138
- },
139
- config: {
140
- "default": null
141
- },
142
- descent: {
143
- type: "string"
144
- },
145
- dest: {
146
- alias: "d",
147
- "default": process.cwd(),
148
- type: "string"
149
- },
150
- destCreate: {
151
- alias: "m",
152
- "default": false,
153
- type: "boolean"
154
- },
155
- destTemplate: {
156
- alias: "s",
157
- type: "string"
158
- },
159
- fixedWidth: {
160
- type: "boolean"
161
- },
162
- fontHeight: {
163
- type: "string"
164
- },
165
- fontId: {
166
- type: "string"
167
- },
168
- fontName: {
169
- alias: "u",
170
- type: "string"
171
- },
172
- fontStyle: {
173
- type: "string"
174
- },
175
- fontWeight: {
176
- type: "string"
177
- },
178
- formats: {
179
- alias: "f"
180
- },
181
- help: {
182
- alias: "h",
183
- type: "boolean"
184
- },
185
- normalize: {
186
- type: "boolean"
187
- },
188
- prependUnicode: {
189
- type: "boolean"
190
- },
191
- round: {
192
- type: "string"
193
- },
194
- sort: {
195
- "default": true,
196
- type: "boolean"
197
- },
198
- startUnicode: {
199
- type: "string"
200
- },
201
- template: {
202
- alias: "t",
203
- type: "string"
204
- },
205
- templateClassName: {
206
- alias: "c",
207
- type: "string"
208
- },
209
- templateFontName: {
210
- alias: "n",
211
- type: "string"
212
- },
213
- addHashInFontUrl: {
214
- "default": false,
215
- type: "boolean"
216
- },
217
- templateFontPath: {
218
- alias: "p",
219
- type: "string"
220
- },
221
- templateCacheString: {
222
- "default": "",
223
- type: "string"
224
- },
225
- verbose: {
226
- "default": false,
227
- type: "boolean"
228
- },
229
- version: {
230
- alias: "v",
231
- type: "boolean"
232
- }
233
- }
234
- });
235
-
236
- var getTemplateDirectory = function () { return path__default['default'].resolve(__dirname, "../templates"); };
237
- var getTemplateFilePath = function (template) {
238
- var templateDirectory = getTemplateDirectory();
239
- return templateDirectory + "/template." + template + ".njk";
240
- };
241
- var getBuiltInTemplates = function () {
242
- var templateDirectory = getTemplateDirectory();
243
- return {
244
- css: { path: path__default['default'].join(templateDirectory, "template.css.njk") },
245
- html: { path: path__default['default'].join(templateDirectory, "template.html.njk") },
246
- json: { path: path__default['default'].join(templateDirectory, "template.json.njk") },
247
- scss: { path: path__default['default'].join(templateDirectory, "template.scss.njk") },
248
- styl: { path: path__default['default'].join(templateDirectory, "template.styl.njk") }
249
- };
250
- };
251
-
252
- /* eslint-disable complexity */
253
-
254
- const testExpression = /(^|\/|\\)(?:((?:u[0-9a-f]{4,6},?)+)-)(.+)\.svg$/i;
255
-
256
- function fileSorter(fileA, fileB) {
257
- const hasUnicodeA = testExpression.test(fileA);
258
- const hasUnicodeB = testExpression.test(fileB);
259
-
260
- if (hasUnicodeA == hasUnicodeB) {
261
- // just compare alphabetically
262
- const fileA_ = fileA.substr(0, fileA.lastIndexOf('.'));
263
- const fileB_ = fileB.substr(0, fileB.lastIndexOf('.'));
264
- return fileA_ < fileB_ ? -1 : 1;
265
- } else {
266
- // map true to 0, because we want it to be first
267
- return (hasUnicodeA ? 0 : 1) - (hasUnicodeB ? 0 : 1);
268
- }
269
- }
270
-
271
- var filesorter = fileSorter;
272
-
273
- /* eslint-disable prefer-template,newline-per-chained-call,complexity */
274
-
275
- const path = path__default['default'];
276
- const fs = fs__default['default'];
277
-
278
- function getMetadataService(options = {}) {
279
- let usedUnicodes = [];
280
-
281
- // Default options
282
- options.prependUnicode = !!options.prependUnicode;
283
- options.startUnicode =
284
- 'number' === typeof options.startUnicode ? options.startUnicode : 0xea01;
285
- options.log = options.log || console.log; // eslint-disable-line
286
- options.err = options.err || console.err; // eslint-disable-line
287
-
288
- // Throw on old options usage
289
- if ('undefined' !== typeof options.appendUnicode) {
290
- throw new Error(
291
- 'The "appendUnicode" option was renamed "prependUnicode".' +
292
- ' See https://github.com/nfroidure/gulp-svgicons2svgfont/issues/33'
293
- );
294
- }
295
-
296
- return function getMetadataFromFile(file, cb) {
297
- const basename = path.basename(file);
298
- const metadata = {
299
- path: file,
300
- name: '',
301
- unicode: [],
302
- renamed: false,
303
- };
304
- const matches = basename.match(/^(?:((?:u[0-9a-f]{4,6},?)+)-)?(.+)\.svg$/i);
305
-
306
- metadata.name =
307
- matches && matches[2] ? matches[2] : 'icon' + options.startUnicode;
308
- if (matches && matches[1]) {
309
- metadata.unicode = matches[1].split(',').map((match) => {
310
- match = match.substr(1);
311
- return match
312
- .split('u')
313
- .map((code) => String.fromCodePoint(parseInt(code, 16)))
314
- .join('');
315
- });
316
- if (-1 !== usedUnicodes.indexOf(metadata.unicode[0])) {
317
- cb(
318
- new Error(
319
- 'The unicode codepoint of the glyph ' +
320
- metadata.name +
321
- ' seems to be already used by another glyph.'
322
- )
323
- );
324
- return;
325
- }
326
- usedUnicodes.push(...metadata.unicode);
327
- } else {
328
- do {
329
- metadata.unicode[0] = String.fromCodePoint(options.startUnicode++);
330
- } while (usedUnicodes.includes(metadata.unicode[0]));
331
- usedUnicodes.push(metadata.unicode[0]);
332
- if (options.prependUnicode) {
333
- metadata.renamed = true;
334
- metadata.path = path.join(
335
- path.dirname(file),
336
- 'u' +
337
- metadata.unicode[0].codePointAt(0).toString(16).toUpperCase() +
338
- '-' +
339
- basename
340
- );
341
- fs.rename(file, metadata.path, (err) => {
342
- if (err) {
343
- cb(
344
- new Error(
345
- 'Could not save codepoint: ' +
346
- 'u' +
347
- metadata.unicode[0]
348
- .codePointAt(0)
349
- .toString(16)
350
- .toUpperCase() +
351
- ' for ' +
352
- basename
353
- )
354
- );
355
- return;
356
- }
357
- cb(null, metadata);
358
- });
359
- }
360
- }
361
- if (!metadata.renamed) {
362
- setImmediate(() => cb(null, metadata));
363
- }
364
- };
365
- }
366
-
367
- var metadata = getMetadataService;
368
-
369
- var getGlyphsData = function (files, options) {
370
- var metadataProvider = options.metadataProvider ||
371
- metadata({
372
- prependUnicode: options.prependUnicode,
373
- startUnicode: options.startUnicode
374
- });
375
- var xmlParser = new xml2js__default['default'].Parser();
376
- var throttle = pLimit__default['default'](options.maxConcurrency);
377
- return Promise.all(files.map(function (srcPath) { return throttle(function () { return new Promise(function (resolve, reject) {
378
- var glyph = fs$1.createReadStream(srcPath);
379
- var glyphContents = "";
380
- // eslint-disable-next-line no-promise-executor-return
381
- return glyph.
382
- on("error", function (glyphError) { return reject(glyphError); }).
383
- on("data", function (data) {
384
- glyphContents += data.toString();
385
- }).
386
- on("end", function () {
387
- // Maybe bug in xml2js
388
- if (glyphContents.length === 0) {
389
- return reject(new Error("Empty file " + srcPath));
390
- }
391
- return xmlParser.parseString(glyphContents, function (error) {
392
- if (error) {
393
- return reject(error);
394
- }
395
- var glyphData = {
396
- contents: glyphContents,
397
- srcPath: srcPath
398
- };
399
- return resolve(glyphData);
400
- });
401
- });
402
- }); }); })).then(function (glyphsData) {
403
- var sortedGlyphsData = glyphsData;
404
- if (options.sort) {
405
- var sortCallback = function (fileA, fileB) { return filesorter(fileA.srcPath, fileB.srcPath); };
406
- sortedGlyphsData = glyphsData.sort(sortCallback);
407
- }
408
- return Promise.all(sortedGlyphsData.map(function (glyphData) { return new Promise(function (resolve, reject) {
409
- metadataProvider(glyphData.srcPath, function (error, metadata) {
410
- if (error) {
411
- return reject(error);
412
- }
413
- metadata.unicode.push(metadata.name.replace(/-/gu, "_"));
414
- glyphData.metadata = metadata;
415
- return resolve(glyphData);
416
- });
417
- }); }));
418
- });
419
- };
420
-
421
- var getOptions = function (initialOptions) {
422
- if (!initialOptions || !initialOptions.files) {
423
- throw new Error("You must pass webfont a `files` glob");
424
- }
425
- return __assign({ centerHorizontally: false, descent: 0, fixedWidth: false, fontHeight: null, fontId: null, fontName: "webfont", fontStyle: "", fontWeight: "", formats: ["svg", "ttf", "eot", "woff", "woff2"], formatsOptions: {
426
- ttf: {
427
- copyright: null,
428
- ts: null,
429
- version: null
430
- }
431
- }, glyphTransformFn: null,
432
- /*
433
- * Maybe allow setup from CLI
434
- * This is usually less than file read maximums while staying performance
435
- */
436
- maxConcurrency: 100, metadata: null, metadataProvider: null, normalize: false, prependUnicode: false, round: 10e12, sort: true, startUnicode: 0xea01, template: null, templateCacheString: null, templateClassName: null, templateFontName: null, templateFontPath: "./", verbose: false }, initialOptions);
437
- };
438
-
439
- var buildConfig = function (options) { return __awaiter(void 0, void 0, void 0, function () {
440
- var searchPath, configPath, configExplorer, config;
441
- return __generator(this, function (_a) {
442
- switch (_a.label) {
443
- case 0:
444
- searchPath = process.cwd();
445
- configPath = null;
446
- if (options.configFile) {
447
- searchPath = null;
448
- configPath = path__default['default'].resolve(process.cwd(), options.configFile);
449
- }
450
- configExplorer = cosmiconfig__default['default']("webfont");
451
- return [4 /*yield*/, configExplorer.search(searchPath)];
452
- case 1:
453
- config = _a.sent();
454
- if (!configPath) return [3 /*break*/, 3];
455
- return [4 /*yield*/, configExplorer.load(configPath)];
456
- case 2:
457
- config = _a.sent();
458
- _a.label = 3;
459
- case 3:
460
- if (!config) {
461
- return [2 /*return*/, {}];
462
- }
463
- return [2 /*return*/, config];
464
- }
465
- });
466
- }); };
467
- var toSvg = function (glyphsData, options) {
468
- var result = "";
469
- return new Promise(function (resolve, reject) {
470
- var log = function () {
471
- Function.prototype();
472
- };
473
- if (options.verbose) {
474
- // eslint-disable-next-line no-console
475
- log = console.log.bind(console);
476
- }
477
- var fontStream = new SVGIcons2SVGFontStream__default['default']({
478
- ascent: options.ascent,
479
- centerHorizontally: options.centerHorizontally,
480
- descent: options.descent,
481
- fixedWidth: options.fixedWidth,
482
- fontHeight: options.fontHeight,
483
- fontId: options.fontId,
484
- fontName: options.fontName,
485
- fontStyle: options.fontStyle,
486
- fontWeight: options.fontWeight,
487
- log: log,
488
- metadata: options.metadata,
489
- normalize: options.normalize,
490
- round: options.round
491
- }).
492
- on("finish", function () { return resolve(result); }).
493
- on("data", function (data) {
494
- result += data;
495
- }).
496
- on("error", function (error) { return reject(error); });
497
- glyphsData.forEach(function (glyphData) {
498
- var glyphStream = new stream.Readable();
499
- glyphStream.push(glyphData.contents);
500
- glyphStream.push(null);
501
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
502
- // @ts-ignore
503
- glyphStream.metadata = glyphData.metadata;
504
- fontStream.write(glyphStream);
505
- });
506
- fontStream.end();
507
- });
508
- };
509
- var toTtf = function (buffer, options) { return Buffer.from(svg2ttf__default['default'](buffer, options).buffer); };
510
- var toEot = function (buffer) { return Buffer.from(ttf2eot__default['default'](buffer).buffer); };
511
- var toWoff = function (buffer, options) { return Buffer.from(ttf2woff__default['default'](buffer, options).buffer); };
512
- var toWoff2 = function (buffer) { return wawoff2__default['default'].compress(buffer); };
513
- var webfont = function (initialOptions) { return __awaiter(void 0, void 0, void 0, function () {
514
- var options, config, foundFiles, filteredFiles, glyphsData, transformedGlyphs, ttfOptions, svg, ttf, result, _a, builtInTemplates, templateFilePath, builtInPath, resolvedTemplateFilePath, hashOption, nunjucksOptions;
515
- return __generator(this, function (_b) {
516
- switch (_b.label) {
517
- case 0:
518
- options = getOptions(initialOptions);
519
- return [4 /*yield*/, buildConfig({
520
- configFile: options.configFile
521
- })];
522
- case 1:
523
- config = _b.sent();
524
- if (Object.keys(config).length > 0) {
525
- options = deepmerge__default['default'](options, config.config, {
526
- arrayMerge: function (_destinationArray, sourceArray) { return sourceArray; }
527
- });
528
- options.filePath = config.filepath;
529
- }
530
- return [4 /*yield*/, globby__default['default']([].concat(options.files))];
531
- case 2:
532
- foundFiles = _b.sent();
533
- filteredFiles = foundFiles.filter(function (foundFile) { return path__default['default'].extname(foundFile) === ".svg"; });
534
- if (filteredFiles.length === 0) {
535
- throw new Error("Files glob patterns specified did not match any files");
536
- }
537
- return [4 /*yield*/, getGlyphsData(filteredFiles, options)];
538
- case 3:
539
- glyphsData = _b.sent();
540
- if (!(options.glyphTransformFn && typeof options.glyphTransformFn === "function")) return [3 /*break*/, 5];
541
- transformedGlyphs = glyphsData.map(function (glyphData) { return __awaiter(void 0, void 0, void 0, function () {
542
- var metadata;
543
- return __generator(this, function (_a) {
544
- switch (_a.label) {
545
- case 0: return [4 /*yield*/, options.glyphTransformFn(glyphData.metadata)];
546
- case 1:
547
- metadata = _a.sent();
548
- return [2 /*return*/, __assign(__assign({}, glyphData), { metadata: metadata })];
549
- }
550
- });
551
- }); });
552
- return [4 /*yield*/, Promise.all(transformedGlyphs)];
553
- case 4:
554
- glyphsData = _b.sent();
555
- _b.label = 5;
556
- case 5:
557
- ttfOptions = {};
558
- if (options.formatsOptions && options.formatsOptions.ttf) {
559
- ttfOptions = options.formatsOptions.ttf;
560
- }
561
- return [4 /*yield*/, toSvg(glyphsData, options)];
562
- case 6:
563
- svg = _b.sent();
564
- ttf = toTtf(svg, ttfOptions);
565
- result = {
566
- glyphsData: glyphsData,
567
- hash: crypto__default['default'].createHash("md5").update(svg).
568
- digest("hex"),
569
- svg: svg,
570
- ttf: ttf
571
- };
572
- if (options.formats.includes("eot")) {
573
- result.eot = toEot(ttf);
574
- }
575
- if (options.formats.includes("woff")) {
576
- result.woff = toWoff(ttf, { metadata: options.metadata });
577
- }
578
- if (!options.formats.includes("woff2")) return [3 /*break*/, 8];
579
- _a = result;
580
- return [4 /*yield*/, toWoff2(ttf)];
581
- case 7:
582
- _a.woff2 = _b.sent();
583
- _b.label = 8;
584
- case 8:
585
- if (options.template) {
586
- builtInTemplates = getBuiltInTemplates();
587
- templateFilePath = void 0;
588
- if (Object.keys(builtInTemplates).includes(options.template)) {
589
- result.usedBuildInTemplate = true;
590
- builtInPath = path__default['default'].resolve(__dirname, "../..");
591
- nunjucks__default['default'].configure(builtInPath);
592
- templateFilePath = getTemplateFilePath(options.template);
593
- }
594
- else {
595
- resolvedTemplateFilePath = path__default['default'].resolve(options.template);
596
- nunjucks__default['default'].configure(path__default['default'].dirname(resolvedTemplateFilePath));
597
- templateFilePath = path__default['default'].resolve(resolvedTemplateFilePath);
598
- }
599
- hashOption = {};
600
- if (options.addHashInFontUrl) {
601
- hashOption = { hash: result.hash };
602
- }
603
- nunjucksOptions = deepmerge__default['default'].all([
604
- {
605
- glyphs: result.glyphsData.map(function (glyph) { return glyph.metadata; })
606
- },
607
- options,
608
- {
609
- cacheString: options.templateCacheString || Date.now(),
610
- className: options.templateClassName || options.fontName,
611
- fontName: options.templateFontName || options.fontName,
612
- fontPath: options.templateFontPath.replace(/\/?$/u, "/")
613
- },
614
- hashOption,
615
- {
616
- fonts: Object.fromEntries(new Map(options.formats.map(function (format) { return [
617
- format, function () {
618
- if (format === "woff2") {
619
- return Buffer.from(result.woff2).toString("base64");
620
- }
621
- return result[format].toString("base64");
622
- },
623
- ]; })))
624
- },
625
- ]);
626
- result.template = nunjucks__default['default'].render(templateFilePath, nunjucksOptions);
627
- }
628
- if (!options.formats.includes("svg")) {
629
- delete result.svg;
630
- }
631
- if (!options.formats.includes("ttf")) {
632
- delete result.ttf;
633
- }
634
- result.config = options;
635
- return [2 /*return*/, result];
636
- }
637
- });
638
- }); };
639
-
640
- var optionsBase = {};
641
- if (typeof meowCLI.flags.config === "string") {
642
- /*
643
- * Should check these possibilities:
644
- * a. name of a node_module
645
- * b. absolute path
646
- * c. relative path relative to `process.cwd()`.
647
- * If none of the above work, we'll try a relative path starting
648
- * in `process.cwd()`.
649
- */
650
- optionsBase.configFile =
651
- resolveFrom__default['default'](process.cwd(), meowCLI.flags.config) ||
652
- path__namespace.join(process.cwd(), meowCLI.flags.config);
653
- }
654
- if (meowCLI.flags.fontName) {
655
- optionsBase.fontName = meowCLI.flags.fontName;
656
- }
657
- if (meowCLI.flags.formats) {
658
- optionsBase.formats = meowCLI.flags.formats;
659
- }
660
- if (meowCLI.flags.dest) {
661
- optionsBase.dest = meowCLI.flags.dest;
662
- }
663
- if (meowCLI.flags.destCreate) {
664
- optionsBase.destCreate = meowCLI.flags.destCreate;
665
- }
666
- if (meowCLI.flags.template) {
667
- optionsBase.template = meowCLI.flags.template;
668
- }
669
- if (meowCLI.flags.templateClassName) {
670
- optionsBase.templateClassName = meowCLI.flags.templateClassName;
671
- }
672
- if (meowCLI.flags.templateFontPath) {
673
- optionsBase.templateFontPath = meowCLI.flags.templateFontPath;
674
- }
675
- if (meowCLI.flags.templateFontName) {
676
- optionsBase.templateFontName = meowCLI.flags.templateFontName;
677
- }
678
- if (meowCLI.flags.templateCacheString) {
679
- optionsBase.templateCacheString = meowCLI.flags.templateCacheString;
680
- }
681
- if (meowCLI.flags.destTemplate) {
682
- optionsBase.destTemplate = meowCLI.flags.destTemplate;
683
- }
684
- if (meowCLI.flags.verbose) {
685
- optionsBase.verbose = meowCLI.flags.verbose;
686
- }
687
- if (meowCLI.flags.fontId) {
688
- optionsBase.fontId = meowCLI.flags.fontId;
689
- }
690
- if (meowCLI.flags.fontStyle) {
691
- optionsBase.fontStyle = meowCLI.flags.fontStyle;
692
- }
693
- if (meowCLI.flags.fontWeight) {
694
- optionsBase.fontWeight = meowCLI.flags.fontWeight;
695
- }
696
- if (meowCLI.flags.fixedWidth) {
697
- optionsBase.fixedWidth = meowCLI.flags.fixedWidth;
698
- }
699
- if (meowCLI.flags.centerHorizontally) {
700
- optionsBase.centerHorizontally = meowCLI.flags.centerHorizontally;
701
- }
702
- if (meowCLI.flags.normalize) {
703
- optionsBase.normalize = meowCLI.flags.normalize;
704
- }
705
- if (meowCLI.flags.fontHeight) {
706
- optionsBase.fontHeight = meowCLI.flags.fontHeight;
707
- }
708
- if (meowCLI.flags.round) {
709
- optionsBase.round = meowCLI.flags.round;
710
- }
711
- if (meowCLI.flags.descent) {
712
- optionsBase.descent = meowCLI.flags.descent;
713
- }
714
- if (meowCLI.flags.ascent) {
715
- optionsBase.ascent = meowCLI.flags.ascent;
716
- }
717
- if (meowCLI.flags.startUnicode) {
718
- optionsBase.startUnicode = meowCLI.flags.startUnicode;
719
- }
720
- if (meowCLI.flags.prependUnicode) {
721
- optionsBase.prependUnicode = meowCLI.flags.prependUnicode;
722
- }
723
- if (meowCLI.flags.metadata) {
724
- optionsBase.metadata = meowCLI.flags.metadata;
725
- }
726
- if (meowCLI.flags.sort === false) {
727
- optionsBase.sort = meowCLI.flags.sort;
728
- }
729
- if (meowCLI.flags.addHashInFontUrl) {
730
- optionsBase.addHashInFontUrl = meowCLI.flags.addHashInFontUrl;
731
- }
732
- if (meowCLI.flags.help || meowCLI.flags.h) {
733
- meowCLI.showHelp();
734
- }
735
- if (meowCLI.flags.version || meowCLI.flags.v) {
736
- meowCLI.showVersion();
737
- }
738
- Promise.resolve().
739
- then(function () {
740
- var options = __assign(__assign({}, optionsBase), { files: meowCLI.input });
741
- if (options.files.length === 0) {
742
- meowCLI.showHelp();
743
- }
744
- return webfont(options).then(function (result) {
745
- result.config = __assign({ dest: options.dest, destTemplate: options.destTemplate }, result.config);
746
- return result;
747
- });
748
- }).
749
- then(function (result) {
750
- var _a = result.config, fontName = _a.fontName, dest = _a.dest, destCreate = _a.destCreate;
751
- var destTemplate = null;
752
- if (result.template) {
753
- (destTemplate = result.config.destTemplate);
754
- if (!destTemplate) {
755
- destTemplate = dest;
756
- }
757
- if (result.usedBuildInTemplate) {
758
- destTemplate = path__namespace.join(destTemplate, result.config.fontName + "." + result.config.template);
759
- }
760
- else {
761
- destTemplate = path__namespace.join(destTemplate, path__namespace.basename(result.config.template).replace(".njk", ""));
762
- }
763
- delete result.hash;
764
- }
765
- return Promise.resolve().
766
- then(function () { return new Promise(function (resolve, reject) {
767
- fs__namespace.access(dest, fs__namespace.constants.F_OK, function (err) { return reject(err); });
768
- }); })["catch"](function (error) {
769
- if (error && destCreate) {
770
- return new Promise(function (resolve) {
771
- fs__namespace.mkdir(dest, { recursive: true }, function () { return resolve(destCreate); });
772
- });
773
- }
774
- return error;
775
- })["finally"](function () { return Promise.all(Object.keys(result).map(function (type) {
776
- if (type === "config" || type === "usedBuildInTemplate" || type === "glyphsData") {
777
- return null;
778
- }
779
- var content = result[type];
780
- // eslint-disable-next-line init-declarations
781
- var file;
782
- if (type === "template") {
783
- file = path__namespace.resolve(destTemplate);
784
- }
785
- else {
786
- file = path__namespace.resolve(path__namespace.join(dest, fontName + "." + type));
787
- }
788
- return fs__namespace.writeFile(file, content, function () {
789
- Function.prototype();
790
- });
791
- })); }).
792
- then(function () { return Promise.resolve(result); });
793
- })["catch"](function (error) {
794
- // eslint-disable-next-line no-console
795
- console.log(error.stack);
796
- var exitCode = 1;
797
- if (typeof error.code === "number") {
798
- exitCode = error.code;
799
- }
800
- process.exit(exitCode);
801
- });