diff --git a/package.json b/package.json index debdf4bc..33b9615b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/add-icon-data.js b/scripts/add-icon-data.js index ee5cd0c5..861f4d23 100644 --- a/scripts/add-icon-data.js +++ b/scripts/add-icon-data.js @@ -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();