Skip to content

Commit

Permalink
fix(cli): print job completition message on successful dataset copy
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2022
1 parent 5412ca8 commit 6da4556
Showing 1 changed file with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,36 @@ const progress = (url: string) => {
})
}

const followProgress = (jobId: string, client: SanityClient, output: CliOutputter) => {
const followProgress = (
jobId: string,
client: SanityClient,
output: CliOutputter
): Promise<void> => {
let currentProgress = 0

const spinner = output.spinner({}).start()
const listenUrl = client.getUrl(`jobs/${jobId}/listen`)

debug(`Listening to ${listenUrl}`)

progress(listenUrl).subscribe({
next: (event) => {
if (typeof event.progress === 'number') {
currentProgress = event.progress
}

spinner.text = `Copy in progress: ${currentProgress}%`
},
error: (err) => {
spinner.fail()
throw new Error(`There was an error copying dataset: ${err.data}`)
},
complete: () => {
spinner.succeed('Copy finished.')
},
return new Promise((resolve, reject) => {
progress(listenUrl).subscribe({
next: (event) => {
if (typeof event.progress === 'number') {
currentProgress = event.progress
}

spinner.text = `Copy in progress: ${currentProgress}%`
},
error: (err) => {
spinner.fail()
reject(new Error(`${err.data}`))
},
complete: () => {
spinner.succeed('Copy finished.')
resolve()
},
})
})
}

Expand Down Expand Up @@ -163,8 +170,7 @@ const copyDatasetCommand: CliCommandDefinition<CopyDatasetFlags> = {
throw new Error('Please supply a jobId')
}

followProgress(jobId, client, output)

await followProgress(jobId, client, output)
return
}

Expand Down Expand Up @@ -223,7 +229,8 @@ const copyDatasetCommand: CliCommandDefinition<CopyDatasetFlags> = {
return
}

followProgress(response.jobId, client, output)
await followProgress(response.jobId, client, output)
output.print(`Job ${chalk.green(response.jobId)} completed`)
} catch (error) {
if (error.statusCode) {
output.print(`${chalk.red(`Dataset copying failed:\n${error.response.body.message}`)}\n`)
Expand Down

0 comments on commit 6da4556

Please sign in to comment.