UNPKG

@farmfe/core

Version:

Farm is a extremely fast web build tool written in Rust. Farm can start a project in milliseconds and perform HMR within 10ms, making it much faster than similar tools like webpack and vite.

86 lines 2.78 kB
import fs from 'fs'; import fsp from 'node:fs/promises'; import path from 'path'; import { normalizePath } from './share.js'; export function generateFileTree(files) { const fileTree = []; for (const file of files) { const parts = file.split('/'); let currentNode = fileTree; for (let i = 0; i < parts.length; i++) { const part = parts[i]; const existingNode = currentNode.find((node) => node.name === part); if (existingNode) { currentNode = existingNode.children; } else { const newNode = { isLeaf: i === parts.length - 1, name: part, children: [] }; currentNode.push(newNode); currentNode = newNode.children; } } } return fileTree; } export function buildFileTreeHtml(node) { let html = ''; for (const fileNode of node) { const { isLeaf, name, children } = fileNode; const indent = isLeaf ? '- ' : '|---- '; const path = name.replace(/ /g, '%20'); html += `<div>${indent}<a href="${path}">${name}</a></div>`; if (!isLeaf) { html += buildFileTreeHtml(children).replace(/^/gm, '&nbsp;&nbsp;&nbsp;&nbsp;'); } } return html; } export function generateFileTreeHtml(node) { return ` <!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"> <title>Out Files</title> </head> <body> <!-- file tree--> <div>${buildFileTreeHtml(node)}</div> </body> </html> `; } export const ERR_SYMLINK_IN_RECURSIVE_READDIR = 'ERR_SYMLINK_IN_RECURSIVE_READDIR'; export async function recursiveReaddir(dir) { if (!fs.existsSync(dir)) { return []; } let directs; try { directs = await fsp.readdir(dir, { withFileTypes: true }); } catch (e) { if (e.code === 'EACCES') { // Ignore permission errors return []; } throw e; } if (directs.some((dirent) => dirent.isSymbolicLink())) { const err = new Error('Symbolic links are not supported in recursiveReaddir'); err.code = ERR_SYMLINK_IN_RECURSIVE_READDIR; throw err; } const files = await Promise.all(directs.map((dirent) => { const res = path.resolve(dir, dirent.name); return dirent.isDirectory() ? recursiveReaddir(res) : normalizePath(res); })); return files.flat(1); } //# sourceMappingURL=file.js.map