-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.js
131 lines (115 loc) · 4.43 KB
/
main.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
import fs from 'fs-extra';
import rimraf from 'rimraf';
import markup from './lib/markup.js';
import readDocs from './lib/read-docs.js';
import createClassesOnDisk from './lib/create-classes.js';
import transformYuiObject from './lib/transform-yui-object.js';
import normalizeEmberDependencies from './lib/normalize-ember-dependencies.js';
import getVersionIndex from './lib/get-version-index.js';
import saveDoc from './lib/save-document.js';
import revProjVersionFiles from './lib/rev-docs.js';
import fixBorkedYuidocFiles from './lib/fix-borked-yuidoc-files.js';
const docsPath = '../ember-api-docs-data';
async function transformObject(doc, projectName, docVersion) {
try {
const object = await transformYuiObject([doc], projectName);
const { data } = markup(object);
const giantDocument = { data };
console.log('normalizing dependencies');
let transformed = await normalizeEmberDependencies(giantDocument);
transformed = await createClassesOnDisk(transformed, projectName, docVersion);
console.log(`Finished processing ${projectName}-${docVersion}`);
transformed = getVersionIndex(transformed, projectName);
revProjVersionFiles(projectName, docVersion);
return transformed;
} catch (e) {
console.log(e);
throw e;
}
}
async function transformProject(project, projectName) {
const docs = [];
for (const doc of project) {
let docVersion = doc.version;
console.log(`Starting to process ${projectName}-${docVersion}`);
const existingFolder = `${docsPath}/json-docs/${projectName}/${docVersion}`;
if (fs.existsSync(existingFolder)) {
rimraf.sync(existingFolder);
}
const transformed = await transformObject(doc, projectName, docVersion);
docs.push(transformed);
}
let [docToSave, ...remainingDocs] = docs.filter(({ data }) => data.id === projectName);
if (!docToSave) {
return void 0;
}
let existingDoc = `${docsPath}/json-docs/${projectName}/projects/${projectName}.json`;
if (fs.existsSync(existingDoc)) {
existingDoc = fs.readJsonSync(existingDoc);
const newData = docToSave.data.relationships['project-versions'].data;
const oldData = existingDoc.data.relationships['project-versions'].data;
const updatedData = mergeById(newData, oldData);
docToSave.data.relationships['project-versions'].data = updatedData;
}
remainingDocs.forEach(({ data }) => {
docToSave.data.relationships['project-versions'].data = docToSave.data.relationships[
'project-versions'
].data.concat(data.relationships['project-versions'].data);
});
await saveDoc(docToSave, projectName);
return projectName;
}
async function transformProjectsDeep(projects, docs) {
const built = [];
for (const projectName of projects) {
const transformed = await transformProject(docs[projectName], projectName);
built.push(transformed);
}
return built;
}
export async function apiDocsProcessor(projects, specificDocsVersion, runClean) {
let filesToProcess = projects.map(project => {
return `${docsPath}/s3-docs/v${specificDocsVersion}/${project}-docs.json`;
});
await Promise.all(filesToProcess.map(fixBorkedYuidocFiles));
const _transformProjectsDeep = transformProjectsDeep.bind(null, projects);
await readDocs(projects, specificDocsVersion, runClean)
.then(_transformProjectsDeep)
.then(() =>
projects.map(project => {
const projRevFile = `${docsPath}/rev-index/${project}.json`;
let projRevFileContent = fs.readJsonSync(
`${docsPath}/json-docs/${project}/projects/${project}.json`,
);
const availableVersions = [];
projRevFileContent.meta = {
availableVersions,
};
projRevFileContent.data.relationships['project-versions'].data.forEach(({ id }) =>
availableVersions.push(id.replace(`${project}-`, '')),
);
console.log({ project, availableVersions });
fs.writeJsonSync(projRevFile, projRevFileContent, { spaces: 2 });
}),
)
.then(() => {
console.log('\n\n\n');
console.log('Done!');
});
}
function mergeById(arr1, arr2) {
const seen = new Set();
const result = [];
let maxLen = arr1.length > arr2.length ? arr1.length : arr2.length;
for (let i = 0; i < maxLen; i++) {
if (i < arr1.length && !seen.has(arr1[i].id)) {
result.push(arr1[i]);
seen.add(arr1[i].id);
}
if (i < arr2.length && !seen.has(arr2[i].id)) {
result.push(arr2[i]);
seen.add(arr2[i].id);
}
}
return result;
}