forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·328 lines (300 loc) · 10.6 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#! /usr/bin/env node
'use strict'
// BUILD.JS: This file is responsible for building static HTML pages
const Metalsmith = require('metalsmith')
const autoprefixer = require('autoprefixer-stylus')
const collections = require('metalsmith-collections')
const feed = require('metalsmith-feed')
const discoverHelpers = require('metalsmith-discover-helpers')
const discoverPartials = require('metalsmith-discover-partials')
const layouts = require('metalsmith-layouts')
const markdown = require('metalsmith-markdown')
const prism = require('metalsmith-prism')
const stylus = require('metalsmith-stylus')
const permalinks = require('metalsmith-permalinks')
const pagination = require('metalsmith-yearly-pagination')
const defaultsDeep = require('lodash.defaultsdeep')
const marked = require('marked')
const path = require('path')
const fs = require('fs')
const ncp = require('ncp')
const junk = require('junk')
const navigation = require('./scripts/plugins/navigation')
const filterStylusPartials = require('./scripts/plugins/filter-stylus-partials')
const anchorMarkdownHeadings = require('./scripts/plugins/anchor-markdown-headings')
const loadVersions = require('./scripts/load-versions')
const latestVersion = require('./scripts/helpers/latestversion')
// Set the default language, also functions as a fallback for properties which
// are not defined in the given language.
const DEFAULT_LANG = 'en'
// Set up the Markdown renderer that we'll use for our Metalsmith build process,
// with the necessary adjustments that we need to make in order to have Prism
// work.
const renderer = new marked.Renderer()
renderer.heading = anchorMarkdownHeadings
const markedOptions = {
langPrefix: 'language-',
renderer: renderer
}
// This function imports a given language file and uses the default language set
// in DEFAULT_LANG as a fallback to prevent any strings that aren't filled out
// from appearing as blank.
function i18nJSON (lang) {
const defaultJSON = require(`./locale/${DEFAULT_LANG}/site.json`)
const templateJSON = require(`./locale/${lang}/site.json`)
return defaultsDeep({}, templateJSON, defaultJSON)
}
// This is the function where the actual magic happens. This contains the main
// Metalsmith build cycle used for building a locale subsite, such as the
// english one.
function buildLocale (source, locale, opts) {
console.log(`[metalsmith] build/${locale} started`)
const labelForBuild = `[metalsmith] build/${locale} finished`
console.time(labelForBuild)
const metalsmith = Metalsmith(__dirname)
metalsmith
// Sets global metadata imported from the locale's respective site.json.
.metadata({ site: i18nJSON(locale), project: source.project })
// Sets the build source as the locale folder.
.source(path.join(__dirname, 'locale', locale))
.use(withPreserveLocale(opts && opts.preserveLocale))
// Extracts the main menu and sub-menu links form locale's site.json and
// adds them to the metadata. This data is used in the navigation template
.use(navigation(source.project.latestVersions))
// Defines the blog post/guide collections used to internally group them for
// easier future handling and feed generation.
.use(collections({
blog: {
pattern: 'blog/**/*.md',
sortBy: 'date',
reverse: true,
refer: false
},
blogAnnounce: {
pattern: 'blog/announcements/*.md',
sortBy: 'date',
reverse: true,
refer: false
},
blogReleases: {
pattern: 'blog/release/*.md',
sortBy: 'date',
reverse: true,
refer: false
},
blogVulnerability: {
pattern: 'blog/vulnerability/*.md',
sortBy: 'date',
reverse: true,
refer: false
},
lastWeekly: {
pattern: 'blog/weekly-updates/*.md',
sortBy: 'date',
reverse: true,
refer: false,
limit: 1
},
knowledgeBase: {
pattern: 'knowledge/**/*.md',
refer: false
},
guides: {
pattern: 'docs/guides/!(index).md'
}
}))
.use(pagination({
path: 'blog/year',
iteratee: (post, idx) => ({
post,
displaySummary: idx < 10
})
}))
.use(markdown(markedOptions))
.use(githubLinks({ locale: locale, site: i18nJSON(locale) }))
.use(prism())
// Set pretty permalinks, we don't want .html suffixes everywhere.
.use(permalinks({
relative: false
}))
// Generates the feed XML files from their respective collections which were
// defined earlier on.
.use(feed({
collection: 'blog',
destination: 'feed/blog.xml',
title: 'Node.js Blog'
}))
.use(feed({
collection: 'blogAnnounce',
destination: 'feed/announce.xml',
title: 'Node.js Announcements'
}))
.use(feed({
collection: 'blogReleases',
destination: 'feed/releases.xml',
title: 'Node.js Blog: Releases'
}))
.use(feed({
collection: 'blogVulnerability',
destination: 'feed/vulnerability.xml',
title: 'Node.js Blog: Vulnerability Reports'
}))
// Finally, this compiles the rest of the layouts present in ./layouts.
// They're language-agnostic, but have to be regenerated for every locale
// anyways.
.use(discoverPartials({
directory: 'layouts/partials',
pattern: /\.hbs$/
}))
.use(discoverHelpers({
directory: 'scripts/helpers',
pattern: /\.js$/
}))
.use(layouts())
// Pipes the generated files into their respective subdirectory in the build
// directory.
.destination(path.join(__dirname, 'build', locale))
// This actually executes the build and stops the internal timer after
// completion.
metalsmith.build((err) => {
if (err) { throw err }
console.timeEnd(labelForBuild)
})
}
// This plugin reads the files present in the english locale that are missing
// in the current locale being built (requires preserveLocale flag)
function withPreserveLocale (preserveLocale) {
return (files, m, next) => {
if (preserveLocale) {
var path = m.path('locale/en')
m.read(path, (err, newfiles) => {
if (err) {
console.error(err)
return next(err)
}
Object.keys(newfiles).forEach((key) => {
if (!files[key]) {
files[key] = newfiles[key]
}
})
next()
})
} else {
next()
}
}
}
// This middleware adds "Edit on GitHub" links to every editable page
function githubLinks (options) {
return (files, m, next) => {
// add suffix (".html" or "/") to each part of regex
// to ignore possible occurrences in titles (e.g. blog posts)
const isEditable = /security\.html|about\/|docs\/|foundation\/|get-involved\/|knowledge\//
Object.keys(files).forEach((path) => {
if (!isEditable.test(path)) {
return
}
const file = files[path]
const url = `https://github.com/nodejs/nodejs.org/edit/master/locale/${options.locale}/${path.replace('.html', '.md')}`
const editText = options.site.editOnGithub || 'Edit on GitHub'
const contents = file.contents.toString().replace(/<h1(.*?)>(.*?)<\/h1>/, (match, $1, $2) => {
return `<a class="edit-link" href="${url}">${editText}</a> <h1${$1}>${$2}</h1>`
})
file.contents = Buffer.from(contents)
})
next()
}
}
// This function builds the layouts folder for all the Stylus files.
function buildLayouts () {
console.log('[metalsmith] build/layouts started')
const labelForBuild = '[metalsmith] build/layouts finished'
console.time(labelForBuild)
fs.mkdir(path.join(__dirname, 'build'), () => {
fs.mkdir(path.join(__dirname, 'build', 'layouts'), () => {
const metalsmith = Metalsmith(__dirname)
metalsmith
// Sets the build source as /layouts/css.
.source(path.join(__dirname, 'layouts', 'css'))
// Deletes Stylus partials since they'll be included in the main CSS
// file anyways.
.use(filterStylusPartials())
.use(stylus({
compress: true,
paths: [path.join(__dirname, 'layouts', 'css')],
use: [autoprefixer()]
}))
// Pipes the generated files into /build/layouts/css.
.destination(path.join(__dirname, 'build', 'layouts', 'css'))
// This actually executes the build and stops the internal timer after
// completion.
metalsmith.build((err) => {
if (err) { throw err }
console.timeEnd(labelForBuild)
})
})
})
}
// This function copies the rest of the static assets to their subfolder in the
// build directory.
function copyStatic () {
console.log('[metalsmith] build/static started')
console.time('[metalsmith] build/static finished')
fs.mkdir(path.join(__dirname, 'build'), () => {
fs.mkdir(path.join(__dirname, 'build', 'static'), () => {
ncp(path.join(__dirname, 'static'), path.join(__dirname, 'build', 'static'), (err) => {
if (err) { return console.error(err) }
console.timeEnd('[metalsmith] build/static finished')
})
})
})
}
function getSource (callback) {
// Loads all node/io.js versions.
loadVersions((err, versions) => {
const source = {
project: {
versions,
latestVersions: {
current: latestVersion.current(versions),
lts: latestVersion.lts(versions)
},
banner: {
visible: false,
text: 'New security releases now available for all release lines',
link: '/en/blog/vulnerability/february-2019-security-releases/'
}
}
}
callback(err, source)
})
}
// This is where the build is orchestrated from, as indicated by the function
// name. It brings together all build steps and dependencies and executes them.
function fullBuild (opts) {
const { preserveLocale, selectedLocales } = opts
// Build static files.
copyStatic()
// Build layouts
buildLayouts()
getSource((err, source) => {
if (err) { throw err }
// Executes the build cycle for every locale.
fs.readdir(path.join(__dirname, 'locale'), (e, locales) => {
locales.filter(file => junk.not(file) && (selectedLocales ? selectedLocales.includes(file) : true))
.forEach((locale) => {
buildLocale(source, locale, { preserveLocale })
})
})
})
}
// Starts the build if the file was executed from the command line
if (require.main === module) {
const preserveLocale = process.argv.includes('--preserveLocale')
const selectedLocales = process.env.DEFAULT_LOCALE ? process.env.DEFAULT_LOCALE.toLowerCase().split(',') : process.env.DEFAULT_LOCALE
fullBuild({ selectedLocales, preserveLocale })
}
exports.getSource = getSource
exports.fullBuild = fullBuild
exports.buildLocale = buildLocale
exports.copyStatic = copyStatic