@@ -34,7 +34,7 @@ function generateTemplates(){
34
34
let flatPosts = posts . reduce ( ( posts_prev , posts_next ) => posts_prev . concat ( posts_next ) ) ;
35
35
let pagination = { next :null , prev :null } ;
36
36
37
- // index pages
37
+ // index pages and pagination
38
38
posts . forEach ( ( post_arr , cur_page ) => {
39
39
pagination = Object . assign ( pagination ,
40
40
{
@@ -57,50 +57,20 @@ function generateTemplates(){
57
57
utils . generatePostTemplate ( post , labels , 'dev' ) ;
58
58
} ) ;
59
59
60
- console . log ( chalk . bold . green ( `Templates generated. \nAvailable on:\n http://localhost:${ PORT } ` ) ) ;
61
- }
62
-
60
+ // other pages
61
+ utils . generatePageTemplate ( 'dev' ) ;
63
62
64
- // fetch github data
63
+ // category pages
64
+ utils . generateCategoryTemplates2 ( labels , 'dev' )
65
65
66
- function fetchAndStoreData ( ) {
67
- blog . fetchBlogPosts ( )
68
- . then ( _posts => {
69
-
70
- posts . push ( _posts ) ;
71
-
72
- if ( ! blog . settings . posts . last_reached ) {
73
- fetchAndStoreData ( ) ;
74
- }
75
- else {
76
- spinner . stop ( ) ;
77
- posts [ 0 ] . unshift ( ...listOfFiles )
78
- startDevMode ( ) ;
79
- }
80
- } )
81
- . catch ( function ( err ) {
82
- spinner . stop ( ) ;
83
- console . log ( chalk . bold . red ( 'Could not fetch github data for some reason\nUsing offline data.' ) ) ;
84
- posts . push ( listOfFiles )
85
- startDevMode ( ) ;
86
- } ) ;
66
+ console . log ( chalk . bold . green ( `Templates generated. \nAvailable on:\n http://localhost:${ PORT } ` ) ) ;
87
67
}
88
68
89
69
function startDevMode ( ) {
90
70
91
71
mkdirp . sync ( path . join ( ROOT_DIR , 'dev' , 'category' ) ) ;
92
72
mkdirp . sync ( path . join ( ROOT_DIR , 'dev' , 'posts' ) ) ;
93
- // Promise.all the creation of category pages
94
- // then create index and other pages
95
- // There is mixed use of sync and async functions
96
- // need to fix that
97
- Promise . all ( utils . generateCategoryTemplates ( labels , 'dev' ) )
98
- . then ( ( ) => {
99
- // index and post pages
100
- generateTemplates ( ) ;
101
- // other pages
102
- utils . generatePageTemplate ( 'dev' ) ;
103
- } ) ;
73
+ generateTemplates ( ) ;
104
74
105
75
// watch for changes in the theme directory and regenerate on change
106
76
chokidar . watch ( THEME_DIR , { ignored : / ( ^ | [ \/ \\ ] ) \. ./ } ) . on ( 'all' , ( event , path ) => {
@@ -110,27 +80,65 @@ function startDevMode(){
110
80
} ) ;
111
81
}
112
82
113
- // start the dev thing
114
- spinner . start ( ) ;
115
- blog . fetchAllLabels ( )
83
+ function getAllPosts ( ) {
84
+ let posts = [ ] ;
85
+ return new Promise ( ( resolve , reject ) => {
86
+ ( function callFetchBlogPosts ( ) {
87
+ blog . fetchBlogPosts ( )
88
+ . then ( _posts => {
89
+ if ( blog . settings . posts . last_reached ) {
90
+ resolve ( posts ) ;
91
+ }
92
+ else {
93
+ callFetchBlogPosts ( ) ;
94
+ }
95
+ posts . push ( _posts ) ;
96
+ } )
97
+ . catch ( function ( err ) {
98
+ console . log ( chalk . bold . red ( 'Could not fetch github data for some reason\nUsing offline data.' ) ) ;
99
+ resolve ( posts ) ;
100
+ } ) ;
101
+ } ) ( )
102
+ } ) ;
103
+ }
104
+
105
+ function getAllLabels ( ) {
106
+ return new Promise ( ( resolve , reject ) => {
107
+ blog . fetchAllLabels ( )
116
108
. then ( _labels => {
117
- labels = _labels ;
118
- let fileContents = utils . getOfflineFileContents ( ) ;
119
- Promise . all ( fileContents ) . then ( ( offlineFileContents ) => {
120
- listOfFiles = offlineFileContents ;
121
- fetchAndStoreData ( ) ;
122
- } ) ;
109
+ resolve ( _labels ) ;
123
110
} )
124
111
. catch ( err => {
125
- let fileContents = utils . getOfflineFileContents ( ) ;
126
- Promise . all ( fileContents ) . then ( ( offlineFileContents ) => {
127
- listOfFiles = offlineFileContents ;
128
- fetchAndStoreData ( ) ;
129
- } ) ;
130
- console . log ( chalk . bold . red ( 'Could not fetch github data due to network issue' ) ) ;
112
+ console . log ( chalk . bold . red ( 'Could not fetch github label data due to network issue' ) ) ;
113
+ resolve ( labels ) ;
131
114
} ) ;
115
+ } ) ;
116
+ }
132
117
133
118
119
+ function fetchAndStoreData ( ) {
120
+ Promise . all ( [ getAllPosts ( ) , getAllLabels ( ) , utils . getOfflineFileContents ( ) ] )
121
+ . then ( vals => {
122
+ spinner . stop ( ) ;
123
+ posts = vals [ 0 ] ;
124
+ labels = vals [ 1 ] ;
125
+ listOfFiles = vals [ 2 ] ;
126
+ posts [ 0 ] . unshift ( ...listOfFiles )
127
+ startDevMode ( ) ;
128
+ } )
129
+ . catch ( err => {
130
+ spinner . stop ( ) ;
131
+ utils . getOfflineFileContents ( )
132
+ . then ( files => {
133
+ posts . push ( files )
134
+ startDevMode ( ) ;
135
+ } )
136
+ } )
137
+ }
138
+
139
+ spinner . start ( ) ;
140
+ fetchAndStoreData ( ) ;
141
+
134
142
// start the dev server silently
135
143
server . start ( {
136
144
port : PORT ,
0 commit comments