angular/aio/tools/transforms/examples-package/services/getExampleRegion.js
Derek Cormier 7c8ca113c1 build(bazel): replace ignore example feature with bazel equivalent
Comment out the entry in the EXAMPLES map to ignore an example.
2022-11-22 13:51:16 -07:00

33 lines
1.6 KiB
JavaScript

module.exports = function getExampleRegion(exampleMap, createDocMessage, collectExamples, log) {
return function getExampleRegionImpl(doc, relativePath, regionName) {
const EXAMPLES_FOLDERS = collectExamples.exampleFolders;
// Find the example in the folders
var exampleFile;
// Try an "annotated" version first
EXAMPLES_FOLDERS.some(EXAMPLES_FOLDER => { return exampleFile = exampleMap[EXAMPLES_FOLDER][relativePath + '.annotated']; });
// If no annotated version is available then try the actual file
if (!exampleFile) {
EXAMPLES_FOLDERS.some(EXAMPLES_FOLDER => { return exampleFile = exampleMap[EXAMPLES_FOLDER][relativePath]; });
}
// If still no file then we error
if (!exampleFile) {
const message = createDocMessage('Missing example file... relativePath: "' + relativePath + '".', doc) + '\n' +
'Example files can be found in the following relative paths: ' + EXAMPLES_FOLDERS.map(function(folder) { return '"' + folder + '"'; }).join(', ');
throw new Error(message);
}
var sourceCodeDoc = exampleFile.regions[regionName || ''];
if (!sourceCodeDoc) {
const message = createDocMessage('Missing example region... relativePath: "' + relativePath + '", region: "' + regionName + '".', doc) + '\n' +
'Regions available are: ' + Object.keys(exampleFile.regions).map(function(region) { return '"' + region + '"'; }).join(', ');
throw new Error(message);
}
sourceCodeDoc.usedInDoc = doc;
log.debug(`Example region ${sourceCodeDoc.id} used in ${doc.id}`);
return sourceCodeDoc.renderedContent;
};
};