Skip to content

Commit f519e07

Browse files
mschinissivakumar-kailasam
authored andcommitted
feat(guides): Finish guides indexing script
1 parent e77749f commit f519e07

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

src/guides.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
require('dotenv').config();
2-
2+
// Dependencies
33
import Bluebird from 'bluebird';
44
import logger from 'src/utils/logger';
55

6+
// Lib
67
import drivers from 'src/drivers';
78
import { readTmpFileAsyncFactory, readTmpFileFactory} from 'src/utils/fs';
9+
import schemas from 'src/schemas';
810

911
// Get 'readTmpFile' and 'readTmpFileAsync' bound by 'api'
1012
const PROJECT_TYPE = 'guides';
@@ -20,7 +22,101 @@ export function run() {
2022
SelectedDriver.init('guides');
2123

2224
return readTmpFileAsync('versions.json')
25+
// Extract available guides versions
2326
.then(emberJson => emberJson.data.attributes['all-versions'])
24-
.map()
27+
// Clear the driver contents
28+
.tap(clearDriver)
29+
.map(getPagesForVersion)
30+
.map(populateSubPagesForVersion)
31+
.map(flattenPagesAndMapSchema)
32+
.map(writeToDriver)
2533
.tap(console.log);
2634
}
35+
36+
/**
37+
* Writes out to the given driver
38+
*
39+
* @param versionObject - Object version to write out
40+
*/
41+
function writeToDriver({ version, pages }) {
42+
// logger.logGreen(`version: ${versionObject.version.data.id}, public classes: ${versionObject.publicClasses.length}, public modules: ${versionObject.publicModules.length}, methods: ${versionObject.methods.length}`);
43+
44+
// Wait for all promises to complete before continuing
45+
return Bluebird.all([
46+
SelectedDriver.write('guides', pages)
47+
]);
48+
}
49+
50+
/**
51+
* Clears the driver indices
52+
*
53+
* @returns {Promise} - Promise with all drivers cleared.
54+
*/
55+
function clearDriver() {
56+
SelectedDriver.clear('guides');
57+
}
58+
59+
function getPagesForVersion(version) {
60+
const pages = readTmpFile(`${version}/pages.json`).data;
61+
62+
return {
63+
version,
64+
pages
65+
};
66+
}
67+
68+
function populateSubPagesForVersion({ version, pages }) {
69+
const populatedPages = pages
70+
.reduce((prev, currentPage) => {
71+
// Return previous result if there are no subpages
72+
if(currentPage.type !== 'pages' || !currentPage.attributes.pages) return prev;
73+
// Populate the pages with the contents of the json file
74+
// Flatten attributes to simplify object
75+
const newPage = {
76+
...currentPage,
77+
...currentPage.attributes,
78+
pages: populateSubPages(version, currentPage.attributes.pages)
79+
};
80+
81+
// Remove attributes.pages to simplify page object
82+
// delete currentPage.attributes;
83+
delete newPage.attributes;
84+
85+
prev.push(newPage);
86+
87+
return prev;
88+
}, []);
89+
90+
return {
91+
version,
92+
pages: populatedPages
93+
};
94+
}
95+
96+
function populateSubPages(version, subPages) {
97+
return subPages
98+
.map(currentPage => {
99+
// Some url attributes include a trailing "/". Make sure that's removed before attempting to load the file
100+
const url = currentPage.url.replace(new RegExp('/$'), '');
101+
// Load page and merge with existing currentPage properties
102+
const pageJson = readTmpFile(`${version}/${url}.json`).data;
103+
return { ...currentPage, ...pageJson };
104+
});
105+
}
106+
107+
function flattenPagesAndMapSchema({ version, pages }) {
108+
const flattenedPages = pages
109+
.reduce((prev, currentPage) => {
110+
console.log('currentPage', currentPage);
111+
// Extract the sub-pages of this page and pass them through the schema
112+
const flattenedSubPages = currentPage.pages
113+
.map(currentSubPage => {
114+
return schemas.guideItemSchema(version, { title: currentPage.title }, currentSubPage);
115+
});
116+
117+
// Merge previous flattened sub-pages with new flattened sub pages
118+
return prev.concat(flattenedSubPages);
119+
}, []);
120+
121+
return { version, pages: flattenedPages };
122+
}

0 commit comments

Comments
 (0)