-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.js
226 lines (195 loc) · 6.98 KB
/
api.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require('dotenv').config()
import { readJsonSync } from 'fs-extra'
import { difference } from 'lodash-es'
import { gt, compare as compareSemVers } from 'semver'
import downloadApiDocs from './api-docs-sync'
import algoliaDriver from './drivers/algolia'
import jsonDriver from './drivers/json'
import schemas from './schemas'
const apiIndexes = ['modules', 'classes', 'methods', 'versions']
export async function runApi(clearIndex = false, useJsonDriver = false) {
let driver = useJsonDriver ? jsonDriver : algoliaDriver
await downloadApiDocs()
apiIndexes.map(driver.init)
if (clearIndex) {
apiIndexes.map(driver.clear)
}
await Promise.all([
processDocs(driver, 'ember'),
processDocs(driver, 'ember-data'),
])
}
async function processDocs(driver, project) {
let prevIndexedVersions = await driver.getPreviouslyIndexedVersions(project)
const {
meta: { availableVersions },
} = readJsonSync(`./tmp/rev-index/${project}.json`)
let versionsToProcess = difference(availableVersions, prevIndexedVersions)
if (versionsToProcess.length === 0) {
console.log(`No new versions to process for ${project}`)
return
}
try {
// iterate versions and drop latest minor of each major in buckets
// make an array of the latest minors you get
let latestPatches = Object.values(versionsToProcess.reduce(addIfLatestPatch, {}));
console.log(`Processing ${project} for versions: ${latestPatches}`)
await latestPatches
.filter(version => filterMissingRevs(version, project))
.map(version => readIndexFileForVersion(version, project))
// Fetch all public modules and public classes
.map(versionIndexObject =>
fetchPublicModuleClassesForVersion(versionIndexObject, project)
)
// Run the schema against all data stored
.map(mapDataForVersion)
.map(content => writeToDriver(driver, content))
let versions = [...prevIndexedVersions, ...versionsToProcess].sort(
compareSemVers
)
await driver.write(
'versions',
[{
id: project,
name: project,
index_date_timestamp: Date.now(),
versions
}],
project
)
} catch (err) {
console.log('Error:: ', err)
}
}
function addIfLatestPatch(latestPatches, version) {
let semvers = version.split('.')
let major = semvers[0];
let minor = semvers[1];
let minorVersion = `${major}.${minor}`;
if (minorVersion in latestPatches && gt(version, latestPatches[minorVersion])) {
latestPatches[minorVersion] = version;
} else if (!(minorVersion in latestPatches)) {
latestPatches[minorVersion] = version;
}
return latestPatches;
}
function filterMissingRevs(version, libName) {
const emberVersionJSONPath = `./tmp/rev-index/${libName}-${version}.json`
let isIncluded = true
try {
readJsonSync(emberVersionJSONPath)
} catch(e) {
isIncluded = false
}
return isIncluded
}
function readIndexFileForVersion(version, libName) {
const emberVersionJSONPath = `./tmp/rev-index/${libName}-${version}.json`
console.debug(`OPENING:: ${emberVersionJSONPath}`)
return readJsonSync(emberVersionJSONPath)
}
function fetchPublicModuleClassesForVersion(versionIndexObject, libName) {
const publicModules = versionIndexObject.data.relationships[
'public-modules'
].data.map(module => {
// Module names are uri encoded
const id = encodeURIComponent(module.id)
const modulePath = `./tmp/json-docs/${libName}/${
versionIndexObject.data.attributes.version
}/modules/${versionIndexObject.meta.module[id]}.json`
console.debug(`OPENING:: ${modulePath}`)
return readJsonSync(modulePath)
})
const publicClasses = versionIndexObject.data.relationships[
'public-classes'
].data.map(classObj => {
// Class names are uri encoded
const id = encodeURIComponent(classObj.id)
const classPath = `./tmp/json-docs/${libName}/${
versionIndexObject.data.attributes.version
}/classes/${versionIndexObject.meta.class[id]}.json`
console.debug(`OPENING:: ${classPath}`)
return readJsonSync(classPath)
})
return {
version: versionIndexObject,
publicModules,
publicClasses,
}
}
/**
* Map the data for version
*
* @param {object} versionObject - The version object to map
* @returns {object} - Extended version object with methods & mapped schemas
*/
function mapDataForVersion(versionObject) {
const staticFunctions = extractStaticFunctionsFromModules(
versionObject.publicModules
)
const methods = extractMethodsFromClasses(versionObject.publicClasses)
return {
...versionObject,
methods: [...methods, ...staticFunctions],
publicModules: versionObject.publicModules.map(schemas.moduleSchema),
publicClasses: versionObject.publicClasses.map(schemas.classSchema),
}
}
function writeToDriver(driver, versionObject) {
const { id } = versionObject.version.data
let tokens = id.split('-')
let version = tokens.pop()
let projectName = tokens.join('-')
console.info(
`version: ${id}, public classes: ${
versionObject.publicClasses.length
}, public modules: ${versionObject.publicModules.length}, methods: ${
versionObject.methods.length
}`
)
return Promise.all([
driver.write('modules', versionObject.publicModules, projectName, version),
driver.write('classes', versionObject.publicClasses, projectName, version),
driver.write('methods', versionObject.methods, projectName, version),
])
}
/**
* Takes an array of classes, extracts the methods from each one,
* and runs the method schema to transform the payload
* @param {Array} classes - Array of "method" objects.
* @return {Array} - Returns an array of transformed method objects
*/
function extractMethodsFromClasses(classes) {
return classes.reduce((methods, currentClass) => {
return (
currentClass.data.attributes.methods
.reduce((classMethods, currentMethod) => {
// Transform the current method and push on to methods.
classMethods.push(schemas.methodSchema(currentMethod, currentClass))
return classMethods
}, [])
// Merge all methods of all classes into a single array
.concat(methods)
)
}, [])
}
function extractStaticFunctionsFromModules(modules) {
return modules.reduce((methods, currentModule) => {
const staticfunctionsObj = currentModule.data.attributes.staticfunctions
// Guard against staticfunctions not existing.
if (!staticfunctionsObj) return methods
// Extract all the static functions from inside their sub-modules
const moduleStaticFunctions = Object.keys(staticfunctionsObj).reduce(
(prevStaticFunctions, currModuleName) => {
return prevStaticFunctions.concat(staticfunctionsObj[currModuleName])
},
[]
)
return moduleStaticFunctions
.reduce((moduleStaticFunctions, currentStaticFunction) => {
moduleStaticFunctions.push(schemas.methodSchema(currentStaticFunction, currentModule))
return moduleStaticFunctions
}, [])
.concat(methods)
}, [])
}