Skip to content

Commit

Permalink
Improve FS API list types
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwuts committed Apr 6, 2023
1 parent 4f2f07d commit 5a38851
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/api/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ProgressCallback } from "../badge-api";
import { concatBuffers } from "../lib/buffers";
import { BadgeUSB } from "../badge-usb";

export type FileListing = {
type _FSListing = {
name: string,
path: string,
type: "dir" | "file",
Expand All @@ -12,6 +12,9 @@ export type FileListing = {
modified: bigint,
} | null
}
export type DirListing = _FSListing & { type: 'dir' };
export type FileListing = _FSListing & { type: 'file' };
export type FSListing = DirListing | FileListing;

export class BadgeFileSystemApi {
constructor(
Expand All @@ -25,14 +28,14 @@ export class BadgeFileSystemApi {
* Lists entries in the folder given by `path`
* @param path default: `/internal`
*/
async list(path: string = '/internal'): Promise<FileListing[]> {
async list(path: string = '/internal'): Promise<FSListing[]> {
if (path == '') {
throw Error('Path must not be empty');
}
let pathEncoded = this.textEncoder.encode(path);
let data: ArrayBuffer = await this.transaction(BadgeUSB.PROTOCOL_COMMAND_FILESYSTEM_LIST, pathEncoded, 4000);

let result: FileListing[] = [];
let result: FSListing[] = [];
while (data.byteLength > 0) {
let dataView = new DataView(data);
let itemType = dataView.getUint8(0);
Expand Down

0 comments on commit 5a38851

Please sign in to comment.