Skip to content

Commit 77fc5b5

Browse files
authored
fix(gatsby-source-wordpress): generate plugin options docs (#32856)
1 parent c991ab4 commit 77fc5b5

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { getPluginOptionsMdString } from "../generate-plugin-options-docs"
2+
import fs from "fs-extra"
3+
import path from "path"
4+
5+
describe(`Plugin options docs`, () => {
6+
test(`Docs have been generated by running "yarn build" or "yarn generate-plugin-options-docs"`, async () => {
7+
const generatedDocsFileContents = await fs.readFile(
8+
path.join(__dirname, `../docs/plugin-options.md`),
9+
`utf-8`
10+
)
11+
12+
const mdString = await getPluginOptionsMdString()
13+
14+
expect(generatedDocsFileContents).toBeTruthy()
15+
expect(mdString).toBeTruthy()
16+
17+
expect(generatedDocsFileContents).toEqual(mdString)
18+
})
19+
})

packages/gatsby-source-wordpress/generate-plugin-options-docs.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ const prettierConfig = require(`../../.prettierrc.js`)
88

99
const {
1010
pluginOptionsSchema,
11-
} = require(`./dist/steps/declare-plugin-options-schema`)
11+
} = require(`./src/steps/declare-plugin-options-schema`)
12+
13+
/**
14+
* This script generates ./docs/plugin-options.md from the plugin options schema.
15+
* ./docs/plugin-options.md should never be edited directly since it's auto-generated. Update ./src/steps/declare-plugin-options-schema.ts instead
16+
*/
1217

1318
// :( poor children
1419
const excludeParentsChildren = [`RootQuery`]
@@ -135,7 +140,7 @@ function joiKeysToMD({
135140
*
136141
* @param {object} description
137142
*/
138-
async function generateMdFileFromSchemaDescription(description) {
143+
async function generateMdStringFromSchemaDescription(description) {
139144
const template = Handlebars.compile(`# Plugin Options
140145
141146
[comment]: # (This file is automatically generated. Do not edit it directly. Instead, edit the Joi schema in ./plugin/src/steps/declare-plugin-options-schema.js)
@@ -162,14 +167,28 @@ async function generateMdFileFromSchemaDescription(description) {
162167
docs,
163168
})
164169

165-
const mdContentsFormatted = prettier.format(mdContents, {
170+
const mdStringFormatted = prettier.format(mdContents, {
166171
parser: `markdown`,
167172
...prettierConfig,
168173
})
169174

170-
await fs.writeFile(`./docs/plugin-options.md`, mdContentsFormatted)
175+
return mdStringFormatted
171176
}
172177

173-
const description = pluginOptionsSchema({ Joi }).describe()
178+
async function getPluginOptionsMdString() {
179+
const description = pluginOptionsSchema({ Joi }).describe()
180+
const mdString = generateMdStringFromSchemaDescription(description)
181+
return mdString
182+
}
183+
184+
async function writePluginOptionsMdFile() {
185+
console.info(`writing out plugin options schema docs to plugin-options.md`)
186+
const mdString = await getPluginOptionsMdString()
187+
await fs.writeFile(`./docs/plugin-options.md`, mdString)
188+
}
189+
190+
if (process.env.NODE_ENV !== `test`) {
191+
writePluginOptionsMdFile()
192+
}
174193

175-
generateMdFileFromSchemaDescription(description)
194+
module.exports = { getPluginOptionsMdString }

packages/gatsby-source-wordpress/src/steps/declare-plugin-options-schema.ts renamed to packages/gatsby-source-wordpress/src/steps/declare-plugin-options-schema.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Step } from "./../utils/run-steps"
2-
import prettier from "prettier"
1+
/**
2+
* This file is intentionally not TS so it can be run in a yarn script without being transpiled.
3+
*/
4+
const prettier = require(`prettier`)
35

4-
const wrapOptions = (innerOptions): string =>
6+
const wrapOptions = innerOptions =>
57
prettier
68
.format(
79
`const something = {
@@ -14,8 +16,8 @@ const wrapOptions = (innerOptions): string =>
1416
.replace(`const something = `, ``)
1517
.replace(`;`, ``)
1618

17-
export const pluginOptionsSchema: Step = ({ Joi }) => {
18-
const getTypeOptions = (): any =>
19+
const pluginOptionsSchema = ({ Joi }) => {
20+
const getTypeOptions = () =>
1921
Joi.object({
2022
where: Joi.string()
2123
.allow(null)
@@ -71,7 +73,7 @@ export const pluginOptionsSchema: Step = ({ Joi }) => {
7173
.allow(null)
7274
.allow(false)
7375
.description(
74-
`Determines wether or not this type will be treated as an interface comprised entirely of other Gatsby node types.`
76+
`Determines whether or not this type will be treated as an interface comprised entirely of other Gatsby node types.`
7577
)
7678
.meta({
7779
example: wrapOptions(`
@@ -156,7 +158,7 @@ export const pluginOptionsSchema: Step = ({ Joi }) => {
156158
showQueryVarsOnError: Joi.boolean()
157159
.default(false)
158160
.description(
159-
`When a GraphQL error is returned and the process exits, this plugin option determines wether or not to log out the query vars that were used in the query that returned GraphQL errors.`
161+
`When a GraphQL error is returned and the process exits, this plugin option determines whether or not to log out the query vars that were used in the query that returned GraphQL errors.`
160162
)
161163
.meta({
162164
example: wrapOptions(`
@@ -198,7 +200,7 @@ export const pluginOptionsSchema: Step = ({ Joi }) => {
198200
panicOnError: Joi.boolean()
199201
.default(false)
200202
.description(
201-
`Determines wether or not to panic when any GraphQL error is returned.
203+
`Determines whether or not to panic when any GraphQL error is returned.
202204
203205
Default is false because sometimes non-critical errors are returned alongside valid data.`
204206
)
@@ -214,7 +216,7 @@ Default is false because sometimes non-critical errors are returned alongside va
214216
onlyReportCriticalErrors: Joi.boolean()
215217
.default(true)
216218
.description(
217-
`Determines wether or not to log non-critical errors. A non-critical error is any error which is returned alongside valid data. In previous versions of WPGraphQL this was very noisy because trying to access an entity that was private returned errors.`
219+
`Determines whether or not to log non-critical errors. A non-critical error is any error which is returned alongside valid data. In previous versions of WPGraphQL this was very noisy because trying to access an entity that was private returned errors.`
218220
)
219221
.meta({
220222
example: wrapOptions(`
@@ -828,7 +830,7 @@ This should be the full url of your GraphQL endpoint.`
828830
}),
829831
useIf: Joi.any()
830832
.description(
831-
`A function used to determine wether or not to apply this plugin options preset. It should return a boolean value. True will cause the preset to apply, false will disclude it.`
833+
`A function used to determine whether or not to apply this plugin options preset. It should return a boolean value. True will cause the preset to apply, false will disclude it.`
832834
)
833835
.default(`() => false`)
834836
.meta({
@@ -900,3 +902,7 @@ This should be the full url of your GraphQL endpoint.`
900902
.allow(null),
901903
})
902904
}
905+
906+
module.exports = {
907+
pluginOptionsSchema,
908+
}

0 commit comments

Comments
 (0)