@govbr-ds/webcomponents
Version:
Biblioteca de Web Components baseado no GovBR-DS
41 lines (40 loc) • 1.66 kB
JavaScript
/*!
* Construído por SERPRO
* © https://serpro.gov.br/ - MIT License.
*/
import * as fs from "fs";
import * as path from "path";
// Cache em memória para reuso
const examplesCache = new Map();
/**
* Lê arquivos de exemplos (html, md, etc) suportando subcomponentes em diretórios aninhados.
* @param componentTag Tag (br-footer-legal)
* @param basePath Raiz monorepo
* @param componentDirRelative Caminho relativo real de src/components (ex: footer/footer-legal) para heurística alternativa
*/
export const examplesToMarkdown = (componentTag, basePath, componentDirRelative) => {
const usageData = {};
const pagesRoot = path.resolve(basePath, 'packages/webcomponents/src/pages/components');
const candidatePaths = [];
if (componentDirRelative !== undefined) {
candidatePaths.push(path.resolve(pagesRoot, componentDirRelative));
}
const flatHeuristic = path.resolve(pagesRoot, componentTag.replace('br-', ''));
if (candidatePaths.includes(flatHeuristic) === false)
candidatePaths.push(flatHeuristic);
const directoryPath = candidatePaths.find((p) => fs.existsSync(p));
if (directoryPath === undefined)
return usageData;
const cached = examplesCache.get(directoryPath);
if (cached !== undefined)
return cached;
const files = fs.readdirSync(directoryPath);
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
const fileContent = fs.readFileSync(filePath, 'utf8');
usageData[file] = fileContent;
});
examplesCache.set(directoryPath, usageData);
return usageData;
};
//# sourceMappingURL=markdown-examples.js.map