1
1
require ( 'dotenv' ) . config ( ) ;
2
-
2
+ // Dependencies
3
3
import Bluebird from 'bluebird' ;
4
4
import logger from 'src/utils/logger' ;
5
5
6
+ // Lib
6
7
import drivers from 'src/drivers' ;
7
8
import { readTmpFileAsyncFactory , readTmpFileFactory } from 'src/utils/fs' ;
9
+ import schemas from 'src/schemas' ;
8
10
9
11
// Get 'readTmpFile' and 'readTmpFileAsync' bound by 'api'
10
12
const PROJECT_TYPE = 'guides' ;
@@ -20,7 +22,101 @@ export function run() {
20
22
SelectedDriver . init ( 'guides' ) ;
21
23
22
24
return readTmpFileAsync ( 'versions.json' )
25
+ // Extract available guides versions
23
26
. then ( emberJson => emberJson . data . attributes [ 'all-versions' ] )
24
- . map ( )
27
+ // Clear the driver contents
28
+ . tap ( clearDriver )
29
+ . map ( getPagesForVersion )
30
+ . map ( populateSubPagesForVersion )
31
+ . map ( flattenPagesAndMapSchema )
32
+ . map ( writeToDriver )
25
33
. tap ( console . log ) ;
26
34
}
35
+
36
+ /**
37
+ * Writes out to the given driver
38
+ *
39
+ * @param versionObject - Object version to write out
40
+ */
41
+ function writeToDriver ( { version, pages } ) {
42
+ // logger.logGreen(`version: ${versionObject.version.data.id}, public classes: ${versionObject.publicClasses.length}, public modules: ${versionObject.publicModules.length}, methods: ${versionObject.methods.length}`);
43
+
44
+ // Wait for all promises to complete before continuing
45
+ return Bluebird . all ( [
46
+ SelectedDriver . write ( 'guides' , pages )
47
+ ] ) ;
48
+ }
49
+
50
+ /**
51
+ * Clears the driver indices
52
+ *
53
+ * @returns {Promise } - Promise with all drivers cleared.
54
+ */
55
+ function clearDriver ( ) {
56
+ SelectedDriver . clear ( 'guides' ) ;
57
+ }
58
+
59
+ function getPagesForVersion ( version ) {
60
+ const pages = readTmpFile ( `${ version } /pages.json` ) . data ;
61
+
62
+ return {
63
+ version,
64
+ pages
65
+ } ;
66
+ }
67
+
68
+ function populateSubPagesForVersion ( { version, pages } ) {
69
+ const populatedPages = pages
70
+ . reduce ( ( prev , currentPage ) => {
71
+ // Return previous result if there are no subpages
72
+ if ( currentPage . type !== 'pages' || ! currentPage . attributes . pages ) return prev ;
73
+ // Populate the pages with the contents of the json file
74
+ // Flatten attributes to simplify object
75
+ const newPage = {
76
+ ...currentPage ,
77
+ ...currentPage . attributes ,
78
+ pages : populateSubPages ( version , currentPage . attributes . pages )
79
+ } ;
80
+
81
+ // Remove attributes.pages to simplify page object
82
+ // delete currentPage.attributes;
83
+ delete newPage . attributes ;
84
+
85
+ prev . push ( newPage ) ;
86
+
87
+ return prev ;
88
+ } , [ ] ) ;
89
+
90
+ return {
91
+ version,
92
+ pages : populatedPages
93
+ } ;
94
+ }
95
+
96
+ function populateSubPages ( version , subPages ) {
97
+ return subPages
98
+ . map ( currentPage => {
99
+ // Some url attributes include a trailing "/". Make sure that's removed before attempting to load the file
100
+ const url = currentPage . url . replace ( new RegExp ( '/$' ) , '' ) ;
101
+ // Load page and merge with existing currentPage properties
102
+ const pageJson = readTmpFile ( `${ version } /${ url } .json` ) . data ;
103
+ return { ...currentPage , ...pageJson } ;
104
+ } ) ;
105
+ }
106
+
107
+ function flattenPagesAndMapSchema ( { version, pages } ) {
108
+ const flattenedPages = pages
109
+ . reduce ( ( prev , currentPage ) => {
110
+ console . log ( 'currentPage' , currentPage ) ;
111
+ // Extract the sub-pages of this page and pass them through the schema
112
+ const flattenedSubPages = currentPage . pages
113
+ . map ( currentSubPage => {
114
+ return schemas . guideItemSchema ( version , { title : currentPage . title } , currentSubPage ) ;
115
+ } ) ;
116
+
117
+ // Merge previous flattened sub-pages with new flattened sub pages
118
+ return prev . concat ( flattenedSubPages ) ;
119
+ } , [ ] ) ;
120
+
121
+ return { version, pages : flattenedPages } ;
122
+ }
0 commit comments