Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 118 additions & 5 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function i18nJSON (lang) {
// 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) {
function buildLocale (source, locale, opts) {
console.log(`[metalsmith] build/${locale} started`)
console.time(`[metalsmith] build/${locale} finished`)
const metalsmith = Metalsmith(__dirname)
Expand Down Expand Up @@ -164,12 +164,124 @@ function buildLocale (source, locale) {

// This actually executes the build and stops the internal timer after
// completion.
metalsmith.build((err) => {
metalsmith.build((err, files) => {
Comment thread
ZYSzys marked this conversation as resolved.
Outdated
if (err) { throw err }
console.timeEnd(`[metalsmith] build/${locale} finished`)
if (opts && opts.preserveLocale) {
buildMissing(source, locale, files)
}
})
}

function buildMissing (source, locale, localeFiles) {
console.log(`[metalsmith] build/${locale} missing files started`)
console.time(`[metalsmith] build/${locale} missing files finished`)

const metalsmith = Metalsmith(__dirname)
Comment thread
fhemberger marked this conversation as resolved.
Outdated
metalsmith
.clean(false)
.metadata({ site: i18nJSON(locale), project: source.project })
// Sets the build source as the DEFAULT_LANG (en) locale folder.
.source(path.join(__dirname, 'locale', DEFAULT_LANG))
// Ignores files already built with current locale
.ignore([...Object.keys(localeFiles).map(file =>
path.join(__dirname, 'locale', 'en', file.replace('.html', '.md'))
), ...Object.keys(localeFiles)
.filter(key => `${localeFiles[key].path}/index.html` === key)
.map(key => path.join(__dirname, 'locale', 'en', `${localeFiles[key].path}.md`))])
.use(navigation(source.project.latestVersions))
.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 }))
.use(prism())
.use(permalinks({
relative: false
}))
.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'
}))
.use(discoverPartials({
directory: 'layouts/partials',
pattern: /\.hbs$/
}))
.use(discoverHelpers({
directory: 'scripts/helpers',
pattern: /\.js$/
}))
.use(layouts())
.destination(path.join(__dirname, 'build', locale))

if (locale !== DEFAULT_LANG) {
metalsmith.build((err) => {
if (err) { throw err }
console.timeEnd(`[metalsmith] build/${locale} missing files finished`)
})
}
}

// This middleware adds "Edit on GitHub" links to every editable page
function githubLinks (options) {
return (files, m, next) => {
Expand Down Expand Up @@ -267,7 +379,7 @@ function getSource (callback) {

// 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 () {
function fullBuild (opts) {
// Build static files.
copyStatic()
// Build layouts
Expand All @@ -278,15 +390,16 @@ function fullBuild () {
// Executes the build cycle for every locale.
fs.readdir(path.join(__dirname, 'locale'), (e, locales) => {
locales.filter(junk.not).forEach((locale) => {
buildLocale(source, locale)
buildLocale(source, locale, opts)
})
})
})
}

// Starts the build if the file was executed from the command line
if (require.main === module) {
fullBuild()
const preserveLocale = process.argv.includes('--preserveLocale')
fullBuild({ preserveLocale })
}

exports.getSource = getSource
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"homepage": "https://nodejs.org",
"scripts": {
"build": "node build.js",
"build:deploy":"node build.js --preserveLocale",
"serve": "node server.js",
Copy link
Copy Markdown
Member

@ZYSzys ZYSzys Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we also have preserveLocale in server.js, is it more reasonable to serve as node server.js --preserveLocale ? Or add one more script like build:deploy but for serve ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think while developing is less needed to build all the files for every language. I think it's more relevant when deploying. But a second script in package.json might help.

Current options to serve with preserveLocale are:

npm start -- -- --preserveLocale
npm run serve -- --preserveLocale
node server.js --preserveLocale

I could add a script to run:

npm run serve:preserveLocale

"external:survey": "rsync -avz --exclude 'node_modules*' --exclude 'package*' external/survey-2018/ build/en/user-survey-report",
"gzip": "find build -type f \\( -name '*.html' -o -name '*.css' -o -name '*.js' -o -name '*.xml' -o -name '*.json' \\) -exec gzip -kf9 {} \\;",
"deploy": "npm run load-schedule && npm run build && npm run external:survey && npm run gzip",
"deploy": "npm run load-schedule && npm run build:deploy && npm run external:survey && npm run gzip",
"load-versions": "node scripts/load-versions.js",
"load-schedule": "curl -sS https://raw.githubusercontent.com/nodejs/Release/master/schedule.json -o source/schedule.json",
"start": "npm run serve",
Expand Down
4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const build = require('./build')

const port = process.env.PORT || 8080

const preserveLocale = process.argv.includes('--preserveLocale')

// Watches for file changes in the locale, layout and static directories, and
// rebuilds the modified one.
const opts = {
Expand Down Expand Up @@ -108,4 +110,4 @@ http.createServer((req, res) => {
}).listen(port, () => console.log(`\x1B[32mServer running at http://localhost:${port}/en/\x1B[39m`))
Comment thread
fhemberger marked this conversation as resolved.
Outdated

// Start the initial build of static HTML pages
build.fullBuild()
build.fullBuild({ preserveLocale })