Skip to content

Commit

Permalink
fix: rename leon kill to leon delete
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Mar 20, 2022
1 parent 7ae8515 commit 68cf570
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CreateBirthCommand } from './commands/create/birth.js'
import { CheckCommand } from './commands/check.js'
import { HelpCommand } from './commands/help.js'
import { InfoCommand } from './commands/info.js'
import { KillCommand } from './commands/kill.js'
import { DeleteCommand } from './commands/delete.js'
import { RunCommand } from './commands/run.js'
import { StartCommand } from './commands/start.js'
import { UpdateCommand } from './commands/update.js'
Expand All @@ -20,9 +20,9 @@ cli.register(Builtins.HelpCommand)
cli.register(Builtins.VersionCommand)
cli.register(CreateBirthCommand)
cli.register(CheckCommand)
cli.register(DeleteCommand)
cli.register(HelpCommand)
cli.register(InfoCommand)
cli.register(KillCommand)
cli.register(RunCommand)
cli.register(StartCommand)
cli.register(UpdateCommand)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fsMock from 'mock-fs'
import chalk from 'chalk'

import { cli } from '../../cli.js'
import { KillCommand } from '../kill.js'
import { DeleteCommand } from '../delete.js'
import { config, ConfigData } from '../../services/Config.js'
import {
LeonInstance,
Expand All @@ -11,18 +11,18 @@ import {
import { isExistingFile } from '../../utils/isExistingFile.js'
import { Log } from '../../services/Log.js'

describe('leon kill', () => {
describe('leon delete', () => {
afterEach(() => {
fsMock.restore()
jest.clearAllMocks()
})

it('should be instance of the command', () => {
const command = cli.process(['kill'])
expect(command).toBeInstanceOf(KillCommand)
const command = cli.process(['delete'])
expect(command).toBeInstanceOf(DeleteCommand)
})

it('succeeds and kill the LeonInstance', async () => {
it('succeeds and delete the LeonInstance', async () => {
console.log = jest.fn()
const leonInstanceOptions: LeonInstanceOptions = {
name: 'random-name',
Expand All @@ -40,14 +40,14 @@ describe('leon kill', () => {
[leonInstance.path]: {}
})
expect(await isExistingFile(leonInstance.path)).toBe(true)
const command = cli.process(['kill', '--yes'])
const command = cli.process(['delete', '--yes'])
const exitCode = await command.execute()
const instances = config.get('instances', [])
expect(exitCode).toEqual(0)
expect(await isExistingFile(leonInstance.path)).toBe(false)
expect(instances).toEqual([])
expect(console.log).toHaveBeenCalledWith(
`Leon instance "${leonInstance.name}" killed.`
`Leon instance "${leonInstance.name}" deleted.`
)
})

Expand All @@ -57,7 +57,7 @@ describe('leon kill', () => {
[config.path]: JSON.stringify({ instances: [] }),
[Log.errorsConfig.path]: ''
})
const command = cli.process(['kill', '--yes'])
const command = cli.process(['delete', '--yes'])
const exitCode = await command.execute()
expect(exitCode).toEqual(1)
expect(console.error).toHaveBeenNthCalledWith(
Expand Down
18 changes: 10 additions & 8 deletions src/commands/kill.ts → src/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { LeonInstance } from '../services/LeonInstance.js'
import { log } from '../services/Log.js'
import { prompt } from '../services/Prompt.js'

export class KillCommand extends Command {
static paths = [['kill']]
export class DeleteCommand extends Command {
static paths = [['delete']]

static usage = {
description: 'Kill a Leon instance.'
description: 'Delete a Leon instance.'
}

public name = Option.String('--name', {
Expand All @@ -23,16 +23,18 @@ export class KillCommand extends Command {
try {
const { yes = false } = this
const leonInstance = LeonInstance.get(this.name)
console.log(`You are about to kill Leon instance "${leonInstance.name}".`)
if (yes || (await prompt.shouldExecute('Are you sure?'))) {
await leonInstance.kill()
console.log(`Leon instance "${leonInstance.name}" killed.`)
console.log(
`You are about to delete Leon instance "${leonInstance.name}".`
)
if (yes || (await prompt.shouldExecute('Are you sure?', 'no'))) {
await leonInstance.delete()
console.log(`Leon instance "${leonInstance.name}" deleted.`)
}
return 0
} catch (error) {
log.error({
error,
commandPath: 'kill'
commandPath: 'delete'
})
return 1
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/LeonInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class LeonInstance implements LeonInstanceOptions {
}
}

public async kill(): Promise<void> {
public async delete(): Promise<void> {
const instances = config.get('instances', [])
const instanceIndex = instances.findIndex((instance) => {
return instance.name === this.name
Expand Down
43 changes: 26 additions & 17 deletions src/services/Prompt.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
import readline from 'node:readline'

export const positiveAnswers = ['y', 'yes', '']
export const positiveAnswers = ['y', 'yes']
export const negativeAnswers = ['n', 'no']
export const acceptedAnswers = [...positiveAnswers, ...negativeAnswers]
export const acceptedAnswers = [...positiveAnswers, ...negativeAnswers, '']

export class Prompt {
public async shouldExecute(message: string): Promise<boolean> {
public async shouldExecute(
message: string,
defaultValue: 'yes' | 'no'
): Promise<boolean> {
const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
})
return await new Promise((resolve) => {
const ask = (): void => {
readlineInterface.question(`${message} (Y/n) `, (answer: string) => {
const cleanedAnswer = answer.toLowerCase().trim()
const isAcceptedAnswer = acceptedAnswers.includes(cleanedAnswer)
if (isAcceptedAnswer) {
readlineInterface.close()
const isUserAccepting = positiveAnswers.includes(cleanedAnswer)
resolve(isUserAccepting)
} else {
console.log(
'The value you provided is unknown. Please reply Yes or No.'
)
ask()
readlineInterface.question(
`${message} ${defaultValue === 'yes' ? '(Y/n)' : '(y/N)'} `,
(answer: string) => {
const cleanedAnswer = answer.toLowerCase().trim()
const isAcceptedAnswer = acceptedAnswers.includes(cleanedAnswer)
if (isAcceptedAnswer) {
readlineInterface.close()
const isUserAccepting =
positiveAnswers.includes(cleanedAnswer) ||
(defaultValue === 'yes' && cleanedAnswer === '')
resolve(isUserAccepting)
} else {
console.log(
'The value you provided is unknown. Please reply "Yes" or "No".'
)
ask()
}
}
})
)
}
ask()
})
}

public async shouldInstall(requirement: string): Promise<boolean> {
return await this.shouldExecute(
`Would you like to download ${requirement}? It is a necessary requirement.`
`Would you like to download ${requirement}? It is a necessary requirement.`,
'yes'
)
}
}
Expand Down

0 comments on commit 68cf570

Please sign in to comment.