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): Convert db/nodes to TS #25199

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/gatsby/src/commands/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import repl from "repl"
import { graphql } from "graphql"
import { bootstrap } from "../bootstrap"
import { trackCli } from "gatsby-telemetry"
import { loadNodeContent, getNodes, getNode, getNodesByType } from "../db/nodes"
import { getNodes, getNode, getNodesByType } from "../redux/nodes"
import { loadNodeContent } from "../db/nodes"
import { store } from "../redux"
import { IProgram } from "./types"

Expand Down
58 changes: 0 additions & 58 deletions packages/gatsby/src/db/nodes.js

This file was deleted.

39 changes: 39 additions & 0 deletions packages/gatsby/src/db/nodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IGatsbyNode } from "../redux/types"
import { store } from "../redux"

export * from "../redux/nodes"
export { runFastFiltersAndSort as runQuery } from "../redux/run-fast-filters"

/**
* Get content for a node from the plugin that created it.
*/
export async function loadNodeContent(node: IGatsbyNode): Promise<string> {
if (typeof node.internal.content === `string`) {
return node.internal.content
}

// Load plugin's loader function
const plugin = store
.getState()
.flattenedPlugins.find(plug => plug.name === node.internal.owner)

if (!plugin) {
throw new Error(
`Could not find owner plugin of node for loadNodeContent with owner \`${node.internal.owner}\``
)
}

const { loadNodeContent } = require(plugin.resolve)

if (!loadNodeContent) {
throw new Error(
`Could not find function loadNodeContent for plugin ${plugin.name}`
)
}

const content = await loadNodeContent(node)

node.internal.content = content

return content
}
29 changes: 15 additions & 14 deletions packages/gatsby/src/utils/source-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import report from "gatsby-cli/lib/reporter"
import { Span } from "opentracing"
import apiRunner from "./api-runner-node"
import { store } from "../redux"
import { getNode, getNodes } from "../db/nodes"
import { getNode, getNodes } from "../redux/nodes"
import { boundActionCreators } from "../redux/actions"
import { IGatsbyState } from "../redux/types"
const { deleteNode } = boundActionCreators

import { Node } from "../../index"

/**
* Finds the name of all plugins which implement Gatsby APIs that
* may create nodes, but which have not actually created any nodes.
Expand Down Expand Up @@ -50,20 +50,21 @@ function warnForPluginsWithoutNodes(state: IGatsbyState, nodes: Node[]): void {
function getStaleNodes(state: IGatsbyState, nodes: Node[]): Node[] {
return nodes.filter(node => {
let rootNode = node
let next: Node | undefined = undefined
pvdz marked this conversation as resolved.
Show resolved Hide resolved

let whileCount = 0
while (
rootNode.parent &&
getNode(rootNode.parent) !== undefined &&
whileCount < 101
) {
rootNode = getNode(rootNode.parent)
whileCount += 1
if (whileCount > 100) {
console.log(
`It looks like you have a node that's set its parent as itself`,
rootNode
)
do {
next = rootNode.parent ? getNode(rootNode.parent) : undefined
if (next) {
rootNode = next
}
} while (next && ++whileCount < 101)

if (whileCount > 100) {
console.log(
pvdz marked this conversation as resolved.
Show resolved Hide resolved
`It looks like you have a node that's set its parent as itself`,
rootNode
)
}

return !state.nodesTouched.has(rootNode.id)
Expand Down