Skip to content

Commit

Permalink
chore(cli): confirm before overwrite existing processor version (#1048)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons authored Nov 19, 2024
1 parent 9a1eeff commit 76e97c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
37 changes: 35 additions & 2 deletions packages/cli/src/commands/run-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ const uploadOptionDefinitions = [
name: 'debug',
description: '(Optional) Run driver in debug mode, default false',
type: Boolean
},
{
name: 'silent-overwrite',
description: '(Optional) Overwrite exiting processor version without confirmation, default false',
type: Boolean
}
]

Expand Down Expand Up @@ -107,6 +112,9 @@ export async function runUpload(processorConfig: YamlProjectConfig, argv: string
if (options.debug) {
processorConfig.debug = true
}
if (options['silent-overwrite']) {
processorConfig.silientOverwrite = true
}
finalizeHost(processorConfig, options.host)
FinalizeProjectName(processorConfig, options.owner, options.name)
console.log(processorConfig)
Expand Down Expand Up @@ -173,10 +181,29 @@ export async function createProjectPrompt(options: YamlProjectConfig, auth: Auth
rl.close()
return false
} else {
rl.close()
return createProjectPrompt(options, auth, type)
}
}

export async function confirm(question: string): Promise<boolean> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const answer: string = await new Promise((resolve) => rl.question(`${question} (yes/no) `, resolve))
if (['y', 'yes'].includes(answer.toLowerCase())) {
rl.close()
return true
} else if (['n', 'no'].includes(answer.toLowerCase())) {
rl.close()
return false
} else {
rl.close()
return confirm(question)
}
}

export interface Auth {
'api-key'?: string
authorization?: string
Expand Down Expand Up @@ -227,14 +254,20 @@ export async function uploadFile(options: YamlProjectConfig, auth: Auth, continu
// console.error(chalk.red('Failed to get upload url'))
console.error(chalk.red(((await initUploadResRaw.json()) as { message: string }).message))
if (initUploadResRaw.status === 404) {
const created = await createProjectPrompt(options, auth )
const created = await createProjectPrompt(options, auth)
if (created) {
await upload()
}
}
return
}
const initUploadRes = (await initUploadResRaw.json()) as { url: string; warning?: string }
const initUploadRes = (await initUploadResRaw.json()) as { url: string; warning?: string; replacingVersion: number }
if (initUploadRes.replacingVersion && !options.silientOverwrite) {
const confirmed = await confirm(`Overwrite the existing version ${initUploadRes.replacingVersion}?`)
if (!confirmed) {
process.exit(0)
}
}
if (initUploadRes.warning) {
console.warn(chalk.yellow('Warning:', initUploadRes.warning))
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface YamlProjectConfig {
networkOverrides?: YamlNetworkOverride[]
debug: boolean
type?: string
silientOverwrite?: boolean
}

export function getFinalizedHost(host: string): string {
Expand Down

0 comments on commit 76e97c1

Please sign in to comment.