@@ -24,18 +24,19 @@ const autoprefixer = require('autoprefixer');
24
24
const { marked } = require ( 'marked' ) ;
25
25
const postcss = require ( 'postcss' ) ;
26
26
const sass = require ( 'sass' ) ;
27
- const ncp = require ( 'ncp' ) ;
28
27
const junk = require ( 'junk' ) ;
29
28
const semver = require ( 'semver' ) ;
30
29
const replace = require ( 'metalsmith-one-replace' ) ;
31
- const glob = require ( 'glob' ) ;
32
- const Handlebars = require ( 'handlebars' ) ;
30
+ const fsExtra = require ( 'fs-extra' ) ;
33
31
34
32
const githubLinks = require ( './scripts/plugins/githubLinks' ) ;
35
33
const navigation = require ( './scripts/plugins/navigation' ) ;
36
34
const anchorMarkdownHeadings = require ( './scripts/plugins/anchor-markdown-headings' ) ;
37
35
const loadVersions = require ( './scripts/load-versions' ) ;
38
36
const latestVersion = require ( './scripts/helpers/latestversion' ) ;
37
+ const withPreserveLocale = require ( './scripts/plugins/withPreserveLocale' ) ;
38
+ const scriptReg = require ( './scripts/plugins/scriptReg' ) ;
39
+ const hbsReg = require ( './scripts/plugins/hbsReg' ) ;
39
40
40
41
// Set the default language, also functions as a fallback for properties which
41
42
// are not defined in the given language.
@@ -191,81 +192,20 @@ function buildLocale(source, locale, opts) {
191
192
// Finally, this compiles the rest of the layouts present in ./layouts.
192
193
// They're language-agnostic, but have to be regenerated for every locale
193
194
// anyways.
194
- . use ( ( files , metalsmith , done ) => {
195
- const fsPromises = require ( 'fs/promises' ) ;
196
- glob (
197
- `${ metalsmith . path ( 'layouts/partials' ) } /**/*.hbs` ,
198
- { } ,
199
- async ( err , matches ) => {
200
- if ( err ) {
201
- throw err ;
202
- }
203
- await Promise . all (
204
- matches . map ( async ( file ) => {
205
- const contents = await fsPromises . readFile ( file , 'utf8' ) ;
206
- const id = path . basename ( file , path . extname ( file ) ) ;
207
- return Handlebars . registerPartial ( id , contents ) ;
208
- } )
209
- ) ;
210
- done ( ) ;
211
- }
212
- ) ;
213
- } )
214
- . use ( ( files , metalsmith , done ) => {
215
- glob (
216
- `${ metalsmith . path ( 'scripts/helpers' ) } /**/*.js` ,
217
- { } ,
218
- ( err , matches ) => {
219
- if ( err ) {
220
- throw err ;
221
- }
222
- matches . forEach ( ( file ) => {
223
- const fn = require ( path . resolve ( file ) ) ;
224
- const id = path . basename ( file , path . extname ( file ) ) ;
225
- return Handlebars . registerHelper ( id , fn ) ;
226
- } ) ;
227
- done ( ) ;
228
- }
229
- ) ;
230
- } )
195
+ . use ( hbsReg ( ) )
196
+ . use ( scriptReg ( ) )
231
197
. use ( layouts ( ) )
232
198
// Pipes the generated files into their respective subdirectory in the build
233
199
// directory.
234
- . destination ( path . join ( __dirname , 'build' , locale ) ) ;
235
-
236
- // This actually executes the build and stops the internal timer after
237
- // completion.
238
- metalsmith . build ( ( err ) => {
239
- if ( err ) {
240
- throw err ;
241
- }
242
- console . timeEnd ( labelForBuild ) ;
243
- } ) ;
244
- }
245
-
246
- // This plugin reads the files present in the english locale that are missing
247
- // in the current locale being built (requires preserveLocale flag)
248
- function withPreserveLocale ( preserveLocale ) {
249
- return ( files , m , next ) => {
250
- if ( preserveLocale ) {
251
- const path = m . path ( 'locale/en' ) ;
252
- m . read ( path , ( err , newfiles ) => {
253
- if ( err ) {
254
- console . error ( err ) ;
255
- return next ( err ) ;
256
- }
257
-
258
- Object . keys ( newfiles ) . forEach ( ( key ) => {
259
- if ( ! files [ key ] ) {
260
- files [ key ] = newfiles [ key ] ;
261
- }
262
- } ) ;
263
- next ( ) ;
264
- } ) ;
265
- } else {
266
- next ( ) ;
267
- }
268
- } ;
200
+ . destination ( path . join ( __dirname , 'build' , locale ) )
201
+ // This actually executes the build and stops the internal timer after
202
+ // completion.
203
+ . build ( ( err ) => {
204
+ if ( err ) {
205
+ throw err ;
206
+ }
207
+ console . timeEnd ( labelForBuild ) ;
208
+ } ) ;
269
209
}
270
210
271
211
// This function builds the static/css folder for all the Sass files.
@@ -275,20 +215,20 @@ async function buildCSS() {
275
215
console . time ( labelForBuild ) ;
276
216
277
217
const src = path . join ( __dirname , 'layouts/css/styles.scss' ) ;
278
- const dest = path . join ( __dirname , 'build/static/css/styles.css' ) ;
279
-
280
218
const sassOpts = {
281
219
outputStyle :
282
220
process . env . NODE_ENV !== 'development' ? 'compressed' : 'expanded'
283
221
} ;
284
222
285
- const graceFulFsPromise = gracefulFs . promises ;
223
+ const resultPromise = sass . compileAsync ( src , sassOpts ) ;
224
+
225
+ const dest = path . join ( __dirname , 'build/static/css/styles.css' ) ;
286
226
287
- await graceFulFsPromise . mkdir ( path . join ( __dirname , 'build/static/css' ) , {
227
+ await fsExtra . promises . mkdir ( path . join ( __dirname , 'build/static/css' ) , {
288
228
recursive : true
289
229
} ) ;
290
230
291
- const result = await sass . compileAsync ( src , sassOpts ) ;
231
+ const result = await resultPromise ;
292
232
293
233
postcss ( [ autoprefixer ] )
294
234
. process ( result . css , { from : src } )
@@ -297,57 +237,46 @@ async function buildCSS() {
297
237
console . warn ( warn . toString ( ) ) ;
298
238
} ) ;
299
239
300
- await graceFulFsPromise . writeFile ( dest , res . css ) ;
240
+ await fsExtra . writeFile ( dest , res . css ) ;
301
241
console . timeEnd ( labelForBuild ) ;
302
242
} ) ;
303
243
}
304
244
305
245
// This function copies the rest of the static assets to their subfolder in the
306
246
// build directory.
307
247
async function copyStatic ( ) {
308
- console . log ( '[ncp] build /static started' ) ;
309
- const labelForBuild = '[ncp] build /static finished' ;
248
+ console . log ( '[fsExtra] copy /static started' ) ;
249
+ const labelForBuild = '[fsExtra] copy /static finished' ;
310
250
console . time ( labelForBuild ) ;
311
251
312
- const graceFulFsPromise = gracefulFs . promises ;
313
-
314
- await graceFulFsPromise . mkdir ( path . join ( __dirname , 'build/static' ) , {
252
+ await fsExtra . promises . mkdir ( path . join ( __dirname , 'build/static/js' ) , {
315
253
recursive : true
316
254
} ) ;
317
255
318
- ncp (
319
- path . join ( __dirname , 'static' ) ,
320
- path . join ( __dirname , 'build/static' ) ,
321
- ( error ) => {
322
- if ( error ) {
323
- return console . error ( error ) ;
324
- }
325
-
326
- ncp (
327
- path . join ( __dirname , 'node_modules/jquery/dist/jquery.min.js' ) ,
328
- path . join ( __dirname , 'build/static/js/jquery.min.js' ) ,
329
- ( error ) => {
330
- if ( error ) {
331
- return console . error ( error ) ;
332
- }
256
+ await Promise . all ( [
257
+ fsExtra . copy (
258
+ path . join ( __dirname , 'static' ) ,
259
+ path . join ( __dirname , 'build/static' ) ,
260
+ { overwrite : false , recursive : true }
261
+ ) ,
262
+
263
+ fsExtra . copyFile (
264
+ path . join (
265
+ __dirname ,
266
+ 'node_modules/jquery.fancytable/dist/fancyTable.min.js'
267
+ ) ,
268
+ path . join ( __dirname , 'build/static/js/fancyTable.min.js' ) ,
269
+ fs . constants . COPYFILE_EXCL | fs . constants . COPYFILE_FICLONE
270
+ ) ,
271
+
272
+ fsExtra . copyFile (
273
+ path . join ( __dirname , 'node_modules/jquery/dist/jquery.min.js' ) ,
274
+ path . join ( __dirname , 'build/static/js/jquery.min.js' ) ,
275
+ fs . constants . COPYFILE_EXCL | fs . constants . COPYFILE_FICLONE
276
+ )
277
+ ] ) ;
333
278
334
- ncp (
335
- path . join (
336
- __dirname ,
337
- 'node_modules/jquery.fancytable/dist/fancyTable.min.js'
338
- ) ,
339
- path . join ( __dirname , 'build/static/js/fancyTable.min.js' ) ,
340
- ( error ) => {
341
- if ( error ) {
342
- return console . error ( error ) ;
343
- }
344
- console . timeEnd ( labelForBuild ) ;
345
- }
346
- ) ;
347
- }
348
- ) ;
349
- }
350
- ) ;
279
+ console . timeEnd ( labelForBuild ) ;
351
280
}
352
281
353
282
function getSource ( callback ) {
@@ -379,15 +308,13 @@ function getSource(callback) {
379
308
380
309
// This is where the build is orchestrated from, as indicated by the function
381
310
// name. It brings together all build steps and dependencies and executes them.
382
- function fullBuild ( opts ) {
311
+ async function fullBuild ( opts ) {
383
312
const { selectedLocales, preserveLocale } = opts ;
384
313
getSource ( async ( err , source ) => {
385
314
if ( err ) {
386
315
throw err ;
387
316
}
388
-
389
- const graceFulFsPromise = gracefulFs . promises ;
390
- const locales = await graceFulFsPromise . readdir (
317
+ const locales = await fsExtra . promises . readdir (
391
318
path . join ( __dirname , 'locale' )
392
319
) ;
393
320
0 commit comments