| import fs from 'fs/promises'; |
| import path from 'path'; |
| import { e as getDirectoriesForTreeKey, w as getCollectionPath, o as object, aE as getSingletonFormat, aF as getSingletonPath, O as updateTreeWithChanges, k as blobSha } from './page-b28190ae.node.js'; |
| import '@keystar/ui/checkbox'; |
| import '@keystar/ui/typography'; |
| import 'react/jsx-runtime'; |
| import '@keystar/ui/text-field'; |
| import 'react'; |
| import '@keystar/ui/button'; |
| import '@keystar/ui/field'; |
| import '@keystar/ui/layout'; |
| import '@keystar/ui/style'; |
| import '@keystar/ui/number-field'; |
| import 'decimal.js'; |
| import '@keystar/ui/combobox'; |
| import 'minimatch'; |
| import '@react-stately/collections'; |
| import '@keystar/ui/picker'; |
| import '@sindresorhus/slugify'; |
| import '@braintree/sanitize-url'; |
| import '@react-aria/i18n'; |
| import '@keystar/ui/dialog'; |
| import '@keystar/ui/slots'; |
| import 'emery'; |
| import '@keystar/ui/drag-and-drop'; |
| import '@keystar/ui/icon'; |
| import '@keystar/ui/icon/icons/trash2Icon'; |
| import '@keystar/ui/list-view'; |
| import '@keystar/ui/tooltip'; |
| import 'slate'; |
| import 'slate-react'; |
| import '@keystar/ui/split-view'; |
| import '@keystar/ui/icon/icons/panelLeftOpenIcon'; |
| import '@keystar/ui/icon/icons/panelLeftCloseIcon'; |
| import '@keystar/ui/icon/icons/panelRightOpenIcon'; |
| import '@keystar/ui/icon/icons/panelRightCloseIcon'; |
| import '@keystar/ui/menu'; |
| import '@keystar/ui/link'; |
| import '@keystar/ui/progress'; |
| 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 }; |