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

[1.0] Refactor and document APIs #1053

Merged
merged 23 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b46a59b
Add basic version of new documentation.js transformer
KyleAMathews May 28, 2017
ab7ba98
Change api 'addFieldToNode' to 'createNodeField'
KyleAMathews May 29, 2017
45f7a31
Fix up node creation and add some simple tests
KyleAMathews May 30, 2017
1067771
Get basic docs page for action creators up
KyleAMathews May 30, 2017
0e39282
change deletePageByPath to just deletePage also sort action creators …
KyleAMathews May 30, 2017
d27833d
Change 'upsertPage' to 'createPage' and document it
KyleAMathews May 30, 2017
8a7575d
Only hide parent object description if its a desctructured object + i…
KyleAMathews May 30, 2017
61b79ff
Update action type
KyleAMathews May 30, 2017
776f77d
Support third level of parameters
KyleAMathews May 30, 2017
8863d25
Document createNode
KyleAMathews May 30, 2017
3697716
change 'addNodeToParent' to 'createParentChildLink' and document
KyleAMathews May 30, 2017
9e1c25a
Change 'addPageDependency' to 'createPageDependency' and document
KyleAMathews May 30, 2017
a8108ab
rename internal APIs
KyleAMathews May 30, 2017
5b0528b
Add links from function names at top to reference
KyleAMathews May 30, 2017
cba9b46
Ignore yarn.lock
KyleAMathews May 30, 2017
3ad7e09
Don't change the changelog...
KyleAMathews May 30, 2017
3c77aa3
Document and slightly modify node APIs
KyleAMathews May 31, 2017
6acd904
Factor out a FunctionsList component for rendering function docs
KyleAMathews May 31, 2017
6d536b3
Add browser/ssr docs and make a few tweaks to APIs
KyleAMathews May 31, 2017
62461fe
Add API specification document
KyleAMathews May 31, 2017
b5d634c
Actually add docs for node/browser/ssr
KyleAMathews May 31, 2017
e6600ab
Tweaks
KyleAMathews May 31, 2017
385bc78
Add README for gatsby-transformer-documentationjs
KyleAMathews May 31, 2017
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Document and slightly modify node APIs
  • Loading branch information
KyleAMathews committed May 31, 2017
commit 3c77aa32562abea07e8b6b651d89af638d6d9fcb
2 changes: 1 addition & 1 deletion docs/docs/migrating-from-v0-to-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Here's how you do that.
// In your gatsby-node.js
const path = require('path')

