Skip to content

Commit 8e8fdfb

Browse files
authored
repo sync
2 parents cb9b4b6 + 7dfea2d commit 8e8fdfb

2 files changed

Lines changed: 11 additions & 18 deletions

File tree

lib/page.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ class Page {
2929

3030
const relativePath = slash(opts.relativePath)
3131
const fullPath = slash(path.join(opts.basePath, relativePath))
32-
const raw = await fs.readFile(fullPath, 'utf8')
3332

34-
return new Page({ ...opts, relativePath, fullPath, raw })
35-
}
36-
37-
static async exists (path) {
33+
// Per https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
34+
// its better to read and handle errors than to check access/stats first
3835
try {
39-
return await fs.stat(path)
36+
const raw = await fs.readFile(fullPath, 'utf8')
37+
return new Page({ ...opts, relativePath, fullPath, raw })
4038
} catch (err) {
4139
if (err.code === 'ENOENT') return false
4240
console.error(err)

lib/pages.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path')
22
const walk = require('walk-sync').entries
33
const Page = require('./page')
44
const languages = require('./languages')
5-
const { mapLimit, filterLimit } = require('async')
5+
const { mapLimit } = require('async')
66

77
const FILE_READ_LIMIT = 100
88

@@ -11,10 +11,10 @@ async function loadPageList () {
1111

1212
// load english pages
1313
const englishPath = path.join(__dirname, '..', languages.en.dir, 'content')
14-
const englishPaths = walk(englishPath)
15-
.filter(({ relativePath }) =>
16-
relativePath.endsWith('.md') && !relativePath.includes('README')
17-
)
14+
const englishPaths = walk(englishPath, {
15+
globs: ['**/*.md'],
16+
ignore: ['**/README.md']
17+
})
1818
const englishPages = await mapLimit(
1919
englishPaths,
2020
FILE_READ_LIMIT,
@@ -23,7 +23,7 @@ async function loadPageList () {
2323
pageList.push(...englishPages)
2424

2525
// load matching pages in other languages
26-
let localizedPaths = Object.values(languages)
26+
const localizedPaths = Object.values(languages)
2727
.filter(({ code }) => code !== 'en')
2828
.map(language => {
2929
const basePath = path.join(__dirname, '..', language.dir, 'content')
@@ -35,18 +35,13 @@ async function loadPageList () {
3535
}))
3636
})
3737
.flat()
38-
localizedPaths = await filterLimit(
39-
localizedPaths,
40-
FILE_READ_LIMIT,
41-
async ({ localizedPath }) => Page.exists(localizedPath)
42-
)
4338
const localizedPages = await mapLimit(
4439
localizedPaths,
4540
FILE_READ_LIMIT,
4641
async ({ basePath, relativePath, languageCode }) =>
4742
await Page.init({ basePath, relativePath, languageCode })
4843
)
49-
pageList.push(...localizedPages)
44+
pageList.push(...localizedPages.filter(Boolean))
5045

5146
return pageList
5247
}

0 commit comments

Comments
 (0)