build(docs-infra): sort NgModule exports by id (#26051)

PR Close #26051
This commit is contained in:
Pete Bacon Darwin 2018-09-21 09:32:50 +01:00 committed by Kara Erickson
parent 56c86c7e79
commit 7f1cace2a2
2 changed files with 32 additions and 15 deletions

View File

@ -2,22 +2,12 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
return {
$runAfter: ['extractDecoratedClassesProcessor', 'computeIdsProcessor'],
$runBefore: ['createSitemap'],
exportDocTypes: ['directive', 'pipe'],
$process(docs) {
docs.forEach(doc => {
if (doc.docType === 'ngmodule') {
Object.keys(doc.ngmoduleOptions).forEach(key => {
const value = doc.ngmoduleOptions[key];
if (value && !Array.isArray(value)) {
doc.ngmoduleOptions[key] = [value];
}
});
}
});
// Match all the directives/pipes to their module
const errors = [];
docs.forEach(doc => {
if (['directive', 'pipe'].indexOf(doc.docType) !== -1) {
if (this.exportDocTypes.indexOf(doc.docType) !== -1) {
if (!doc.ngModules || doc.ngModules.length === 0) {
errors.push(createDocMessage(`"${doc.id}" has no @ngModule tag. Docs of type "${doc.docType}" must have this tag.`, doc));
return;
@ -38,7 +28,8 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
}
const ngModuleDoc = ngModuleDocs[0];
const container = ngModuleDoc[doc.docType + 's'] = ngModuleDoc[doc.docType + 's'] || [];
const containerName = getContainerName(doc.docType);
const container = ngModuleDoc[containerName] = ngModuleDoc[containerName] || [];
container.push(doc);
doc.ngModules[index] = ngModuleDoc;
@ -50,6 +41,32 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
errors.forEach(error => log.error(error));
throw new Error('Failed to process NgModule relationships.');
}
docs.forEach(doc => {
if (doc.docType === 'ngmodule') {
Object.keys(doc.ngmoduleOptions).forEach(key => {
const value = doc.ngmoduleOptions[key];
if (value && !Array.isArray(value)) {
doc.ngmoduleOptions[key] = [value];
}
});
this.exportDocTypes.forEach(type => {
const containerName = getContainerName(type);
const container = doc[containerName];
if (container) {
container.sort(byId);
}
});
}
});
}
};
};
function getContainerName(docType) {
return docType + 's';
}
function byId(a, b) {
return a.id > b.id ? 1 : -1;
}

View File

@ -37,7 +37,7 @@ describe('processNgModuleDocs processor', () => {
expect(docs[0].ngmoduleOptions.c).toEqual([42]);
});
it('should link directive/pipe docs with their NgModule docs', () => {
it('should link directive/pipe docs with their NgModule docs (sorted by id)', () => {
const aliasMap = injector.get('aliasMap');
const ngModule1 = { docType: 'ngmodule', id: 'NgModule1', aliases: ['NgModule1'], ngmoduleOptions: {}};
const ngModule2 = { docType: 'ngmodule', id: 'NgModule2', aliases: ['NgModule2'], ngmoduleOptions: {}};
@ -50,7 +50,7 @@ describe('processNgModuleDocs processor', () => {
aliasMap.addDoc(ngModule1);
aliasMap.addDoc(ngModule2);
processor.$process([ngModule1, ngModule2, directive1, directive2, directive3, pipe1, pipe2, pipe3]);
processor.$process([ngModule1, pipe2, directive2, directive3, pipe1, ngModule2, directive1, pipe3]);
expect(ngModule1.directives).toEqual([directive1, directive3]);
expect(ngModule1.pipes).toEqual([pipe1, pipe3]);