1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2024-12-16 01:10:30 +02:00

Handle cleanly interruption signals on add-icon-data script (#10620)

This commit is contained in:
Álvaro Mondéjar 2024-03-13 16:31:45 +01:00 committed by GitHub
parent 8aab5ab4e5
commit 1401fb2a05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 102 additions and 85 deletions

View File

@ -84,7 +84,7 @@
"url": "https://opencollective.com/simple-icons"
},
"devDependencies": {
"@inquirer/prompts": "3.2.0",
"@inquirer/prompts": "4.3.0",
"chalk": "5.3.0",
"del-cli": "5.1.0",
"editorconfig-checker": "5.1.1",

View File

@ -1,6 +1,6 @@
import process from 'node:process';
import chalk from 'chalk';
import { input, confirm, checkbox } from '@inquirer/prompts';
import { input, confirm, checkbox, ExitPromptError } from '@inquirer/prompts';
import autocomplete from 'inquirer-autocomplete-standalone';
import getRelativeLuminance from 'get-relative-luminance';
import { search } from 'fast-fuzzy';
@ -88,99 +88,116 @@ const getIconDataFromAnswers = (answers) => ({
: {}),
});
const answers = {};
const run = async () => {
const answers = {};
answers.title = await input({
message: 'Title:',
validate: titleValidator,
});
answers.title = await input({
message: 'Title:',
validate: titleValidator,
});
answers.hex = await input({
message: 'Hex:',
validate: hexValidator,
transformer: hexTransformer,
});
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:',
answers.source = await input({
message: 'Source 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.hasGuidelines = await confirm({
message: 'The icon has brand guidelines?',
});
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,
if (answers.hasGuidelines) {
answers.guidelines = await input({
message: 'Guidelines URL:',
validate: sourceValidator,
});
}
}
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'),
});
answers.hasLicense = await confirm({
message: 'The icon has brand license?',
});
const icon = getIconDataFromAnswers(answers);
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;
},
});
if (answers.confirmToAdd) {
iconsData.icons.push(icon);
iconsData.icons.sort((a, b) => collator.compare(a.title, b.title));
await writeIconsData(iconsData);
console.log(chalk.green('\nData written successfully.'));
} else {
console.log(chalk.red('\nAborted.'));
process.exit(1);
}
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));
await writeIconsData(iconsData);
console.log(chalk.green('\nData written successfully.'));
} else {
console.log(chalk.red('\nAborted.'));
process.exit(1);
}
};
const main = async () => {
try {
await run();
} catch (err) {
if (err instanceof ExitPromptError) {
console.log(chalk.red('\nAborted.'));
process.exit(1);
}
throw err;
}
};
await main();