Skip to content

Commit

Permalink
Merge pull request #41 from oclif/mdonnalley/ci-time
Browse files Browse the repository at this point in the history
fix: show correct time in CI mode
  • Loading branch information
mdonnalley authored Oct 4, 2024
2 parents 997c40c + a4b2270 commit 7a2a924
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
141 changes: 141 additions & 0 deletions examples/sf-specific/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import {MultiStageOutput} from '../../src/multi-stage-output.js'

async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

type Data = {
mdapiDeploy: any
sourceMemberPolling: any
status: string
message: string
username: string
id: string
}

function round(value: number, precision: number): number {
const multiplier = 10 ** (precision || 0)
return Math.round(value * multiplier) / multiplier
}

function formatProgress(current: number, total: number): string {
if (total === 0) {
return '0/0 (0%)'
}

return `${current}/${total} (${round((current / total) * 100, 0)}%)`
}

const stages = [
'Preparing',
'Waiting for the org to respond',
'Deploying Metadata',
'Running Tests',
'Updating Source Tracking',
'Done',
]

const ms = new MultiStageOutput<Data>({
jsonEnabled: false,
postStagesBlock: [
{
bold: true,
get: (data): string | undefined => data?.status,
label: 'Status',
type: 'dynamic-key-value',
},
{
get: (data): string | undefined => data?.id,
label: 'Deploy ID',
neverCollapse: true,
type: 'static-key-value',
},
{
get: (data): string | undefined => data?.username,
label: 'Target Org',
type: 'static-key-value',
},
],
preStagesBlock: [
{
get: (data): string | undefined => data?.message,
type: 'message',
},
],
stageSpecificBlock: [
{
get: (data): string | undefined =>
data?.mdapiDeploy?.numberComponentsTotal
? formatProgress(data?.mdapiDeploy?.numberComponentsDeployed ?? 0, data?.mdapiDeploy?.numberComponentsTotal)
: undefined,
label: 'Components',
stage: 'Deploying Metadata',
type: 'dynamic-key-value',
},
{
get: (data): string | undefined =>
data?.mdapiDeploy?.numberTestsTotal && data?.mdapiDeploy?.numberTestsCompleted
? formatProgress(data?.mdapiDeploy?.numberTestsCompleted, data?.mdapiDeploy?.numberTestsTotal)
: undefined,
label: 'Tests',
stage: 'Running Tests',
type: 'dynamic-key-value',
},
{
get: (data): string | undefined =>
data?.sourceMemberPolling?.original
? formatProgress(data.sourceMemberPolling.remaining, data.sourceMemberPolling.original)
: undefined,
label: 'Members',
stage: 'Updating Source Tracking',
type: 'dynamic-key-value',
},
],
stages,
title: 'Deploying Metadata',
})

ms.goto('Preparing')
await sleep(400)

const username = '[email protected]'
ms.updateData({
id: '0AfKP000000Xugj0AC',
message: `Deploying v61.0 metadata to ${username} using the v62.0 REST API`,
status: 'In Progress',
username,
})

ms.goto('Deploying Metadata')

for (let i = 0; i <= 10; i++) {
ms.updateData({
mdapiDeploy: {
numberComponentsDeployed: i,
numberComponentsTotal: 10,
},
})

// eslint-disable-next-line no-await-in-loop
await sleep(200)
}

ms.updateData({status: 'Succeeded'})
ms.skipTo('Updating Source Tracking')

for (let i = 0; i <= 10; i++) {
ms.updateData({
sourceMemberPolling: {
original: 10,
remaining: i,
},
})
// eslint-disable-next-line no-await-in-loop
await sleep(100)
}

ms.goto('Done')

ms.stop()
6 changes: 5 additions & 1 deletion src/multi-stage-output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ class CIMultiStageOutput<T extends Record<string, unknown>> {
}

case 'current': {
if (!this.startTimes.has(stage)) this.startTimes.set(stage, Date.now())
if (Date.now() - this.lastUpdateTime < this.messageTimeout) break
this.lastUpdateTime = Date.now()
if (!this.startTimes.has(stage)) this.startTimes.set(stage, Date.now())
ux.stdout(`${this.design.icons.current.figure} ${stage}…`)
this.printInfo(this.preStagesBlock, 3)
this.printInfo(
Expand All @@ -191,6 +191,10 @@ class CIMultiStageOutput<T extends Record<string, unknown>> {

case 'failed':
case 'skipped':
case 'paused':
case 'aborted':
case 'async':
case 'warning':
case 'completed': {
this.seenStages.add(stage)
if (this.hasStageTime && status !== 'skipped') {
Expand Down

0 comments on commit 7a2a924

Please sign in to comment.