diff --git a/apps/cli/src/commands/base.command.ts b/apps/cli/src/commands/base.command.ts index 1b572727..081490f6 100644 --- a/apps/cli/src/commands/base.command.ts +++ b/apps/cli/src/commands/base.command.ts @@ -6,6 +6,7 @@ import { } from '../types/command/command.types' import { fetchProfileConfig } from '../util/configuration' import Logger from '../util/logger' +import { getDefaultProfile } from '../util/profile' /** * The base class for all commands. All commands should extend this class. @@ -143,7 +144,7 @@ export default abstract class BaseCommand { this.baseUrl = globalOptions.baseUrl } } else { - const defaultProfileName = profiles.default + const defaultProfileName = getDefaultProfile(profiles) const defaultProfile = profiles[defaultProfileName] if (defaultProfile) { diff --git a/apps/cli/src/commands/profile/create.profile.ts b/apps/cli/src/commands/profile/create.profile.ts index 8b07a33f..94b722c0 100644 --- a/apps/cli/src/commands/profile/create.profile.ts +++ b/apps/cli/src/commands/profile/create.profile.ts @@ -50,11 +50,32 @@ export default class CreateProfile extends BaseCommand { } async action({ options }: CommandActionData): Promise { + intro('Creating a new profile') + + const { name, apiKey, baseUrl, setDefault } = await this.parseInput(options) + + this.profiles = await fetchProfileConfig() + await this.checkOverwriteExistingProfile(name) + + const s = spinner() + s.start('Saving changes...') + + this.setProfileConfigData(name, apiKey, baseUrl, setDefault) + await writeProfileConfig(this.profiles) + + s.stop() + outro(`Profile ${name} created successfully`) + } + + private async parseInput(options: CommandActionData['options']): Promise<{ + name: string + apiKey: string + baseUrl: string + setDefault: boolean + }> { let { name, apiKey } = options const { baseUrl, setDefault } = options - intro('Creating a new profile') - if (!name) { name = await text({ message: 'Enter the name of the profile', @@ -69,19 +90,7 @@ export default class CreateProfile extends BaseCommand { }) } - this.profiles = await fetchProfileConfig() - await this.checkOverwriteExistingProfile(name) - - const s = spinner() - s.start('Saving changes...') - - this.setProfileData(name, apiKey, baseUrl, setDefault) - - await writeProfileConfig(this.profiles) - - s.stop() - - outro(`Profile ${name} created successfully`) + return { name, apiKey, baseUrl, setDefault } } private async checkOverwriteExistingProfile(name: string): Promise { @@ -97,7 +106,7 @@ export default class CreateProfile extends BaseCommand { } } - private setProfileData( + private setProfileConfigData( name: string, apiKey: string, baseUrl: string, diff --git a/apps/cli/src/commands/profile/delete.profile.ts b/apps/cli/src/commands/profile/delete.profile.ts index 5f075c28..24aa8c3a 100644 --- a/apps/cli/src/commands/profile/delete.profile.ts +++ b/apps/cli/src/commands/profile/delete.profile.ts @@ -9,6 +9,7 @@ import { writeProfileConfig } from '../../util/configuration' import { ProfileConfig } from 'src/types/index.types' +import { checkIsDefaultProfile, checkProfileExists } from 'src/util/profile' export default class DeleteProfile extends BaseCommand { profiles: ProfileConfig @@ -32,13 +33,20 @@ export default class DeleteProfile extends BaseCommand { s.start('Deleting the profile') this.profiles = await fetchProfileConfig() + checkProfileExists(this.profiles, profile, s) + await this.makeConfirmation(profile, s) - if (this.profileNotFound(profile)) { - s.stop(`Profile ${profile} not found`) - return + this.profiles[profile] = undefined + if (checkIsDefaultProfile(this.profiles, profile)) { + delete this.profiles.default } + await writeProfileConfig(this.profiles) + + s.stop(`Profile ${profile} deleted`) + } - if (this.isDefaultProfile(profile)) { + private async makeConfirmation(profile: string, s: any) { + if (checkIsDefaultProfile(this.profiles, profile)) { const choice = await confirm({ message: 'Are you sure you want to delete the default profile?' }) @@ -48,23 +56,5 @@ export default class DeleteProfile extends BaseCommand { return } } - - this.profiles[profile] = undefined - - if (this.isDefaultProfile(profile)) { - delete this.profiles.default - } - - await writeProfileConfig(this.profiles) - - s.stop(`Profile ${profile} deleted`) - } - - private profileNotFound(profile: string): boolean { - return !this.profiles[profile] - } - - private isDefaultProfile(profile: string): boolean { - return this.profiles.default === profile } } diff --git a/apps/cli/src/commands/profile/list.profile.ts b/apps/cli/src/commands/profile/list.profile.ts index e6c1e160..25cb6ecc 100644 --- a/apps/cli/src/commands/profile/list.profile.ts +++ b/apps/cli/src/commands/profile/list.profile.ts @@ -5,8 +5,12 @@ import { import BaseCommand from '../base.command' import { fetchProfileConfig } from '../../util/configuration' import Logger from '../../util/logger' +import { ProfileConfig } from '../../types/index.types' +import { getDefaultProfile } from '../../util/profile' export default class ListProfile extends BaseCommand { + private profiles: ProfileConfig + getName(): string { return 'list' } @@ -29,22 +33,30 @@ export default class ListProfile extends BaseCommand { async action({ options }: CommandActionData): Promise { const { verbose } = options - const profiles = await fetchProfileConfig() - const defaultProfile = profiles.default - delete profiles.default + this.profiles = await fetchProfileConfig() + const defaultProfile = getDefaultProfile(this.profiles) + delete this.profiles.default Logger.log('Profiles:') - Object.keys(profiles).forEach((profile) => { - if (defaultProfile === profile) { - Logger.log(`- ${profile} (default)`) - } else { - Logger.log(`- ${profile}`) - } + Object.keys(this.profiles).forEach((profile) => + this.printProfile(profile, defaultProfile, verbose) + ) + } - if (verbose) { - Logger.log(` - API Key: ${profiles[profile].apiKey}`) - Logger.log(` - Base URL: ${profiles[profile].baseUrl}`) - } - }) + private printProfile( + profile: string, + defaultProfile: string, + verbose: boolean + ): void { + if (defaultProfile === profile) { + Logger.log(`- ${profile} (default)`) + } else { + Logger.log(`- ${profile}`) + } + + if (verbose) { + Logger.log(` - API Key: ${this.profiles[profile].apiKey}`) + Logger.log(` - Base URL: ${this.profiles[profile].baseUrl}`) + } } } diff --git a/apps/cli/src/commands/profile/update.profile.ts b/apps/cli/src/commands/profile/update.profile.ts index 2f7e772d..4307eaf1 100644 --- a/apps/cli/src/commands/profile/update.profile.ts +++ b/apps/cli/src/commands/profile/update.profile.ts @@ -10,6 +10,7 @@ import { writeProfileConfig } from '../../util/configuration' import { spinner } from '@clack/prompts' +import { checkProfileExists, checkIsDefaultProfile } from '../../util/profile' export default class UpdateProfile extends BaseCommand { private profiles: ProfileConfig @@ -55,12 +56,20 @@ export default class UpdateProfile extends BaseCommand { const s = spinner() s.start('Updating the profile') - if (!this.profiles[profile]) { - s.stop(`Profile ${profile} not found`) - return - } + checkProfileExists(this.profiles, profile, s) + this.updateProfileData(profile, name, apiKey, baseUrl) + await writeProfileConfig(this.profiles) + + s.stop(`Profile ${profile} updated`) + } - const isDefaultProfile = this.profiles.default === profile + private updateProfileData( + profile: string, + name: string, + apiKey: string, + baseUrl: string + ): void { + const isDefaultProfile = checkIsDefaultProfile(this.profiles, profile) if (apiKey) { this.profiles[profile].apiKey = apiKey @@ -77,9 +86,5 @@ export default class UpdateProfile extends BaseCommand { this.profiles.default = name } } - - await writeProfileConfig(this.profiles) - - s.stop(`Profile ${profile} updated`) } } diff --git a/apps/cli/src/commands/profile/use.profile.ts b/apps/cli/src/commands/profile/use.profile.ts index a4bfa52a..8ab5f2b0 100644 --- a/apps/cli/src/commands/profile/use.profile.ts +++ b/apps/cli/src/commands/profile/use.profile.ts @@ -8,6 +8,7 @@ import { fetchProfileConfig, writeProfileConfig } from '../../util/configuration' +import { checkProfileExists } from '../../util/profile' export default class UseProfile extends BaseCommand { getName(): string { @@ -30,10 +31,7 @@ export default class UseProfile extends BaseCommand { const profiles = await fetchProfileConfig() - if (!profiles[profile]) { - s.stop(`Profile ${profile} not found`) - return - } + checkProfileExists(profiles, profile, s) // Set the active profile profiles.default = profile diff --git a/apps/cli/src/util/profile.ts b/apps/cli/src/util/profile.ts new file mode 100644 index 00000000..8c4b513f --- /dev/null +++ b/apps/cli/src/util/profile.ts @@ -0,0 +1,25 @@ +import { ProfileConfig } from '../types/index.types' + +export const checkProfileExists = ( + profiles: ProfileConfig, + profile: string, + s?: any +): void => { + if (!profiles[profile]) { + if (s) { + s.stop(`Profile ${profile} not found`) + return + } else throw new Error(`Profile ${profile} not found`) + } +} + +export const checkIsDefaultProfile = ( + profiles: ProfileConfig, + profile: string +): boolean => { + return profiles.default === profile +} + +export const getDefaultProfile = (profiles: ProfileConfig): string => { + return profiles.default +}