forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·212 lines (191 loc) · 5.9 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
#!/usr/bin/env node
'use strict';
const Metalsmith = require('metalsmith');
const autoprefixer = require('autoprefixer-stylus');
const collections = require('metalsmith-collections');
const feed = require('metalsmith-feed');
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 path = require('path');
const fs = require('fs');
const ncp = require('ncp');
const filterStylusPartials = require('./plugins/filter-stylus-partials.js');
const mapHandlebarsPartials = require('./plugins/map-handlebars-partials.js');
/** Build **/
// load template.json for given language, but use default language as fallback
// for properties which are not present in the given language
var Handlebars = require('handlebars');
const DEFAULT_LANG = 'en';
function i18nJSON(lang) {
var defaultJSON = require(`./locale/${DEFAULT_LANG}/site.json`);
var templateJSON = require(`./locale/${lang}/site.json`);
var finalJSON = JSON.parse(JSON.stringify(defaultJSON));
var merge = function(targetJSON, customJSON) {
Object.keys(customJSON).forEach(function(key) {
let value = customJSON[key];
if (typeof value === "object") {
merge(targetJSON[key], value);
} else {
targetJSON[key] = value;
}
});
};
merge(finalJSON, templateJSON)
return finalJSON;
};
function traverse(obj, str) {
return str.split(".").reduce(function(o, x) { return o[x] }, obj);
}
const source = {
project: {
versions: require('./source/versions.json')
}
};
function buildlocale (locale) {
console.time('[metalsmith] build/' + locale + ' finished');
const siteJSON = path.join(__dirname, 'locale', locale, 'site.json');
const metalsmith = Metalsmith(__dirname);
metalsmith
.metadata({
site: require(siteJSON),
project: source.project,
i18n: i18nJSON(locale)
})
.source(path.join(__dirname, 'locale', locale))
.use(collections({
blog : {
pattern: 'blog/**/*.md',
sortBy: 'date',
reverse: true,
refer: false
},
tscMinutes: {
pattern: 'foundation/tsc/minutes/*.md',
sortBy: 'date',
reverse: true,
refer: false
}
}))
.use(markdown({ langPrefix: 'language-' }))
.use(prism())
.use(filterStylusPartials())
.use(stylus({
compress: true,
paths:[path.join(__dirname, 'layouts', 'css')],
use: [autoprefixer()]
}))
.use(permalinks())
.use(feed({
collection: 'blog',
destination: 'blog.xml',
title: 'Node.js Blog'
}))
.use(feed({
collection: 'tscMinutes',
destination: 'tsc-minutes.xml',
title: 'Node.js Technical Steering Committee meetings'
}))
.use(layouts({
engine: 'handlebars',
pattern: '**/*.html',
partials: mapHandlebarsPartials(metalsmith, 'layouts', 'partials'),
helpers: {
equals: function (v1, v2, options) {
return (v1 === v2) ? options.fn(this) : options.inverse(this);
},
i18n: function() {
var env, key;
// function(key, env)
if (arguments.length === 2) {
key = arguments[0];
env = arguments[1];
}
// function(scope, key, env)
if (arguments.length === 3) {
key = arguments[0] + '.' + arguments[1];
env = arguments[2];
}
var data = env.data.root;
var result = traverse(data.i18n, key);
return new Handlebars.SafeString(result);
}
}
}))
.destination(path.join(__dirname, 'build', locale));
metalsmith.build(function (err) {
if (err) { throw err; }
console.timeEnd('[metalsmith] build/' + locale + ' finished');
});
}
function copystatic () {
console.time('[metalsmith] build/static finished');
fs.mkdir(path.join(__dirname, 'build'), function () {
fs.mkdir(path.join(__dirname, 'build', 'static'), function () {
ncp(path.join(__dirname, 'static'), path.join(__dirname, 'build', 'static'), function (err) {
if (err) { return console.error(err); }
console.timeEnd('[metalsmith] build/static finished');
});
});
});
}
function fullbuild () {
copystatic();
fs.readdir(path.join(__dirname, 'locale'), function (e, locales) {
locales.forEach(function (locale) {
buildlocale(locale);
});
});
}
fullbuild();
if (process.argv[2] === 'serve') {
server();
}
function server () {
/** Static file server **/
const st = require('st');
const http = require('http');
const mount = st({
path: path.join(__dirname, 'build'),
cache: false,
index: 'index.html'
});
http.createServer(
function (req, res) { mount(req, res); }
).listen(8080,
function () { console.log('http://localhost:8080/en/'); }
);
/** File Watches for Re-Builds **/
const chokidar = require('chokidar');
const opts = {
persistent: true,
ignoreInitial: true,
followSymlinks: true,
usePolling: true,
alwaysStat: false,
depth: undefined,
interval: 100,
ignorePermissionErrors: false,
atomic: true
};
const locales = chokidar.watch(path.join(__dirname, 'locale'), opts);
const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts);
const staticf = chokidar.watch(path.join(__dirname, 'static'), opts);
function getlocale (p) {
const pre = path.join(__dirname, 'locale');
return p.slice(pre.length + 1, p.indexOf('/', pre.length + 1));
}
locales.on('change', function (p) {
buildlocale(getlocale(p));
});
locales.on('add', function (p) {
buildlocale(getlocale(p));
locales.add(p);
});
layouts.on('change', fullbuild);
layouts.on('add', function (p) { layouts.add(p); fullbuild(); });
staticf.on('change', copystatic);
staticf.on('add', function (p) { staticf.add(p); copystatic(); });
}