2018-09-08 16:57:46 -04:00
|
|
|
module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage, log) {
|
2018-08-29 12:23:08 -04:00
|
|
|
return {
|
2018-09-08 16:57:46 -04:00
|
|
|
$runAfter: ['extractDecoratedClassesProcessor', 'computeIdsProcessor'],
|
|
|
|
$runBefore: ['createSitemap'],
|
2018-09-21 04:32:50 -04:00
|
|
|
exportDocTypes: ['directive', 'pipe'],
|
2018-08-29 12:23:08 -04:00
|
|
|
$process(docs) {
|
2018-09-08 16:57:46 -04:00
|
|
|
// Match all the directives/pipes to their module
|
|
|
|
const errors = [];
|
|
|
|
docs.forEach(doc => {
|
2018-09-21 04:32:50 -04:00
|
|
|
if (this.exportDocTypes.indexOf(doc.docType) !== -1) {
|
2018-09-08 16:57:46 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.ngModules.forEach((ngModule, index) => {
|
|
|
|
|
|
|
|
const ngModuleDocs = getDocFromAlias(ngModule, doc);
|
|
|
|
|
|
|
|
if (ngModuleDocs.length === 0) {
|
|
|
|
errors.push(createDocMessage(`"@ngModule ${ngModule}" does not match a public NgModule`, doc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngModuleDocs.length > 1) {
|
|
|
|
errors.push(createDocMessage(`"@ngModule ${ngModule}" is ambiguous. Matches: ${ngModuleDocs.map(d => d.id).join(', ')}`, doc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ngModuleDoc = ngModuleDocs[0];
|
2018-09-21 04:32:50 -04:00
|
|
|
const containerName = getContainerName(doc.docType);
|
|
|
|
const container = ngModuleDoc[containerName] = ngModuleDoc[containerName] || [];
|
2018-09-08 16:57:46 -04:00
|
|
|
container.push(doc);
|
|
|
|
|
|
|
|
doc.ngModules[index] = ngModuleDoc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (errors.length) {
|
|
|
|
errors.forEach(error => log.error(error));
|
|
|
|
throw new Error('Failed to process NgModule relationships.');
|
|
|
|
}
|
2018-09-21 04:32:50 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-08-29 12:23:08 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2018-09-21 04:32:50 -04:00
|
|
|
|
|
|
|
function getContainerName(docType) {
|
|
|
|
return docType + 's';
|
|
|
|
}
|
|
|
|
|
|
|
|
function byId(a, b) {
|
|
|
|
return a.id > b.id ? 1 : -1;
|
|
|
|
}
|