Files
outline/server/utils/ImportHelper.ts
T
codegen-sh[bot] 758d4edbb9 Upgrade @typescript-eslint dependencies to v8.33.0 (#9363)
* Upgrade @typescript-eslint dependencies from v6.21.0 to v8.33.0

- Updated @typescript-eslint/eslint-plugin from ^6.21.0 to ^8.33.0
- Updated @typescript-eslint/parser from ^6.21.0 to ^8.33.0
- Tested linting functionality to ensure compatibility
- This brings the latest TypeScript ESLint features and bug fixes

* lint

* tsc

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2025-06-01 11:01:15 -04:00

68 lines
1.5 KiB
TypeScript

import path from "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[];
};
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;
if ([".git", ".DS_Store", "__MACOSX"].includes(name)) {
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;
}
}