UNPKG

2.44 kBJavaScriptView Raw
1"use strict";
2
3const report = require(`gatsby-cli/lib/reporter`);
4
5const apiRunner = require(`./api-runner-node`);
6
7const {
8 store
9} = require(`../redux`);
10
11const {
12 getNode,
13 getNodes
14} = require(`../db/nodes`);
15
16const {
17 boundActionCreators
18} = require(`../redux/actions`);
19
20const {
21 deleteNode
22} = boundActionCreators;
23/**
24 * Finds the name of all plugins which implement Gatsby APIs that
25 * may create nodes, but which have not actually created any nodes.
26 */
27
28function discoverPluginsWithoutNodes(storeState, nodes) {
29 // Find out which plugins own already created nodes
30 const nodeOwnerSet = new Set([`default-site-plugin`]);
31 nodes.forEach(node => nodeOwnerSet.add(node.internal.owner));
32 return storeState.flattenedPlugins.filter(plugin => // "Can generate nodes"
33 plugin.nodeAPIs.includes(`sourceNodes`) && // "Has not generated nodes"
34 !nodeOwnerSet.has(plugin.name)).map(plugin => plugin.name);
35}
36/**
37 * Warn about plugins that should have created nodes but didn't.
38 */
39
40
41function warnForPluginsWithoutNodes(state, nodes) {
42 const pluginsWithNoNodes = discoverPluginsWithoutNodes(state, nodes);
43 pluginsWithNoNodes.map(name => report.warn(`The ${name} plugin has generated no Gatsby nodes. Do you need it?`));
44}
45/**
46 * Return the set of nodes for which its root node has not been touched
47 */
48
49
50function getStaleNodes(state, nodes) {
51 return nodes.filter(node => {
52 let rootNode = node;
53 let whileCount = 0;
54
55 while (rootNode.parent && getNode(rootNode.parent) !== undefined && whileCount < 101) {
56 rootNode = getNode(rootNode.parent);
57 whileCount += 1;
58
59 if (whileCount > 100) {
60 console.log(`It looks like you have a node that's set its parent as itself`, rootNode);
61 }
62 }
63
64 return !state.nodesTouched.has(rootNode.id);
65 });
66}
67/**
68 * Find all stale nodes and delete them
69 */
70
71
72function deleteStaleNodes(state, nodes) {
73 const staleNodes = getStaleNodes(state, nodes);
74
75 if (staleNodes.length > 0) {
76 staleNodes.forEach(node => deleteNode({
77 node
78 }));
79 }
80}
81
82module.exports = async ({
83 webhookBody = {},
84 parentSpan
85} = {}) => {
86 await apiRunner(`sourceNodes`, {
87 traceId: `initial-sourceNodes`,
88 waitForCascadingActions: true,
89 parentSpan,
90 webhookBody
91 });
92 const state = store.getState();
93 const nodes = getNodes();
94 warnForPluginsWithoutNodes(state, nodes);
95 deleteStaleNodes(state, nodes);
96};
97//# sourceMappingURL=source-nodes.js.map
\No newline at end of file