From 7f1cace2a204aa2e47f9441eb8745a500bdc505a Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Fri, 21 Sep 2018 09:32:50 +0100 Subject: [PATCH] build(docs-infra): sort NgModule exports by id (#26051) PR Close #26051 --- .../processors/processNgModuleDocs.js | 43 +++++++++++++------ .../processors/processNgModuleDocs.spec.js | 4 +- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.js b/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.js index bc0c98f3d7..d92a2715b8 100644 --- a/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.js +++ b/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.js @@ -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; +} diff --git a/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.spec.js b/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.spec.js index 24f72c91cc..adfed1035c 100644 --- a/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.spec.js +++ b/aio/tools/transforms/angular-api-package/processors/processNgModuleDocs.spec.js @@ -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]);