Skip to content

Commit 27391a2

Browse files
authored
chore: code refactoring (#4574)
1. Move reg by Handlebars into two files from 'build.js'. 2. Use awaitable fsExtra.promise.xxx instead of callback functions.
1 parent a7e3251 commit 27391a2

File tree

4 files changed

+137
-122
lines changed

4 files changed

+137
-122
lines changed

Diff for: build.js

+49-122
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ const autoprefixer = require('autoprefixer');
2424
const { marked } = require('marked');
2525
const postcss = require('postcss');
2626
const sass = require('sass');
27-
const ncp = require('ncp');
2827
const junk = require('junk');
2928
const semver = require('semver');
3029
const replace = require('metalsmith-one-replace');
31-
const glob = require('glob');
32-
const Handlebars = require('handlebars');
30+
const fsExtra = require('fs-extra');
3331

3432
const githubLinks = require('./scripts/plugins/githubLinks');
3533
const navigation = require('./scripts/plugins/navigation');
3634
const anchorMarkdownHeadings = require('./scripts/plugins/anchor-markdown-headings');
3735
const loadVersions = require('./scripts/load-versions');
3836
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');
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.
307247
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';
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

353282
function 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

Diff for: scripts/plugins/hbsReg.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const glob = require('glob');
2+
const Handlebars = require('handlebars');
3+
const path = require('path');
4+
const fs = require('fs');
5+
6+
// Reg all the *.hbs by Handlebars for rendering
7+
function hbsReg() {
8+
return (_, metalsmith, done) => {
9+
glob(
10+
`${metalsmith.path('layouts/partials')}/**/*.hbs`,
11+
{},
12+
async (err, matches) => {
13+
if (err) {
14+
throw err;
15+
}
16+
17+
const valuePromises = matches.map((file) => {
18+
return new Promise((resolve, reject) => {
19+
fs.readFile(file, 'utf8', (err, value) => {
20+
if (err) return reject(err);
21+
const id = path.basename(file, path.extname(file));
22+
Handlebars.registerPartial(id, value);
23+
return resolve(true);
24+
});
25+
});
26+
});
27+
28+
await Promise.all(valuePromises);
29+
done();
30+
}
31+
);
32+
};
33+
}
34+
35+
module.exports = hbsReg;

Diff for: scripts/plugins/scriptReg.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const glob = require('glob');
2+
const Handlebars = require('handlebars');
3+
const path = require('path');
4+
5+
// Reg all the *.js under `helpers` by Handlebars for rendering
6+
function scriptReg() {
7+
return (_, metalsmith, done) => {
8+
glob(
9+
`${metalsmith.path('scripts/helpers')}/**/*.js`,
10+
{},
11+
(err, matches) => {
12+
if (err) {
13+
throw err;
14+
}
15+
matches.forEach((file) => {
16+
const fn = require(file);
17+
const id = path.basename(file, path.extname(file));
18+
return Handlebars.registerHelper(id, fn);
19+
});
20+
done();
21+
}
22+
);
23+
};
24+
}
25+
26+
module.exports = scriptReg;

Diff for: scripts/plugins/withPreserveLocale.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This plugin reads the files present in the english locale that are missing
2+
// in the current locale being built (requires preserveLocale flag)
3+
4+
function withPreserveLocale(preserveLocale) {
5+
return (files, m, next) => {
6+
if (preserveLocale) {
7+
const path = m.path('locale/en');
8+
m.read(path, (err, newfiles) => {
9+
if (err) {
10+
console.error(err);
11+
return next(err);
12+
}
13+
14+
Object.keys(newfiles).forEach((key) => {
15+
if (!files[key]) {
16+
files[key] = newfiles[key];
17+
}
18+
});
19+
next();
20+
});
21+
} else {
22+
next();
23+
}
24+
};
25+
}
26+
27+
module.exports = withPreserveLocale;

0 commit comments

Comments
 (0)