UNPKG

@ramirezcgn/fantasticon

Version:

Fork of fantasticon that implements a forked version of svgicons2svgfont that support more options than the original.

70 lines 3.38 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeAssets = exports.loadAssets = exports.loadPaths = exports.ASSETS_EXTENSION = void 0; const glob_1 = require("glob"); const path_1 = require("path"); const path_2 = require("../utils/path"); const fs_async_1 = require("./fs-async"); exports.ASSETS_EXTENSION = 'svg'; const loadPaths = (dir) => __awaiter(void 0, void 0, void 0, function* () { const globPath = (0, path_1.join)(dir, `**/*.${exports.ASSETS_EXTENSION}`); // Fixes: https://github.com/tancredi/fantasticon/issues/470 const globPathFixed = globPath.replace(/\\/g, '/'); const files = yield (0, glob_1.glob)(globPathFixed, {}); if (!files.length) { throw new Error(`No SVGs found in ${dir}`); } return files.sort((a, b) => a.localeCompare(b)); }); exports.loadPaths = loadPaths; const failForConflictingId = ({ relativePath: pathA, id }, { relativePath: pathB }) => { throw new Error(`Conflicting result from 'getIconId': '${id}' - conflicting input files:\n` + [pathA, pathB].map(fpath => ` - ${fpath}`).join('\n')); }; const loadAssets = (_a) => __awaiter(void 0, [_a], void 0, function* ({ inputDir, getIconId }) { const paths = yield (0, exports.loadPaths)(inputDir); const out = {}; let index = 0; for (const path of paths) { const relativePath = (0, path_1.relative)((0, path_1.resolve)(inputDir), (0, path_1.resolve)(path)); const parts = (0, path_2.splitSegments)(relativePath); const basename = (0, path_2.removeExtension)(parts.pop()); const absolutePath = (0, path_1.resolve)(path); const iconId = getIconId({ basename, relativeDirPath: (0, path_1.join)(...parts), absoluteFilePath: absolutePath, relativeFilePath: relativePath, index }); const result = { id: iconId, relativePath, absolutePath }; if (out[iconId]) { failForConflictingId(out[iconId], result); } out[iconId] = result; index++; } return out; }); exports.loadAssets = loadAssets; const writeAssets = (assets_1, _b) => __awaiter(void 0, [assets_1, _b], void 0, function* (assets, { name, pathOptions = {}, outputDir }) { const results = []; for (const ext of Object.keys(assets)) { const filename = [name, ext].join('.'); const writePath = pathOptions[ext] || (0, path_1.join)(outputDir, filename); results.push({ content: assets[ext], writePath }); yield (0, fs_async_1.writeFile)(writePath, assets[ext]); } return results; }); exports.writeAssets = writeAssets; //# sourceMappingURL=assets.js.map