Skip to content

Commit 7878d0f

Browse files
authored
fix(gatsby): update script to generate apis.json to accomodate Typescript (#24023)
* Fix script to generate apis.json * add snapshot test * fix tests
1 parent 276b347 commit 7878d0f

File tree

2 files changed

+103
-29
lines changed

2 files changed

+103
-29
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import fs from "fs-extra"
2+
import childProcess from "child_process"
3+
import systemPath from "path"
4+
5+
const apiPath = systemPath.join(__dirname, "../../apis.json")
6+
7+
it("generates the expected api output", done => {
8+
childProcess.exec("node ../output-api-file.js", async () => {
9+
const json = await fs.readJSON(apiPath)
10+
11+
expect(json).toMatchInlineSnapshot(`
12+
Object {
13+
"browser": Object {
14+
"disableCorePrefetching": Object {},
15+
"onClientEntry": Object {},
16+
"onInitialClientRender": Object {},
17+
"onPostPrefetchPathname": Object {},
18+
"onPreRouteUpdate": Object {},
19+
"onPrefetchPathname": Object {},
20+
"onRouteUpdate": Object {},
21+
"onRouteUpdateDelayed": Object {},
22+
"onServiceWorkerActive": Object {},
23+
"onServiceWorkerInstalled": Object {},
24+
"onServiceWorkerRedundant": Object {},
25+
"onServiceWorkerUpdateFound": Object {},
26+
"onServiceWorkerUpdateReady": Object {},
27+
"registerServiceWorker": Object {},
28+
"replaceComponentRenderer": Object {
29+
"deprecated": true,
30+
},
31+
"replaceHydrateFunction": Object {},
32+
"shouldUpdateScroll": Object {},
33+
"wrapPageElement": Object {},
34+
"wrapRootElement": Object {},
35+
},
36+
"node": Object {
37+
"createPages": Object {},
38+
"createPagesStatefully": Object {},
39+
"createResolvers": Object {
40+
"version": "2.2.0",
41+
},
42+
"createSchemaCustomization": Object {
43+
"version": "2.12.0",
44+
},
45+
"generateSideEffects": Object {},
46+
"onCreateBabelConfig": Object {},
47+
"onCreateDevServer": Object {},
48+
"onCreateNode": Object {},
49+
"onCreatePage": Object {},
50+
"onCreateWebpackConfig": Object {},
51+
"onPostBootstrap": Object {},
52+
"onPostBuild": Object {},
53+
"onPreBootstrap": Object {},
54+
"onPreBuild": Object {},
55+
"onPreExtractQueries": Object {},
56+
"onPreInit": Object {},
57+
"preprocessSource": Object {},
58+
"resolvableExtensions": Object {},
59+
"setFieldsOnGraphQLNodeType": Object {},
60+
"sourceNodes": Object {},
61+
},
62+
"ssr": Object {
63+
"onPreRenderHTML": Object {},
64+
"onRenderBody": Object {},
65+
"replaceRenderer": Object {},
66+
"wrapPageElement": Object {},
67+
"wrapRootElement": Object {},
68+
},
69+
}
70+
`)
71+
done()
72+
})
73+
})
Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
1-
const path = require('path')
2-
const documentation = require('documentation')
3-
const fs = require('fs-extra')
1+
const path = require("path")
2+
const documentation = require("documentation")
3+
const fs = require("fs-extra")
44

55
const OUTPUT_FILE_NAME = `apis.json`
66

77
async function outputFile() {
8-
const apis = await Promise.all([
9-
path.join('cache-dir', 'api-ssr-docs.js'),
10-
path.join('src', 'utils', 'api-browser-docs.ts'),
11-
path.join('src', 'utils', 'api-node-docs.ts')
12-
]
13-
.map(filePath => {
8+
const apis = await Promise.all(
9+
[
10+
path.join("cache-dir", "api-ssr-docs.js"),
11+
path.join("src", "utils", "api-browser-docs.ts"),
12+
path.join("src", "utils", "api-node-docs.ts")
13+
].map(filePath => {
1414
const resolved = path.resolve(filePath)
15-
const [,api] = path.basename(filePath).split('-')
16-
return documentation.build(resolved, {
17-
shallow: true
18-
})
15+
const [, api] = path.basename(filePath).split("-")
16+
return documentation
17+
.build(resolved, {
18+
shallow: true
19+
})
1920
.then(contents => {
20-
return [
21-
contents,
22-
api
23-
]
21+
return [contents, api]
2422
})
2523
})
2624
)
2725

2826
const output = apis.reduce((merged, [output, api]) => {
29-
merged[api] = output.reduce((mergedOutput, doc) => {
30-
const isAPI = doc.namespace.startsWith('.')
31-
if (isAPI) {
32-
const tags = doc.tags.reduce((mergedTags, tag) => {
33-
mergedTags[tag.title] = tag.description
34-
return mergedTags
35-
}, {})
36-
mergedOutput[doc.name] = {
37-
deprecated: !!tags.deprecated || undefined,
38-
version: tags.gatsbyVersion
39-
}
27+
merged[api] = output.reduce((mergedOutput, doc, i) => {
28+
if (doc.kind === "typedef") return mergedOutput
29+
30+
const tags = doc.tags.reduce((mergedTags, tag) => {
31+
mergedTags[tag.title] = tag.description
32+
return mergedTags
33+
}, {})
34+
mergedOutput[doc.name] = {
35+
deprecated: !!tags.deprecated || undefined,
36+
version: tags.gatsbyVersion
4037
}
4138
return mergedOutput
4239
}, {})
4340
return merged
4441
}, {})
4542

46-
return fs.writeFile(path.resolve(OUTPUT_FILE_NAME), JSON.stringify(output, null, 2), 'utf8')
43+
return fs.writeFile(
44+
path.resolve(OUTPUT_FILE_NAME),
45+
JSON.stringify(output, null, 2),
46+
"utf8"
47+
)
4748
}
4849

4950
outputFile()

0 commit comments

Comments
 (0)