browser-fs-readdir
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -26,3 +26,6 @@ /** | ||
*/ | ||
declare function getFiles(dirHandle: FileSystemDirectoryHandle, recursive?: boolean, path?: string, skipDirectory?: (entry: FileSystemDirectoryHandle) => boolean): Promise<FileWithDirectoryAndFileHandle[]>; | ||
declare function getFiles(dirHandle: FileSystemDirectoryHandle, recursive?: boolean, path?: string, skipDirectory?: (entry: FileSystemDirectoryHandle) => boolean, progress?: { | ||
loaded: number; | ||
total: number; | ||
}, onProgress?: (event: ProgressEvent) => void): Promise<FileWithDirectoryAndFileHandle[]>; | ||
@@ -41,4 +44,5 @@ /** | ||
skipDirectory?: (entry: FileSystemDirectoryHandle) => boolean; | ||
onProgress?: (event: ProgressEvent) => void; | ||
}): Promise<FileWithDirectoryAndFileHandle[]>; | ||
export { type FileWithDirectoryAndFileHandle, readdir as default, getFiles, readdir }; |
// src/lib/fs.ts | ||
async function getFiles(dirHandle, recursive = false, path = dirHandle.name, skipDirectory) { | ||
async function getFiles(dirHandle, recursive = false, path = dirHandle.name, skipDirectory, progress, onProgress) { | ||
const dirs = []; | ||
const files = []; | ||
progress = progress || { loaded: 0, total: 0 }; | ||
for await (const entry of dirHandle.values()) { | ||
const nestedPath = `${path}/${entry.name}`; | ||
if (entry.kind === "file") { | ||
progress.total += 1; | ||
files.push( | ||
@@ -13,2 +15,4 @@ entry.getFile().then((f) => { | ||
file.handle = entry; | ||
progress.loaded += 1; | ||
onProgress?.(new ProgressEvent("progress", { lengthComputable: true, ...progress })); | ||
return Object.defineProperty(file, "webkitRelativePath", { | ||
@@ -22,3 +26,3 @@ configurable: true, | ||
} else if (entry.kind === "directory" && recursive && (!skipDirectory || !skipDirectory(entry))) { | ||
dirs.push(getFiles(entry, recursive, nestedPath, skipDirectory)); | ||
dirs.push(getFiles(entry, recursive, nestedPath, skipDirectory, progress, onProgress)); | ||
} | ||
@@ -31,3 +35,10 @@ } | ||
async function readdir(dirHandle, options) { | ||
return await getFiles(dirHandle, options?.recursive, void 0, options?.skipDirectory); | ||
return await getFiles( | ||
dirHandle, | ||
options?.recursive, | ||
void 0, | ||
options?.skipDirectory, | ||
void 0, | ||
options?.onProgress | ||
); | ||
} | ||
@@ -34,0 +45,0 @@ var lib_default = readdir; |
{ | ||
"name": "browser-fs-readdir", | ||
"description": "Read contents of a directory with the File System Access API.", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"author": "prin <hi@prin.studio>", | ||
@@ -23,3 +23,4 @@ "license": "MIT", | ||
"prepare": "husky", | ||
"commitlint": "commitlint --edit" | ||
"commitlint": "commitlint --edit", | ||
"prepublishOnly": "npm run build:lib" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
21887
174