UNPKG

@baseplate-dev/sync

Version:

Library for syncing Baseplate descriptions

42 lines 1.84 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 { TEMPLATES_INFO_FILENAME } from '../constants.js'; import { templatesInfoFileSchema } from './metadata.js'; /** * Reads all templates info files in the output directory and returns an array of template metadata file entries. * * @param outputDirectory - The directory to read templates info files from. * @returns An array of template metadata file entries. */ export async function readTemplateInfoFiles(outputDirectory) { const templateInfoFiles = await globby(path.join('**', TEMPLATES_INFO_FILENAME), { absolute: true, onlyFiles: true, fs: fsAdapter, gitignore: true, cwd: outputDirectory, }); const templateFileArrays = await Promise.all(templateInfoFiles.flatMap(async (infoFile) => { const infoFileContents = await readJsonWithSchema(infoFile, templatesInfoFileSchema); return 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) { throw new Error(`Could not find source file (${filename}) specified in templates info file: ${infoFile}`); } return { absolutePath: filePath, templateInfo, modifiedTime, }; })); })); return templateFileArrays.flat(); } //# sourceMappingURL=read-template-info-files.js.map