Skip to content

Commit

Permalink
fix: another pass of refactoring
Browse files Browse the repository at this point in the history
env and blob commands

Co-authored-by: Thomas Lane <163203257+tlane25@users.noreply.github.com>
Co-authored-by: Thomas Lane <tlane25@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 22, 2024
1 parent f6aa58d commit 9ba5ada
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 68 deletions.
9 changes: 7 additions & 2 deletions src/commands/blobs/blobs-delete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { getStore } from '@netlify/blobs'

import { chalk, error as printError, log } from '../../utils/command-helpers.js'
import { blobDeletePrompts } from '../../utils/prompts/blob-delete-prompts.js'
import { promptBlobDelete } from '../../utils/prompts/blob-delete-prompts.js'

/**
* The blobs:delete command
*/
export const blobsDelete = async (storeName: string, key: string, _options: Record<string, unknown>, command: any) => {
// Prevents prompts from blocking scripted commands
if (command.scriptedCommand) {
_options.force = true
}

const { api, siteInfo } = command.netlify
const { force } = _options

Expand All @@ -18,7 +23,7 @@ export const blobsDelete = async (storeName: string, key: string, _options: Reco
})

if (force === undefined) {
await blobDeletePrompts(key, storeName)
await promptBlobDelete(key, storeName)
}

