UNPKG

@docuify/engine

Version:

A flexible, pluggable engine for building and transforming documentation content from source files.

45 lines (43 loc) 1.25 kB
// lib/base/baseSource.ts var BaseSource = class { }; // lib/sources/localfile.ts import fs from "fs/promises"; import path from "path"; var LocalFile = class extends BaseSource { constructor(rootDir) { super(); this.rootDir = rootDir; this.rootDir = path.resolve(process.cwd(), rootDir); } name = "local-file-source"; // Recursively fetch files from the local directory async fetch() { const files = []; await this._readDirRecursive(this.rootDir, files); return files; } async _readDirRecursive(dir, files, parentPath = "") { const entries = await fs.readdir(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); const relativePath = parentPath ? `${parentPath}/${entry.name}` : entry.name; if (entry.isDirectory()) { await this._readDirRecursive(fullPath, files, relativePath); } else if (entry.isFile()) { files.push({ path: relativePath, type: "file", extension: path.extname(entry.name).slice(1), // remove dot loadContent: async () => { return fs.readFile(fullPath, "utf-8"); } }); } } } }; export { LocalFile };