1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2024-11-16 00:59:07 +02:00

expose getIconsData* util functions (#7394)

This commit is contained in:
Álvaro Mondéjar 2022-05-08 06:48:32 -05:00 committed by GitHub
parent 388727251f
commit b2cf3885ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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) =>