From 605badc2ac60ba55897c90c63a02e9abd594099f Mon Sep 17 00:00:00 2001 From: Tomoki Miyauchi Date: Sun, 25 Aug 2024 17:26:37 +0900 Subject: [PATCH] feat: update deps, change adaptor interface --- deno.json | 10 ++++---- deno.lock | 36 +++++++++++++++++++++------- src/deno/adaptor.ts | 11 ++------- src/file_system_access.ts | 8 +++---- src/show_directory_picker.ts | 35 ++++++++++++++------------- src/show_open_file_picker.ts | 46 ++++++++++++++++++------------------ src/type.ts | 14 ++++++----- 7 files changed, 88 insertions(+), 72 deletions(-) diff --git a/deno.json b/deno.json index 73df9ac..facc16e 100644 --- a/deno.json +++ b/deno.json @@ -8,13 +8,13 @@ }, "imports": { "@denosaurs/plug": "jsr:@denosaurs/plug@^1.0.6", - "@miyauci/fs": "jsr:@miyauci/fs@1.0.0-beta.1", - "@miyauci/infra": "jsr:@miyauci/infra@1.0.0-beta.2", + "@miyauci/file-system-access": "./src/mod.ts", + "@miyauci/file-system-access/deno": "./src/deno/mod.ts", + "@miyauci/fs": "jsr:@miyauci/fs@1.0.0-beta.13", + "@miyauci/infra": "jsr:@miyauci/infra@^1.0.0", "@std/bytes": "jsr:@std/bytes@^1.0.2", "@std/media-types": "jsr:@std/media-types@^1.0.2", - "@std/path": "jsr:@std/path@^1.0.1", - "@miyauci/file-system-access": "./src/mod.ts", - "@miyauci/file-system-access/deno": "./src/deno/mod.ts" + "@std/path": "jsr:@std/path@^1.0.1" }, "exports": { ".": "./src/mod.ts", diff --git a/deno.lock b/deno.lock index 3540f17..99343be 100644 --- a/deno.lock +++ b/deno.lock @@ -4,17 +4,20 @@ "specifiers": { "jsr:@denosaurs/plug": "jsr:@denosaurs/plug@1.0.6", "jsr:@denosaurs/plug@^1.0.6": "jsr:@denosaurs/plug@1.0.6", - "jsr:@miyauci/fs@1.0.0-beta.1": "jsr:@miyauci/fs@1.0.0-beta.1", - "jsr:@miyauci/infra@1.0.0-beta.2": "jsr:@miyauci/infra@1.0.0-beta.2", + "jsr:@miyauci/format@^1.1.0": "jsr:@miyauci/format@1.1.0", + "jsr:@miyauci/fs@1.0.0-beta.13": "jsr:@miyauci/fs@1.0.0-beta.13", + "jsr:@miyauci/infra@^1.0.0": "jsr:@miyauci/infra@1.0.0", "jsr:@std/assert@^0.221.0": "jsr:@std/assert@0.221.0", "jsr:@std/bytes@^1.0.2": "jsr:@std/bytes@1.0.2", "jsr:@std/encoding@^0.221.0": "jsr:@std/encoding@0.221.0", "jsr:@std/fmt@^0.221.0": "jsr:@std/fmt@0.221.0", "jsr:@std/fs@^0.221.0": "jsr:@std/fs@0.221.0", + "jsr:@std/fs@^1.0.1": "jsr:@std/fs@1.0.1", "jsr:@std/media-types@^1.0.2": "jsr:@std/media-types@1.0.2", "jsr:@std/path@^0.221.0": "jsr:@std/path@0.221.0", - "jsr:@std/path@^1.0.1": "jsr:@std/path@1.0.1", + "jsr:@std/path@^1.0.1": "jsr:@std/path@1.0.2", "jsr:@std/path@^1.0.2": "jsr:@std/path@1.0.2", + "jsr:@std/regexp@^0.221.0": "jsr:@std/regexp@0.221.0", "npm:@types/node": "npm:@types/node@18.16.19" }, "jsr": { @@ -27,17 +30,25 @@ "jsr:@std/path@^0.221.0" ] }, - "@miyauci/fs@1.0.0-beta.1": { - "integrity": "0c2af5c5ced0946c216e7fbebcd02a802fd956a0f9aaa7b522612fc44e4c3903", + "@miyauci/format@1.1.0": { + "integrity": "28467f7b2f50c3bef19bdb73f3e0ee348e4171883c37fcf7bf2ae5277c45925a", "dependencies": [ - "jsr:@miyauci/infra@1.0.0-beta.2", + "jsr:@std/regexp@^0.221.0" + ] + }, + "@miyauci/fs@1.0.0-beta.13": { + "integrity": "8d68de4951a9edc38145ddf246a1f75b1fde3171f1219ba06d0d23b6ec0bd0f5", + "dependencies": [ + "jsr:@miyauci/format@^1.1.0", + "jsr:@miyauci/infra@^1.0.0", "jsr:@std/bytes@^1.0.2", + "jsr:@std/fs@^1.0.1", "jsr:@std/media-types@^1.0.2", "jsr:@std/path@^1.0.2" ] }, - "@miyauci/infra@1.0.0-beta.2": { - "integrity": "ad42a2378b9bd7ca2565cbbfc75ae0e5d792a0c13913e4c006e928995647c1bd" + "@miyauci/infra@1.0.0": { + "integrity": "033cb39b6347182011a3a194d5ee2df67243e35397e7089e214c47adc5335a9d" }, "@std/assert@0.221.0": { "integrity": "a5f1aa6e7909dbea271754fd4ab3f4e687aeff4873b4cef9a320af813adb489a" @@ -58,6 +69,9 @@ "jsr:@std/path@^0.221.0" ] }, + "@std/fs@1.0.1": { + "integrity": "d6914ca2c21abe591f733b31dbe6331e446815e513e2451b3b9e472daddfefcb" + }, "@std/media-types@1.0.2": { "integrity": "abb78dc8f7d88141cba8c4d60fc1e8b421e5c7b0d7c84f2f708bc666cad46784" }, @@ -72,6 +86,9 @@ }, "@std/path@1.0.2": { "integrity": "a452174603f8c620bd278a380c596437a9eef50c891c64b85812f735245d9ec7" + }, + "@std/regexp@0.221.0": { + "integrity": "6ef49d56386414ed448252aeda15573087b69eb2c4b59dd7390506cc76ac7f6d" } }, "npm": { @@ -85,7 +102,8 @@ "workspace": { "dependencies": [ "jsr:@denosaurs/plug@^1.0.6", - "jsr:@miyauci/fs@1.0.0-beta.1", + "jsr:@miyauci/fs@1.0.0-beta.13", + "jsr:@miyauci/infra@^1.0.0", "jsr:@std/bytes@^1.0.2", "jsr:@std/media-types@^1.0.2", "jsr:@std/path@^1.0.1" diff --git a/src/deno/adaptor.ts b/src/deno/adaptor.ts index efbaa46..5aceb41 100644 --- a/src/deno/adaptor.ts +++ b/src/deno/adaptor.ts @@ -1,17 +1,10 @@ -import { type FileEntry, UA, type UserAgent } from "@miyauci/fs"; -import { FileSystem } from "@miyauci/fs/deno"; +import { BucketFileSystem } from "@miyauci/fs/deno"; import { openDirectoryDialog, openFileDialog } from "./ffi.ts"; import type { Adaptor, OpenDirectoryPicker, OpenFileDialog } from "../type.ts"; -import { typeByExtension } from "@std/media-types"; -import { extname } from "@std/path/extname"; export class DenoAdaptor implements Adaptor { openFileDialog: OpenFileDialog = openFileDialog; openDirectoryDialog: OpenDirectoryPicker = openDirectoryDialog; - userAgent: UserAgent = new UA(); - typeByEntry(entry: FileEntry): string | undefined { - return typeByExtension(extname(entry.name)); - } - locateEntry = FileSystem.prototype.locateEntry; + locateEntry = BucketFileSystem.prototype.locateEntry; } diff --git a/src/file_system_access.ts b/src/file_system_access.ts index d1f8b6e..b54c6e0 100644 --- a/src/file_system_access.ts +++ b/src/file_system_access.ts @@ -3,19 +3,19 @@ import type { FileSystemFileHandle, } from "@miyauci/fs"; import type { Adaptor } from "./type.ts"; -import { crateShowOpenFilePicker } from "./show_open_file_picker.ts"; +import { createShowOpenFilePicker } from "./show_open_file_picker.ts"; import type { DirectoryPickerOptions, OpenFilePickerOptions } from "./type.ts"; import { createShowDirectoryPicker } from "./show_directory_picker.ts"; export class FileSystemAccess { constructor(adaptor: Adaptor) { - this.showOpenFilePicker = crateShowOpenFilePicker( - adaptor, + this.showOpenFilePicker = createShowOpenFilePicker( + adaptor.locateEntry, adaptor.openFileDialog.bind(adaptor), ); this.showDirectoryPicker = createShowDirectoryPicker( - adaptor, + adaptor.locateEntry, adaptor.openDirectoryDialog.bind(adaptor), ); } diff --git a/src/show_directory_picker.ts b/src/show_directory_picker.ts index 0180f56..a835d62 100644 --- a/src/show_directory_picker.ts +++ b/src/show_directory_picker.ts @@ -1,17 +1,18 @@ import { - createFileSystemDirectoryHandle, + createNewFileSystemDirectoryHandle, + type FileSystem, type FileSystemDirectoryHandle, - type FileSystemFileOrDirectoryHandleContext, } from "@miyauci/fs"; -import { List } from "@miyauci/infra"; +import { List, Set } from "@miyauci/infra"; import { isTooSensitiveOrDangerous } from "./algorithm.ts"; -import type { DirectoryPickerOptions, OpenDirectoryPicker } from "./type.ts"; +import type { + DirectoryPickerOptions, + LocateEntry, + OpenDirectoryPicker, +} from "./type.ts"; export function showDirectoryPickerWith( - context: Pick< - FileSystemFileOrDirectoryHandleContext, - "locateEntry" | "typeByEntry" | "userAgent" - >, + locateEntry: LocateEntry, openDirectoryPicker: OpenDirectoryPicker, options?: DirectoryPickerOptions, ): Promise { @@ -45,11 +46,16 @@ export function showDirectoryPickerWith( // 2. At the discretion of the user agent, either go back to the beginning of these in parallel steps, or reject p with an "AbortError" DOMException and abort. } - // 7. Set result to a new FileSystemDirectoryHandle associated with entry. - const result = createFileSystemDirectoryHandle( + const fileSystem = { + locateEntry, root, + observations: new Set(), + } satisfies FileSystem; + + // 7. Set result to a new FileSystemDirectoryHandle associated with entry. + const result = createNewFileSystemDirectoryHandle( + fileSystem, new List([""]), - context, ); // 8. Remember a picked directory given options["id"], entry and environment. @@ -85,12 +91,9 @@ export function showDirectoryPickerWith( } export function createShowDirectoryPicker( - context: Pick< - FileSystemFileOrDirectoryHandleContext, - "locateEntry" | "typeByEntry" | "userAgent" - >, + locateEntry: LocateEntry, openDirectoryPicker: OpenDirectoryPicker, ) { return (options?: DirectoryPickerOptions) => - showDirectoryPickerWith(context, openDirectoryPicker, options); + showDirectoryPickerWith(locateEntry, openDirectoryPicker, options); } diff --git a/src/show_open_file_picker.ts b/src/show_open_file_picker.ts index 66f0b22..f212d88 100644 --- a/src/show_open_file_picker.ts +++ b/src/show_open_file_picker.ts @@ -1,16 +1,18 @@ import { - FileSystemFileHandle, - type FileSystemFileOrDirectoryHandleContext, + createNewFileSystemFileHandle, + type FileSystem, + type FileSystemFileHandle, } from "@miyauci/fs"; import { isTooSensitiveOrDangerous } from "./algorithm.ts"; -import type { OpenFileDialog, OpenFilePickerOptions } from "./type.ts"; -import { List } from "jsr:@miyauci/infra@1.0.0-beta.2"; +import type { + LocateEntry, + OpenFileDialog, + OpenFilePickerOptions, +} from "./type.ts"; +import { List, Set } from "@miyauci/infra"; export function showOpenFilePickerWith( - context: Pick< - FileSystemFileOrDirectoryHandleContext, - "locateEntry" | "typeByEntry" | "userAgent" - >, + locateEntry: LocateEntry, openFileDialog: OpenFileDialog, options: OpenFilePickerOptions = {}, ): Promise { @@ -35,7 +37,6 @@ export function showOpenFilePickerWith( // When possible, this prompt should start out showing starting directory. // 3. Wait for the user to have made their selection. - const entries = openFileDialog(options); // 4. If the user dismissed the prompt without making a selection, reject p with an "AbortError" DOMException and abort. @@ -54,15 +55,17 @@ export function showOpenFilePickerWith( // 2. At the discretion of the user agent, either go back to the beginning of these in parallel steps, or reject p with an "AbortError" DOMException and abort. } - // 2. Add a new FileSystemFileHandle associated with entry to result. - result.push( - new FileSystemFileHandle({ - locateEntry: context.locateEntry.bind(context), - locator: { kind: "file", path: new List([name]), root }, - typeByEntry: context.typeByEntry.bind(context), - userAgent: context.userAgent, - }), + const fileSystem = { + locateEntry, + observations: new Set(), + root, + } satisfies FileSystem; + const handle = createNewFileSystemFileHandle( + fileSystem, + new List([name]), ); + // 2. Add a new FileSystemFileHandle associated with entry to result. + result.push(handle); } // 8. Remember a picked directory given options["id"], entries[0] and environment. @@ -83,13 +86,10 @@ export function showOpenFilePickerWith( // environment: unknown, // ) {} -export function crateShowOpenFilePicker( - context: Pick< - FileSystemFileOrDirectoryHandleContext, - "locateEntry" | "typeByEntry" | "userAgent" - >, +export function createShowOpenFilePicker( + locateEntry: LocateEntry, open: OpenFileDialog, ) { return (options?: OpenFilePickerOptions) => - showOpenFilePickerWith(context, open, options); + showOpenFilePickerWith(locateEntry, open, options); } diff --git a/src/type.ts b/src/type.ts index 92af282..7e80cf0 100644 --- a/src/type.ts +++ b/src/type.ts @@ -1,6 +1,7 @@ import type { - FileSystemFileOrDirectoryHandleContext, + FileSystemEntry, FileSystemHandle, + FileSystemPath, } from "@miyauci/fs"; export interface FilePickerOptions { @@ -65,11 +66,12 @@ export interface OpenFileDialog { (options?: OpenFilePickerOptions): { root: string; name: string }[]; } -export interface Adaptor extends - Pick< - FileSystemFileOrDirectoryHandleContext, - "locateEntry" | "typeByEntry" | "userAgent" - > { +export interface Adaptor { openFileDialog: OpenFileDialog; openDirectoryDialog: OpenDirectoryPicker; + locateEntry: LocateEntry; +} + +export interface LocateEntry { + (path: FileSystemPath): FileSystemEntry | null; }