mirror of
https://github.com/Mibew/simple-icons.git
synced 2025-05-02 17:16:42 +03:00
Refactor add-icon-data script (#10639)
This commit is contained in:
parent
842d60ca2d
commit
4cd4603432
@ -13,171 +13,122 @@ import {
|
|||||||
} from '../sdk.mjs';
|
} from '../sdk.mjs';
|
||||||
import { getJsonSchemaData, writeIconsData } from './utils.js';
|
import { getJsonSchemaData, writeIconsData } from './utils.js';
|
||||||
|
|
||||||
const hexPattern = /^#?[a-f0-9]{3,8}$/i;
|
|
||||||
|
|
||||||
const iconsData = JSON.parse(await getIconsDataString());
|
const iconsData = JSON.parse(await getIconsDataString());
|
||||||
const jsonSchema = await getJsonSchemaData();
|
const jsonSchema = await getJsonSchemaData();
|
||||||
|
|
||||||
const titleValidator = (text) => {
|
const HEX_REGEX = /^#?[a-f0-9]{3,8}$/i;
|
||||||
if (!text) return 'This field is required';
|
|
||||||
if (
|
|
||||||
iconsData.icons.find(
|
|
||||||
(x) => x.title === text || titleToSlug(x.title) === titleToSlug(text),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return 'This icon title or slug already exists';
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
const hexValidator = (text) =>
|
const aliasTypes = ['aka', 'old'].map((key) => ({
|
||||||
hexPattern.test(text) || 'This should be a valid hex code';
|
name: `${key} (${jsonSchema.definitions.brand.properties.aliases.properties[key].description})`,
|
||||||
|
value: key,
|
||||||
|
}));
|
||||||
|
|
||||||
const sourceValidator = (text) =>
|
const licenseTypes =
|
||||||
URL_REGEX.test(text) || 'This should be a secure URL';
|
jsonSchema.definitions.brand.properties.license.oneOf[0].properties.type.enum.map(
|
||||||
|
(license) => ({ name: license, value: license }),
|
||||||
|
);
|
||||||
|
|
||||||
const hexTransformer = (text) => {
|
const isValidURL = (input) =>
|
||||||
const color = normalizeColor(text);
|
URL_REGEX.test(input) || 'Must be a valid and secure (https://) URL.';
|
||||||
const luminance = hexPattern.test(text)
|
|
||||||
|
const isValidHexColor = (input) =>
|
||||||
|
HEX_REGEX.test(input) || 'Must be a valid hex code.';
|
||||||
|
|
||||||
|
const isNewIcon = (input) =>
|
||||||
|
!iconsData.icons.find(
|
||||||
|
(icon) =>
|
||||||
|
icon.title === input || titleToSlug(icon.title) === titleToSlug(input),
|
||||||
|
) || 'This icon title or slug already exists.';
|
||||||
|
|
||||||
|
const previewHexColor = (input) => {
|
||||||
|
const color = normalizeColor(input);
|
||||||
|
const luminance = HEX_REGEX.test(input)
|
||||||
? getRelativeLuminance.default(`#${color}`)
|
? getRelativeLuminance.default(`#${color}`)
|
||||||
: -1;
|
: -1;
|
||||||
if (luminance === -1) return text.toUpperCase();
|
if (luminance === -1) return input.toUpperCase();
|
||||||
return chalk.bgHex(`#${color}`).hex(luminance < 0.4 ? '#fff' : '#000')(
|
return chalk.bgHex(`#${color}`).hex(luminance < 0.4 ? '#fff' : '#000')(
|
||||||
text.toUpperCase(),
|
input.toUpperCase(),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const aliasesTransformer = (text) =>
|
try {
|
||||||
text
|
const answers = {
|
||||||
.split(',')
|
title: await input({
|
||||||
.map((x) => chalk.cyan(x))
|
message: 'What is the title of this icon?',
|
||||||
.join(',');
|
validate: (input) =>
|
||||||
|
input.trim().length > 0 ? isNewIcon(input) : 'This field is required.',
|
||||||
|
}),
|
||||||
|
hex: normalizeColor(
|
||||||
|
await input({
|
||||||
|
message: 'What is the brand color of this icon?',
|
||||||
|
validate: isValidHexColor,
|
||||||
|
transformer: previewHexColor,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
source: await input({
|
||||||
|
message: 'What is the source URL of the icon?',
|
||||||
|
validate: isValidURL,
|
||||||
|
}),
|
||||||
|
guidelines: (await confirm({
|
||||||
|
message: 'Does this icon have brand guidelines?',
|
||||||
|
}))
|
||||||
|
? await input({
|
||||||
|
message: 'What is the URL for the brand guidelines?',
|
||||||
|
validate: isValidURL,
|
||||||
|
})
|
||||||
|
: undefined,
|
||||||
|
license: (await confirm({
|
||||||
|
message: 'Does this icon have a license?',
|
||||||
|
}))
|
||||||
|
? {
|
||||||
|
type: await autocomplete({
|
||||||
|
message: "What is the icon's license?",
|
||||||
|
source: async (input) => {
|
||||||
|
input = (input || '').trim();
|
||||||
|
return input
|
||||||
|
? search(input, licenseTypes, { keySelector: (x) => x.value })
|
||||||
|
: licenseTypes;
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
url:
|
||||||
|
(await input({
|
||||||
|
message: `What is the URL for the license? (optional)`,
|
||||||
|
validate: (input) => input.length === 0 || isValidURL(input),
|
||||||
|
})) || undefined,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
aliases: (await confirm({
|
||||||
|
message: 'Does this icon have brand aliases?',
|
||||||
|
default: false,
|
||||||
|
}))
|
||||||
|
? await checkbox({
|
||||||
|
message: 'What types of aliases do you want to add?',
|
||||||
|
choices: aliasTypes,
|
||||||
|
}).then(async (aliases) => {
|
||||||
|
const result = {};
|
||||||
|
for (const alias of aliases) {
|
||||||
|
result[alias] = await input({
|
||||||
|
message: `What ${alias} aliases would you like to add? (separate with commas)`,
|
||||||
|
}).then((aliases) =>
|
||||||
|
aliases.split(',').map((alias) => alias.trim()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
})
|
||||||
|
: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
const aliasesChoices = Object.entries(
|
console.log(
|
||||||
jsonSchema.definitions.brand.properties.aliases.properties,
|
'About to write the following to simple-icons.json:\n' +
|
||||||
)
|
JSON.stringify(answers, null, 4),
|
||||||
.filter(([k]) => ['aka', 'old'].includes(k))
|
);
|
||||||
.map(([k, v]) => ({ name: `${k}: ${v.description}`, value: k }));
|
|
||||||
|
|
||||||
const getIconDataFromAnswers = (answers) => ({
|
if (
|
||||||
title: answers.title,
|
await confirm({
|
||||||
hex: normalizeColor(answers.hex),
|
message: 'Is this OK?',
|
||||||
source: answers.source,
|
})
|
||||||
...(answers.hasGuidelines ? { guidelines: answers.guidelines } : {}),
|
) {
|
||||||
...(answers.hasLicense
|
iconsData.icons.push(answers);
|
||||||
? {
|
|
||||||
license: {
|
|
||||||
type: answers.licenseType,
|
|
||||||
...(answers.licenseUrl ? { url: answers.licenseUrl } : {}),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(answers.hasAliases
|
|
||||||
? {
|
|
||||||
aliases: aliasesChoices.reduce((previous, current) => {
|
|
||||||
const promptKey = `${current.value}AliasesList`;
|
|
||||||
if (answers[promptKey])
|
|
||||||
return {
|
|
||||||
...previous,
|
|
||||||
[current.value]: answers[promptKey]
|
|
||||||
.split(',')
|
|
||||||
.map((x) => x.trim()),
|
|
||||||
};
|
|
||||||
return previous;
|
|
||||||
}, {}),
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const run = async () => {
|
|
||||||
const answers = {};
|
|
||||||
|
|
||||||
answers.title = await input({
|
|
||||||
message: 'Title:',
|
|
||||||
validate: titleValidator,
|
|
||||||
});
|
|
||||||
|
|
||||||
answers.hex = await input({
|
|
||||||
message: 'Hex:',
|
|
||||||
validate: hexValidator,
|
|
||||||
transformer: hexTransformer,
|
|
||||||
});
|
|
||||||
|
|
||||||
answers.source = await input({
|
|
||||||
message: 'Source URL:',
|
|
||||||
validate: sourceValidator,
|
|
||||||
});
|
|
||||||
|
|
||||||
answers.hasGuidelines = await confirm({
|
|
||||||
message: 'The icon has brand guidelines?',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (answers.hasGuidelines) {
|
|
||||||
answers.guidelines = await input({
|
|
||||||
message: 'Guidelines URL:',
|
|
||||||
validate: sourceValidator,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
answers.hasLicense = await confirm({
|
|
||||||
message: 'The icon has brand license?',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (answers.hasLicense) {
|
|
||||||
const licenseTypes =
|
|
||||||
jsonSchema.definitions.brand.properties.license.oneOf[0].properties.type.enum.map(
|
|
||||||
(license) => {
|
|
||||||
return { value: license };
|
|
||||||
},
|
|
||||||
);
|
|
||||||
answers.licenseType = await autocomplete({
|
|
||||||
message: 'License type:',
|
|
||||||
source: async (input) => {
|
|
||||||
input = (input || '').trim();
|
|
||||||
return input
|
|
||||||
? search(input, licenseTypes, { keySelector: (x) => x.value })
|
|
||||||
: licenseTypes;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
answers.licenseUrl = await input({
|
|
||||||
message: `License URL ${chalk.reset('(optional)')}:`,
|
|
||||||
validate: (text) => text.length === 0 || sourceValidator(text),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
answers.hasAliases = await confirm({
|
|
||||||
message: 'This icon has brand aliases?',
|
|
||||||
default: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (answers.hasAliases) {
|
|
||||||
answers.aliasesTypes = await checkbox({
|
|
||||||
message: 'What types of aliases do you want to add?',
|
|
||||||
choices: aliasesChoices,
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const x of aliasesChoices) {
|
|
||||||
if (!answers?.aliasesTypes?.includes(x.value)) continue;
|
|
||||||
answers[`${x.value}AliasesList`] = await input({
|
|
||||||
message: x.value + chalk.reset(' (separate with commas)'),
|
|
||||||
validate: (text) => text.trim().length > 0,
|
|
||||||
transformer: aliasesTransformer,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
answers.confirmToAdd = await confirm({
|
|
||||||
message: [
|
|
||||||
'About to write the following to simple-icons.json:',
|
|
||||||
chalk.reset(JSON.stringify(getIconDataFromAnswers(answers), null, 4)),
|
|
||||||
chalk.reset('Is this OK?'),
|
|
||||||
].join('\n\n'),
|
|
||||||
});
|
|
||||||
|
|
||||||
const icon = getIconDataFromAnswers(answers);
|
|
||||||
|
|
||||||
if (answers.confirmToAdd) {
|
|
||||||
iconsData.icons.push(icon);
|
|
||||||
iconsData.icons.sort((a, b) => collator.compare(a.title, b.title));
|
iconsData.icons.sort((a, b) => collator.compare(a.title, b.title));
|
||||||
await writeIconsData(iconsData);
|
await writeIconsData(iconsData);
|
||||||
console.log(chalk.green('\nData written successfully.'));
|
console.log(chalk.green('\nData written successfully.'));
|
||||||
@ -185,19 +136,11 @@ const run = async () => {
|
|||||||
console.log(chalk.red('\nAborted.'));
|
console.log(chalk.red('\nAborted.'));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
};
|
} catch (err) {
|
||||||
|
if (err instanceof ExitPromptError) {
|
||||||
const main = async () => {
|
console.log(chalk.red('\nAborted.'));
|
||||||
try {
|
process.exit(1);
|
||||||
await run();
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof ExitPromptError) {
|
|
||||||
console.log(chalk.red('\nAborted.'));
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
await main();
|
throw err;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user