From dcb07310d9f1db14a5dd33491346e2b197927377 Mon Sep 17 00:00:00 2001 From: uncenter <47499684+uncenter@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:59:32 -0500 Subject: [PATCH] Allow to select between licenses in `add-icon-data` script (#10564) * Improve add-icon-data script * Handle force closed prompts, add success message * Remove Boolean casting * Remove try/catch --- package.json | 1 + scripts/add-icon-data.js | 37 ++++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 17e1123d..32495617 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "fake-diff": "1.0.0", "get-relative-luminance": "1.0.0", "husky": "9.0.10", + "inquirer-autocomplete-standalone": "0.8.1", "is-ci": "3.0.1", "jsonschema": "1.4.1", "markdown-link-check": "3.11.2", diff --git a/scripts/add-icon-data.js b/scripts/add-icon-data.js index 7620b854..8e1385a2 100644 --- a/scripts/add-icon-data.js +++ b/scripts/add-icon-data.js @@ -1,6 +1,7 @@ import process from 'node:process'; import chalk from 'chalk'; import { input, confirm, checkbox } from '@inquirer/prompts'; +import autocomplete from 'inquirer-autocomplete-standalone'; import getRelativeLuminance from 'get-relative-luminance'; import { URL_REGEX, @@ -89,18 +90,18 @@ const getIconDataFromAnswers = (answers) => ({ const answers = {}; answers.title = await input({ - message: 'Title', + message: 'Title:', validate: titleValidator, }); answers.hex = await input({ - message: 'Hex', + message: 'Hex:', validate: hexValidator, transformer: hexTransformer, }); answers.source = await input({ - message: 'Source', + message: 'Source URL:', validate: sourceValidator, }); @@ -110,7 +111,7 @@ answers.hasGuidelines = await confirm({ if (answers.hasGuidelines) { answers.guidelines = await input({ - message: 'Guidelines', + message: 'Guidelines URL:', validate: sourceValidator, }); } @@ -120,19 +121,32 @@ answers.hasLicense = await confirm({ }); if (answers.hasLicense) { - answers.licenseType = await input({ - message: 'License type', - validate: (text) => Boolean(text), + 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 + ? licenseTypes.filter((license) => + license.value.toLowerCase().includes(input.toLowerCase()), + ) + : licenseTypes; + }, }); answers.licenseUrl = await input({ - message: 'License URL' + chalk.reset(' (optional)'), + message: `License URL ${chalk.reset('(optional)')}:`, validate: (text) => !Boolean(text) || sourceValidator(text), }); } answers.hasAliases = await confirm({ - message: 'This icon has brands aliases?', + message: 'This icon has brand aliases?', default: false, }); @@ -154,7 +168,7 @@ if (answers.hasAliases) { answers.confirmToAdd = await confirm({ message: [ - 'About to write to simple-icons.json', + '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'), @@ -166,7 +180,8 @@ 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('Aborted.'); + console.log(chalk.red('\nAborted.')); process.exit(1); }