2025-03-04 03:12:53 +01:00
|
|
|
// @ts-check
|
2024-06-06 14:40:35 +02:00
|
|
|
/**
|
|
|
|
* @file Internal utilities.
|
|
|
|
*
|
|
|
|
* Here resides all the functionality that does not qualifies to reside
|
|
|
|
* in the SDK because is not publicly exposed.
|
|
|
|
*/
|
|
|
|
|
2022-09-25 09:04:58 +08:00
|
|
|
import fs from 'node:fs/promises';
|
2024-03-25 01:38:18 +08:00
|
|
|
import path from 'node:path';
|
2024-12-19 18:32:41 +08:00
|
|
|
import {
|
2024-12-21 22:49:25 +08:00
|
|
|
collator,
|
|
|
|
getDirnameFromImportMeta,
|
2025-01-06 12:08:28 +08:00
|
|
|
getIconSlug,
|
2024-12-21 22:49:25 +08:00
|
|
|
getIconsDataPath,
|
2025-03-05 03:21:55 +01:00
|
|
|
titleToSlug,
|
2024-12-19 18:32:41 +08:00
|
|
|
} from '../sdk.mjs';
|
2022-09-28 10:11:27 +08:00
|
|
|
|
|
|
|
const __dirname = getDirnameFromImportMeta(import.meta.url);
|
|
|
|
|
2024-06-06 14:40:35 +02:00
|
|
|
/**
|
|
|
|
* @typedef {import("../sdk.js").IconData} IconData
|
2025-03-05 03:21:55 +01:00
|
|
|
* @typedef {import("../sdk.js").DuplicateAlias} DuplicateAlias
|
2024-06-06 14:40:35 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-28 10:11:27 +08:00
|
|
|
/**
|
|
|
|
* Get JSON schema data.
|
2025-03-04 14:49:47 +01:00
|
|
|
* @param {string} [rootDirectory] Path to the root directory of the project.
|
2024-06-06 14:40:35 +02:00
|
|
|
* @returns {Promise<any>} JSON schema data.
|
2022-09-28 10:11:27 +08:00
|
|
|
*/
|
|
|
|
export const getJsonSchemaData = async (
|
2024-12-21 22:49:25 +08:00
|
|
|
rootDirectory = path.resolve(__dirname, '..'),
|
2024-12-17 14:50:29 +01:00
|
|
|
) =>
|
2024-12-21 22:49:25 +08:00
|
|
|
JSON.parse(
|
|
|
|
await fs.readFile(path.resolve(rootDirectory, '.jsonschema.json'), 'utf8'),
|
|
|
|
);
|
2022-09-28 10:11:27 +08:00
|
|
|
|
2022-09-25 09:04:58 +08:00
|
|
|
/**
|
|
|
|
* Write icons data to _data/simple-icons.json.
|
2024-12-21 23:43:30 +01:00
|
|
|
* @param {IconData[]} iconsData Icons data array.
|
2025-03-04 03:12:53 +01:00
|
|
|
* @param {boolean} [minify] Whether to minify the JSON output.
|
|
|
|
* @param {string} [rootDirectory] Path to the root directory of the project.
|
2022-09-25 09:04:58 +08:00
|
|
|
*/
|
2023-04-19 15:23:13 +02:00
|
|
|
export const writeIconsData = async (
|
2024-12-21 22:49:25 +08:00
|
|
|
iconsData,
|
2025-03-04 03:12:53 +01:00
|
|
|
minify = false,
|
2024-12-21 22:49:25 +08:00
|
|
|
rootDirectory = path.resolve(__dirname, '..'),
|
2023-04-19 15:23:13 +02:00
|
|
|
) => {
|
2024-12-21 22:49:25 +08:00
|
|
|
await fs.writeFile(
|
|
|
|
getIconsDataPath(rootDirectory),
|
|
|
|
`${JSON.stringify(iconsData, null, minify ? 0 : '\t')}\n`,
|
|
|
|
'utf8',
|
|
|
|
);
|
2022-09-25 09:04:58 +08:00
|
|
|
};
|
2024-10-17 00:11:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get SPDX license IDs from `spdx-license-ids` package.
|
2025-03-04 14:49:47 +01:00
|
|
|
* @param {string} [rootDirectory] Path to the root directory of the project.
|
2024-10-17 00:11:55 +08:00
|
|
|
* @returns {Promise<string[]>} Set of SPDX license IDs.
|
|
|
|
*/
|
|
|
|
export const getSpdxLicenseIds = async (
|
2024-12-21 22:49:25 +08:00
|
|
|
rootDirectory = path.resolve(__dirname, '..'),
|
2024-12-17 14:50:29 +01:00
|
|
|
) =>
|
2024-12-21 22:49:25 +08:00
|
|
|
JSON.parse(
|
|
|
|
await fs.readFile(
|
|
|
|
path.resolve(
|
|
|
|
rootDirectory,
|
|
|
|
'node_modules',
|
|
|
|
'spdx-license-ids',
|
|
|
|
'index.json',
|
|
|
|
),
|
|
|
|
'utf8',
|
|
|
|
),
|
|
|
|
);
|
2024-12-19 18:32:41 +08:00
|
|
|
|
|
|
|
/**
|
2025-03-05 03:21:55 +01:00
|
|
|
* The compare function for sorting icons in *_data/simple-icons.json*.
|
2024-12-19 18:32:41 +08:00
|
|
|
* @param {IconData} a Icon A.
|
|
|
|
* @param {IconData} b Icon B.
|
|
|
|
* @returns {number} Comparison result.
|
|
|
|
*/
|
|
|
|
export const sortIconsCompare = (a, b) => {
|
2024-12-21 22:49:25 +08:00
|
|
|
return a.title === b.title
|
2025-01-06 12:08:28 +08:00
|
|
|
? collator.compare(getIconSlug(a), getIconSlug(b))
|
2024-12-21 22:49:25 +08:00
|
|
|
: collator.compare(a.title, b.title);
|
2024-12-19 18:32:41 +08:00
|
|
|
};
|
2025-02-23 01:22:52 +08:00
|
|
|
|
|
|
|
/**
|
2025-03-05 03:21:55 +01:00
|
|
|
* The compare function for sorting icon duplicate aliases in *_data/simple-icons.json*.
|
|
|
|
* @param {DuplicateAlias} a Duplicate alias A.
|
|
|
|
* @param {DuplicateAlias} b Duplicate alias B.
|
|
|
|
* @returns {number} Comparison result.
|
|
|
|
*/
|
|
|
|
const sortDuplicatesCompare = (a, b) => {
|
|
|
|
return a.title === b.title
|
|
|
|
? collator.compare(titleToSlug(a.title), titleToSlug(b.title))
|
|
|
|
: collator.compare(a.title, b.title);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort icon data or duplicate alias object.
|
|
|
|
* @template {IconData | DuplicateAlias} T Either icon data or duplicate alias.
|
|
|
|
* @param {T} icon The icon data or duplicate alias as it appears in *_data/simple-icons.json*.
|
|
|
|
* @returns {T} The sorted icon data or duplicate alias.
|
2025-02-23 01:22:52 +08:00
|
|
|
*/
|
2025-03-05 03:21:55 +01:00
|
|
|
const sortIconOrDuplicate = (icon) => {
|
2025-02-23 01:22:52 +08:00
|
|
|
const keyOrder = [
|
|
|
|
'title',
|
|
|
|
'slug',
|
|
|
|
'hex',
|
|
|
|
'source',
|
|
|
|
'guidelines',
|
|
|
|
'license',
|
|
|
|
'aliases',
|
|
|
|
// This is not appears in icon data but it's in the alias object.
|
|
|
|
'loc',
|
|
|
|
];
|
2025-03-04 03:12:53 +01:00
|
|
|
|
2025-03-05 03:21:55 +01:00
|
|
|
/** @type {T} */
|
|
|
|
const sortedIcon = Object.assign(
|
|
|
|
Object.fromEntries(
|
|
|
|
Object.entries(icon).sort(
|
|
|
|
([key1], [key2]) => keyOrder.indexOf(key1) - keyOrder.indexOf(key2),
|
|
|
|
),
|
2025-02-23 01:22:52 +08:00
|
|
|
),
|
|
|
|
);
|
2025-03-05 03:21:55 +01:00
|
|
|
|
2025-02-23 01:22:52 +08:00
|
|
|
return sortedIcon;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort license object.
|
|
|
|
* @param {IconData['license']} license The license object as it appears in *_data/simple-icons.json*.
|
2025-03-05 03:21:55 +01:00
|
|
|
* @returns {IconData['license']} The sorted license object.
|
2025-02-23 01:22:52 +08:00
|
|
|
*/
|
|
|
|
const sortLicense = (license) => {
|
|
|
|
if (!license) return undefined;
|
|
|
|
const keyOrder = ['type', 'url'];
|
2025-03-05 03:21:55 +01:00
|
|
|
|
|
|
|
/** @type {IconData['license']} */
|
|
|
|
const sortedLicense = Object.assign(
|
|
|
|
Object.fromEntries(
|
|
|
|
Object.entries(license).sort(
|
|
|
|
([key1], [key2]) => keyOrder.indexOf(key1) - keyOrder.indexOf(key2),
|
|
|
|
),
|
2025-02-23 01:22:52 +08:00
|
|
|
),
|
|
|
|
);
|
2025-03-05 03:21:55 +01:00
|
|
|
|
2025-02-23 01:22:52 +08:00
|
|
|
return sortedLicense;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort object key alphabetically.
|
|
|
|
* @param {IconData['aliases']} object The aliases object as it appears in *_data/simple-icons.json*.
|
2025-03-05 03:21:55 +01:00
|
|
|
* @returns {{[_: string]: string} | undefined} The sorted aliases object.
|
2025-02-23 01:22:52 +08:00
|
|
|
*/
|
|
|
|
const sortAlphabetically = (object) => {
|
|
|
|
if (!object) return undefined;
|
2025-03-05 03:21:55 +01:00
|
|
|
const sorted = Object.assign(
|
|
|
|
Object.fromEntries(
|
|
|
|
Object.entries(object).sort(([key1], [key2]) => (key1 > key2 ? 1 : -1)),
|
|
|
|
),
|
2025-02-23 01:22:52 +08:00
|
|
|
);
|
|
|
|
return sorted;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort icons data.
|
|
|
|
* @param {IconData[]} iconsData The icons data as it appears in *_data/simple-icons.json*.
|
|
|
|
* @returns {IconData[]} The sorted icons data.
|
|
|
|
*/
|
|
|
|
export const formatIconData = (iconsData) => {
|
2025-03-05 03:21:55 +01:00
|
|
|
const icons = iconsData.map((icon) => {
|
|
|
|
return sortIconOrDuplicate({
|
2025-02-23 01:22:52 +08:00
|
|
|
...icon,
|
|
|
|
license: sortLicense(icon.license),
|
|
|
|
aliases: icon.aliases
|
|
|
|
? sortAlphabetically({
|
|
|
|
aka: icon.aliases.aka?.sort(collator.compare),
|
|
|
|
dup: icon.aliases.dup
|
2025-03-05 03:21:55 +01:00
|
|
|
? icon.aliases.dup.sort(sortDuplicatesCompare).map((d) =>
|
|
|
|
sortIconOrDuplicate({
|
|
|
|
...d,
|
|
|
|
loc: sortAlphabetically(d.loc),
|
|
|
|
}),
|
|
|
|
)
|
2025-02-23 01:22:52 +08:00
|
|
|
: undefined,
|
|
|
|
loc: sortAlphabetically(icon.aliases.loc),
|
|
|
|
old: icon.aliases.old?.sort(collator.compare),
|
|
|
|
})
|
|
|
|
: undefined,
|
|
|
|
});
|
|
|
|
});
|
2025-03-05 03:21:55 +01:00
|
|
|
icons.sort(sortIconsCompare);
|
|
|
|
return icons;
|
2025-02-23 01:22:52 +08:00
|
|
|
};
|