@govbr-ds/webcomponents
Version:
Biblioteca de Web Components baseado no GovBR-DS
50 lines (49 loc) • 2.29 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 evitar IO repetido durante o mesmo processo de build
const sectionsCache = new Map();
/**
* Lê os arquivos markdown de uma pasta "sections" associada ao componente.
* Agora suporta subcomponentes em diretórios aninhados (ex: footer/footer-legal).
*
* @param componentTag Tag do componente (ex: br-footer-legal)
* @param basePath Raiz do monorepo
* @param componentDirRelative Caminho relativo real dentro de src/components (ex: footer/footer-legal). Se não informado tenta resolver pela heurística original (tag sem br-)
*/
export const sectionsToMarkdown = (componentTag, basePath, componentDirRelative) => {
const usageData = {};
const componentsRoot = path.resolve(basePath, 'packages/webcomponents/src/components');
// Primeiro tenta pelo caminho relativo real fornecido
const candidatePaths = [];
if (componentDirRelative !== undefined) {
candidatePaths.push(path.resolve(componentsRoot, componentDirRelative, 'sections'));
}
// Heurística antiga (estrutura plana) - só adiciona se diferente para evitar duplicado
const flatHeuristic = path.resolve(componentsRoot, componentTag.replace('br-', ''), 'sections');
if (candidatePaths.includes(flatHeuristic) === false)
candidatePaths.push(flatHeuristic);
// Procura um diretório existente
const directoryPath = candidatePaths.find((p) => fs.existsSync(p));
if (directoryPath === undefined)
return usageData;
const cached = sectionsCache.get(directoryPath);
if (cached !== undefined)
return cached;
const files = fs.readdirSync(directoryPath);
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
const fileExtension = path.extname(file);
if (fileExtension === '.md') {
const fileNameWithoutExtension = path.basename(file, fileExtension);
const fileContent = fs.readFileSync(filePath, 'utf8');
usageData[fileNameWithoutExtension] = fileContent;
}
});
sectionsCache.set(directoryPath, usageData);
return usageData;
};
//# sourceMappingURL=markdown-sections.js.map