Skip to content

Commit 6b93c1c

Browse files
author
LB
authored
remove progress bar (#29436)
1 parent ae12e63 commit 6b93c1c

File tree

4 files changed

+2
-150
lines changed

4 files changed

+2
-150
lines changed

packages/gatsby-plugin-sharp/src/__tests__/utils.js

+1-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,10 @@
11
jest.mock(`gatsby-cli/lib/reporter`)
22
jest.mock(`progress`)
3-
const {
4-
createGatsbyProgressOrFallbackToExternalProgressBar,
5-
calculateImageSizes,
6-
} = require(`../utils`)
3+
const { calculateImageSizes } = require(`../utils`)
74
const reporter = require(`gatsby-cli/lib/reporter`)
85
const progress = require(`progress`)
96
const sharp = require(`sharp`)
107

11-
describe(`createGatsbyProgressOrFallbackToExternalProgressBar`, () => {
12-
beforeEach(() => {
13-
progress.mockClear()
14-
})
15-
16-
it(`should use createProgress from gatsby-cli when available`, () => {
17-
createGatsbyProgressOrFallbackToExternalProgressBar(`test`, reporter)
18-
expect(reporter.createProgress).toBeCalled()
19-
expect(progress).not.toBeCalled()
20-
})
21-
22-
it(`should fallback to a local implementation when createProgress does not exists on reporter`, () => {
23-
reporter.createProgress = null
24-
const bar = createGatsbyProgressOrFallbackToExternalProgressBar(
25-
`test`,
26-
reporter
27-
)
28-
expect(progress).toHaveBeenCalledTimes(1)
29-
expect(bar).toHaveProperty(`start`, expect.any(Function))
30-
expect(bar).toHaveProperty(`tick`, expect.any(Function))
31-
expect(bar).toHaveProperty(`done`, expect.any(Function))
32-
expect(bar).toHaveProperty(`total`)
33-
})
34-
35-
it(`should fallback to a local implementation when no reporter is present`, () => {
36-
const bar = createGatsbyProgressOrFallbackToExternalProgressBar(`test`)
37-
expect(progress).toHaveBeenCalledTimes(1)
38-
expect(bar).toHaveProperty(`start`, expect.any(Function))
39-
expect(bar).toHaveProperty(`tick`, expect.any(Function))
40-
expect(bar).toHaveProperty(`done`, expect.any(Function))
41-
expect(bar).toHaveProperty(`total`)
42-
})
43-
})
44-
458
const file = {
469
absolutePath: `~/Usr/gatsby-sites/src/img/photo.png`,
4710
}

packages/gatsby-plugin-sharp/src/gatsby-node.js

+1-24
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,15 @@ const {
77
} = require(`./index`)
88
const { pathExists } = require(`fs-extra`)
99
const { slash } = require(`gatsby-core-utils`)
10-
const { getProgressBar, createOrGetProgressBar } = require(`./utils`)
1110

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

15-
// create the progressbar once and it will be killed in another lifecycle
16-
const finishProgressBar = () => {
17-
const progressBar = getProgressBar()
18-
if (progressBar) {
19-
progressBar.done()
20-
}
21-
}
22-
23-
exports.onPostBuild = () => finishProgressBar()
24-
2514
exports.onCreateDevServer = async ({ app, cache, reporter }) => {
2615
if (!_lazyJobsEnabled()) {
27-
finishProgressBar()
2816
return
2917
}
3018

31-
createOrGetProgressBar()
32-
finishProgressBar()
33-
3419
app.use(async (req, res, next) => {
3520
const decodedURI = decodeURIComponent(req.path)
3621
const pathOnDisk = path.resolve(path.join(`./public/`, decodedURI))
@@ -119,10 +104,7 @@ exports.onPostBootstrap = async ({ reporter, cache, store }) => {
119104
}
120105
}
121106

122-
exports.onPreBootstrap = async (
123-
{ actions, emitter, reporter, cache, store },
124-
pluginOptions
125-
) => {
107+
exports.onPreBootstrap = async ({ actions, emitter, cache }, pluginOptions) => {
126108
setActions(actions)
127109
setPluginOptions(pluginOptions)
128110

@@ -174,8 +156,6 @@ exports.onPreBootstrap = async (
174156
const job = action.payload.job
175157
const imageCount = job.args.operations.length
176158
imageCountInJobsMap.set(job.contentDigest, imageCount)
177-
const progress = createOrGetProgressBar(reporter)
178-
progress.addImageToProcess(imageCount)
179159
}
180160
})
181161

@@ -188,9 +168,6 @@ exports.onPreBootstrap = async (
188168
return
189169
}
190170

191-
const imageCount = imageCountInJobsMap.get(jobContentDigest)
192-
const progress = createOrGetProgressBar(reporter)
193-
progress.tick(imageCount)
194171
imageCountInJobsMap.delete(jobContentDigest)
195172
}
196173
})

