Skip to content

Commit

Permalink
Update gatsby-transformer-sharp, replace request (#740)
Browse files Browse the repository at this point in the history
We've been using request without actually declaring it as a dependency. It has
been deprecated in the meantime. The latest update of gatsby-transformer-sharp
does not come with request anymore, so I decided to replace it with node-fetch.

Supersedes #728.
  • Loading branch information
aahlenst authored Jan 22, 2021
1 parent d67487d commit 3791d9e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 253 deletions.
22 changes: 14 additions & 8 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const path = require("path");
const fs = require("fs");
const request = require("request");
const fetch = require("node-fetch");

const { pipeline } = require("stream");
const { promisify } = require("util");
const { createFilePath } = require("gatsby-source-filesystem");

exports.createPages = async ({ graphql, actions }) => {
Expand All @@ -11,13 +13,17 @@ exports.createPages = async ({ graphql, actions }) => {
const authorPage = path.resolve("./src/templates/author-page.js");

for (let author of Object.keys(authorJson)) {

fs.exists(`content/assets/authors/${author}.jpg`, function(exists) {
if (! exists) {
let githubUsername = authorJson[author].github;
request(`https://github.com/${githubUsername}.png?size=250`).pipe(fs.createWriteStream(`content/assets/authors/${author}.jpg`));
}
});
try {
await fs.promises.access(`content/assets/authors/${author}.jpg`);
} catch (error) {
const githubUsername = authorJson[author].github;
const streamPipeline = promisify(pipeline);
const response = await fetch(`https://github.com/${githubUsername}.png?size=250`);
if (!response.ok) {
throw new Error(`Unexpected response: ${response.statusText}`);
}
await streamPipeline(response.body, fs.createWriteStream(`content/assets/authors/${author}.jpg`));
}

createPage({
path: `/author/${author}`,
Expand Down
Loading

0 comments on commit 3791d9e

Please sign in to comment.