exports.onNodeCreate = ({ node, boundActionCreators, getNode }) => {
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
let slug
if (node.internal.type === `MarkdownRemark`) {
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-remark-copy-linked-files/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module.exports = ({ files, markdownNode, markdownAST, getNode }) => {
// Copy linked files to the public directory and modify the AST to point to
// new location of the files.
const visitor = link => {
if (isRelativeUrl(link.url)) {
if (
isRelativeUrl(link.url) &&
getNode(markdownNode.parent).internal.type === `File`
) {
const linkPath = path.join(getNode(markdownNode.parent).dir, link.url)
const linkNode = _.find(files, file => {
if (file && file.absolutePath) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`transformer-react-doc-gen: onNodeCreate should extract out a description, params, and examples 1`] = `
exports[`transformer-react-doc-gen: onCreateNode should extract out a description, params, and examples 1`] = `
Array [
Object {
"children": Array [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* apple()
*/
const apple = apple => {
console.log("hi")
console.log(`hi`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import groupBy from "lodash/groupBy"
import path from "path"
import gatsbyNode from "../gatsby-node"

describe(`transformer-react-doc-gen: onNodeCreate`, () => {
describe(`transformer-react-doc-gen: onCreateNode`, () => {
let boundActionCreators, node, createdNodes, updatedNodes
let run = (node = node, opts = {}) =>
gatsbyNode.onNodeCreate(
gatsbyNode.onCreateNode(
{
node,
boundActionCreators,
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby-transformer-documentationjs/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const documentation = require("documentation")
const crypto = require("crypto")
const documentation = require(`documentation`)
const crypto = require(`crypto`)
const digest = str => crypto.createHash(`md5`).update(str).digest(`hex`)
const remark = require(`remark`)
const _ = require("lodash")
const _ = require(`lodash`)
const Prism = require(`prismjs`)

const stringifyMarkdownAST = (node = ``) => {
Expand Down Expand Up @@ -46,10 +46,10 @@ function createDescriptionNode(
}

/**
* Implement the onNodeCreate API to create documentation.js nodes
* Implement the onCreateNode API to create documentation.js nodes
* @param {Object} super this is a super param
*/
exports.onNodeCreate = async ({
exports.onCreateNode = async ({
node,
loadNodeContent,
boundActionCreators,
Expand Down
8 changes: 4 additions & 4 deletions packages/gatsby-transformer-json/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Promise = require(`bluebird`)
const _ = require(`lodash`)

const { onNodeCreate } = require(`../gatsby-node`)
const { onCreateNode } = require(`../gatsby-node`)

describe(`Process JSON nodes correctly`, () => {
const node = {
Expand Down Expand Up @@ -29,7 +29,7 @@ describe(`Process JSON nodes correctly`, () => {
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
Expand All @@ -52,7 +52,7 @@ describe(`Process JSON nodes correctly`, () => {
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
Expand All @@ -74,7 +74,7 @@ describe(`Process JSON nodes correctly`, () => {
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-transformer-json/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require(`fs`)
const _ = require(`lodash`)
const crypto = require(`crypto`)

async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
async function onCreateNode({ node, boundActionCreators, loadNodeContent }) {
const { createNode, createParentChildLink } = boundActionCreators

// Don't reprocess our own nodes! (note: this doesn't normally happen
Expand Down Expand Up @@ -57,4 +57,4 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
return
}

exports.onNodeCreate = onNodeCreate
exports.onCreateNode = onCreateNode
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs"
import path from "path"
import { groupBy } from "lodash"
import onNodeCreate from "../on-node-create"
import onCreateNode from "../on-node-create"

const readFile = file =>
new Promise((y, n) => {
Expand All @@ -12,10 +12,10 @@ const readFile = file =>
)
})

describe(`transformer-react-doc-gen: onNodeCreate`, () => {
describe(`transformer-react-doc-gen: onCreateNode`, () => {
let loadNodeContent, boundActionCreators, node, createdNodes, updatedNodes
let run = (node, opts = {}) =>
onNodeCreate(
onCreateNode(
{
node,
loadNodeContent,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-transformer-react-docgen/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exports.onNodeCreate = require(`./on-node-create`).default
exports.extendNodeType = require(`./extend-node-type`).default
exports.onCreateNode = require(`./on-node-create`).default
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`).default
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function createPropNodes(node, component, boundActionCreators) {
return node
}

export default function onNodeCreate(
export default function onCreateNode(
{ node, loadNodeContent, boundActionCreators },
pluginOptions
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Promise = require(`bluebird`)

const { onNodeCreate } = require(`../gatsby-node`)
const { onCreateNode } = require(`../gatsby-node`)

describe(`Process markdown content correctly`, () => {
const node = {
Expand Down Expand Up @@ -29,7 +29,7 @@ Where oh where is my little pony?
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-transformer-remark/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exports.onNodeCreate = require(`./on-node-create`)
exports.extendNodeType = require(`./extend-node-type`)
exports.onCreateNode = require(`./on-node-create`)
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`)
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const grayMatter = require(`gray-matter`)
const crypto = require(`crypto`)

module.exports = async function onNodeCreate({
module.exports = async function onCreateNode({
node,
getNode,
loadNodeContent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Promise = require(`bluebird`)

const { onNodeCreate } = require(`../gatsby-node`)
const { onCreateNode } = require(`../gatsby-node`)

describe(`Process image nodes correctly`, () => {
it(`correctly creates an ImageSharp node from a file image node`, async () => {
Expand All @@ -16,7 +16,7 @@ describe(`Process image nodes correctly`, () => {
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
boundActionCreators,
}).then(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-transformer-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exports.onNodeCreate = require(`./on-node-create`)
exports.extendNodeType = require(`./extend-node-type`)
exports.onCreateNode = require(`./on-node-create`)
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`)
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-sharp/src/on-node-create.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require(`lodash`)

module.exports = async function onNodeCreate({ node, boundActionCreators }) {
module.exports = async function onCreateNode({ node, boundActionCreators }) {
const { createNode, createParentChildLink } = boundActionCreators

const extensions = [`jpeg`, `jpg`, `png`, `webp`, `tif`, `tiff`, `svg`]
Expand Down
8 changes: 4 additions & 4 deletions packages/gatsby-transformer-yaml/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Promise = require(`bluebird`)
const yaml = require(`js-yaml`)
const _ = require(`lodash`)

const { onNodeCreate } = require(`../gatsby-node`)
const { onCreateNode } = require(`../gatsby-node`)

describe(`Process YAML nodes correctly`, () => {
const node = {
Expand All @@ -27,7 +27,7 @@ describe(`Process YAML nodes correctly`, () => {
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
Expand All @@ -50,7 +50,7 @@ describe(`Process YAML nodes correctly`, () => {
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
Expand All @@ -72,7 +72,7 @@ describe(`Process YAML nodes correctly`, () => {
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onNodeCreate({
await onCreateNode({
node,
loadNodeContent,
boundActionCreators,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-transformer-yaml/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const jsYaml = require(`js-yaml`)
const _ = require(`lodash`)
const crypto = require(`crypto`)

async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
async function onCreateNode({ node, boundActionCreators, loadNodeContent }) {
const { createNode, createParentChildLink } = boundActionCreators
if (node.internal.mediaType !== `text/yaml`) {
return
Expand Down Expand Up @@ -49,4 +49,4 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
return
}

exports.onNodeCreate = onNodeCreate
exports.onCreateNode = onCreateNode
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ data
// have full control over adding/removing pages. The normal
// "createPages" API is called every time (during development)
// that data changes.
await apiRunnerNode(`createPagesStateful`, {
await apiRunnerNode(`createPagesStatefully`, {
graphql: graphqlRunner,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const createPath = require(`./create-path`)
// algorithm is glob /pages directory for js/jsx/cjsx files *not*
// underscored. Then create url w/ our path algorithm *unless* user
// takes control of that page component in gatsby-node.
exports.createPagesStateful = async ({ store, boundActionCreators }) => {
exports.createPagesStatefully = async ({ store, boundActionCreators }) => {
const { createPage, deletePage } = boundActionCreators
const program = store.getState().program
const pagesDirectory = systemPath.posix.join(program.directory, `/src/pages`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/plugin-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const apiRunnerNode = require(`../utils/api-runner-node`)

emitter.on(`CREATE_NODE`, action => {
const node = store.getState().nodes[action.payload.id]
apiRunnerNode(`onNodeCreate`, { node })
apiRunnerNode(`onCreateNode`, { node })
})

emitter.on(`CREATE_PAGE`, action => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/build-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = async () => {
intermediateType.name = typeName
intermediateType.nodes = nodes

const fieldsFromPlugins = await apiRunner(`extendNodeType`, {
const fieldsFromPlugins = await apiRunner(`setFieldsOnGraphQLNodeType`, {
type: intermediateType,
allNodes: getNodes(),
})
Expand Down
11 changes: 10 additions & 1 deletion packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const mapSeries = require(`async/mapSeries`)

const cache = require(`./cache`)

const apiList = require(`./api-node-docs`)

// Bind action creators per plugin so can auto-add plugin
// meta-data to data they create.
const boundPluginActionCreators = {}
Expand Down Expand Up @@ -49,7 +51,7 @@ const runAPI = (plugin, api, args) => {

const gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
if (gatsbyNode[api]) {
// if (!_.includes([`onNodeCreate`, `onCreatePage`], api)) {
// if (!_.includes([`onCreateNode`, `onCreatePage`], api)) {
// console.log(`calling api handler in ${plugin.resolve} for api ${api}`)
// }
const result = gatsbyNode[api](
Expand Down Expand Up @@ -79,6 +81,13 @@ const hasAPIFile = plugin => glob.sync(`${plugin.resolve}/gatsby-node*`)[0]

module.exports = async (api, args = {}, pluginSource) =>
new Promise(resolve => {
// Check that the API is documented.
if (!apiList[api]) {
console.log(`api`, api, `is not yet documented`)
// process.exit()
return resolve()
}

const { store } = require(`../redux`)
const plugins = store.getState().flattenedPlugins
// Get the list of plugins that implement gatsby-node
Expand Down
2 changes: 1 addition & 1 deletion www/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
}

// Create slugs for files.
exports.onNodeCreate = ({ node, boundActionCreators, getNode }) => {
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
let slug
if (node.internal.type === `File`) {
Expand Down
Loading