Skip to content

Commit

Permalink
implemented suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Jun 27, 2024
1 parent ba62505 commit 64747bc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 49 deletions.
85 changes: 51 additions & 34 deletions apps/cli/src/commands/configure/configure.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
writeUserRootConfig
} from '../../util/configuration'
import { existsSync } from 'fs'
import Logger from 'src/util/logger'

export default class ConfigureCommand implements BaseCommand {
prepareCommand(program: Command): void {
Expand All @@ -33,12 +34,17 @@ export default class ConfigureCommand implements BaseCommand {
}

private async action(parsedData) {
const { list } = parsedData
try {
const { list } = parsedData

if (list) {
await this.listConfigurations()
} else {
await this.createConfiguration(parsedData)
if (list) {
await this.listConfigurations()
} else {
await this.createConfiguration(parsedData)
}
} catch (error) {
Logger.error(error)
process.exit(1)
}
}

Expand All @@ -50,12 +56,9 @@ export default class ConfigureCommand implements BaseCommand {
}

private async createConfiguration(parsedData) {
const { force, baseUrl } = parsedData
const { baseUrl } = parsedData
let { workspace, project, environment, apiKey, privateKey } = parsedData

let upsertUserRootConfig = true
let upsertProjectRootConfig = true

// If a flag isn't set, prompt the user for the value

intro('Configure the CLI')
Expand Down Expand Up @@ -90,6 +93,44 @@ export default class ConfigureCommand implements BaseCommand {
})
}

const s = spinner()
s.start('Configuring the CLI')

const { upsertProjectRootConfig, upsertUserRootConfig } =
await this.handleExistingConfigurations(parsedData)

if (upsertUserRootConfig) {
Logger.info('Writing user root config')
writeUserRootConfig(project, {
apiKey,
privateKey
})
}

if (upsertProjectRootConfig) {
Logger.info('Writing project root config')
writeProjectRootConfig({
workspace,
project,
environment,
baseUrl: baseUrl || undefined
})
}

s.stop()

outro('Configuration complete!')
}

private async handleExistingConfigurations(parsedData: {
force: boolean
project: string
}) {
const { force, project } = parsedData

let upsertProjectRootConfig = true
let upsertUserRootConfig = true

if (!force) {
const userRootConfigExists = existsSync(
getUserRootConfigurationFilePath(project)
Expand Down Expand Up @@ -118,30 +159,6 @@ export default class ConfigureCommand implements BaseCommand {
}
}

const s = spinner()
s.start('Configuring the CLI')

console.log(upsertProjectRootConfig, upsertUserRootConfig)
if (upsertUserRootConfig) {
console.log('writing user root config')
writeUserRootConfig(project, {
apiKey,
privateKey
})
}

if (upsertProjectRootConfig) {
console.log('writing project root config')
writeProjectRootConfig({
workspace,
project,
environment,
baseUrl: baseUrl || undefined
})
}

s.stop()

outro('Configuration complete!')
return { upsertProjectRootConfig, upsertUserRootConfig }
}
}
24 changes: 17 additions & 7 deletions apps/cli/src/commands/run/run.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,27 @@ export default class RunCommand implements BaseCommand {
}
}

private async getWebsocketType(baseUrl: string) {
if (baseUrl.startsWith('https')) {
return 'wss'
}
return 'ws'
}

private async connectToSocket(data: ProjectRootConfig & UserRootConfig) {
Logger.info('Connecting to socket...')
const host = data.baseUrl.substring(data.baseUrl.lastIndexOf('/') + 1)

const ioClient = io(`ws://${host}/change-notifier`, {
autoConnect: false,
extraHeaders: {
'x-keyshade-token': data.apiKey
},
transports: ['websocket']
})
const ioClient = io(
`${this.getWebsocketType(this.baseUrl)}://${host}/change-notifier`,
{
autoConnect: false,
extraHeaders: {
'x-keyshade-token': data.apiKey
},
transports: ['websocket']
}
)

ioClient.connect()

Expand Down
22 changes: 14 additions & 8 deletions apps/cli/src/util/logger.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import * as chalk from 'chalk'
import * as moment from 'moment'

const Logger = {
info(message: string) {
export default class Logger {
static log(message: string) {
console.log(
`${chalk.blue('[LOG]')} ${chalk.blue(
moment().format('YYYY-MM-DD HH:mm:ss')
)} - ${message}`
)
}

static info(message: string) {
console.info(
`${chalk.green('[INFO]')} ${chalk.green(
moment().format('YYYY-MM-DD HH:mm:ss')
)} - ${message}`
)
},
}

error(message: string) {
static error(message: string) {
console.error(
`${chalk.red('[ERROR]')} ${chalk.red(
moment().format('YYYY-MM-DD HH:mm:ss')
)} - ${message}`
)
},
}

warn(message: string) {
static warn(message: string) {
console.warn(
`${chalk.yellow('[WARN]')} ${chalk.yellow(
moment().format('YYYY-MM-DD HH:mm:ss')
)} - ${message}`
)
}
}

export default Logger

0 comments on commit 64747bc

Please sign in to comment.