Skip to content

Commit dd80e1a

Browse files
committed
feat: use new reporter API
1 parent 242e99e commit dd80e1a

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

packages/gatsby-transformer-video/src/binaries.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from "path"
22

33
import execa from "execa"
44
import { access, ensureDir } from "fs-extra"
5+
import reporter from "gatsby-cli/lib/reporter"
56

67
export async function libsInstalled({ platform }) {
78
try {
@@ -31,7 +32,7 @@ export async function downloadLibs({ binariesDir, platform }) {
3132

3233
switch (platform) {
3334
case `win32`:
34-
console.log(
35+
reporter.info(
3536
`Downloading FFMPEG && FFPROBE (Note: This script is not yet tested on windows)`
3637
)
3738
await execa(
@@ -45,33 +46,34 @@ export async function downloadLibs({ binariesDir, platform }) {
4546
execaConfig
4647
)
4748

48-
console.log(`Unzipping FFMPEG && FFPROBE`)
49+
reporter.info(`Unzipping FFMPEG && FFPROBE`)
4950
await execa(`tar`, [`-xf`, `ffmpeg.zip`], execaConfig)
5051

51-
console.log(`Cleanup`)
52+
reporter.info(`Cleanup`)
5253
await execa(`mv`, [`bin/*`, `.`], execaConfig)
5354
await execa(`rm`, [`-rf`, `ffmpeg-latest-win64-static`], execaConfig)
5455
break
5556
case `linux`:
56-
console.log(`Downloading FFMPEG && FFPROBE`)
57+
reporter.info(`Downloading FFMPEG && FFPROBE`)
5758
await execa(
5859
`wget`,
5960
[
6061
`-O`,
62+
`-nv`,
6163
`ffmpeg.zip`,
6264
`https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz`,
6365
],
6466
execaConfig
6567
)
6668

67-
console.log(`Unzipping FFMPEG && FFPROBE`)
69+
reporter.info(`Unzipping FFMPEG && FFPROBE`)
6870
await execa(`tar`, [`-xf`, `ffmpeg.zip`, `--strip`, `1`], execaConfig)
6971

70-
console.log(`Cleanup`)
72+
reporter.info(`Cleanup`)
7173
await execa(`rm`, [`ffmpeg.zip`], execaConfig)
7274
break
7375
case `darwin`:
74-
console.log(`Downloading FFMPEG`)
76+
reporter.info(`Downloading FFMPEG`)
7577

7678
await execa(
7779
`curl`,
@@ -85,7 +87,7 @@ export async function downloadLibs({ binariesDir, platform }) {
8587
execaConfig
8688
)
8789

88-
console.log(`Downloading FFPROBE`)
90+
reporter.info(`Downloading FFPROBE`)
8991
await execa(
9092
`curl`,
9193
[
@@ -97,11 +99,11 @@ export async function downloadLibs({ binariesDir, platform }) {
9799
execaConfig
98100
)
99101

100-
console.log(`Unzipping...`)
102+
reporter.info(`Unzipping...`)
101103
await execa(`unzip`, [`-o`, `ffmpeg.zip`], execaConfig)
102104
await execa(`unzip`, [`-o`, `ffprobe.zip`], execaConfig)
103105

104-
console.log(`Cleanup...`)
106+
reporter.info(`Cleanup...`)
105107
await execa(`rm`, [`ffmpeg.zip`, `ffprobe.zip`], execaConfig)
106108
break
107109
default:

packages/gatsby-transformer-video/src/ffmpeg.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import imageminMozjpeg from "imagemin-mozjpeg"
1111
import PQueue from "p-queue"
1212
import sharp from "sharp"
1313
import { createFileNodeFromBuffer } from "gatsby-source-filesystem"
14+
import reporter from "gatsby-cli/lib/reporter"
1415

1516
import { cacheContentfulVideo } from "./helpers"
1617

@@ -53,14 +54,17 @@ export default class FFMPEG {
5354
})
5455

5556
// Execute FFMMPEG and log progress
56-
executeFfmpeg = async ({ ffmpegSession, cachePath, loggingPrefix }) => {
57+
executeFfmpeg = async ({ ffmpegSession, cachePath }) => {
5758
let startTime
5859
let lastLoggedPercent = 0.1
5960

61+
const { name } = parse(cachePath)
62+
6063
return new Promise((resolve, reject) => {
6164
ffmpegSession
6265
.on(`start`, commandLine => {
63-
console.log(`${loggingPrefix} Executing:\n\n${commandLine}\n`)
66+
reporter.info(`${name} - converting`)
67+
reporter.verbose(`${name} - executing:\n\n${commandLine}\n`)
6468
startTime = performance.now()
6569
})
6670
.on(`progress`, progress => {
@@ -74,18 +78,19 @@ export default class FFMPEG {
7478
const loggedTimeLeft =
7579
estTimeLeft !== Infinity && ` (~${estTimeLeft}s)`
7680

77-
console.log(`${loggingPrefix} ${percent}%${loggedTimeLeft}`)
81+
reporter.info(`${name} - ${percent}%${loggedTimeLeft}`)
7882
lastLoggedPercent = progress.percent
7983
}
8084
})
8185
.on(`error`, (err, stdout, stderr) => {
82-
console.log(`\n---\n`, stdout, stderr, `\n---\n`)
83-
console.log(`${loggingPrefix} An error occurred:`)
86+
reporter.info(`\n---\n`, stdout, stderr, `\n---\n`)
87+
reporter.info(`${name} - An error occurred:`)
8488
console.error(err)
8589
reject(err)
8690
})
8791
.on(`end`, () => {
88-
console.log(`${loggingPrefix} 100%`)
92+
reporter.verbose(`${name} - 100%`)
93+
reporter.info(`${name} - converted`)
8994
resolve()
9095
})
9196
.save(cachePath)
@@ -129,11 +134,8 @@ export default class FFMPEG {
129134
}
130135

131136
// Queue video for conversion
132-
queueConvertVideo = async (...args) => {
133-
const videoData = await this.queue.add(() => this.convertVideo(...args))
134-
135-
return videoData
136-
}
137+
queueConvertVideo = async (...args) =>
138+
this.queue.add(() => this.convertVideo(...args))
137139

138140
// Converts a video based on a given profile, populates cache and public dir
139141
convertVideo = async ({
@@ -147,15 +149,14 @@ export default class FFMPEG {
147149
const alreadyExists = await pathExists(cachePath)
148150

149151
if (!alreadyExists) {
150-
const loggingPrefix = `[FFMPEG]`
151152
const ffmpegSession = ffmpeg().input(sourcePath)
152153
const filters = this.createFilters({ fieldArgs, info }).join(`,`)
153154
const videoStreamMetadata = this.parseVideoStream(info.streams)
154155

155156
profile({ ffmpegSession, filters, fieldArgs, videoStreamMetadata })
156157

157158
this.enhanceFfmpegForFilters({ ffmpegSession, fieldArgs })
158-
await this.executeFfmpeg({ ffmpegSession, cachePath, loggingPrefix })
159+
await this.executeFfmpeg({ ffmpegSession, cachePath })
159160
}
160161

161162
// If public file does not exist, copy cached file
@@ -176,6 +177,10 @@ export default class FFMPEG {
176177
return { publicPath }
177178
}
178179

180+
// Queue take screenshots
181+
queueTakeScreenshots = (...args) =>
182+
this.queue.add(() => this.takeScreenshots(...args))
183+
179184
takeScreenshots = async (
180185
video,
181186
fieldArgs,
@@ -224,10 +229,12 @@ export default class FFMPEG {
224229
ffmpeg(path)
225230
.on(`filenames`, function (filenames) {
226231
screenshotRawNames = filenames
227-
console.log(`[FFMPEG] Taking ${filenames.length} screenshots`)
232+
reporter.info(
233+
`${name} - Taking ${filenames.length} ${width}px screenshots`
234+
)
228235
})
229236
.on(`error`, (err, stdout, stderr) => {
230-
console.log(`[FFMPEG] Failed to take screenshots:`)
237+
reporter.info(`${name} - Failed to take ${width}px screenshots:`)
231238
console.error(err)
232239
reject(err)
233240
})
@@ -244,8 +251,6 @@ export default class FFMPEG {
244251

245252
const screenshotNodes = []
246253

247-
console.log({ screenshotRawNames, tmpDir })
248-
249254
for (const screenshotRawName of screenshotRawNames) {
250255
try {
251256
const rawScreenshotPath = resolve(tmpDir, screenshotRawName)
@@ -254,7 +259,7 @@ export default class FFMPEG {
254259
try {
255260
await access(rawScreenshotPath)
256261
} catch {
257-
console.warn(`Screenshot ${rawScreenshotPath} could not be found!`)
262+
reporter.warn(`Screenshot ${rawScreenshotPath} could not be found!`)
258263
continue
259264
}
260265

@@ -280,7 +285,7 @@ export default class FFMPEG {
280285

281286
screenshotNodes.push(node)
282287
} catch (err) {
283-
console.log(`Failed to take screenshots:`)
288+
reporter.info(`${name} - failed to take screenshots:`)
284289
console.error(err)
285290
throw err
286291
}

packages/gatsby-transformer-video/src/gatsby-node.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import os from "os"
33

44
import { ensureDir } from "fs-extra"
55
import { GraphQLString, GraphQLInt, GraphQLFloat } from "gatsby/graphql"
6+
import reporter from "gatsby-cli/lib/reporter"
67

78
import FFMPEG from "./ffmpeg"
89

@@ -303,14 +304,14 @@ exports.createResolvers = async (
303304

304305
exports.onPreInit = async ({ store }, { downloadBinaries = true }) => {
305306
if (!downloadBinaries) {
306-
console.log(`Skipped download of FFMPEG & FFPROBE binaries`)
307+
reporter.verbose(`Skipped download of FFMPEG & FFPROBE binaries`)
307308
return
308309
}
309310

310311
const alreadyInstalled = await libsInstalled({ platform })
311312

312313
if (alreadyInstalled) {
313-
console.log(`FFMPEG && FFPROBE are already available on this machine`)
314+
reporter.verbose(`FFMPEG && FFPROBE are already available on this machine`)
314315
return
315316
}
316317

@@ -322,14 +323,14 @@ exports.onPreInit = async ({ store }, { downloadBinaries = true }) => {
322323
try {
323324
await libsAlreadyDownloaded({ binariesDir })
324325

325-
console.log(`FFMPEG & FFPROBE binaries already downloaded`)
326+
reporter.verbose(`FFMPEG & FFPROBE binaries already downloaded`)
326327
} catch {
327328
try {
328-
console.log(`FFMPEG & FFPROBE getting binaries for ${platform}@${arch}`)
329+
reporter.info(`FFMPEG & FFPROBE getting binaries for ${platform}@${arch}`)
329330

330331
await downloadLibs({ binariesDir, platform })
331332

332-
console.log(
333+
reporter.info(
333334
`Finished. This system is ready to convert videos with GatsbyJS`
334335
)
335336
} catch (err) {

packages/gatsby-transformer-video/src/helpers.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { resolve } from "path"
44

55
import PQueue from "p-queue"
66
import axios from "axios"
7+
import reporter from "gatsby-cli/lib/reporter"
78

89
const downloadQueue = new PQueue({ concurrency: 3 })
910

@@ -26,14 +27,13 @@ export async function cacheContentfulVideo({ video, cacheDir }) {
2627

2728
try {
2829
await access(path, fs.constants.R_OK)
29-
30-
console.log(`Already downloaded ${url}`)
30+
reporter.verbose(`Already downloaded ${url}`)
3131
downloadCache[url] = path
3232

3333
return downloadCache[url]
3434
} catch {
3535
if (url in downloadCache) {
36-
console.log(`Already downloading ${url}`)
36+
// Already downloading
3737
return downloadCache[url]
3838
}
3939

@@ -44,16 +44,14 @@ export async function cacheContentfulVideo({ video, cacheDir }) {
4444
while (!downloaded) {
4545
try {
4646
await downloadQueue.add(async () => {
47-
console.log(`Downloading https:${url}`)
47+
reporter.info(`Downloading ${url}`)
4848

4949
const response = await axios({
5050
method: `get`,
5151
url: `https:${url}`,
5252
responseType: `stream`,
5353
})
5454

55-
console.log(`Writing ${fileName} to disk`)
56-
5755
await new Promise((resolve, reject) => {
5856
const file = fs.createWriteStream(path)
5957

@@ -69,18 +67,20 @@ export async function cacheContentfulVideo({ video, cacheDir }) {
6967

7068
if (tries === 3) {
7169
throw new Error(
72-
`Download of https:${url} failed after three times:\n\n${e}`
70+
`Download of ${url} failed after three times:\n\n${e}`
7371
)
7472
}
75-
console.log(
76-
`Unable to download https:${url}\n\nRetrying again after 1s (${tries}/3)`
73+
reporter.info(
74+
`Unable to download ${url}\n\nRetrying again after 1s (${tries}/3)`
7775
)
7876
console.error(e)
7977
console.log(Object.keys(e), e.message)
8078
await new Promise(resolve => setTimeout(resolve, 1000))
8179
}
8280
}
8381

82+
reporter.info(`Downloaded: ${url}`)
83+
8484
return path
8585
}
8686

0 commit comments

Comments
 (0)