@mintlify/prebuild
Version:
Helpful functions for Mintlify's prebuild step
54 lines (53 loc) • 1.86 kB
JavaScript
import { isMintIgnored, toPosixPath } from '@mintlify/common';
import { readdirSync } from 'fs';
import { readdir } from 'fs/promises';
import path from 'path';
export const getFileListSync = (dirName, og = dirName) => {
const files = [];
const items = readdirSync(dirName, { withFileTypes: true });
for (const item of items) {
const fullPath = path.join(dirName, item.name);
if (item.isDirectory()) {
files.push(...getFileListSync(fullPath, og));
}
else {
files.push(toPosixPath(path.relative(og, fullPath)));
}
}
return files;
};
// TODO consolidate this function
export const getFileListWithDirectories = (dirName, og = dirName) => {
const files = [];
const items = readdirSync(dirName, { withFileTypes: true });
for (const item of items) {
const fullPath = path.join(dirName, item.name);
if (item.isDirectory()) {
const relativeDir = toPosixPath(path.relative(og, fullPath));
if (relativeDir !== '.git') {
files.push(relativeDir);
files.push(...getFileListWithDirectories(fullPath, og));
}
}
else {
files.push(toPosixPath(path.relative(og, fullPath)));
}
}
return files;
};
export async function* getFileList(dirName, og = dirName, mintIgnore = []) {
const items = await readdir(dirName, { withFileTypes: true });
for (const item of items) {
const fullPath = path.join(dirName, item.name);
const relativePath = toPosixPath(path.relative(og, fullPath));
if (isMintIgnored(relativePath, mintIgnore)) {
continue;
}
if (item.isDirectory()) {
yield* getFileList(fullPath, og, mintIgnore);
}
else {
yield `/${relativePath}`;
}
}
}