1
1
'use strict' ;
2
- const fs = require ( 'fs ' ) ;
2
+ const fse = require ( 'fse ' ) ;
3
3
const gulp = require ( 'gulp' ) ;
4
4
const util = require ( 'gulp-util' ) ;
5
5
const plumber = require ( 'gulp-plumber' ) ;
@@ -12,12 +12,14 @@ const source = require('vinyl-source-stream');
12
12
const buffer = require ( 'vinyl-buffer' ) ;
13
13
const uglify = require ( 'gulp-uglify' ) ;
14
14
const del = require ( 'del' ) ;
15
+ const confCal = require ( 'conf-cal' ) ;
15
16
16
17
const srcDirs = {
17
18
html : './src/html' ,
18
19
json : './src/json' ,
19
20
js : './src/js' ,
20
21
css : './src/scss' ,
22
+ confcal : './src/confcal' ,
21
23
} ;
22
24
const srcPaths = {
23
25
html : `${ srcDirs . html } /*.ejs` ,
@@ -26,30 +28,88 @@ const srcPaths = {
26
28
} ;
27
29
const destPath = './' ;
28
30
29
- const json = fs . readdirSync ( './src/json' )
30
- . map ( fName => fName . split ( '.json' ) [ 0 ] )
31
- . reduce ( ( cur , acc ) => {
32
- return Object . assign ( cur , { [ acc ] : require ( `./src/json/${ acc } .json` ) } ) ;
33
- } , { } ) ;
31
+ function readSchedule ( ) {
32
+ const calendar = { } ;
33
+ return fse . readdir ( `${ __dirname } /src/confcal` )
34
+ . then ( fNames =>
35
+ fNames . map ( fName => {
36
+ const parts = / ^ ( [ ^ . ] * ) .c o n f c a l $ / . exec ( fName ) ;
37
+ if ( parts ) {
38
+ return parts [ 1 ] ;
39
+ }
40
+ } ) . filter ( Boolean )
41
+ )
42
+ . then ( days => Promise . all ( days . map ( day => {
43
+ const filePath = `${ __dirname } /src/confcal/${ day } .confcal` ;
44
+ return fse . readFile ( filePath , 'utf8' )
45
+ . then ( data => confCal ( {
46
+ apiKey : process . env . GOOGLE_API_KEY ,
47
+ cache : `${ __dirname } /src/confcal/geo.cache`
48
+ } , data ) )
49
+ . then ( data => {
50
+ data . day = day ;
51
+ return data ;
52
+ } )
53
+ . catch ( error => {
54
+ error . message = `Error reading ${ filePath } : ${ error . message } ` ;
55
+ return Promise . reject ( error ) ;
56
+ } )
57
+ } ) ) )
58
+ . then ( cals => {
59
+ for ( const cal of cals . sort ( ( a , b ) => a . date > b . date ) ) {
60
+ calendar [ cal . day ] = cal ;
61
+ }
62
+ } )
63
+ . then ( ( ) => calendar )
64
+ }
34
65
66
+ function readJsonsTo ( allData ) {
67
+ return fse . readdir ( `${ __dirname } /src/json` )
68
+ . then ( fNames => fNames . map ( fName => fName . split ( '.json' ) [ 0 ] ) )
69
+ . then ( names => Promise . all (
70
+ names . map ( name => {
71
+ const filePath = `${ __dirname } /src/json/${ name } .json` ;
72
+ return fse . readFile ( filePath , 'utf8' )
73
+ . then ( data => JSON . parse ( data ) )
74
+ . then ( data => allData [ name ] = data )
75
+ . catch ( err => Promise . reject ( new Error ( `Error reading ${ filePath } : ${ err . message } ` ) ) ) ;
76
+ } )
77
+ ) )
78
+ }
35
79
36
- gulp . task ( 'html' , ( ) => {
37
- return gulp
80
+ function readData ( ) {
81
+ const allData = {
82
+ momentTz : require ( 'moment-timezone' ) ,
83
+ isSpeakerForEntry ( entry , speaker ) {
84
+ return entry . person === speaker . name || entry . person === speaker [ '氏名' ] || entry . person === speaker . nickName
85
+ }
86
+
87
+ } ;
88
+ return Promise . all ( [
89
+ readSchedule ( ) . then ( calendar => allData . calendar = calendar ) ,
90
+ readJsonsTo ( allData )
91
+ ] )
92
+ . then ( ( ) => allData ) ;
93
+ }
94
+
95
+ gulp . task ( 'html' , ( ) => readData ( ) . then ( data => new Promise ( ( resolve , reject ) => {
96
+ const stream = gulp
38
97
. src ( srcPaths . html )
39
98
. pipe ( plumber ( ( error ) => {
40
99
util . log ( util . colors . red ( error . message ) ) ;
41
- gulp . task ( 'html' ) . emit ( 'end' ) ;
100
+ resolve ( ) ;
42
101
} ) )
43
- . pipe ( ejs ( json , { } , { ext : '.html' } ) )
102
+ . pipe ( ejs ( data , { } , { ext : '.html' } ) )
44
103
. pipe ( gulp . dest ( destPath ) ) ;
45
- } ) ;
104
+ stream . on ( 'end' , ( ) => resolve ( ) ) ;
105
+ } ) ) ) ;
46
106
47
107
gulp . task ( 'css' , ( ) => {
48
108
return gulp
49
109
. src ( srcPaths . css )
50
110
. pipe ( plumber ( ( error ) => {
51
111
util . log ( util . colors . red ( error . message ) ) ;
52
- gulp . task ( 'css' ) . emit ( 'end' ) ;
112
+ // gulp.task('css').emit('end');
53
113
} ) )
54
114
. pipe ( sass ( { style : 'expanded' } ) )
55
115
. pipe ( autoprefixer ( ) )
@@ -75,6 +135,7 @@ gulp.task('js', () => {
75
135
gulp . task ( 'watch' , ( ) => {
76
136
gulp . watch ( `${ srcDirs . html } /**/*.ejs` , [ 'html' ] ) ;
77
137
gulp . watch ( `${ srcDirs . json } /**/*.json` , [ 'html' ] ) ;
138
+ gulp . watch ( `${ srcDirs . confcal } /**/*.confcal` , [ 'html' ] ) ;
78
139
gulp . watch ( `${ srcDirs . css } /**/*.scss` , [ 'css' ] ) ;
79
140
gulp . watch ( `${ srcDirs . js } /**/*.js` , [ 'js' ] ) ;
80
141
} ) ;
0 commit comments