1
+ 'use strict' ;
2
+
3
+ const fetch = require ( "node-fetch" ) ,
4
+ _ = require ( "lodash" ) ,
5
+ Entry = require ( './lib/entry.js' ) ,
6
+ 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
+ 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
+ name = alias . getName ( ) ;
39
+ return `${ member } .${ name } ` ;
40
+ } )
41
+
42
+ const params = entry . getParams ( ) . map (
43
+ ( [ type , name , desc ] ) => ( { type, name, desc } ) ) ;
44
+
45
+ const entryData = {
46
+ aliases,
47
+ version,
48
+ params,
49
+ call : entry . getCall ( ) ,
50
+ category : entry . getCategory ( ) ,
51
+ desc : entry . getDesc ( ) ,
52
+ example : entry . getExample ( ) ,
53
+ hash : entry . getHash ( ) ,
54
+ lineNumber : entry . getLineNumber ( ) ,
55
+ members : entry . getMembers ( ) ,
56
+ name : entry . getName ( ) ,
57
+ related : entry . getRelated ( ) ,
58
+ returns : entry . getReturns ( ) ,
59
+ since : entry . getSince ( ) ,
60
+ type : entry . getType ( ) ,
61
+ isAlias : entry . isAlias ( ) ,
62
+ isCtor : entry . isCtor ( ) ,
63
+ isFunction : entry . isFunction ( ) ,
64
+ isLicense : entry . isLicense ( ) ,
65
+ isPlugin : entry . isPlugin ( ) ,
66
+ isStatic : entry . isStatic ( ) ,
67
+ }
68
+
69
+ const nodeData = _ . assign ( { } , entryData , {
70
+ id : createNodeId ( `lodash_method_${ version } _${ entryData . hash } _${ entryData . lineNumber } ` ) ,
71
+ parent : null ,
72
+ children : [ ] ,
73
+ internal : {
74
+ type : 'LodashMethod' ,
75
+ content : JSON . stringify ( entryData ) ,
76
+ contentDigest : createContentDigest ( JSON . stringify ( entryData ) ) ,
77
+ }
78
+ } ) ;
79
+
80
+ return nodeData ;
81
+ }
82
+
83
+ return (
84
+ fetch ( url )
85
+ . then ( res => res . text ( ) )
86
+ . then ( body => {
87
+ const entries = getEntries ( body ) ;
88
+
89
+ // For each entry, create node.
90
+ _ . each ( entries , entry => {
91
+ entry = new Entry ( entry , body ) ;
92
+ const nodeData = processEntry ( entry ) ;
93
+ if ( nodeData ) {
94
+ createNode ( nodeData ) ;
95
+ }
96
+ } )
97
+ } )
98
+ )
99
+ }
0 commit comments