forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall-products.js
87 lines (76 loc) · 2.85 KB
/
all-products.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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('@github-docs/frontmatter')
const getApplicableVersions = require('./get-applicable-versions')
// 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)
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 => {
const relPath = productId
const dir = slash(path.join('content', relPath))
const toc = slash(path.join(dir, 'index.md'))
const { data } = frontmatter(fs.readFileSync(toc, 'utf8'))
const applicableVersions = getApplicableVersions(data.versions, toc)
const href = slash(path.join('/', applicableVersions[0], productId))
internalProducts[productId] = {
id: productId,
name: data.shortTitle || data.title,
href,
dir,
toc,
wip: data.wip || false,
hidden: data.hidden || false
}
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)
module.exports = allProducts