2
2
3
3
'use strict'
4
4
5
- // BUILD.JS: This file is responsible for building static HTML pages and a
6
- // server for local development.
5
+ // BUILD.JS: This file is responsible for building static HTML pages
7
6
8
7
const Metalsmith = require ( 'metalsmith' )
9
8
const autoprefixer = require ( 'autoprefixer-stylus' )
@@ -65,7 +64,7 @@ function i18nJSON (lang) {
65
64
// This is the function where the actual magic happens. This contains the main
66
65
// Metalsmith build cycle used for building a locale subsite, such as the
67
66
// english one.
68
- function buildlocale ( source , locale ) {
67
+ function buildLocale ( source , locale ) {
69
68
console . time ( '[metalsmith] build/' + locale + ' finished' )
70
69
const siteJSON = path . join ( __dirname , 'locale' , locale , 'site.json' )
71
70
const metalsmith = Metalsmith ( __dirname )
@@ -234,7 +233,7 @@ function githubLinks (options) {
234
233
235
234
// This function copies the rest of the static assets to their subfolder in the
236
235
// build directory.
237
- function copystatic ( ) {
236
+ function copyStatic ( ) {
238
237
console . time ( '[metalsmith] build/static finished' )
239
238
fs . mkdir ( path . join ( __dirname , 'build' ) , function ( ) {
240
239
fs . mkdir ( path . join ( __dirname , 'build' , 'static' ) , function ( ) {
@@ -249,9 +248,9 @@ function copystatic () {
249
248
250
249
// This is where the build is orchestrated from, as indicated by the function
251
250
// name. It brings together all build steps and dependencies and executes them.
252
- function fullbuild ( ) {
251
+ function fullBuild ( ) {
253
252
// Copies static files.
254
- copystatic ( )
253
+ copyStatic ( )
255
254
// Loads all node/io.js versions.
256
255
loadVersions ( function ( err , versions ) {
257
256
if ( err ) { throw err }
@@ -272,71 +271,17 @@ function fullbuild () {
272
271
// Executes the build cycle for every locale.
273
272
fs . readdir ( path . join ( __dirname , 'locale' ) , function ( e , locales ) {
274
273
locales . filter ( junk . not ) . forEach ( function ( locale ) {
275
- buildlocale ( source , locale )
274
+ buildLocale ( source , locale )
276
275
} )
277
276
} )
278
277
} )
279
278
}
280
279
281
- // The server function, where the site is exposed through a static file server
282
- // locally.
283
- function server ( ) {
284
- // Initializes the server and mounts it in the generated build directory.
285
- const st = require ( 'st' )
286
- const http = require ( 'http' )
287
- const mount = st ( {
288
- path : path . join ( __dirname , 'build' ) ,
289
- cache : false ,
290
- index : 'index.html'
291
- } )
292
- http . createServer (
293
- function ( req , res ) { mount ( req , res ) }
294
- ) . listen ( 8080 ,
295
- function ( ) { console . log ( 'http://localhost:8080/en/' ) }
296
- )
297
-
298
- // Watches for file changes in the locale, layout and static directories, and
299
- // rebuilds the modified one.
300
- const chokidar = require ( 'chokidar' )
301
- const opts = {
302
- persistent : true ,
303
- ignoreInitial : true ,
304
- followSymlinks : true ,
305
- usePolling : true ,
306
- alwaysStat : false ,
307
- depth : undefined ,
308
- interval : 100 ,
309
- ignorePermissionErrors : false ,
310
- atomic : true
311
- }
312
- const locales = chokidar . watch ( path . join ( __dirname , 'locale' ) , opts )
313
- const layouts = chokidar . watch ( path . join ( __dirname , 'layouts' ) , opts )
314
- const staticf = chokidar . watch ( path . join ( __dirname , 'static' ) , opts )
315
-
316
- // Gets the locale name by path.
317
- function getlocale ( p ) {
318
- const pre = path . join ( __dirname , 'locale' )
319
- return p . slice ( pre . length + 1 , p . indexOf ( '/' , pre . length + 1 ) )
320
- }
321
- locales . on ( 'change' , function ( p ) {
322
- buildlocale ( p , getlocale ( p ) )
323
- } )
324
- locales . on ( 'add' , function ( p ) {
325
- buildlocale ( p , getlocale ( p ) )
326
- locales . add ( p )
327
- } )
328
-
329
- layouts . on ( 'change' , fullbuild )
330
- layouts . on ( 'add' , function ( p ) { layouts . add ( p ) ; fullbuild ( ) } )
331
-
332
- staticf . on ( 'change' , copystatic )
333
- staticf . on ( 'add' , function ( p ) { staticf . add ( p ) ; copystatic ( ) } )
280
+ // Starts the build if the file was executed from the command line
281
+ if ( require . main === module ) {
282
+ fullBuild ( )
334
283
}
335
284
336
- // Starts the build.
337
- fullbuild ( )
338
-
339
- // If the command-line option was provided, starts the static server.
340
- if ( process . argv [ 2 ] === 'serve' ) {
341
- server ( )
342
- }
285
+ exports . fullBuild = fullBuild
286
+ exports . buildLocale = buildLocale
287
+ exports . copyStatic = copyStatic
0 commit comments