UNPKG

@baseplate-dev/sync

Version:

Library for syncing Baseplate descriptions

65 lines 2.68 kB
import { handleFileNotFoundError, readJsonWithSchema, } from '@baseplate-dev/utils/node'; import { globby } from 'globby'; import fsAdapter from 'node:fs'; import fs from 'node:fs/promises'; import path from 'node:path'; import { shouldIncludeFile } from '../../utils/ignore-patterns.js'; import { TEMPLATES_INFO_FILENAME } from '../constants.js'; import { templatesInfoFileSchema } from './metadata.js'; /** * Reads all templates info files in the output directory and returns template metadata entries. * * @param outputDirectory - The directory to read templates info files from. * @param ignoreInstance - Optional ignore instance to filter out ignored paths * @returns An object containing valid entries and orphaned entries (where the file no longer exists). */ export async function readTemplateInfoFiles(outputDirectory, ignoreInstance) { const templateInfoFiles = await globby([ path.posix.join('**', TEMPLATES_INFO_FILENAME), '!/apps/**', '!/packages/**', ], { absolute: true, onlyFiles: true, fs: fsAdapter, gitignore: true, cwd: outputDirectory, }); // Filter out ignored paths const filteredTemplateInfoFiles = ignoreInstance ? templateInfoFiles.filter((filePath) => { const relativePath = path.relative(outputDirectory, filePath); return shouldIncludeFile(relativePath, ignoreInstance); }) : templateInfoFiles; const entries = []; const orphanedEntries = []; await Promise.all(filteredTemplateInfoFiles.map(async (infoFile) => { const infoFileContents = await readJsonWithSchema(infoFile, templatesInfoFileSchema); await Promise.all(Object.entries(infoFileContents).map(async ([filename, templateInfo]) => { const filePath = path.join(path.dirname(infoFile), filename); const modifiedTime = await fs .stat(filePath) .then((stats) => stats.mtime) .catch(handleFileNotFoundError); if (modifiedTime) { entries.push({ absolutePath: filePath, templateInfo, modifiedTime, }); } else { // File no longer exists - this is an orphaned entry orphanedEntries.push({ absolutePath: filePath, templateInfo, metadataFilePath: infoFile, fileName: filename, }); } })); })); return { entries, orphanedEntries }; } //# sourceMappingURL=read-template-info-files.js.map