Skip to content

Commit

Permalink
remove progress bar (#29436)
Browse files Browse the repository at this point in the history
  • Loading branch information
LB committed Feb 11, 2021
1 parent ae12e63 commit 6b93c1c
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 150 deletions.
39 changes: 1 addition & 38 deletions packages/gatsby-plugin-sharp/src/__tests__/utils.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
jest.mock(`gatsby-cli/lib/reporter`)
jest.mock(`progress`)
const {
createGatsbyProgressOrFallbackToExternalProgressBar,
calculateImageSizes,
} = require(`../utils`)
const { calculateImageSizes } = require(`../utils`)
const reporter = require(`gatsby-cli/lib/reporter`)
const progress = require(`progress`)
const sharp = require(`sharp`)

describe(`createGatsbyProgressOrFallbackToExternalProgressBar`, () => {
beforeEach(() => {
progress.mockClear()
})

it(`should use createProgress from gatsby-cli when available`, () => {
createGatsbyProgressOrFallbackToExternalProgressBar(`test`, reporter)
expect(reporter.createProgress).toBeCalled()
expect(progress).not.toBeCalled()
})

it(`should fallback to a local implementation when createProgress does not exists on reporter`, () => {
reporter.createProgress = null
const bar = createGatsbyProgressOrFallbackToExternalProgressBar(
`test`,
reporter
)
expect(progress).toHaveBeenCalledTimes(1)
expect(bar).toHaveProperty(`start`, expect.any(Function))
expect(bar).toHaveProperty(`tick`, expect.any(Function))
expect(bar).toHaveProperty(`done`, expect.any(Function))
expect(bar).toHaveProperty(`total`)
})

it(`should fallback to a local implementation when no reporter is present`, () => {
const bar = createGatsbyProgressOrFallbackToExternalProgressBar(`test`)
expect(progress).toHaveBeenCalledTimes(1)
expect(bar).toHaveProperty(`start`, expect.any(Function))
expect(bar).toHaveProperty(`tick`, expect.any(Function))
expect(bar).toHaveProperty(`done`, expect.any(Function))
expect(bar).toHaveProperty(`total`)
})
})

const file = {
absolutePath: `~/Usr/gatsby-sites/src/img/photo.png`,
}
Expand Down
25 changes: 1 addition & 24 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,15 @@ const {
} = require(`./index`)
const { pathExists } = require(`fs-extra`)
const { slash } = require(`gatsby-core-utils`)
const { getProgressBar, createOrGetProgressBar } = require(`./utils`)

const { setPluginOptions } = require(`./plugin-options`)
const path = require(`path`)

// create the progressbar once and it will be killed in another lifecycle
const finishProgressBar = () => {
const progressBar = getProgressBar()
if (progressBar) {
progressBar.done()
}
}

exports.onPostBuild = () => finishProgressBar()

exports.onCreateDevServer = async ({ app, cache, reporter }) => {
if (!_lazyJobsEnabled()) {
finishProgressBar()
return
}

createOrGetProgressBar()
finishProgressBar()

app.use(async (req, res, next) => {
const decodedURI = decodeURIComponent(req.path)
const pathOnDisk = path.resolve(path.join(`./public/`, decodedURI))
Expand Down Expand Up @@ -119,10 +104,7 @@ exports.onPostBootstrap = async ({ reporter, cache, store }) => {
}
}

exports.onPreBootstrap = async (
{ actions, emitter, reporter, cache, store },
pluginOptions
) => {
exports.onPreBootstrap = async ({ actions, emitter, cache }, pluginOptions) => {
setActions(actions)
setPluginOptions(pluginOptions)

Expand Down Expand Up @@ -174,8 +156,6 @@ exports.onPreBootstrap = async (
const job = action.payload.job
const imageCount = job.args.operations.length
imageCountInJobsMap.set(job.contentDigest, imageCount)
const progress = createOrGetProgressBar(reporter)
progress.addImageToProcess(imageCount)
}
})

Expand All @@ -188,9 +168,6 @@ exports.onPreBootstrap = async (
return
}

const imageCount = imageCountInJobsMap.get(jobContentDigest)
const progress = createOrGetProgressBar(reporter)
progress.tick(imageCount)
imageCountInJobsMap.delete(jobContentDigest)
}
})
Expand Down
6 changes: 0 additions & 6 deletions packages/gatsby-plugin-sharp/src/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const fs = require(`fs-extra`)
const got = require(`got`)
const { createContentDigest } = require(`gatsby-core-utils`)
const worker = require(`./gatsby-worker`)
const { createOrGetProgressBar } = require(`./utils`)

const processImages = async (jobId, job, actions) => {
try {
Expand Down Expand Up @@ -83,14 +82,9 @@ const scheduleJob = async (job, actions, reporter) => {
{ name: `gatsby-plugin-sharp` }
)

const progressBar = createOrGetProgressBar(reporter)
const transformsCount = job.args.operations.length
progressBar.addImageToProcess(transformsCount)

const promise = new Promise((resolve, reject) => {
setImmediate(() => {
processImages(jobId, convertedJob, actions).then(result => {
progressBar.tick(transformsCount)
resolve(result)
}, reject)
})
Expand Down
82 changes: 0 additions & 82 deletions packages/gatsby-plugin-sharp/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,4 @@
import ProgressBar from "progress"
import sharp from "./safe-sharp"
// TODO remove in V3
export function createGatsbyProgressOrFallbackToExternalProgressBar(
message,
reporter
) {
if (reporter && reporter.createProgress) {
return reporter.createProgress(message)
}

const bar = new ProgressBar(
` [:bar] :current/:total :elapsed s :percent ${message}`,
{
total: 0,
width: 30,
clear: true,
}
)

return {
start() {},
tick(increment = 1) {
bar.tick(increment)
},
done() {},
set total(value) {
bar.total = value
},
}
}

let progressBar
let pendingImagesCounter = 0
let firstPass = true
export const createOrGetProgressBar = reporter => {
if (!progressBar) {
progressBar = createGatsbyProgressOrFallbackToExternalProgressBar(
`Generating image thumbnails`,
reporter
)

const originalDoneFn = progressBar.done

// TODO this logic should be moved to the reporter.
// when done is called we remove the progressbar instance and reset all the things
// this will be called onPostBuild or when devserver is created
progressBar.done = () => {
originalDoneFn.call(progressBar)
progressBar = null
pendingImagesCounter = 0
}

progressBar.addImageToProcess = imageCount => {
if (pendingImagesCounter === 0) {
progressBar.start()
}
pendingImagesCounter += imageCount
progressBar.total = pendingImagesCounter
}

// when we create a progressBar for the second time so when .done() has been called before
// we create a modified tick function that automatically stops the progressbar when total is reached
// this is used for development as we're watching for changes
if (!firstPass) {
let progressBarCurrentValue = 0
const originalTickFn = progressBar.tick
progressBar.tick = (ticks = 1) => {
originalTickFn.call(progressBar, ticks)
progressBarCurrentValue += ticks

if (progressBarCurrentValue === pendingImagesCounter) {
progressBar.done()
}
}
}
firstPass = false
}

return progressBar
}

export const getProgressBar = () => progressBar

export function rgbToHex(red, green, blue) {
return `#${(blue | (green << 8) | (red << 16) | (1 << 24))
Expand Down

0 comments on commit 6b93c1c

Please sign in to comment.