1 | "use strict";
|
2 |
|
3 | const report = require(`gatsby-cli/lib/reporter`);
|
4 |
|
5 | const apiRunner = require(`./api-runner-node`);
|
6 |
|
7 | const {
|
8 | store
|
9 | } = require(`../redux`);
|
10 |
|
11 | const {
|
12 | getNode,
|
13 | getNodes
|
14 | } = require(`../db/nodes`);
|
15 |
|
16 | const {
|
17 | boundActionCreators
|
18 | } = require(`../redux/actions`);
|
19 |
|
20 | const {
|
21 | deleteNode
|
22 | } = boundActionCreators;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function discoverPluginsWithoutNodes(storeState, nodes) {
|
29 |
|
30 | const nodeOwnerSet = new Set([`default-site-plugin`]);
|
31 | nodes.forEach(node => nodeOwnerSet.add(node.internal.owner));
|
32 | return storeState.flattenedPlugins.filter(plugin =>
|
33 | plugin.nodeAPIs.includes(`sourceNodes`) &&
|
34 | !nodeOwnerSet.has(plugin.name)).map(plugin => plugin.name);
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function 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 |
|
47 |
|
48 |
|
49 |
|
50 | function 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 |
|
69 |
|
70 |
|
71 |
|
72 | function 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 |
|
82 | module.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 |
|
\ | No newline at end of file |