Files
outline/server/utils/ImportHelper.ts
Tom MoorandClaude Fable 5 a1d0520c9f Add missing JSDoc to exported members in app and server utils (#12940)
* Add missing JSDoc to exported members in app and server utils

Documents all exported functions, classes, enums, and constants in
app/utils and server/utils that were missing JSDoc, per the project
documentation guidelines.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AsEQYA8zN813thxu7GjjzU

* Use @returns tag to match codebase convention

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AsEQYA8zN813thxu7GjjzU

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-07-08 08:30:11 -04:00

72 lines
1.6 KiB
TypeScript

import path from "node:path";
import fs from "fs-extra";
import { deserializeFilename } from "./fs";
export type FileTreeNode = {
/** The title, extracted from the file name */
title: string;
/** The file name including extension */
name: string;
/** Full path to the file within the zip file */
path: string;
/** Any nested children */
children: FileTreeNode[];
};
/**
* Helper for working with directories of files during imports.
*/
export default class ImportHelper {
/**
* Collects the files and folders for a directory filePath.
*/
public static async toFileTree(
filePath: string,
currentDepth = 0
): Promise<FileTreeNode | null> {
const name = path.basename(filePath);
const title = deserializeFilename(path.parse(path.basename(name)).name);
const item = {
path: filePath,
name,
title,
children: [] as FileTreeNode[],
};
let stats;
// Ignore macOS metadata directories and hidden files
if (name === "__MACOSX" || name.startsWith(".")) {
return null;
}
try {
stats = await fs.stat(filePath);
} catch (_err) {
return null;
}
if (stats.isFile()) {
return item;
}
if (stats.isDirectory()) {
const dirData = await fs.readdir(filePath);
if (dirData === null) {
return null;
}
item.children = (
await Promise.all(
dirData.map((child) =>
this.toFileTree(path.join(filePath, child), currentDepth + 1)
)
)
).filter(Boolean) as FileTreeNode[];
} else {
return null;
}
return item;
}
}