Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions packages/gatsby/src/utils/__tests__/get-latest-gatsby-files.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const mockFiles = new Map<string, string>()

/* eslint-disable @typescript-eslint/no-var-requires */
jest.mock(`fs-extra`, () => {
return {
readJSON: jest.fn(),
writeFile: jest.fn(),
pathExists: jest.fn(),
readJSON: jest.fn().mockImplementation(async filePath => {
const content = mockFiles.get(filePath)

if (content) {
return JSON.parse(content)
}
throw new Error(`File not found`)
}),
writeFile: jest.fn().mockImplementation(async (filePath, content) => {
mockFiles.set(filePath, content)
}),
pathExists: jest
.fn()
.mockImplementation(async filePath => mockFiles.has(filePath)),
}
})
jest.mock(`axios`, () => {
Expand All @@ -19,7 +32,7 @@ import { getLatestAPIs, IAPIResponse } from "../get-latest-gatsby-files"

beforeEach(() => {
;[fs, axios].forEach(mock =>
Object.keys(mock).forEach(key => mock[key].mockReset())
Object.keys(mock).forEach(key => mock[key].mockClear())
)
})

Expand All @@ -33,7 +46,6 @@ const getMockAPIFile = (): IAPIResponse => {

describe(`default behavior: has network connectivity`, () => {
beforeEach(() => {
fs.pathExists.mockResolvedValueOnce(false)
axios.get.mockResolvedValueOnce({ data: getMockAPIFile() })
})

Expand Down Expand Up @@ -65,8 +77,6 @@ describe(`downloading APIs failure`, () => {

it(`falls back to downloaded cached file, if it exists`, async () => {
const apis = getMockAPIFile()
fs.pathExists.mockResolvedValueOnce(true)
fs.readJSON.mockResolvedValueOnce(apis)

const data = await getLatestAPIs()

Expand All @@ -78,9 +88,8 @@ describe(`downloading APIs failure`, () => {
})

it(`falls back to local apis.json if latest-apis.json not cached`, async () => {
const apis = getMockAPIFile()
fs.pathExists.mockResolvedValueOnce(false)
fs.readJSON.mockResolvedValueOnce(apis)
mockFiles.clear()
getMockAPIFile()

await getLatestAPIs()

Expand Down
34 changes: 20 additions & 14 deletions packages/gatsby/src/utils/get-latest-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,36 @@ const _getFile = async <T>({
outputFileName: string
defaultReturn: T
}): Promise<T> => {
let fileToUse = path.join(ROOT, fileName)
try {
const { data } = await axios.get(`${UNPKG_ROOT}${fileName}`, {
timeout: 5000,
})

await fs.writeFile(outputFileName, JSON.stringify(data, null, 2), `utf8`)
await fs.writeFile(
outputFileName,
typeof data === `string` ? data : JSON.stringify(data, null, 2),
`utf8`
)

return data
fileToUse = outputFileName
} catch (e) {
// if file was previously cached, use it
if (await fs.pathExists(outputFileName)) {
return fs.readJSON(outputFileName)
fileToUse = outputFileName
}
}

if (fileName.endsWith(`.json`)) {
return fs.readJSON(path.join(ROOT, fileName)).catch(() => defaultReturn)
} else {
try {
const importedFile = await import(path.join(ROOT, fileName))
const adapters = preferDefault(importedFile)
return adapters
} catch (e) {
// no-op
return defaultReturn
}
if (fileToUse.endsWith(`.json`)) {
return fs.readJSON(fileToUse).catch(() => defaultReturn)
} else {
try {
const importedFile = await import(fileToUse)
const adapters = preferDefault(importedFile)
return adapters
} catch (e) {
// no-op
return defaultReturn
}
}
}
Expand Down