3
3
// The server where the site is exposed through a static file server
4
4
// while developing locally.
5
5
6
- const path = require ( 'path' )
7
- const st = require ( 'st' )
6
+ const fs = require ( 'fs' )
8
7
const http = require ( 'http' )
8
+ const path = require ( 'path' )
9
9
const chokidar = require ( 'chokidar' )
10
+ const junk = require ( 'junk' )
11
+ const st = require ( 'st' )
12
+ const build = require ( './build' )
13
+
10
14
const mount = st ( {
11
15
path : path . join ( __dirname , 'build' ) ,
12
16
cache : false ,
13
17
index : 'index.html' ,
14
18
passthrough : true
15
19
} )
16
20
17
- const build = require ( './build' )
18
- const fs = require ( 'fs' )
19
21
const port = process . env . PORT || 8080
20
- const junk = require ( 'junk' )
21
-
22
22
const selectedLocales = process . env . DEFAULT_LOCALE ? process . env . DEFAULT_LOCALE . toLowerCase ( ) . split ( ',' ) : process . env . DEFAULT_LOCALE
23
23
const preserveLocale = process . argv . includes ( '--preserveLocale' )
24
24
const serveOnly = process . argv . includes ( '--serve-only' )
@@ -30,8 +30,9 @@ const opts = {
30
30
usePolling : true
31
31
}
32
32
const locales = chokidar . watch ( path . join ( __dirname , 'locale' ) , opts )
33
- const layouts = chokidar . watch ( path . join ( __dirname , 'layouts' ) , opts )
34
- const statics = chokidar . watch ( path . join ( __dirname , 'static' ) , opts )
33
+ const css = chokidar . watch ( path . join ( __dirname , 'layouts/css/**/*.styl' ) , opts )
34
+ const layouts = chokidar . watch ( path . join ( __dirname , 'layouts/**/*.hbs' ) , opts )
35
+ const staticFiles = chokidar . watch ( path . join ( __dirname , 'static' ) , opts )
35
36
36
37
// Gets the locale name by path.
37
38
function getLocale ( filePath ) {
@@ -44,10 +45,11 @@ function getLocale (filePath) {
44
45
// 2. Choose what languages for the menu.
45
46
function dynamicallyBuildOnLanguages ( source , locale ) {
46
47
if ( ! selectedLocales || selectedLocales . length === 0 ) {
47
- fs . readdir ( path . join ( __dirname , 'locale' ) , ( e , locales ) => {
48
- if ( e ) {
49
- throw e
48
+ fs . readdir ( path . join ( __dirname , 'locale' ) , ( err , locales ) => {
49
+ if ( err ) {
50
+ throw err
50
51
}
52
+
51
53
const filteredLocales = locales . filter ( file => junk . not ( file ) )
52
54
const localesData = build . generateLocalesData ( filteredLocales )
53
55
build . buildLocale ( source , locale , { preserveLocale, localesData } )
@@ -59,7 +61,9 @@ function dynamicallyBuildOnLanguages (source, locale) {
59
61
}
60
62
61
63
build . getSource ( ( err , source ) => {
62
- if ( err ) { throw err }
64
+ if ( err ) {
65
+ throw err
66
+ }
63
67
64
68
locales . on ( 'change' , ( filePath ) => {
65
69
const locale = getLocale ( filePath )
@@ -81,15 +85,21 @@ build.getSource((err, source) => {
81
85
} )
82
86
} )
83
87
88
+ css . on ( 'change' , ( ) => build . buildCSS ( ) )
89
+ css . on ( 'add' , ( filePath ) => {
90
+ css . add ( filePath )
91
+ build . buildCSS ( )
92
+ } )
93
+
84
94
layouts . on ( 'change' , ( ) => build . fullBuild ( { selectedLocales, preserveLocale } ) )
85
95
layouts . on ( 'add' , ( filePath ) => {
86
96
layouts . add ( filePath )
87
97
build . fullBuild ( { selectedLocales, preserveLocale } )
88
98
} )
89
99
90
- statics . on ( 'change' , build . copyStatic )
91
- statics . on ( 'add' , ( filePath ) => {
92
- statics . add ( filePath )
100
+ staticFiles . on ( 'change' , build . copyStatic )
101
+ staticFiles . on ( 'add' , ( filePath ) => {
102
+ staticFiles . add ( filePath )
93
103
build . copyStatic ( )
94
104
} )
95
105
@@ -104,9 +114,13 @@ http.createServer((req, res) => {
104
114
req . url = `/${ mainLocale } `
105
115
}
106
116
mount ( req , res )
107
- } ) . listen ( port , ( ) => console . log ( `\x1B[32mServer running at http://localhost:${ port } /${ mainLocale } /\x1B[39m` ) )
117
+ } ) . listen ( port , ( ) => {
118
+ console . log ( `\x1B[32mServer running at http://localhost:${ port } /${ mainLocale } /\x1B[39m` )
119
+ } )
108
120
109
121
if ( ! serveOnly ) {
110
122
// Start the initial build of static HTML pages
123
+ build . copyStatic ( )
124
+ build . buildCSS ( )
111
125
build . fullBuild ( { selectedLocales, preserveLocale } )
112
126
}
0 commit comments