packages/gatsby-plugin-sharp/src/scheduler.js

-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const fs = require(`fs-extra`)
44
const got = require(`got`)
55
const { createContentDigest } = require(`gatsby-core-utils`)
66
const worker = require(`./gatsby-worker`)
7-
const { createOrGetProgressBar } = require(`./utils`)
87

98
const processImages = async (jobId, job, actions) => {
109
try {
@@ -83,14 +82,9 @@ const scheduleJob = async (job, actions, reporter) => {
8382
{ name: `gatsby-plugin-sharp` }
8483
)
8584

86-
const progressBar = createOrGetProgressBar(reporter)
87-
const transformsCount = job.args.operations.length
88-
progressBar.addImageToProcess(transformsCount)
89-
9085
const promise = new Promise((resolve, reject) => {
9186
setImmediate(() => {
9287
processImages(jobId, convertedJob, actions).then(result => {
93-
progressBar.tick(transformsCount)
9488
resolve(result)
9589
}, reject)
9690
})

packages/gatsby-plugin-sharp/src/utils.js

-82
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,4 @@
1-
import ProgressBar from "progress"
21
import sharp from "./safe-sharp"
3-
// TODO remove in V3
4-
export function createGatsbyProgressOrFallbackToExternalProgressBar(
5-
message,
6-
reporter
7-
) {
8-
if (reporter && reporter.createProgress) {
9-
return reporter.createProgress(message)
10-
}
11-
12-
const bar = new ProgressBar(
13-
` [:bar] :current/:total :elapsed s :percent ${message}`,
14-
{
15-
total: 0,
16-
width: 30,
17-
clear: true,
18-
}
19-
)
20-
21-
return {
22-
start() {},
23-
tick(increment = 1) {
24-
bar.tick(increment)
25-
},
26-
done() {},
27-
set total(value) {
28-
bar.total = value
29-
},
30-
}
31-
}
32-
33-
let progressBar
34-
let pendingImagesCounter = 0
35-
let firstPass = true
36-
export const createOrGetProgressBar = reporter => {
37-
if (!progressBar) {
38-
progressBar = createGatsbyProgressOrFallbackToExternalProgressBar(
39-
`Generating image thumbnails`,
40-
reporter
41-
)
42-
43-
const originalDoneFn = progressBar.done
44-
45-
// TODO this logic should be moved to the reporter.
46-
// when done is called we remove the progressbar instance and reset all the things
47-
// this will be called onPostBuild or when devserver is created
48-
progressBar.done = () => {
49-
originalDoneFn.call(progressBar)
50-
progressBar = null
51-
pendingImagesCounter = 0
52-
}
53-
54-
progressBar.addImageToProcess = imageCount => {
55-
if (pendingImagesCounter === 0) {
56-
progressBar.start()
57-
}
58-
pendingImagesCounter += imageCount
59-
progressBar.total = pendingImagesCounter
60-
}
61-
62-
// when we create a progressBar for the second time so when .done() has been called before
63-
// we create a modified tick function that automatically stops the progressbar when total is reached
64-
// this is used for development as we're watching for changes
65-
if (!firstPass) {
66-
let progressBarCurrentValue = 0
67-
const originalTickFn = progressBar.tick
68-
progressBar.tick = (ticks = 1) => {
69-
originalTickFn.call(progressBar, ticks)
70-
progressBarCurrentValue += ticks
71-
72-
if (progressBarCurrentValue === pendingImagesCounter) {
73-
progressBar.done()
74-
}
75-
}
76-
}
77-
firstPass = false
78-
}
79-
80-
return progressBar
81-
}
82-
83-
export const getProgressBar = () => progressBar
842

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

0 commit comments

Comments
 (0)