Skip to content

Commit

Permalink
feat: remove casting in favor of runtime checking
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Oct 16, 2024
1 parent c1f0eef commit f7a4910
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ export const commands = {
LOGOUT: 'logout',
} as const

export type RegionCode = 'eu' | 'us' | 'cn' | 'ca' | 'ap'
export interface ReadonlyArray<T> {
includes: (searchElement: any, fromIndex?: number) => searchElement is T
}
export const regionCodes = ['eu', 'us', 'cn', 'ca', 'ap'] as const
export type RegionCode = typeof regionCodes[number]

export const regions: Record<Uppercase<RegionCode>, RegionCode> = {
EU: 'eu',
Expand Down
16 changes: 10 additions & 6 deletions src/creds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { access, readFile, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { FileSystemError, handleFileSystemError, konsola } from './utils'
import chalk from 'chalk'
import type { RegionCode } from './constants'
import { regionCodes } from './constants'

export interface NetrcMachine {
login: string
password: string
region: RegionCode
region: string
}

export const getNetrcFilePath = () => {
Expand Down Expand Up @@ -36,6 +36,10 @@ const tokenizeNetrcContent = (content: string) => {
.filter(token => token.length > 0)
}

function includes<T extends U, U>(coll: ReadonlyArray<T>, el: U): el is T {
return coll.includes(el as T)
}

const parseNetrcTokens = (tokens: string[]) => {
const machines: Record<string, NetrcMachine> = {}
let i = 0
Expand All @@ -53,12 +57,12 @@ const parseNetrcTokens = (tokens: string[]) => {
&& tokens[i] !== 'machine'
&& tokens[i] !== 'default'
) {
const key = tokens[i] as keyof NetrcMachine
const key = tokens[i]
const value = tokens[++i]
if (key === 'region') {
machineData[key] = value as RegionCode
if (key === 'region' && includes(regionCodes, value)) {
machineData[key] = value
}
else {
else if (key === 'login' || key === 'password') {
machineData[key] = value
}
i++
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { formatHeader, handleError, konsola } from './utils'
import { getProgram } from './program'
import './commands/login'
import './commands/logout'
import { loginWithToken } from './commands/login/actions'

dotenv.config() // This will load variables from .env into process.env
const program = getProgram()
Expand All @@ -26,14 +27,14 @@ program.on('command:*', () => {

program.command('test').action(async () => {
konsola.title(`Test`, '#8556D3', 'Attempting a test...')
konsola.error('This is an error message')
/* try {

try {
// await loginWithEmailAndPassword('aw', 'passwrod', 'eu')
await loginWithToken('WYSYDHYASDHSYD', 'eu')
}
catch (error) {
handleError(error as Error)
} */
}
})

/* console.log(`
Expand Down

0 comments on commit f7a4910

Please sign in to comment.