From 7b12c962fc4011d4db70247ad05abcc9e680ff82 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 20 Aug 2021 14:31:58 +0200 Subject: [PATCH] fix enoent --- package.json | 3 +- packages/gatsby-core-utils/package.json | 2 +- .../src/fetch-remote-file.ts | 28 +++++++-- .../src/__tests__/fetch-contentful-asset.js | 21 +++---- .../gatsby-source-filesystem/package.json | 2 +- .../__tests__/create-file-node-from-buffer.js | 2 + .../src/__tests__/create-remote-file-node.js | 6 +- .../src/create-remote-file-node.js | 3 +- yarn.lock | 60 +------------------ 9 files changed, 48 insertions(+), 79 deletions(-) diff --git a/package.json b/package.json index 9b154d697ffd8..00f25b4bd635f 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,8 @@ "private": true, "resolutions": { "theme-ui": "0.4.0-rc.14", - "csstype": "2.6.17" + "csstype": "2.6.17", + "fs-extra": "9.1.0" }, "scripts": { "bootstrap": "npm-run-all -s check-versions \"lerna-prepare -- --{@}\" --", diff --git a/packages/gatsby-core-utils/package.json b/packages/gatsby-core-utils/package.json index 9111219f7e7fc..dbc33656eee8e 100644 --- a/packages/gatsby-core-utils/package.json +++ b/packages/gatsby-core-utils/package.json @@ -32,7 +32,7 @@ "ci-info": "2.0.0", "configstore": "^5.0.1", "file-type": "^16.5.3", - "fs-extra": "^8.1.0", + "fs-extra": "^9.1.0", "node-object-hash": "^2.3.8", "proper-lockfile": "^4.1.2", "tmp": "^0.2.1", diff --git a/packages/gatsby-core-utils/src/fetch-remote-file.ts b/packages/gatsby-core-utils/src/fetch-remote-file.ts index 5ea09bf6e6876..598b536927382 100644 --- a/packages/gatsby-core-utils/src/fetch-remote-file.ts +++ b/packages/gatsby-core-utils/src/fetch-remote-file.ts @@ -70,7 +70,13 @@ const requestRemoteNode = ( // Called if we stall for 30s without receiving any data const handleTimeout = async (): Promise => { fsWriteStream.close() - fs.removeSync(tmpFilename) + + try { + await fs.remove(tmpFilename) + } catch (err) { + // ignore error + } + if (attempt < STALL_RETRY_LIMIT) { // Retry by calling ourself recursively resolve( @@ -109,11 +115,19 @@ const requestRemoteNode = ( responseStream.pipe(fsWriteStream) // If there's a 400/500 response or other error. - responseStream.on(`error`, error => { + responseStream.on(`error`, async error => { if (timeout) { clearTimeout(timeout) } - fs.removeSync(tmpFilename) + + // close the stream + fsWriteStream.close() + + try { + await fs.remove(tmpFilename) + } catch (err) { + // ignore error + } reject(error) }) @@ -127,12 +141,16 @@ const requestRemoteNode = ( responseStream.on(`response`, response => { resetTimeout() - fsWriteStream.on(`finish`, () => { + fsWriteStream.on(`finish`, async () => { fsWriteStream.close() // We have an incomplete download if (!haveAllBytesBeenWritten) { - fs.removeSync(tmpFilename) + try { + await fs.remove(tmpFilename) + } catch (err) { + // ignore error + } if (attempt < INCOMPLETE_RETRY_LIMIT) { resolve( diff --git a/packages/gatsby-source-contentful/src/__tests__/fetch-contentful-asset.js b/packages/gatsby-source-contentful/src/__tests__/fetch-contentful-asset.js index f91da840a0e0d..f520989ff4d31 100644 --- a/packages/gatsby-source-contentful/src/__tests__/fetch-contentful-asset.js +++ b/packages/gatsby-source-contentful/src/__tests__/fetch-contentful-asset.js @@ -3,7 +3,7 @@ */ import os from "os" -import fs from "fs" +import fs from "fs-extra" import path from "path" import nock from "nock" @@ -32,13 +32,14 @@ let cache const host = `https://images.ctfassets.net` describe(`fetch-contentful-assets`, () => { - beforeAll(() => { + beforeAll(async () => { cache = createMockCache() + await fs.ensureDir(cache.directory) }) - afterAll(() => { + afterAll(async () => { if (cache) { - fs.removeSync(cache.directory) + await fs.remove(cache.directory) } }) @@ -47,7 +48,7 @@ describe(`fetch-contentful-assets`, () => { reporter.verbose.mockClear() }) - test(`resolves regular response`, async () => { + it(`resolves regular response`, async () => { const path = `/resolves.jpg` const url = [host, path].join(``) const scope = nock(host).get(path).reply(200) @@ -58,7 +59,7 @@ describe(`fetch-contentful-assets`, () => { expect(scope.isDone()).toBeTruthy() }) - test(`returns from cache on second call`, async () => { + it(`returns from cache on second call`, async () => { const path = `/resolves-from-cache.jpg` const url = [host, path].join(``) const scope = nock(host).get(path).twice().reply(200) @@ -73,7 +74,7 @@ describe(`fetch-contentful-assets`, () => { ]) }) - test(`does not retry for no reason`, async () => { + it(`does not retry for no reason`, async () => { const path = `/no-retry.jpg` const url = [host, path].join(``) const scope = nock(host).get(path).twice().reply(200) @@ -84,7 +85,7 @@ describe(`fetch-contentful-assets`, () => { expect(scope.isDone()).toBeFalsy() }) - test(`does not retry on 404`, async () => { + it(`does not retry on 404`, async () => { const path = `/no-retry-on-404.jpg` const url = [host, path].join(``) const scope = nock(host).get(path).twice().reply(404) @@ -102,7 +103,7 @@ describe(`fetch-contentful-assets`, () => { ]) }) - test(`does retry on 503`, async () => { + it(`does retry on 503`, async () => { const path = `/retry-on-503.jpg` const url = [host, path].join(``) @@ -121,7 +122,7 @@ describe(`fetch-contentful-assets`, () => { expect(scope.isDone()).toBeTruthy() }) - test(`stops retry after 3 attempts`, async () => { + it(`stops retry after 3 attempts`, async () => { const path = `/stop-retry-after-3-attempts.jpg` const url = [host, path].join(``) diff --git a/packages/gatsby-source-filesystem/package.json b/packages/gatsby-source-filesystem/package.json index 04aa230ce4d80..fd259d5952668 100644 --- a/packages/gatsby-source-filesystem/package.json +++ b/packages/gatsby-source-filesystem/package.json @@ -11,7 +11,7 @@ "better-queue": "^3.8.10", "chokidar": "^3.4.3", "file-type": "^16.0.0", - "fs-extra": "^8.1.0", + "fs-extra": "^9.1.0", "gatsby-core-utils": "^2.13.0-next.0", "got": "^9.6.0", "md5-file": "^5.0.0", diff --git a/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js b/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js index e2326e61fa1b7..944643ac01068 100644 --- a/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js +++ b/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js @@ -159,6 +159,7 @@ describe(`create-file-node-from-buffer`, () => { expect(() => { createFileNodeFromBuffer({ ...defaultArgs, + buffer: createMockBuffer(`test`), getCache: () => createMockCache(), }) }).not.toThrow() @@ -168,6 +169,7 @@ describe(`create-file-node-from-buffer`, () => { expect(() => { createFileNodeFromBuffer({ ...defaultArgs, + buffer: createMockBuffer(`test`), cache: createMockCache(), }) }).not.toThrow() diff --git a/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js b/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js index 3b79ee5a00881..c09332b80bc9b 100644 --- a/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js +++ b/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js @@ -27,7 +27,7 @@ jest.mock(`../create-file-node`, () => { createFileNode: jest.fn(), } }) -const reporter = require(`gatsby/reporter`) +const reporter = {} const got = require(`got`) const createRemoteFileNode = require(`../create-remote-file-node`) @@ -285,6 +285,8 @@ describe(`create-remote-file-node`, () => { createRemoteFileNode({ ...defaultArgs, getCache: () => createMockCache(), + }).catch(err => { + // ignore the url error }) }).not.toThrow() }) @@ -294,6 +296,8 @@ describe(`create-remote-file-node`, () => { createRemoteFileNode({ ...defaultArgs, cache: createMockCache(), + }).catch(err => { + // ignore the url error }) }).not.toThrow() }) diff --git a/packages/gatsby-source-filesystem/src/create-remote-file-node.js b/packages/gatsby-source-filesystem/src/create-remote-file-node.js index aca385ae98c05..3332b0827a05c 100644 --- a/packages/gatsby-source-filesystem/src/create-remote-file-node.js +++ b/packages/gatsby-source-filesystem/src/create-remote-file-node.js @@ -1,9 +1,8 @@ const fs = require(`fs-extra`) -const { createContentDigest } = require(`gatsby-core-utils`) +const { createContentDigest, fetchRemoteFile } = require(`gatsby-core-utils`) const path = require(`path`) const { isWebUri } = require(`valid-url`) const Queue = require(`better-queue`) -const { fetchRemoteFile } = require(`gatsby-core-utils`) const { createFileNode } = require(`./create-file-node`) const { getRemoteFileExtension, createFilePath } = require(`./utils`) diff --git a/yarn.lock b/yarn.lock index 567583a0bdcc0..2447f1ca4172c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12688,51 +12688,7 @@ fs-exists-sync@^0.1.0: resolved "https://registry.yarnpkg.com/fs-exists-sync/-/fs-exists-sync-0.1.0.tgz#982d6893af918e72d08dec9e8673ff2b5a8d6add" integrity sha1-mC1ok6+RjnLQjeyehnP/K1qNat0= -fs-extra@6.0.0: - version "6.0.0" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.0.tgz#0f0afb290bb3deb87978da816fcd3c7797f3a817" - integrity sha512-lk2cUCo8QzbiEWEbt7Cw3m27WMiRG321xsssbcIpfMhpRjrlC08WBOVQqj1/nQYYNnPtyIhP1oqLO3QwT2tPCw== - dependencies: - graceful-fs "^4.1.2" - jsonfile "^4.0.0" - universalify "^0.1.0" - -fs-extra@8.1.0, fs-extra@^8.1.0: - version "8.1.0" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0" - integrity sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g== - dependencies: - graceful-fs "^4.2.0" - jsonfile "^4.0.0" - universalify "^0.1.0" - -fs-extra@9.0.1: - version "9.0.1" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc" - integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ== - dependencies: - at-least-node "^1.0.0" - graceful-fs "^4.2.0" - jsonfile "^6.0.1" - universalify "^1.0.0" - -fs-extra@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-1.0.0.tgz#cd3ce5f7e7cb6145883fcae3191e9877f8587950" - dependencies: - graceful-fs "^4.1.2" - jsonfile "^2.1.0" - klaw "^1.0.0" - -fs-extra@^4.0.2: - version "4.0.3" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94" - dependencies: - graceful-fs "^4.1.2" - jsonfile "^4.0.0" - universalify "^0.1.0" - -fs-extra@^9.0.1, fs-extra@^9.1.0: +fs-extra@6.0.0, fs-extra@8.1.0, fs-extra@9.0.1, fs-extra@9.1.0, fs-extra@^1.0.0, fs-extra@^4.0.2, fs-extra@^8.1.0, fs-extra@^9.0.1, fs-extra@^9.1.0: version "9.1.0" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d" integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ== @@ -13497,7 +13453,7 @@ got@^9.6.0: to-readable-stream "^1.0.0" url-parse-lax "^3.0.0" -graceful-fs@^4.0.0, graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9, graceful-fs@^4.2.0, graceful-fs@^4.2.2, graceful-fs@^4.2.3, graceful-fs@^4.2.4: +graceful-fs@^4.0.0, graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.2, graceful-fs@^4.2.3, graceful-fs@^4.2.4: version "4.2.4" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb" integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw== @@ -17011,12 +16967,6 @@ json5@^2.0.0, json5@^2.1.0, json5@^2.1.2, json5@^2.1.3: dependencies: minimist "^1.2.5" -jsonfile@^2.1.0: - version "2.4.0" - resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8" - optionalDependencies: - graceful-fs "^4.1.6" - jsonfile@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb" @@ -17173,12 +17123,6 @@ kind-of@^6.0.0, kind-of@^6.0.2, kind-of@^6.0.3: resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd" integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw== -klaw@^1.0.0: - version "1.3.1" - resolved "https://registry.yarnpkg.com/klaw/-/klaw-1.3.1.tgz#4088433b46b3b1ba259d78785d8e96f73ba02439" - optionalDependencies: - graceful-fs "^4.1.9" - kleur@4.1.4, kleur@^4.1.3: version "4.1.4" resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.4.tgz#8c202987d7e577766d039a8cd461934c01cda04d"