|
| 1 | +"use strict" |
| 2 | + |
| 3 | +const fetch = require("node-fetch") |
| 4 | +const _ = require("lodash") |
| 5 | +const Entry = require("./lib/entry.js") |
| 6 | +const getEntries = Entry.getEntries |
| 7 | + |
| 8 | +exports.sourceNodes = ( |
| 9 | + { actions, createNodeId, createContentDigest }, |
| 10 | + configOptions |
| 11 | +) => { |
| 12 | + const { createNode } = actions |
| 13 | + |
| 14 | + // Gatsby adds a configOption that's not needed for this plugin, delete it |
| 15 | + delete configOptions.plugins |
| 16 | + |
| 17 | + // TODO: Extend this to include multiple versions |
| 18 | + const url = configOptions.versions[0].url |
| 19 | + const version = configOptions.versions[0].version |
| 20 | + |
| 21 | + const processEntry = entry => { |
| 22 | + try { |
| 23 | + // Exit early if the entry is private or has no name |
| 24 | + if (!entry || !entry.getName() || entry.isPrivate()) { |
| 25 | + return |
| 26 | + } |
| 27 | + } catch (err) { |
| 28 | + // Some of the non-lodash methods were throwing a hard to trace error |
| 29 | + // from the lib code. Rather than trace back, it was easier to catch |
| 30 | + // since they aren't part of lodash. |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Special handling of aliases to get call names. Without this, there are |
| 35 | + // circular JS object references. |
| 36 | + const aliases = entry.getAliases().map(alias => { |
| 37 | + const member = entry.getMembers(0) || "" |
| 38 | + const name = alias.getName() |
| 39 | + return `${member}.${name}` |
| 40 | + }) |
| 41 | + |
| 42 | + const params = entry |
| 43 | + .getParams() |
| 44 | + .map(([type, name, desc]) => ({ type, name, desc })) |
| 45 | + |
| 46 | + const entryData = { |
| 47 | + aliases, |
| 48 | + call: entry.getCall(), |
| 49 | + category: entry.getCategory(), |
| 50 | + desc: entry.getDesc(), |
| 51 | + example: entry.getExample(), |
| 52 | + hash: entry.getHash(), |
| 53 | + isAlias: entry.isAlias(), |
| 54 | + isCtor: entry.isCtor(), |
| 55 | + isFunction: entry.isFunction(), |
| 56 | + isLicense: entry.isLicense(), |
| 57 | + isPlugin: entry.isPlugin(), |
| 58 | + isStatic: entry.isStatic(), |
| 59 | + lineNumber: entry.getLineNumber(), |
| 60 | + members: entry.getMembers(), |
| 61 | + name: entry.getName(), |
| 62 | + params, |
| 63 | + related: entry.getRelated(), |
| 64 | + returns: entry.getReturns(), |
| 65 | + since: entry.getSince(), |
| 66 | + type: entry.getType(), |
| 67 | + version, |
| 68 | + } |
| 69 | + |
| 70 | + const nodeData = { |
| 71 | + ...entryData, |
| 72 | + children: [], |
| 73 | + id: createNodeId( |
| 74 | + `lodash_method_${version}_${entryData.hash}_${entryData.lineNumber}` |
| 75 | + ), |
| 76 | + internal: { |
| 77 | + content: JSON.stringify(entryData), |
| 78 | + contentDigest: createContentDigest(JSON.stringify(entryData)), |
| 79 | + type: "LodashMethod", |
| 80 | + }, |
| 81 | + parent: null, |
| 82 | + } |
| 83 | + |
| 84 | + return nodeData |
| 85 | + } |
| 86 | + |
| 87 | + return fetch(url) |
| 88 | + .then(res => res.text()) |
| 89 | + .then(body => { |
| 90 | + const entries = getEntries(body) |
| 91 | + |
| 92 | + // For each entry, create node. |
| 93 | + _.each(entries, entry => { |
| 94 | + entry = new Entry(entry, body) |
| 95 | + const nodeData = processEntry(entry) |
| 96 | + if (nodeData) { |
| 97 | + createNode(nodeData) |
| 98 | + } |
| 99 | + }) |
| 100 | + }) |
| 101 | +} |
0 commit comments