From b2cf3885ce82351a74b3562c7a19b9e7a45ce67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Sun, 8 May 2022 06:48:32 -0500 Subject: [PATCH] expose `getIconsData*` util functions (#7394) --- scripts/utils.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/scripts/utils.js b/scripts/utils.js index 194dc2ecae75..db0fed588c19 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -95,24 +95,28 @@ export const htmlFriendlyToTitle = (htmlFriendlyTitle) => /** * Get contents of _data/simple-icons.json. + * @param {String|undefined} rootDir Path to the root directory of the project. */ -export const getIconsDataString = () => { - const __dirname = getDirnameFromImportMeta(import.meta.url); - const rootDir = path.resolve(__dirname, '..'); +export const getIconsDataString = (rootDir) => { + if (rootDir === undefined) { + rootDir = path.resolve(getDirnameFromImportMeta(import.meta.url), '..'); + } const iconDataPath = path.resolve(rootDir, '_data', 'simple-icons.json'); return fs.readFile(iconDataPath, 'utf8'); }; /** - * Get icon data as object from _data/simple-icons.json. + * Get icons data as object from _data/simple-icons.json. + * @param {String|undefined} rootDir Path to the root directory of the project. */ -export const getIconsData = async () => { - const fileContents = await getIconsDataString(); +export const getIconsData = async (rootDir) => { + const fileContents = await getIconsDataString(rootDir); return JSON.parse(fileContents).icons; }; /** - * Get the directory name from import.meta.url. + * Get the directory name where this file is located from `import.meta.url`, + * equivalent to the `__dirname` global variable in CommonJS. * @param {String} importMetaUrl import.meta.url */ export const getDirnameFromImportMeta = (importMetaUrl) =>