From a9a226c58adabea85666dd5074356aeac53150fb Mon Sep 17 00:00:00 2001 From: LitoMore Date: Wed, 3 May 2023 10:16:53 +0800 Subject: [PATCH] Migrate `inquirer` to `@inquirer/prompts` (#8592) --- package.json | 2 +- scripts/add-icon-data.js | 151 ++++++++++++++++++--------------------- 2 files changed, 69 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index d5804911..a830c5d4 100644 --- a/package.json +++ b/package.json @@ -60,13 +60,13 @@ "url": "https://opencollective.com/simple-icons" }, "devDependencies": { + "@inquirer/prompts": "^1.0.0", "chalk": "5.2.0", "editorconfig-checker": "5.0.1", "esbuild": "0.17.18", "fake-diff": "1.0.0", "get-relative-luminance": "1.0.0", "husky": "8.0.3", - "inquirer": "9.2.0", "is-ci": "3.0.1", "jsonschema": "1.4.1", "mocha": "10.2.0", diff --git a/scripts/add-icon-data.js b/scripts/add-icon-data.js index e321e29b..93852dc1 100644 --- a/scripts/add-icon-data.js +++ b/scripts/add-icon-data.js @@ -1,5 +1,5 @@ -import inquirer from 'inquirer'; import chalk from 'chalk'; +import { input, confirm, checkbox } from '@inquirer/prompts'; import getRelativeLuminance from 'get-relative-luminance'; import { URL_REGEX, @@ -83,96 +83,81 @@ const getIconDataFromAnswers = (answers) => ({ : {}), }); -const dataPrompt = [ - { - type: 'input', - name: 'title', - message: 'Title', - validate: titleValidator, - }, - { - type: 'input', - name: 'hex', - message: 'Hex', - validate: hexValidator, - filter: (text) => normalizeColor(text), - transformer: hexTransformer, - }, - { - type: 'input', - name: 'source', - message: 'Source', - validate: sourceValidator, - }, - { - type: 'confirm', - name: 'hasGuidelines', - message: 'The icon has brand guidelines?', - }, - { - type: 'input', - name: 'guidelines', +const answers = {}; + +answers.title = await input({ + message: 'Title', + validate: titleValidator, +}); + +answers.hex = await input({ + message: 'Hex', + validate: hexValidator, + filter: (text) => normalizeColor(text), + transformer: hexTransformer, +}); + +answers.source = await input({ + message: 'Source', + validate: sourceValidator, +}); + +answers.hasGuidelines = await confirm({ + message: 'The icon has brand guidelines?', +}); + +if (answers.hasGuidelines) { + answers.guidelines = await input({ message: 'Guidelines', validate: sourceValidator, - when: ({ hasGuidelines }) => hasGuidelines, - }, - { - type: 'confirm', - name: 'hasLicense', - message: 'The icon has brand license?', - }, - { - type: 'input', - name: 'licenseType', + }); +} + +answers.hasLicense = await confirm({ + message: 'The icon has brand license?', +}); + +if (answers.hasLicense) { + answers.licenseType = await input({ message: 'License type', validate: (text) => Boolean(text), - when: ({ hasLicense }) => hasLicense, - }, - { - type: 'input', - name: 'licenseUrl', - message: 'License URL', - suffix: ' (optional)', + }); + + answers.licenseUrl = await input({ + message: 'License URL' + chalk.reset(' (optional)'), validate: (text) => !Boolean(text) || sourceValidator(text), - when: ({ hasLicense }) => hasLicense, - }, - { - type: 'confirm', - name: 'hasAliases', - message: 'The icon has brand aliases?', - default: false, - }, - { - type: 'checkbox', - name: 'aliasesTypes', + }); +} + +answers.hasAliases = await confirm({ + message: 'This icon has brands aliases?', + default: false, +}); + +if (answers.hasAliases) { + answers.aliasesTypes = await checkbox({ message: 'What types of aliases do you want to add?', choices: aliasesChoices, - when: ({ hasAliases }) => hasAliases, - }, - ...aliasesChoices.map((x) => ({ - type: 'input', - name: `${x.value}AliasesList`, - message: x.value, - suffix: ' (separate with commas)', - validate: (text) => Boolean(text), - transformer: aliasesTransformer, - when: (answers) => answers?.aliasesTypes?.includes(x.value), - })), - { - type: 'confirm', - name: 'confirmToAdd', - message: (answers) => { - const icon = getIconDataFromAnswers(answers); - return [ - 'About to write to simple-icons.json', - chalk.reset(JSON.stringify(icon, null, 4)), - chalk.reset('Is this OK?'), - ].join('\n\n'); - }, - }, -]; + }); + + 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) => Boolean(text), + transformer: aliasesTransformer, + }); + } +} + +answers.confirmToAdd = await confirm({ + message: [ + 'About to write to simple-icons.json', + chalk.reset(JSON.stringify(getIconDataFromAnswers(answers), null, 4)), + chalk.reset('Is this OK?'), + ].join('\n\n'), +}); -const answers = await inquirer.prompt(dataPrompt); const icon = getIconDataFromAnswers(answers); if (answers.confirmToAdd) {