@keystatic/core

Search for an npm package
import fs from 'fs/promises';
import path from 'path';
import { b as getDirectoriesForTreeKey, c as getCollectionPath, o as object, e as getSingletonFormat, h as getSingletonPath, u as updateTreeWithChanges, i as blobSha } from './index-ca270311.node.react-server.js';
import 'react/jsx-runtime';
import 'emery';
import '@braintree/sanitize-url';
import ignore from 'ignore';
async function readDirEntries(dir) {
let entries;
try {
entries = await fs.readdir(dir, {
withFileTypes: true
});
} catch (err) {
if (err.code === 'ENOENT') {
return [];
}
throw err;
}
return entries;
}
async function collectEntriesInDir(baseDir, ancestors) {
const currentRelativeDir = ancestors.map(p => p.segment).join('/');
const entries = await readDirEntries(path.join(baseDir, currentRelativeDir));
const gitignore = entries.find(entry => entry.isFile() && entry.name === '.gitignore');
const gitignoreFilterForDescendents = gitignore ? ignore().add(await fs.readFile(path.join(baseDir, currentRelativeDir, gitignore.name), 'utf8')).createFilter() : () => true;
const pathSegments = ancestors.map(x => x.segment);
return (await Promise.all(entries.filter(entry => {
if (!entry.isDirectory() && !entry.isFile() || entry.name === '.git' || entry.name === 'node_modules' || entry.name === '.next') {
return false;
}
const innerPath = `${pathSegments.concat(entry.name).join('/')}${entry.isDirectory() ? '/' : ''}`;
if (!gitignoreFilterForDescendents(innerPath)) {
return false;
}
let currentPath = entry.name;
for (let i = ancestors.length - 1; i >= 0; i--) {
const ancestor = ancestors[i];
currentPath = `${ancestor.segment}/${currentPath}`;
if (!ancestor.gitignoreFilterForDescendents(currentPath)) {
return false;
}
}
return true;
}).map(async entry => {
if (entry.isDirectory()) {
return collectEntriesInDir(baseDir, [...ancestors, {
gitignoreFilterForDescendents,
segment: entry.name
}]);
} else {
const innerPath = pathSegments.concat(entry.name).join('/');
const contents = await fs.readFile(path.join(baseDir, innerPath));
return {
path: innerPath,
contents: {
byteLength: contents.byteLength,
sha: await blobSha(contents)
}
};
}
}))).flat();
}
async function readToDirEntries(baseDir) {
const additions = await collectEntriesInDir(baseDir, []);
const {
entries
} = await updateTreeWithChanges(new Map(), {
additions: additions,
deletions: []
});
return entries;
}
function getAllowedDirectories(config) {
const allowedDirectories = [];
for (const [collection, collectionConfig] of Object.entries((_config$collections = config.collections) !== null && _config$collections !== void 0 ? _config$collections : {})) {
var _config$collections;
allowedDirectories.push(...getDirectoriesForTreeKey(object(collectionConfig.schema), getCollectionPath(config, collection), undefined, {
data: 'yaml',
contentField: undefined,
dataLocation: 'index'
}));
if (collectionConfig.template) {
allowedDirectories.push(collectionConfig.template);
}
}
for (const [singleton, singletonConfig] of Object.entries((_config$singletons = config.singletons) !== null && _config$singletons !== void 0 ? _config$singletons : {})) {
var _config$singletons;
allowedDirectories.push(...getDirectoriesForTreeKey(object(singletonConfig.schema), getSingletonPath(config, singleton), undefined, getSingletonFormat(config, singleton)));
}
return [...new Set(allowedDirectories)];
}
export { getAllowedDirectories as g, readToDirEntries as r };