1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/lib/create-tree.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

56 lines
1.8 KiB
JavaScript

import { fileURLToPath } from 'url'
import path from 'path'
import xFs from 'fs'
import Page from './page.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const fs = xFs.promises
export default async function createTree (originalPath, langObj) {
// This basePath definition is needed both here and in lib/page-data.js because this
// function runs recursively, and the value for originalPath changes on recursive runs.
const basePath = path.posix.join(__dirname, '..', langObj.dir, 'content')
// On recursive runs, this is processing page.children items in `/<link>` format.
// If the path exists as is, assume this is a directory with a child index.md.
// Otherwise, assume it's a child .md file and add `.md` to the path.
let filepath
try {
await fs.access(originalPath)
filepath = `${originalPath}/index.md`
} catch {
filepath = `${originalPath}.md`
}
const relativePath = filepath.replace(`${basePath}/`, '')
// Initialize the Page! This is where the file reads happen.
const page = await Page.init({
basePath,
relativePath,
languageCode: langObj.code
})
if (!page) {
// Do not throw an error if Early Access is not available.
if (relativePath.startsWith('early-access')) return
// Do not throw an error if translated page is not available.
if (langObj.code !== 'en') return
throw Error(`Cannot initialize page for ${filepath} in ${langObj.code}`)
}
// Create the root tree object on the first run, and create children recursively.
const item = {
page
}
// Process frontmatter children recursively.
if (item.page.children) {
item.childPages = (await Promise.all(item.page.children
.map(async (child) => await createTree(path.posix.join(originalPath, child), langObj))))
.filter(Boolean)
}
return item
}