Skip to content

Commit ec955d9

Browse files
committed
Extracted the server code out of build.js.
To keep the build process found in build.js as focused as possible, this extracts the server portion into it's own file.
1 parent cbeb2a0 commit ec955d9

File tree

3 files changed

+81
-69
lines changed

3 files changed

+81
-69
lines changed

build.js

+12-67
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
'use strict'
44

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
76

87
const Metalsmith = require('metalsmith')
98
const autoprefixer = require('autoprefixer-stylus')
@@ -65,7 +64,7 @@ function i18nJSON (lang) {
6564
// This is the function where the actual magic happens. This contains the main
6665
// Metalsmith build cycle used for building a locale subsite, such as the
6766
// english one.
68-
function buildlocale (source, locale) {
67+
function buildLocale (source, locale) {
6968
console.time('[metalsmith] build/' + locale + ' finished')
7069
const siteJSON = path.join(__dirname, 'locale', locale, 'site.json')
7170
const metalsmith = Metalsmith(__dirname)
@@ -234,7 +233,7 @@ function githubLinks (options) {
234233

235234
// This function copies the rest of the static assets to their subfolder in the
236235
// build directory.
237-
function copystatic () {
236+
function copyStatic () {
238237
console.time('[metalsmith] build/static finished')
239238
fs.mkdir(path.join(__dirname, 'build'), function () {
240239
fs.mkdir(path.join(__dirname, 'build', 'static'), function () {
@@ -249,9 +248,9 @@ function copystatic () {
249248

250249
// This is where the build is orchestrated from, as indicated by the function
251250
// name. It brings together all build steps and dependencies and executes them.
252-
function fullbuild () {
251+
function fullBuild () {
253252
// Copies static files.
254-
copystatic()
253+
copyStatic()
255254
// Loads all node/io.js versions.
256255
loadVersions(function (err, versions) {
257256
if (err) { throw err }
@@ -272,71 +271,17 @@ function fullbuild () {
272271
// Executes the build cycle for every locale.
273272
fs.readdir(path.join(__dirname, 'locale'), function (e, locales) {
274273
locales.filter(junk.not).forEach(function (locale) {
275-
buildlocale(source, locale)
274+
buildLocale(source, locale)
276275
})
277276
})
278277
})
279278
}
280279

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()
334283
}
335284

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

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"homepage": "https://nodejs.org",
66
"scripts": {
77
"build": "node build.js",
8-
"serve": "node build.js serve",
8+
"serve": "node server.js",
99
"load-versions": "node scripts/load-versions.js",
1010
"start": "npm run serve",
11-
"test": "standard build.js scripts/*.js scripts/**/*.js tests/**/*.js && tape tests/**/*.test.js | faucet"
11+
"test": "standard *.js scripts/*.js scripts/**/*.js tests/**/*.js && tape tests/**/*.test.js | faucet"
1212
},
1313
"repository": {
1414
"type": "git",

server.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
// The server where the site is exposed through a static file server
4+
// while developing locally.
5+
6+
const path = require('path')
7+
const st = require('st')
8+
const http = require('http')
9+
const chokidar = require('chokidar')
10+
const mount = st({
11+
path: path.join(__dirname, 'build'),
12+
cache: false,
13+
index: 'index.html'
14+
})
15+
16+
const build = require('./build')
17+
18+
const port = process.env.PORT || 8080
19+
20+
// Watches for file changes in the locale, layout and static directories, and
21+
// rebuilds the modified one.
22+
const opts = {
23+
persistent: true,
24+
ignoreInitial: true,
25+
followSymlinks: true,
26+
usePolling: true,
27+
alwaysStat: false,
28+
depth: undefined,
29+
interval: 100,
30+
ignorePermissionErrors: false,
31+
atomic: true
32+
}
33+
const locales = chokidar.watch(path.join(__dirname, 'locale'), opts)
34+
const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts)
35+
const statics = chokidar.watch(path.join(__dirname, 'static'), opts)
36+
37+
// Gets the locale name by path.
38+
function getLocale (filePath) {
39+
const pre = path.join(__dirname, 'locale')
40+
return filePath.slice(pre.length + 1, filePath.indexOf('/', pre.length + 1))
41+
}
42+
43+
locales.on('change', (filePath) => {
44+
build.buildLocale(filePath, getLocale(filePath))
45+
})
46+
locales.on('add', (filePath) => {
47+
build.buildLocale(filePath, getLocale(filePath))
48+
locales.add(filePath)
49+
})
50+
51+
layouts.on('change', build.fullBuild)
52+
layouts.on('add', (filePath) => {
53+
layouts.add(filePath)
54+
build.fullBuild()
55+
})
56+
57+
statics.on('change', build.copyStatic)
58+
statics.on('add', (filePath) => {
59+
statics.add(filePath)
60+
build.copyStatic()
61+
})
62+
63+
// Initializes the server and mounts it in the generated build directory.
64+
http.createServer(mount).listen(port, () => console.log(`http://localhost:${port}/en/`))
65+
66+
// Start the initial build of static HTML pages
67+
build.fullBuild()

0 commit comments

Comments
 (0)