Skip to content

Commit

Permalink
chore(gatsby): revert progressbar functionality (gatsbyjs#14884)
Browse files Browse the repository at this point in the history
This reverts commit 967597c.
  • Loading branch information
wardpeet committed Jun 18, 2019
1 parent 7ca4089 commit 00818b9
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 429 deletions.
1 change: 0 additions & 1 deletion packages/gatsby-cli/package.json
Expand Up @@ -33,7 +33,6 @@
"object.entries": "^1.1.0",
"opentracing": "^0.14.3",
"pretty-error": "^2.1.1",
"progress": "^2.0.3",
"prompts": "^2.1.0",
"react": "^16.8.4",
"resolve-cwd": "^2.0.0",
Expand Down
81 changes: 3 additions & 78 deletions packages/gatsby-cli/src/reporter/index.js
Expand Up @@ -93,88 +93,13 @@ const reporter: Reporter = {
const spanArgs = parentSpan ? { childOf: parentSpan } : {}
const span = tracer.startSpan(name, spanArgs)

const activity = reporterInstance.createActivity({
type: `spinner`,
id: name,
status: ``,
})
const activity = reporterInstance.createActivity(name)

return {
start() {
activity.update({
startTime: process.hrtime(),
})
},
setStatus(status) {
activity.update({
status: status,
})
},
...activity,
end() {
span.finish()
activity.done()
},
span,
}
},

/**
* Create a progress bar for an activity
* @param {string} name - Name of activity.
* @param {number} total - Total items to be processed.
* @param {number} start - Start count to show.
* @param {ActivityArgs} activityArgs - optional object with tracer parentSpan
* @returns {ActivityTracker} The activity tracker.
*/
createProgress(
name: string,
total,
start = 0,
activityArgs: ActivityArgs = {}
): ActivityTracker {
const { parentSpan } = activityArgs
const spanArgs = parentSpan ? { childOf: parentSpan } : {}
const span = tracer.startSpan(name, spanArgs)

let hasStarted = false
let current = start
const activity = reporterInstance.createActivity({
type: `progress`,
id: name,
current,
total,
})

return {
start() {
if (hasStarted) {
return
}

hasStarted = true
activity.update({
startTime: process.hrtime(),
})
},
setStatus(status) {
activity.update({
status: status,
})
},
tick() {
activity.update({
current: ++current,
})
},
done() {
span.finish()
activity.done()
},
set total(value) {
total = value
activity.update({
total: value,
})
activity.end()
},
span,
}
Expand Down
@@ -1,7 +1,14 @@
import React from "react"
import convertHrtime from "convert-hrtime"
import { Box } from "ink"
import Spinner from "ink-spinner"

export const calcElapsedTime = startTime => {
const elapsed = process.hrtime(startTime)

return convertHrtime(elapsed)[`seconds`].toFixed(3)
}

export default function Activity({ name, status }) {
let statusText = name
if (status) {
Expand Down

This file was deleted.

119 changes: 46 additions & 73 deletions packages/gatsby-cli/src/reporter/reporters/ink/reporter.js
@@ -1,29 +1,19 @@
import React from "react"
import { Static, Box } from "ink"
import { isCI } from "ci-info"
import chalk from "chalk"
import Spinner from "./components/spinner"
import ProgressBar from "./components/progress-bar"
import Activity, { calcElapsedTime } from "./components/activity"
import { Message } from "./components/messages"
import isTTY from "../../../util/is-tty"
import calcElapsedTime from "../../../util/calc-elapsed-time"

const showProgress = isTTY

const successTextGenerator = {
spinner: activity => {
let successText = `${activity.id} - ${calcElapsedTime(
activity.startTime
)} s`
if (activity.status) {
successText += ` — ${activity.status}`
}

return successText
},
progress: activity =>
`${activity.id}${activity.current}/${activity.total} - ${calcElapsedTime(
activity.startTime
)} s`,
const showProgress = process.stdout.isTTY && !isCI

const generateActivityFinishedText = (name, activity) => {
let successText = `${name} - ${calcElapsedTime(activity.startTime)} s`
if (activity.status) {
successText += ` — ${activity.status}`
}

return successText
}

export default class GatsbyReporter extends React.Component {
Expand All @@ -36,40 +26,44 @@ export default class GatsbyReporter extends React.Component {

format = chalk

createActivity = ({ id, ...options }) => {
this.setState(state => {
return {
activities: {
...state.activities,
[id]: {
id,
...options,
},
},
}
})

createActivity = name => {
return {
update: newState => {
start: () => {
this.setState(state => {
return {
activities: {
...state.activities,
[name]: {
status: ``,
startTime: process.hrtime(),
},
},
}
})
},
setStatus: status => {
this.setState(state => {
const activity = state.activities[name]

return {
activities: {
...state.activities,
[id]: {
...state.activities[id],
...newState,
[name]: {
...activity,
status: status,
},
},
}
})
},
done: () => {
const activity = this.state.activities[id]
this.success(successTextGenerator[activity.type]({ id, ...activity }))
end: () => {
const activity = this.state.activities[name]

this.success(generateActivityFinishedText(name, activity))

this.setState(state => {
const activities = { ...state.activities }
delete activities[id]
delete activities[name]

return {
activities,
Expand Down Expand Up @@ -122,48 +116,27 @@ export default class GatsbyReporter extends React.Component {
}

render() {
const { activities, messages, disableColors } = this.state

const spinners = []
const progressBars = []
if (showProgress) {
Object.keys(activities).forEach(activityName => {
const activity = activities[activityName]
if (activity.type === `spinner`) {
spinners.push(activity)
}
if (activity.type === `progress` && activity.startTime) {
progressBars.push(activity)
}
})
}

return (
<Box flexDirection="column">
<Box flexDirection="column">
<Static>
{messages.map((msg, index) => (
{this.state.messages.map((msg, index) => (
<Box textWrap="wrap" key={index}>
<Message type={msg.type} hideColors={disableColors}>
<Message type={msg.type} hideColors={this.state.disableColors}>
{msg.text}
</Message>
</Box>
))}
</Static>

{spinners.map(activity => (
<Spinner key={activity.id} name={activity.id} {...activity} />
))}

{progressBars.map(activity => (
<ProgressBar
key={activity.id}
message={activity.id}
total={activity.total}
current={activity.current}
startTime={activity.startTime}
/>
))}
{showProgress &&
Object.keys(this.state.activities).map(activityName => (
<Activity
key={activityName}
name={activityName}
{...this.state.activities[activityName]}
/>
))}
</Box>
</Box>
)
Expand Down

0 comments on commit 00818b9

Please sign in to comment.