UNPKG

ads-icons-react

Version:
48 lines (44 loc) 1.72 kB
#! /usr/bin/env node const fs = require("fs").promises; // Use the promise-based version of fs const path = require("path"); const { glob } = require("glob"); /** * Asynchronously reads the content of an icon file and returns an object containing * the file name and its data. * * @async * @param {string} file - The path to the icon file to be read. * @returns {Promise<{file: string, data: string}>} A promise that resolves to an object * containing the 'file' name (path) and the 'data' as a UTF-8 string. * @throws {Error} Throws an error if reading the file fails. */ const readIcon = async (file) => { const data = await fs.readFile(file, "utf8"); return { file, data }; }; /** * Asynchronously searches for SVG icon files within the 'node_modules/@mozaic-ds/icons/build/svg' * directory and reads their contents. Returns an array of objects, each representing an icon * with its file name and data. * * @async * @returns {Promise<Array<{file: string, data: string}>>} A promise that resolves to an array of * objects, where each object contains the 'file' name (only the basename) and the 'data' of an icon. * @throws {Error} Throws an error if the search or file reading fails. */ const fetchIcons = async () => { try { const inputPath = path.relative( process.cwd(), "node_modules/@mozaic-ds/icons/build/svg/**/*.svg" ); const icons = await glob(inputPath); const iconsData = await Promise.all(icons.map(readIcon)); iconsData.forEach((icon) => (icon.file = path.basename(icon.file))); return iconsData; } catch (err) { console.error(err); throw err; // Rethrowing the error to be handled by the caller } }; module.exports = fetchIcons;