Skip to content

Commit

Permalink
🎓 Extend the Enmap class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jul 7, 2024
1 parent 1e0e37e commit e011c9f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ if (!(await isGlobal())) {
}

await Page.register()
const cachePage = Page.all.find((pagee) => pagee.interaction.name === page.get())
const cachePage = Page.all.find((pagee) => pagee.interaction.name === page.getData())
Page.execute(cachePage?.interaction.name ?? 'zones')
35 changes: 9 additions & 26 deletions src/class/cache.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
import Enmap from 'enmap'
import { Path } from 'glob'

export default class Cache<Result>{
static all: Array<Enmap> = []
private cache!: Enmap<string, string>
public name: string
export default class Cache<Result> extends Enmap<string, string> {
static all: Enmap<string, string>[] = []

constructor(key: string) {
this.name = key
this.loader(key)
super({ name: key })
Cache.all.push(this)
}

private find (key: string) {
return Cache.all.find((cache) => cache.name === key)
}
private create (key: string) {
const newCache = new Enmap({ name: key })
Cache.all = [...Cache.all, newCache]
return newCache
}
private loader(key: string) {
let cache = this.find(key)
if (cache === undefined) cache = this.create(key)
this.cache = cache as Enmap<string, string>
return this
}

get (): Result {
return JSON.parse(String(this.cache.get('data')))
getData (): Result {
return JSON.parse(this.get('data') ?? '{}')
}
save(data: Result) {
this.cache.set('data', JSON.stringify(data))
super.set('data', JSON.stringify(data))
return this
}
exist() { return this.cache.has('data') }
clear() { return this.cache.clear() }
exist() { return this.has('data') }
}
2 changes: 1 addition & 1 deletion src/class/questions.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Questions {
new inquirer.Separator(),
...footerBar,
],
message: !['zones', null].includes(page.get()) ? `[${zone.get()?.name}] - ${this.message}` : this.message
message: !['zones', null].includes(page.getData()) ? `[${zone.getData()?.name}] - ${this.message}` : this.message
})
return result.value as string
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dns/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ new Page({
requirements: [zone],
async run(options) {
const [zone] = options.interaction.requirements
const { id } = zone.get()
const { id } = zone.getData()
const type = await new Questions({ message: '🎯 Escolha o tipo de Record' }).autoComplete<string>({
pageName: options.interaction.name,
choices: Object.values(RecordsType).map((type) => ({ value: type, name: type }))
Expand Down
8 changes: 4 additions & 4 deletions src/pages/dns/delete.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ new Page({
name: 'dns-delete',
previous: 'dns',
type: PageTypes.SubCommand,
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.get().id })).result)],
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.getData().id })).result)],
requirements: [records],
async run(options) {
/**
* Para deixar o console visivelmente mais bonito
* Isso deixará o expaçamento uniforme
*/
const maxTypeLength = Math.max(...records.get().map((record) => record.type.length))
const maxNameLength = Math.max(...records.get().map((record) => record.name.length))
const maxTypeLength = Math.max(...records.getData().map((record) => record.type.length))
const maxNameLength = Math.max(...records.getData().map((record) => record.name.length))

/**
* Colocar em ordem crescente
*/
const sortedRecord = records.get().sort((r1, r2) => r1.type.length - r2.type.length)
const sortedRecord = records.getData().sort((r1, r2) => r1.type.length - r2.type.length)

const selectsRecord = await new Questions({ message: 'Selecione os Records para serem deletadas' }).select({
pageName: options.interaction.name,
Expand Down
8 changes: 4 additions & 4 deletions src/pages/dns/edit.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ new Page({
name: 'dns-edit',
previous: 'dns',
type: PageTypes.SubCommand,
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.get().id })).result)],
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.getData().id })).result)],
requirements: [records],
async run(options) {
/**
* Para deixar o console visivelmente mais bonito
* Isso deixará o expaçamento uniforme
*/
const maxTypeLength = Math.max(...records.get().map((record) => record.type.length))
const maxNameLength = Math.max(...records.get().map((record) => record.name.length))
const maxTypeLength = Math.max(...records.getData().map((record) => record.type.length))
const maxNameLength = Math.max(...records.getData().map((record) => record.name.length))

/**
* Colocar em ordem crescente
*/
const sortedRecord = records.get().sort((r1, r2) => r1.type.length - r2.type.length)
const sortedRecord = records.getData().sort((r1, r2) => r1.type.length - r2.type.length)

const selectsRecord = await new Questions({ message: 'Selecione os Records para serem editadas' }).select({
pageName: options.interaction.name,
Expand Down
8 changes: 4 additions & 4 deletions src/pages/dns/search.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ new Page({
name: 'dns-search',
previous: 'dns',
type: PageTypes.SubCommand,
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.get().id })).result)],
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.getData().id })).result)],
requirements: [records],
async run(options) {
/**
* Para deixar o console visivelmente mais bonito
* Isso deixará o expaçamento uniforme
*/
const maxTypeLength = Math.max(...records.get().map((record) => record.type.length))
const maxNameLength = Math.max(...records.get().map((record) => record.name.length))
const maxTypeLength = Math.max(...records.getData().map((record) => record.type.length))
const maxNameLength = Math.max(...records.getData().map((record) => record.name.length))

/**
* Colocar em ordem crescente
*/
const sortedRecord = records.get().sort((r1, r2) => r1.type.length - r2.type.length)
const sortedRecord = records.getData().sort((r1, r2) => r1.type.length - r2.type.length)

const selectRecord = await new Questions({ message: 'Selecione um Record para editar' }).select({
pageName: options.interaction.name,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/zones.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default new Page ({
const [zones] = options.interaction.requirements
const response = await new Questions({ message: '🚧 Selecione a zona que deseja modificar' }).select({
pageName: options.interaction.name,
choices: zones.get().map((zone)=> ({
choices: zones.getData().map((zone)=> ({
name: zone.name,
value: `${zone.name}_${zone.id}`
} satisfies ListChoiceOptions))
Expand Down

0 comments on commit e011c9f

Please sign in to comment.