Skip to content

Commit

Permalink
chore(gatsby): Migrate utils/get-latest-apis.js to ts (#22097)
Browse files Browse the repository at this point in the history
* migrate get-lates-api to ts

* modify require statements

* use pathExistsSync

* update interface of api response

* Update packages/gatsby/src/utils/get-latest-apis.ts

Co-Authored-By: Michaël De Boey <info@michaeldeboey.be>

* use fs.exists

* fix a type issue

* fix lint

* fix lint

* fix tests

Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
Co-authored-by: Blaine Kasten <blainekasten@gmail.com>
  • Loading branch information
3 people authored May 21, 2020
1 parent cb5d055 commit b81b604
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
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`)

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`)

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 */
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

0 comments on commit b81b604

Please sign in to comment.