Merge branch 'main' into revert-16952-revert-16947-optimize-sitetree
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
const assert = require('assert')
|
||||
const fs = require('fs').promises
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const walk = require('walk-sync')
|
||||
const { mapLimit } = require('async')
|
||||
const yaml = require('js-yaml')
|
||||
const { isRegExp, set } = require('lodash')
|
||||
const filenameToKey = require('./filename-to-key')
|
||||
|
||||
const FILE_READ_LIMIT = 100
|
||||
|
||||
module.exports = async function dataDirectory (dir, opts = {}) {
|
||||
module.exports = function dataDirectory (dir, opts = {}) {
|
||||
const defaultOpts = {
|
||||
preprocess: (content) => { return content },
|
||||
ignorePatterns: [/README\.md$/i],
|
||||
@@ -42,10 +39,9 @@ module.exports = async function dataDirectory (dir, opts = {}) {
|
||||
// ignore files that don't have a whitelisted file extension
|
||||
return opts.extensions.includes(path.extname(filename).toLowerCase())
|
||||
})
|
||||
const files = await mapLimit(
|
||||
filenames,
|
||||
FILE_READ_LIMIT,
|
||||
async filename => [filename, await fs.readFile(filename, 'utf8')]
|
||||
|
||||
const files = filenames.map(
|
||||
filename => [filename, fs.readFileSync(filename, 'utf8')]
|
||||
)
|
||||
files.forEach(([filename, fileContent]) => {
|
||||
// derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename
|
||||
|
||||
10
lib/page.js
10
lib/page.js
@@ -1,5 +1,5 @@
|
||||
const assert = require('assert')
|
||||
const fs = require('fs').promises
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const cheerio = require('cheerio')
|
||||
const patterns = require('./patterns')
|
||||
@@ -23,13 +23,13 @@ const slash = require('slash')
|
||||
const statsd = require('./statsd')
|
||||
|
||||
class Page {
|
||||
static async init (opts) {
|
||||
opts = await Page.read(opts)
|
||||
static init (opts) {
|
||||
opts = Page.read(opts)
|
||||
if (!opts) return
|
||||
return new Page(opts)
|
||||
}
|
||||
|
||||
static async read (opts) {
|
||||
static read (opts) {
|
||||
assert(opts.relativePath, 'relativePath is required')
|
||||
assert(opts.basePath, 'basePath is required')
|
||||
|
||||
@@ -39,7 +39,7 @@ class Page {
|
||||
// Per https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
|
||||
// its better to read and handle errors than to check access/stats first
|
||||
try {
|
||||
const raw = await fs.readFile(fullPath, 'utf8')
|
||||
const raw = fs.readFileSync(fullPath, 'utf8')
|
||||
return { ...opts, relativePath, fullPath, raw }
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') return false
|
||||
|
||||
24
lib/pages.js
24
lib/pages.js
@@ -2,21 +2,16 @@ const path = require('path')
|
||||
const walk = require('walk-sync').entries
|
||||
const Page = require('./page')
|
||||
const languages = require('./languages')
|
||||
const { mapLimit } = require('async')
|
||||
|
||||
const FILE_READ_LIMIT = 100
|
||||
|
||||
async function loadPageList () {
|
||||
function loadPageList () {
|
||||
// load english pages
|
||||
const englishPath = path.join(__dirname, '..', languages.en.dir, 'content')
|
||||
const englishPaths = walk(englishPath, {
|
||||
globs: ['**/*.md'],
|
||||
ignore: ['**/README.md']
|
||||
})
|
||||
const englishPages = await mapLimit(
|
||||
englishPaths,
|
||||
FILE_READ_LIMIT,
|
||||
async opts => await Page.read({
|
||||
const englishPages = englishPaths.map(
|
||||
opts => Page.read({
|
||||
...opts,
|
||||
languageCode: languages.en.code
|
||||
})
|
||||
@@ -34,11 +29,10 @@ async function loadPageList () {
|
||||
}))
|
||||
})
|
||||
.flat()
|
||||
const localizedPages = await mapLimit(
|
||||
localizedPaths,
|
||||
FILE_READ_LIMIT,
|
||||
async ({ basePath, relativePath, languageCode }) =>
|
||||
await Page.read({ basePath, relativePath, languageCode })
|
||||
|
||||
const localizedPages = localizedPaths.map(
|
||||
({ basePath, relativePath, languageCode }) =>
|
||||
Page.read({ basePath, relativePath, languageCode })
|
||||
)
|
||||
|
||||
// Build out the list of prepared pages
|
||||
@@ -64,8 +58,8 @@ function createMapFromArray (pageList) {
|
||||
return pageMap
|
||||
}
|
||||
|
||||
async function loadPageMap (pageList) {
|
||||
const pages = pageList || await loadPageList()
|
||||
function loadPageMap (pageList) {
|
||||
const pages = pageList || loadPageList()
|
||||
return createMapFromArray(pages)
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ module.exports = new StatsD({
|
||||
prefix: 'docs.',
|
||||
mock,
|
||||
globalTags: {
|
||||
app: 'docs'
|
||||
app: 'docs',
|
||||
heroku_app: process.env.HEROKU_APP_NAME
|
||||
}
|
||||
})
|
||||
|
||||
@@ -7,10 +7,10 @@ const loadSiteTree = require('./site-tree')
|
||||
// Instrument these functions so that
|
||||
// it's wrapped in a timer that reports to Datadog
|
||||
const dog = {
|
||||
loadPages: statsd.asyncTimer(loadPages, 'load_pages'),
|
||||
loadPageMap: statsd.asyncTimer(loadPageMap, 'load_page_map'),
|
||||
loadPages: statsd.timer(loadPages, 'load_pages'),
|
||||
loadPageMap: statsd.timer(loadPageMap, 'load_page_map'),
|
||||
loadRedirects: statsd.asyncTimer(loadRedirects, 'load_redirects'),
|
||||
loadSiteData: statsd.asyncTimer(loadSiteData, 'load_site_data'),
|
||||
loadSiteData: statsd.timer(loadSiteData, 'load_site_data'),
|
||||
loadSiteTree: statsd.asyncTimer(loadSiteTree, 'load_site_tree')
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user