Skip to content

Commit

Permalink
❌ Remove prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jul 7, 2024
1 parent 351bd60 commit 68ffcd6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 88 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@types/jest": "^29.5.12",
"@types/node": "^20.14.10",
"@types/node-forge": "^1.3.11",
"@types/prompts": "^2.4.9",
"babel-plugin-module-resolver": "^5.0.2",
"eslint": "^9.6.0",
"globals": "^15.8.0",
Expand Down
33 changes: 14 additions & 19 deletions src/class/crypt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Lang } from '@/controller/lang.js'
import { i18, rootPath } from '@/index.js'
import { i18, Lang } from '@/controller/lang.js'
import { rootPath } from '@/index.js'
import { exists } from '@/lib/exists.js'
import { isJson } from '@/lib/validate.js'
import { DataCrypted } from '@/types/crypt.js'
Expand All @@ -12,7 +12,7 @@ import 'dotenv/config'
import { readFile, rm, writeFile } from 'fs/promises'
import forge from 'node-forge'
import { join } from 'path'
import prompts from 'prompts'
import { Questions } from './questions.js'

export const credentials = new Map<string, string | object | boolean | number>()

Expand Down Expand Up @@ -58,26 +58,22 @@ export class Crypt {
}

async create () {
const select = await prompts({
name: 'type',
type: 'select',
message: i18('crypt.question'),
initial: 0,
const response = await new Questions({ message: i18('crypt.question') }).select({
choices: [
{
title: i18('crypt.generate_title'),
description: i18('crypt.generate_description'),
name: i18('crypt.generate_title'),
short: i18('crypt.generate_description'),
value: 'random'
},
{
title: i18('crypt.define_title'),
description: i18('crypt.define_description'),
name: i18('crypt.define_title'),
short: i18('crypt.define_description'),
value: 'defined'
}
]
})

switch (select.type) {
switch (response) {
case 'random': {
const password = randomBytes(256).toString('hex')
await writeFile(join(rootPath, '..', '.env'), `token=${password}`)
Expand All @@ -86,15 +82,14 @@ export class Crypt {
break
}
case 'defined': {
const key = await prompts({
name: 'value',
const key = await new Questions({ message: i18('crypt.your_password') }).select({
type: 'password',
message: i18('crypt.your_password'),
validate: (value: string) => passwordStrength(value).id < 2 ? i18('crypt.weak_password') : true
})
if (key.value === undefined) throw new Error(i18('error.undefined', { element: 'Password' }))
await writeFile(join(rootPath, '..', '.env'), `token=${key.value}`)
credentials.set('token', key.value)

if (key === undefined) throw new Error(i18('error.undefined', { element: 'Password' }))
await writeFile(join(rootPath, '..', '.env'), `token=${key}`)
credentials.set('token', key)
await this.write({})
break
}
Expand Down
87 changes: 54 additions & 33 deletions src/class/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { linkTables } from '@/lib/linkTables.js'
import { PageTypes } from '@/types/page.js'
import 'dotenv/config'
import enquirer from 'enquirer'
import inquirer, { CheckboxQuestion, ListChoiceOptions, ListQuestion } from 'inquirer'
import inquirer, { Answers, CheckboxQuestion, ListChoiceOptions, ListQuestion, PasswordQuestion } from 'inquirer'
import autoComplete from 'inquirer-autocomplete-standalone'
import { Page } from './pages.js'

Expand All @@ -26,41 +26,62 @@ export class Questions {
return result.value
}

async select ({ choices, pageName, type }: { choices: ListChoiceOptions[], type?: ListQuestion['type'] | CheckboxQuestion['type'], pageName: string }): Promise<string> {
if (process.env.isTest) return choices[0].value
async select ({ choices, pageName, type, validate }: {
choices?: ListChoiceOptions[],
type?: ListQuestion['type'] | CheckboxQuestion['type'] | PasswordQuestion['type'],
pageName?: string
validate?(input: any, answers?: Answers | undefined): boolean | string | Promise<boolean> | Promise<string>
}): Promise<string> {
if (process.env.isTest) return choices?.[0].value

const pageSelect = Page.all.find((page) => page.interaction.name === pageName) as Page<PageTypes>
const footerBar: ListChoiceOptions[] = []

if (pageName !== undefined) {
const pageSelect = Page.all.find((page) => page.interaction.name === pageName) as Page<PageTypes>

if (pageSelect.interaction.type !== PageTypes.Option) footerBar.push({
name: '🔄 Recarregar',
value: 'reload'
})
if (pageSelect.interaction.type !== PageTypes.Command) footerBar.push({
name: '↩️ Voltar',
value: 'back'
})
if (pageSelect.interaction.type !== PageTypes.Command) footerBar.push({
name: '📍 Home',
value: 'zones'
})
footerBar.push({
name: '❌ Sair',
value: 'exit',
})

}

let result

if (type === 'password') {
result = await inquirer.prompt({
name: 'value',
type,
validate
})

} else {
result = await inquirer.prompt({
name: 'value',
type: type ?? 'list',
pageSize: 20,
choices: [
new inquirer.Separator(),
...choices ?? [],
new inquirer.Separator(),
...footerBar,
],
message: !['zones', null].includes(page.getData()) ? `[${zone.getData()?.name}] - ${this.message}` : this.message
})
}

if (pageSelect.interaction.type !== PageTypes.Option) footerBar.push({
name: '🔄 Recarregar',
value: 'reload'
})
if (pageSelect.interaction.type !== PageTypes.Command) footerBar.push({
name: '↩️ Voltar',
value: 'back'
})
if (pageSelect.interaction.type !== PageTypes.Command) footerBar.push({
name: '📍 Home',
value: 'zones'
})
footerBar.push({
name: '❌ Sair',
value: 'exit',
})

const result = await inquirer.prompt({
name: 'value',
type: type ?? 'list',
pageSize: 20,
choices: [
new inquirer.Separator(),
...choices,
new inquirer.Separator(),
...footerBar,
],
message: !['zones', null].includes(page.getData()) ? `[${zone.getData()?.name}] - ${this.message}` : this.message
})
return result.value as string
}

Expand Down
27 changes: 14 additions & 13 deletions src/controller/lang.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Crypt } from '@/class/crypt.js'
import { Questions } from '@/class/questions.js'
import { rootPath } from '@/index.js'
import { exists } from '@/lib/exists.js'
import flags from 'country-code-to-flag-emoji'
import { glob } from 'glob'
import i18next from 'i18next'
import i18next, { TFunction } from 'i18next'
import Backend, { FsBackendOptions } from 'i18next-fs-backend'
import { ListChoiceOptions } from 'inquirer'
import { join } from 'path'
import prompts, { Choice } from 'prompts'

export class Lang {
async setLanguage (lang: string, change?: boolean) {
Expand All @@ -31,17 +32,11 @@ export class Lang {
for (const lang of allLangs) {
if (langs.filter((langExist) => langExist === lang).length == 0) langs.push(lang)
}
const choices: Choice[] = langs.map((lang) => ({ title: `${flags(lang)} - ${lang}`, value: lang } satisfies Choice))
const response = await prompts({
name: 'Language',
type: 'select',
choices,
message: 'Which language should I continue with?',
initial: 1
})
if (response.Language === undefined) throw new Error('Please select a language')
const choices: ListChoiceOptions[] = langs.map((lang) => ({ name: `${flags(lang)} - ${lang}`, value: lang } satisfies ListChoiceOptions))
const response = await new Questions({ message: 'Which language should I continue with?' }).select({ choices })
if (response === undefined) throw new Error('Please select a language')

this.setLanguage(response.Language, true)
this.setLanguage(response, true)
}
/**
* Inicializar i18
Expand All @@ -57,4 +52,10 @@ export class Lang {

}
}


/**
* Package language controller
*
* @type {TFunction<'translation'>}
*/
export const i18: TFunction<'translation'> = await new Lang().create()
12 changes: 1 addition & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import Cache from '@/class/cache.js'
import { Record } from 'cloudflare/resources/dns/records.mjs'
import { Zone } from 'cloudflare/resources/zones/zones.mjs'
import { TFunction } from 'i18next'
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import { Lang } from './controller/lang.js'

/**
* The directory name of the current module.
Expand Down Expand Up @@ -39,12 +37,4 @@ export const zone: Cache<{ name: string, id: string }> = new Cache<{ name: strin
*
* @type {Cache<Record[]>}
*/
export const records: Cache<Record[]> = new Cache<Record[]>('records')


/**
* Package language controller
*
* @type {TFunction<'translation'>}
*/
export const i18: TFunction<'translation'> = await new Lang().create()
export const records: Cache<Record[]> = new Cache<Record[]>('records')

0 comments on commit 68ffcd6

Please sign in to comment.