1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2025-11-23 21:34:49 +02:00

Add types to source code (#10637)

This commit is contained in:
Álvaro Mondéjar Rubio
2024-06-06 14:40:35 +02:00
committed by GitHub
parent 1224e341d7
commit 236f5fc715
25 changed files with 619 additions and 205 deletions

View File

@@ -1,4 +1,12 @@
#!/usr/bin/env node
/**
* @file
* Script to add data for a new icon to the simple-icons dataset.
*/
/**
* @typedef {import("../sdk.js").IconData} IconData
*/
import process from 'node:process';
import {ExitPromptError} from '@inquirer/core';
import {checkbox, confirm, input} from '@inquirer/prompts';
@@ -15,6 +23,7 @@ import {
} from '../sdk.mjs';
import {getJsonSchemaData, writeIconsData} from './utils.js';
/** @type {{icons: import('../sdk.js').IconData[]}} */
const iconsData = JSON.parse(await getIconsDataString());
const jsonSchema = await getJsonSchemaData();
@@ -25,25 +34,42 @@ const aliasTypes = ['aka', 'old'].map((key) => ({
value: key,
}));
/** @type {{name: string, value: string}[]} */
const licenseTypes =
jsonSchema.definitions.brand.properties.license.oneOf[0].properties.type.enum.map(
(license) => ({name: license, value: license}),
(/** @type {string} */ license) => ({name: license, value: license}),
);
/**
* @param {string} input URL input
* @returns {Promise<boolean|string>} Whether the input is a valid URL
*/
const isValidURL = async (input) => {
const regex = await urlRegex();
return regex.test(input) || 'Must be a valid and secure (https://) URL.';
};
/**
* @param {string} input Hex color
* @returns {boolean|string} Whether the input is a valid hex color
*/
const isValidHexColor = (input) =>
HEX_REGEX.test(input) || 'Must be a valid hex code.';
/**
* @param {string} input New icon input
* @returns {boolean} Whether the icon is new
*/
const isNewIcon = (input) =>
!iconsData.icons.some(
(icon) =>
icon.title === input || titleToSlug(icon.title) === titleToSlug(input),
) || 'This icon title or slug already exists.';
);
/**
* @param {string} input Color input
* @returns {string} Preview of the color
*/
const previewHexColor = (input) => {
const color = normalizeColor(input);
const luminance = HEX_REGEX.test(input)
@@ -60,7 +86,9 @@ try {
title: await input({
message: 'What is the title of this icon?',
validate: (input) =>
input.trim().length > 0 ? isNewIcon(input) : 'This field is required.',
input.trim().length > 0
? isNewIcon(input) || 'This icon title or slug already exists.'
: 'This field is required.',
}),
hex: normalizeColor(
await input({
@@ -111,6 +139,7 @@ try {
}).then(async (aliases) => {
const result = {};
for (const alias of aliases) {
// @ts-ignore
// eslint-disable-next-line no-await-in-loop
result[alias] = await input({
message: `What ${alias} aliases would you like to add? (separate with commas)`,