forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (136 loc) · 5.44 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { fileURLToPath } from 'url'
import fs from 'fs'
import path from 'path'
import { readCompressedJsonFileFallback } from '../read-json-file.js'
import { getAutomatedPageMiniTocItems } from '../get-mini-toc-items.js'
import { allVersions } from '../all-versions.js'
import languages from '../languages.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const schemasPath = path.join(__dirname, 'static/decorated')
const ENABLED_APPS_FILENAME = path.join(__dirname, 'static/apps/enabled-for-apps.json')
const contentPath = path.join(process.cwd(), 'content/rest')
/*
Loads the schemas from the static/decorated folder into a single
object organized by version. Not all products are calendar date
versioned.
Example:
{
free-pro-team@latest: {
2022-08-09: {
category: {
subcategory: [operations],
}
},
2022-11-14: {
category: {
subcategory: [operations],
}
}
}
enterprise-server@3.2: {
'not_api_versioned': {
category: {
subcategory: [operations],
}
}
}
}
*/
const NOT_API_VERSIONED = 'not_api_versioned'
const restOperationData = new Map()
const restOperations = new Map()
Object.keys(languages).forEach((language) => {
restOperationData.set(language, new Map())
Object.keys(allVersions).forEach((version) => {
// setting to undefined will allow us to perform checks
// more easily later on
restOperationData.get(language).set(version, new Map())
if (allVersions[version].apiVersions && allVersions[version].apiVersions.length > 0) {
allVersions[version].apiVersions.forEach((date) => {
restOperationData.get(language).get(version).set(date, new Map())
})
} else {
// Products that are not been calendar date versioned
restOperationData.get(language).get(version).set(NOT_API_VERSIONED, new Map())
}
})
})
export const categoriesWithoutSubcategories = fs
.readdirSync(contentPath)
.filter((file) => {
return file.endsWith('.md') && !file.includes('index.md') && !file.includes('README.md')
})
.map((filteredFile) => filteredFile.replace('.md', ''))
// version: plan + release e.g. For ghes-3.5, ghes is the plan and 3.5 is the release
// apiVersion (not all versions have apiVersions): REST API Calendar Dates
// openApiVersion (below, every version has an openApiVersion mapping): There's a mapping between our Docs versions
// and the OpenApi Version bc it's not the same
export default async function getRest(version, apiVersion, category, subCategory) {
const openApiVersion = getOpenApiVersion(version)
const filename = apiVersion ? `${openApiVersion}.${apiVersion}.json` : `${openApiVersion}.json`
const apiDate = apiVersion || NOT_API_VERSIONED
if (!restOperations.has(openApiVersion)) {
restOperations.set(openApiVersion, new Map())
restOperations.get(openApiVersion).set(apiDate, new Map())
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
restOperations
.get(openApiVersion)
.set(apiDate, readCompressedJsonFileFallback(path.join(schemasPath, filename)))
} else if (!restOperations.get(openApiVersion).has(apiDate)) {
restOperations.get(openApiVersion).set(apiDate, new Map())
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
restOperations
.get(openApiVersion)
.set(apiDate, readCompressedJsonFileFallback(path.join(schemasPath, filename)))
}
if (subCategory) {
return restOperations.get(openApiVersion).get(apiDate)[category][subCategory]
} else if (category) {
return restOperations.get(openApiVersion).get(apiDate)[category]
} else {
return restOperations.get(openApiVersion).get(apiDate)
}
}
function getOpenApiVersion(version) {
if (!(version in allVersions)) {
throw new Error(`Unrecognized version '${version}'. Not found in ${Object.keys(allVersions)}`)
}
return allVersions[version].openApiVersionName
}
// Generates the miniToc for a rest reference page.
export async function getRestMiniTocItems(
category,
subCategory,
apiVersion,
restOperations,
language,
version,
context
) {
const apiDate = apiVersion || NOT_API_VERSIONED
if (!restOperationData.get(language).get(version).get(apiDate).has(category)) {
restOperationData.get(language).get(version).get(apiDate).set(category, new Map())
}
if (!restOperationData.get(language).get(version).get(apiDate).get(category).get(subCategory)) {
const languageTree = restOperationData.get(language)
const titles = restOperations.map((operation) => operation.title)
const restOperationsMiniTocItems = await getAutomatedPageMiniTocItems(titles, context, 3)
languageTree.get(version).get(apiDate).get(category).set(subCategory, {
restOperationsMiniTocItems,
})
restOperationData.set(restOperationData, languageTree)
}
return restOperationData.get(language).get(version).get(apiDate).get(category).get(subCategory)
}
const enabledForApps = {}
export async function getEnabledForApps(docsVersion, apiVersion) {
if (Object.keys(enabledForApps).length === 0) {
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
Object.assign(enabledForApps, readCompressedJsonFileFallback(ENABLED_APPS_FILENAME))
}
const openApiVersion = getOpenApiVersion(docsVersion) + (apiVersion ? `.${apiVersion}` : '')
return enabledForApps[openApiVersion]
}