diff --git a/index.ts b/index.ts index 663f2d895c70986be9f65314aa9743d33d7d2508..ec78eea40714c684519a79f54c2e415e61b32e49 100644 --- a/index.ts +++ b/index.ts @@ -6,7 +6,7 @@ import { TezosToolkit } from '@taquito/taquito' import { InMemorySigner } from '@taquito/signer' // IMPORT UTILS -import { askQuestion } from './src/utils/askQuestion.js' +import { askMultipleQuestion } from './src/utils/askQuestion.js' import { getAsciiArtText } from './src/utils/getAsciiArtText.js' import { getRole } from './src/utils/getRole.js' @@ -36,26 +36,29 @@ program.command('main') // DISPLAY HEADER await getAsciiArtText() console.log('\n') - console.log(chalk.bgBlue(`Vous êtes connecté en tant que ${getRole(tezos)}\n`)) + console.log(chalk.bgBlue(`Vous êtes connecté en tant que ${getRole(tezos)}`)) + console.log('\n') + console.log(chalk.yellow('Que souhaitez-vous faire ?')) + console.log('\n') // START MAIN APPLICATION while (true) { let choice: string switch (getRole(tezos)) { case 'ADMIN_FA_TOKEN': - choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Créer une association\n2: Créer un token\n3: Bruler mes tokens\n4: Voir mon portefeuille\n0: Quitter\n`) + choice = await askMultipleQuestion(['Créer une association', 'Créer un token', 'Bruler mes tokens', 'Voir mon portefeuille', 'Quitter']) await handleAdminFATokenChoice(choice, tezos) break case 'ADMIN_ASSOCIATION': - choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Créer un token\n2: Bruler mes tokens\n3: Voir mon portefeuille\n0: Quitter\n`) + choice = await askMultipleQuestion(['Créer un token', 'Bruler mes tokens', 'Voir mon portefeuille', 'Quitter']) await handleAdminAssociationChoice(choice, tezos) break case 'ADHERENT': - choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Faire une proposition\n2: Créer un token FA\n3: Voir mon portefeuille\n0: Quitter\n`) + choice = await askMultipleQuestion(['Faire une proposition', 'Créer un token FA', 'Voir mon portefeuille', 'Quitter']) await handleAdherentChoice(choice, tezos) break case 'CONNECTED': - choice = await askQuestion(`${chalk.yellow('Que souhaitez-vous faire ?')} \n\n1: Rejoindre une association\n2: Créer un token FA\n3: Voir les associations\n4: Voir mon portefeuille\n0: Quitter\n`) + choice = await askMultipleQuestion(['Rejoindre une association', 'Créer un token FA', 'Voir les associations', 'Voir mon portefeuille', 'Quitter']) await handleConnectedChoice(choice, tezos) break } diff --git a/src/handlers/handlers.ts b/src/handlers/handlers.ts index 3dd42fe80045fd388fedb067df6295e2c33488a2..00a29722234b2c5ee9c5a01dab2b07a214846d1c 100644 --- a/src/handlers/handlers.ts +++ b/src/handlers/handlers.ts @@ -3,7 +3,7 @@ import chalk from 'chalk' import { type TezosToolkit } from '@taquito/taquito' // IMPORT UTILS -import { askQuestion } from '../utils/askQuestion.js' +import { askOneQuestion } from '../utils/askQuestion.js' // IMPORT TYPES import { type Association } from '../types/Association.js' @@ -22,8 +22,8 @@ import { burnToken } from '../services/token.service.js' * @returns {Promise<void>} A promise resolved once the association creation process is complete. */ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { - const name = await askQuestion(chalk.yellow('Nom: ')) - const description = await askQuestion(chalk.yellow('Description: ')) + const name = await askOneQuestion('Nom: ') + const description = await askOneQuestion('Description: ') const association: Association = { name, description @@ -45,7 +45,7 @@ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the token creation process is complete. */ async function handleCreateToken (tezos: TezosToolkit): Promise<void> { - const nbTokenFungible: string = await askQuestion(chalk.yellow('Nombre de token fongible: ')) + const nbTokenFungible: string = await askOneQuestion('Nombre de token fongible: ') try { const op = await createFAToken(nbTokenFungible, tezos) @@ -76,8 +76,8 @@ async function handleGetBalance (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. */ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { - const title = await askQuestion(chalk.yellow('Titre: ')) - const description = await askQuestion(chalk.yellow('Description: ')) + const title = await askOneQuestion('Titre: ') + const description = await askOneQuestion('Description: ') const proposal: Proposal = { title, description @@ -99,7 +99,7 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the joining process is complete. */ async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> { - const name = await askQuestion(chalk.yellow("Nom de l'association: ")) + const name = await askOneQuestion("Nom de l'association: ") try { console.log("Inscription à l'association en cours...") @@ -136,7 +136,7 @@ async function handleGetAssociations (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the joining process is complete. */ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> { - const name = await askQuestion(chalk.yellow("Nom de l'association: ")) + const name = await askOneQuestion("Nom de l'association: ") try { console.log("Récupération des détails de l'association en cours...") @@ -154,7 +154,7 @@ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. */ async function handleBurnToken (tezos: TezosToolkit): Promise<void> { - const nbTokenToBurn = await askQuestion(chalk.yellow('Nombre de token à bruler: ')) + const nbTokenToBurn = await askOneQuestion('Nombre de token à bruler: ') try { console.log('Brulure des tokens en cours...') diff --git a/src/handlers/roleHandlers.ts b/src/handlers/roleHandlers.ts index d81b60a8a29521c6ebcc8e2652b36010de876809..4fe367867439792e02375ee41f79f42fd16c4d55 100644 --- a/src/handlers/roleHandlers.ts +++ b/src/handlers/roleHandlers.ts @@ -1,5 +1,5 @@ import { type TezosToolkit } from '@taquito/taquito' -import { handleBurnToken, handleCreateAssociation, handleCreateProposal, handleCreateToken, handleGetAssociations, handleGetBalance, handleJoinAssociation } from './handlers' +import { handleBurnToken, handleCreateAssociation, handleCreateProposal, handleCreateToken, handleGetAssociations, handleGetBalance, handleJoinAssociation } from './handlers.js' import chalk from 'chalk' /** @@ -10,8 +10,6 @@ import chalk from 'chalk' */ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '0': - break case '1': await handleCreateAssociation(tezos) break @@ -24,6 +22,8 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P case '4': await handleGetBalance(tezos) break + case '5': + break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break @@ -38,8 +38,6 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P */ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '0': - break case '1': await handleCreateToken(tezos) break @@ -49,6 +47,8 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit case '3': await handleGetBalance(tezos) break + case '5': + break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break @@ -63,8 +63,6 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit */ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '0': - break case '1': await handleCreateProposal(tezos) break @@ -74,6 +72,8 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi case '3': await handleGetBalance(tezos) break + case '5': + break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break @@ -88,8 +88,6 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi */ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '0': - break case '1': await handleJoinAssociation(tezos) break @@ -102,6 +100,8 @@ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Prom case '4': await handleGetBalance(tezos) break + case '5': + break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break diff --git a/src/utils/askQuestion.ts b/src/utils/askQuestion.ts index 35f470902480715aa4c37cde7cd2255f977a2c09..1f9227e92a018648a8cceef471103fe4cc8f485f 100644 --- a/src/utils/askQuestion.ts +++ b/src/utils/askQuestion.ts @@ -1,17 +1,36 @@ +import chalk from 'chalk' import { createInterface } from 'readline' -async function askQuestion (question: string): Promise<string> { +async function askMultipleQuestion (options: string[]): Promise<string> { + const readline = createInterface({ + input: process.stdin, + output: process.stdout + }) + + return await new Promise((resolve) => { + options.forEach((option, index) => { + console.log(`${index + 1}: ${option}`) + }) + + readline.question('\nVotre choix: ', (answer: string) => { + readline.close() + resolve(answer.trim()) + }) + }) +} + +async function askOneQuestion (question: string): Promise<string> { const readline = createInterface({ input: process.stdin, output: process.stdout }) return await new Promise(resolve => { - readline.question(question, (answer: string) => { + readline.question(chalk.yellow(question), (answer: string) => { resolve(answer.trim()) readline.close() }) }) } -export { askQuestion } +export { askMultipleQuestion, askOneQuestion } diff --git a/test/handlers/roleHandlers.spec.ts b/test/handlers/roleHandlers.spec.ts index 3ec72c594cdaa490baebf66f74a25b24ffec5e48..a01f14c0d64d89f31cbf0e4ad1407960fb6bfdcd 100644 --- a/test/handlers/roleHandlers.spec.ts +++ b/test/handlers/roleHandlers.spec.ts @@ -22,9 +22,9 @@ describe('roleHandlers', () => { }) describe('handleAdminFATokenChoice', () => { - describe('when choice is "0"', () => { + describe('when choice is "5"', () => { it('should not call any handler', async () => { - await handleAdminFATokenChoice('0', mockedTezosToolkit) + await handleAdminFATokenChoice('5', mockedTezosToolkit) expect(handleCreateAssociation).not.toHaveBeenCalled() expect(handleCreateToken).not.toHaveBeenCalled() @@ -76,9 +76,9 @@ describe('roleHandlers', () => { }) describe('handleAdminAssociationChoice', () => { - describe('when choice is "0"', () => { + describe('when choice is "5"', () => { it('should not call any handler', async () => { - await handleAdminAssociationChoice('0', mockedTezosToolkit) + await handleAdminAssociationChoice('5', mockedTezosToolkit) expect(handleCreateToken).not.toHaveBeenCalled() expect(handleBurnToken).not.toHaveBeenCalled() @@ -121,9 +121,9 @@ describe('roleHandlers', () => { }) describe('handleAdherentChoice', () => { - describe('when choice is "0"', () => { + describe('when choice is "5"', () => { it('should not call any handler', async () => { - await handleAdherentChoice('0', mockedTezosToolkit) + await handleAdherentChoice('5', mockedTezosToolkit) expect(handleCreateProposal).not.toHaveBeenCalled() expect(handleCreateToken).not.toHaveBeenCalled() @@ -166,9 +166,9 @@ describe('roleHandlers', () => { }) describe('handleConnectedChoice', () => { - describe('when choice is "0"', () => { + describe('when choice is "5"', () => { it('should not call any handler', async () => { - await handleConnectedChoice('0', mockedTezosToolkit) + await handleConnectedChoice('5', mockedTezosToolkit) expect(handleJoinAssociation).not.toHaveBeenCalled() expect(handleCreateToken).not.toHaveBeenCalled()