@@ -24,18 +24,19 @@ const autoprefixer = require('autoprefixer');
2424const { marked } = require ( 'marked' ) ;
2525const postcss = require ( 'postcss' ) ;
2626const sass = require ( 'sass' ) ;
27- const ncp = require ( 'ncp' ) ;
2827const junk = require ( 'junk' ) ;
2928const semver = require ( 'semver' ) ;
3029const replace = require ( 'metalsmith-one-replace' ) ;
31- const glob = require ( 'glob' ) ;
32- const Handlebars = require ( 'handlebars' ) ;
30+ const fsExtra = require ( 'fs-extra' ) ;
3331
3432const githubLinks = require ( './scripts/plugins/githubLinks' ) ;
3533const navigation = require ( './scripts/plugins/navigation' ) ;
3634const anchorMarkdownHeadings = require ( './scripts/plugins/anchor-markdown-headings' ) ;
3735const loadVersions = require ( './scripts/load-versions' ) ;
3836const 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' ) ;
3940
4041// Set the default language, also functions as a fallback for properties which
4142// are not defined in the given language.
@@ -191,81 +192,20 @@ function buildLocale(source, locale, opts) {
191192 // Finally, this compiles the rest of the layouts present in ./layouts.
192193 // They're language-agnostic, but have to be regenerated for every locale
193194 // 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 ( ) )
231197 . use ( layouts ( ) )
232198 // Pipes the generated files into their respective subdirectory in the build
233199 // 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+ } ) ;
269209}
270210
271211// This function builds the static/css folder for all the Sass files.
@@ -275,20 +215,20 @@ async function buildCSS() {
275215 console . time ( labelForBuild ) ;
276216
277217 const src = path . join ( __dirname , 'layouts/css/styles.scss' ) ;
278- const dest = path . join ( __dirname , 'build/static/css/styles.css' ) ;
279-
280218 const sassOpts = {
281219 outputStyle :
282220 process . env . NODE_ENV !== 'development' ? 'compressed' : 'expanded'
283221 } ;
284222
285- const graceFulFsPromise = gracefulFs . promises ;
223+ const resultPromise = sass . compileAsync ( src , sassOpts ) ;
224+
225+ const dest = path . join ( __dirname , 'build/static/css/styles.css' ) ;
286226
287- await graceFulFsPromise . mkdir ( path . join ( __dirname , 'build/static/css' ) , {
227+ await fsExtra . promises . mkdir ( path . join ( __dirname , 'build/static/css' ) , {
288228 recursive : true
289229 } ) ;
290230
291- const result = await sass . compileAsync ( src , sassOpts ) ;
231+ const result = await resultPromise ;
292232
293233 postcss ( [ autoprefixer ] )
294234 . process ( result . css , { from : src } )
@@ -297,57 +237,46 @@ async function buildCSS() {
297237 console . warn ( warn . toString ( ) ) ;
298238 } ) ;
299239
300- await graceFulFsPromise . writeFile ( dest , res . css ) ;
240+ await fsExtra . writeFile ( dest , res . css ) ;
301241 console . timeEnd ( labelForBuild ) ;
302242 } ) ;
303243}
304244
305245// This function copies the rest of the static assets to their subfolder in the
306246// build directory.
307247async 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' ;
310250 console . time ( labelForBuild ) ;
311251
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' ) , {
315253 recursive : true
316254 } ) ;
317255
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+ ] ) ;
333278
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 ) ;
351280}
352281
353282function getSource ( callback ) {
@@ -379,15 +308,13 @@ function getSource(callback) {
379308
380309// This is where the build is orchestrated from, as indicated by the function
381310// name. It brings together all build steps and dependencies and executes them.
382- function fullBuild ( opts ) {
311+ async function fullBuild ( opts ) {
383312 const { selectedLocales, preserveLocale } = opts ;
384313 getSource ( async ( err , source ) => {
385314 if ( err ) {
386315 throw err ;
387316 }
388-
389- const graceFulFsPromise = gracefulFs . promises ;
390- const locales = await graceFulFsPromise . readdir (
317+ const locales = await fsExtra . promises . readdir (
391318 path . join ( __dirname , 'locale' )
392319 ) ;
393320
0 commit comments