Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): Migrate utils/get-latest-apis.js to ts #22097

Merged
merged 11 commits into from
May 21, 2020
2 changes: 1 addition & 1 deletion packages/gatsby/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
try {
const getLatestAPIs = require('../dist/utils/get-latest-apis')
const { getLatestAPIs } = require('../dist/utils/get-latest-apis')
getLatestAPIs()
} catch (e) {
// we're probably just bootstrapping and not published yet!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
handleMultipleReplaceRenderers,
warnOnIncompatiblePeerDependency,
} = require(`../validate`)
const getLatestAPIs = require(`../../../utils/get-latest-apis`)
const { getLatestAPIs } = require(`../../../utils/get-latest-apis`)
blainekasten marked this conversation as resolved.
Show resolved Hide resolved

beforeEach(() => {
Object.keys(reporter).forEach(key => reporter[key].mockReset())
Expand Down Expand Up @@ -68,7 +68,7 @@ describe(`collatePluginAPIs`, () => {
},
]

let result = collatePluginAPIs({ currentAPIs: apis, flattenedPlugins })
const result = collatePluginAPIs({ currentAPIs: apis, flattenedPlugins })
expect(result).toMatchSnapshot()
})

Expand All @@ -95,7 +95,7 @@ describe(`collatePluginAPIs`, () => {
},
]

let result = collatePluginAPIs({ currentAPIs: apis, flattenedPlugins })
const result = collatePluginAPIs({ currentAPIs: apis, flattenedPlugins })
expect(result).toMatchSnapshot()
})
})
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/bootstrap/load-plugins/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const stringSimilarity = require(`string-similarity`)
const { version: gatsbyVersion } = require(`gatsby/package.json`)
const reporter = require(`gatsby-cli/lib/reporter`)
const resolveModuleExports = require(`../resolve-module-exports`)
const getLatestAPIs = require(`../../utils/get-latest-apis`)
const { getLatestAPIs } = require(`../../utils/get-latest-apis`)
blainekasten marked this conversation as resolved.
Show resolved Hide resolved

const getGatsbyUpgradeVersion = entries =>
entries.reduce((version, entry) => {
Expand Down Expand Up @@ -42,8 +42,8 @@ const getErrorContext = (badExports, exportType, currentAPIs, latestAPIs) => {
})

const gatsbyUpgradeVersion = getGatsbyUpgradeVersion(entries)
let errors = []
let fixes = [].concat(
const errors = []
const fixes = [].concat(
gatsbyUpgradeVersion ? [`npm install gatsby@^${gatsbyUpgradeVersion}`] : []
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
/* eslint-disable @typescript-eslint/no-var-requires */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to use require in order mock objects to be inferred as any. I came up with using as jest.Mock<> as an alternative but I guess this is a bit redundant and no different from using any.

jest.mock(`fs-extra`, () => {
return {
exists: jest.fn(),
readJSON: jest.fn(),
writeFile: jest.fn(),
pathExists: jest.fn(),
}
})
jest.mock(`axios`, () => {
return {
get: jest.fn(),
}
})

const path = require(`path`)
const fs = require(`fs-extra`)
const axios = require(`axios`)
const getLatestAPIs = require(`../get-latest-apis`)
import { getLatestAPIs, IAPIResponse } from "../get-latest-apis"

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

const getMockAPIFile = () => {
const getMockAPIFile = (): IAPIResponse => {
return {
node: {},
browser: {},
Expand All @@ -31,7 +33,7 @@ const getMockAPIFile = () => {

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

Expand Down Expand Up @@ -63,7 +65,7 @@ describe(`downloading APIs failure`, () => {

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

const data = await getLatestAPIs()
Expand All @@ -77,7 +79,7 @@ describe(`downloading APIs failure`, () => {

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

await getLatestAPIs()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const path = require(`path`)
const fs = require(`fs-extra`)
const axios = require(`axios`)
import path from "path"
import fs from "fs-extra"
import axios from "axios"

const API_FILE = `https://unpkg.com/gatsby/apis.json`
const ROOT = path.join(__dirname, `..`, `..`)
const OUTPUT_FILE = path.join(ROOT, `latest-apis.json`)

module.exports = async function getLatestAPI() {
export interface IAPIResponse {
browser: Record<string, any>
node: Record<string, any>
ssr: Record<string, any>
}

export const getLatestAPIs = async (): Promise<IAPIResponse> => {
try {
const { data } = await axios.get(API_FILE, { timeout: 5000 })

await fs.writeFile(OUTPUT_FILE, JSON.stringify(data, null, 2), `utf8`)

return data
} catch (e) {
if (await fs.exists(OUTPUT_FILE)) {
if (await fs.pathExists(OUTPUT_FILE)) {
return fs.readJSON(OUTPUT_FILE)
}
// possible offline/network issue
Expand Down