Skip to content

Commit

Permalink
update all-products so it reads from the homepage frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahs committed Apr 1, 2021
1 parent 73c6785 commit 2295496
Showing 1 changed file with 18 additions and 61 deletions.
79 changes: 18 additions & 61 deletions lib/all-products.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,28 @@
const fs = require('fs')
const path = require('path')
const slash = require('slash')
const assert = require('assert')
const { difference } = require('lodash')
const yaml = require('js-yaml')
const contentDir = path.join(process.cwd(), 'content')
const frontmatter = require('./read-frontmatter')
const getApplicableVersions = require('./get-applicable-versions')
const removeFPTFromPath = require('./remove-fpt-from-path')

// the product order is determined by data/products.yml
const productsFile = path.join(process.cwd(), 'data/products.yml')
const productsYml = yaml.load(fs.readFileSync(productsFile, 'utf8'))
const sortedProductIds = productsYml.productsInOrder

const contentProductIds = fs.readdirSync(contentDir, { withFileTypes: true })
.map(entry => {
// `fs.readdir` provides file entries based on `fs.lstat`, which doesn't
// resolve symbolic links to their target file/directory. We need to take
// an extra step here to resolve the Early Access symlinked directory.
const { name } = entry
if (entry.isSymbolicLink()) {
entry = fs.statSync(path.join(contentDir, entry.name))
entry.name = name
}
return entry
})
.filter(entry => entry.isDirectory())
.map(entry => entry.name)

// require the content/<subdir> list to match the list in data/products.yml,
// with the exception of content/early-access, which lives in a separate private repo
const publicContentProductIds = contentProductIds.filter(id => id !== 'early-access')
assert(difference(sortedProductIds, publicContentProductIds).length === 0)
assert(difference(publicContentProductIds, sortedProductIds).length === 0)
// Both internal and external products are specified in content/index.md
const homepage = path.posix.join(process.cwd(), 'content/index.md')
const { data } = frontmatter(fs.readFileSync(homepage, 'utf8'))
const productIds = data.children
const externalProducts = data.externalProducts

const internalProducts = {}

// add optional early access content dir to sorted products list if present
const earlyAccessId = contentProductIds.find(id => id === 'early-access')
if (earlyAccessId) sortedProductIds.push(earlyAccessId)

sortedProductIds.forEach(productId => {
productIds.forEach(productId => {
const relPath = productId
const dir = slash(path.join('content', relPath))
const toc = slash(path.join(dir, 'index.md'))
const dir = path.posix.join('content', relPath)

// Early Access may not exist in the current checkout
if (!fs.existsSync(dir)) return

const toc = path.posix.join(dir, 'index.md')
const { data } = frontmatter(fs.readFileSync(toc, 'utf8'))
const applicableVersions = getApplicableVersions(data.versions, toc)
const href = removeFPTFromPath(slash(path.join('/', applicableVersions[0], productId)))
const href = removeFPTFromPath(path.posix.join('/', applicableVersions[0], productId))

internalProducts[productId] = {
id: productId,
Expand All @@ -62,27 +37,9 @@ sortedProductIds.forEach(productId => {
internalProducts[productId].versions = applicableVersions
})

const externalProducts = {
cli: {
id: 'cli',
name: 'GitHub CLI',
href: 'https://cli.github.com/manual',
external: true
},
atom: {
id: 'atom',
name: 'Atom',
href: 'https://atom.io/docs',
external: true
},
electron: {
id: 'electron',
name: 'Electron',
href: 'https://electronjs.org/docs',
external: true
}
}

const allProducts = Object.assign({}, internalProducts, externalProducts)
const productMap = Object.assign({}, internalProducts, externalProducts)

module.exports = allProducts
module.exports = {
productIds,
productMap
}

0 comments on commit 2295496

Please sign in to comment.