try {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/blobs/blobs-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ export const blobsSet = async (
options: Options,
command: BaseCommand,
) => {

// Prevents prompts from blocking scripted commands
if (command.scriptedCommand){
if (command.scriptedCommand) {
options.force = true
}

Expand Down
3 changes: 1 addition & 2 deletions src/commands/env/env-clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ const cloneEnvVars = async ({ api, force, siteFrom, siteTo }): Promise<boolean>
}

export const envClone = async (options: OptionValues, command: BaseCommand) => {

// Prevents prompts from blocking scripted commands
if (command.scriptedCommand){
if (command.scriptedCommand) {
options.force = true
}

Expand Down
3 changes: 1 addition & 2 deletions src/commands/env/env-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ const setInEnvelope = async ({ api, context, force, key, scope, secret, siteInfo
}

export const envSet = async (key: string, value: string, options: OptionValues, command: BaseCommand) => {

// Prevents prompts from blocking scripted commands
if (command.scriptedCommand){
if (command.scriptedCommand) {
options.force = true
}

Expand Down
5 changes: 2 additions & 3 deletions src/commands/env/env-unset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OptionValues } from 'commander'

import { chalk, log, logJson, exit } from '../../utils/command-helpers.js'
import { AVAILABLE_CONTEXTS, translateFromEnvelopeToMongo } from '../../utils/env/index.js'
import { promptOverwriteEnvVariable } from '../../utils/prompts/unset-set-prompts.js'
import { promptOverwriteEnvVariable } from '../../utils/prompts/env-unset-prompts.js'
import BaseCommand from '../base-command.js'
/**
* Deletes a given key from the env of a site configured with Envelope
Expand Down Expand Up @@ -68,9 +68,8 @@ const unsetInEnvelope = async ({ api, context, force, key, siteInfo }) => {
}

export const envUnset = async (key: string, options: OptionValues, command: BaseCommand) => {

// Prevents prompts from blocking scripted commands
if (command.scriptedCommand){
if (command.scriptedCommand) {
options.force = true
}

Expand Down
24 changes: 11 additions & 13 deletions src/utils/prompts/blob-delete-prompts.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { chalk, log } from '../command-helpers.js'
import { log } from '../command-helpers.js'

import { confirmPrompt } from './confirm-prompt.js'
import { destructiveCommandMessages } from './prompt-messages.js'

export const promptBlobDelete = async (key: string, storeName: string): Promise<void> => {
const { overwriteNoticeMessage } = destructiveCommandMessages
const { generateWarningMessage, overwriteConfirmationMessage } = destructiveCommandMessages.blobDelete

const warningMessage = generateWarningMessage(key, storeName)

const generateBlobWarningMessage = (key: string, storeName: string): void => {
log()
log(
`${chalk.redBright('Warning')}: The following blob key ${chalk.cyan(key)} will be deleted from store ${chalk.cyan(
storeName,
)}:`,
)
log(warningMessage)
log()
log(`${chalk.yellowBright('Notice')}: To overwrite without this warning, you can use the --force flag.`)
}

export const blobDeletePrompts = async (key: string, storeName: string): Promise<void> => {
generateBlobWarningMessage(key, storeName)
await confirmPrompt('Do you want to proceed with deleting the value at this key?')
log(overwriteNoticeMessage)
await confirmPrompt(overwriteConfirmationMessage)
}
6 changes: 3 additions & 3 deletions src/utils/prompts/confirm-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { log, exit } from '../command-helpers.js'

export const confirmPrompt = async (message: string): Promise<void> => {
try {
const { wantsToSet } = await inquirer.prompt({
const { confirm } = await inquirer.prompt({
type: 'confirm',
name: 'wantsToSet',
name: 'confirm',
message,
default: false,
})
log()
if (!wantsToSet) {
if (!confirm) {
exit()
}
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/prompts/env-clone-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export async function promptEnvCloneOverwrite(siteId: string, existingEnvVars: E
log()
log(noticeEnvVarsMessage)
log()
existingEnvVarKeys.forEach(log)
existingEnvVarKeys.forEach((envVar) => {
log(envVar)
})
log()
log(overwriteNoticeMessage)

Expand Down
File renamed without changes.
9 changes: 8 additions & 1 deletion src/utils/prompts/prompt-messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { chalk } from '../command-helpers.js'
import { EnvVar } from '../types.js'

export const destructiveCommandMessages = {
overwriteNoticeMessage: `${chalk.yellowBright(
Expand All @@ -12,6 +11,14 @@ export const destructiveCommandMessages = {
overwriteConfirmationMessage: 'Do you want to proceed with overwriting this blob key existing value?',
},

blobDelete: {
generateWarningMessage: (key: string, storeName: string) =>
`${chalk.redBright('Warning')}: The following blob key ${chalk.cyan(key)} will be deleted from store ${chalk.cyan(
storeName,
)}:`,
overwriteConfirmationMessage: 'Do you want to proceed with deleting the value at this key?',
},

envSet: {
generateWarningMessage: (variableName: string) =>
`${chalk.redBright('Warning')}: The environment variable ${chalk.bgBlueBright(variableName)} already exists.`,
Expand Down
30 changes: 14 additions & 16 deletions tests/integration/commands/blobs/blobs-delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { describe, expect, test, vi, beforeEach } from 'vitest'
import BaseCommand from '../../../../src/commands/base-command.js'
import { createBlobsCommand } from '../../../../src/commands/blobs/blobs.js'
import { log } from '../../../../src/utils/command-helpers.js'
import { destructiveCommandMessages } from '../../../../src/utils/prompts/prompt-messages.js'
import { Route } from '../../utils/mock-api-vitest.js'
import { getEnvironmentVariables, withMockApi } from '../../utils/mock-api.js'

Expand Down Expand Up @@ -44,13 +45,10 @@ describe('blob:delete command', () => {
const storeName = 'my-store'
const key = 'my-key'

const warningMessage = `${chalk.redBright('Warning')}: The following blob key ${chalk.cyan(
key,
)} will be deleted from store ${chalk.cyan(storeName)}:`
const { overwriteNoticeMessage } = destructiveCommandMessages
const { generateWarningMessage, overwriteConfirmationMessage } = destructiveCommandMessages.blobDelete

const noticeMessage = `${chalk.yellowBright(
'Notice',
)}: To overwrite without this warning, you can use the --force flag.`
const warningMessage = generateWarningMessage(key, storeName)

const successMessage = `${chalk.greenBright('Success')}: Blob ${chalk.yellow(key)} deleted from store ${chalk.yellow(
storeName,
Expand All @@ -73,19 +71,19 @@ describe('blob:delete command', () => {
const program = new BaseCommand('netlify')
createBlobsCommand(program)

const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: true })
const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: true })

await program.parseAsync(['', '', 'blob:delete', storeName, key])

expect(promptSpy).toHaveBeenCalledWith({
type: 'confirm',
name: 'wantsToSet',
message: expect.stringContaining('Do you want to proceed with deleting the value at this key?'),
name: 'confirm',
message: expect.stringContaining(overwriteConfirmationMessage),
default: false,
})

expect(log).toHaveBeenCalledWith(warningMessage)
expect(log).toHaveBeenCalledWith(noticeMessage)
expect(log).toHaveBeenCalledWith(overwriteNoticeMessage)
expect(log).toHaveBeenCalledWith(successMessage)
})
})
Expand All @@ -103,7 +101,7 @@ describe('blob:delete command', () => {
const program = new BaseCommand('netlify')
createBlobsCommand(program)

const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: false })
const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: false })

try {
await program.parseAsync(['', '', 'blob:delete', storeName, key])
Expand All @@ -114,13 +112,13 @@ describe('blob:delete command', () => {

expect(promptSpy).toHaveBeenCalledWith({
type: 'confirm',
name: 'wantsToSet',
message: expect.stringContaining('Do you want to proceed with deleting the value at this key?'),
name: 'confirm',
message: expect.stringContaining(overwriteConfirmationMessage),
default: false,
})

expect(log).toHaveBeenCalledWith(warningMessage)
expect(log).toHaveBeenCalledWith(noticeMessage)
expect(log).toHaveBeenCalledWith(overwriteNoticeMessage)
expect(log).not.toHaveBeenCalledWith(successMessage)
})
})
Expand All @@ -145,7 +143,7 @@ describe('blob:delete command', () => {
expect(promptSpy).not.toHaveBeenCalled()

expect(log).not.toHaveBeenCalledWith(warningMessage)
expect(log).not.toHaveBeenCalledWith(noticeMessage)
expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage)
expect(log).toHaveBeenCalledWith(successMessage)
})
})
Expand Down Expand Up @@ -176,7 +174,7 @@ describe('blob:delete command', () => {
expect(promptSpy).not.toHaveBeenCalled()

expect(log).not.toHaveBeenCalledWith(warningMessage)
expect(log).not.toHaveBeenCalledWith(noticeMessage)
expect(log).not.toHaveBeenCalledWith(overwriteNoticeMessage)
expect(log).not.toHaveBeenCalledWith(successMessage)
})
})
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/commands/blobs/blobs-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ describe('blob:set command', () => {
const program = new BaseCommand('netlify')
createBlobsCommand(program)

const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: true })
const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: true })

await program.parseAsync(['', '', 'blob:set', storeName, key, newValue])

expect(promptSpy).toHaveBeenCalledWith({
type: 'confirm',
name: 'wantsToSet',
name: 'confirm',
message: expect.stringContaining(overwriteConfirmationMessage),
default: false,
})
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('blob:set command', () => {
const program = new BaseCommand('netlify')
createBlobsCommand(program)

const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: false })
const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: false })

try {
await program.parseAsync(['', '', 'blob:set', storeName, key, newValue])
Expand All @@ -150,7 +150,7 @@ describe('blob:set command', () => {

expect(promptSpy).toHaveBeenCalledWith({
type: 'confirm',
name: 'wantsToSet',
name: 'confirm',
message: expect.stringContaining(overwriteConfirmationMessage),
default: false,
})
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/commands/env/env-clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ describe('env:clone command', () => {
const program = new BaseCommand('netlify')
createEnvCommand(program)

const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: true })
const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: true })

await program.parseAsync(['', '', 'env:clone', '-t', siteIdTwo])

expect(promptSpy).toHaveBeenCalledWith({
type: 'confirm',
name: 'wantsToSet',
name: 'confirm',
message: expect.stringContaining(overwriteConfirmationMessage),
default: false,
})
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('env:clone command', () => {
const program = new BaseCommand('netlify')
createEnvCommand(program)

const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ wantsToSet: false })
const promptSpy = vi.spyOn(inquirer, 'prompt').mockResolvedValue({ confirm: false })

try {
await program.parseAsync(['', '', 'env:clone', '-t', siteIdTwo])
Expand Down
Loading

0 comments on commit 9ba5ada

Please sign in to comment.