diff --git a/.changeset/shaggy-cows-itch.md b/.changeset/shaggy-cows-itch.md new file mode 100644 index 0000000..15565db --- /dev/null +++ b/.changeset/shaggy-cows-itch.md @@ -0,0 +1,5 @@ +--- +"obsidian-show-hidden-files": patch +--- + +Replace platform detection with feature detection. This is more robust against [GH#5](https://github.com/polyipseity/obsidian-show-hidden-files/issues/5). diff --git a/sources/@types/obsidian.ts b/sources/@types/obsidian.ts index d3c5168..0c9311d 100644 --- a/sources/@types/obsidian.ts +++ b/sources/@types/obsidian.ts @@ -13,11 +13,6 @@ declare module "obsidian" { interface Vault extends Private<$Vault, PrivateKey> { } interface Workspace extends Private<$Workspace, PrivateKey> { } } -import type { - Deopaque, - Platform, - Private, -} from "@polyipseity/obsidian-plugin-library" import type { FileExplorerView, FileItem, @@ -28,6 +23,7 @@ import type { View, WorkspaceLeaf, } from "obsidian" +import type { Private } from "@polyipseity/obsidian-plugin-library" import type { i18n } from "i18next" declare const PRIVATE_KEY: unique symbol @@ -50,16 +46,15 @@ interface $DataAdapter { realPath: string, path: string, ) => PromiseLike - readonly reconcileFileChanged: ( - realPath: Deopaque extends Platform.Mobile ? string : never, - path: Deopaque extends Platform.Mobile ? string : never, - stat: Deopaque extends Platform.Mobile ? MobileStat : never, - // eslint-disable-next-line @typescript-eslint/no-invalid-void-type - ) => Deopaque extends Platform.Mobile ? void : never - readonly reconcileFileInternal: ( - realPath: Deopaque extends Platform.Desktop ? string : never, - path: Deopaque extends Platform.Desktop ? string : never, - ) => Deopaque extends Platform.Desktop ? PromiseLike : never + readonly reconcileFileChanged?: ( + realPath: string, + path: string, + stat: MobileStat, + ) => void + readonly reconcileFileInternal?: ( + realPath: string, + path: string, + ) => PromiseLike readonly reconcileFolderCreation: ( realPath: string, path: string, @@ -81,9 +76,9 @@ interface $FileItem { } interface $Filesystem { - readonly stat: ( - fullRealPath: Deopaque extends Platform.Mobile ? string : never, - ) => Deopaque extends Platform.Mobile ? PromiseLike : never + readonly stat?: ( + fullRealPath: string, + ) => PromiseLike } interface $MobileStat extends Omit { diff --git a/sources/show-hidden-files.ts b/sources/show-hidden-files.ts index 066cf8f..aab2e5f 100644 --- a/sources/show-hidden-files.ts +++ b/sources/show-hidden-files.ts @@ -1,13 +1,11 @@ import type { Command, MobileStat } from "obsidian" import { - Platform, type PluginContext, Rules, SettingRules, addCommand, anyToError, deepFreeze, - inSet, printError, revealPrivate, revealPrivateAsync, @@ -307,30 +305,26 @@ async function showFile(context: PluginContext, path: string): Promise { [context.app.vault.adapter], async adapter0 => { const realPath = adapter0.getRealPath(path), - { CURRENT, DESKTOP, MOBILE } = Platform - if (inSet(DESKTOP, CURRENT)) { - await adapter0.reconcileFileInternal( + { fs } = adapter0 + if ("reconcileFileInternal" in adapter0) { + await adapter0.reconcileFileInternal( realPath, path, ) - } else if (inSet(MOBILE, CURRENT)) { - const stat = await (async (): Promise => { - try { - return await adapter0.fs.stat( - adapter0.getFullRealPath(realPath), - ) - } catch { return null } - })() + } else if ("stat" in fs && "reconcileFileChanged" in adapter0) { + const fsStat = fs.stat.bind(fs), + adapterRFC = adapter0.reconcileFileChanged.bind(adapter0), + stat = await (async (): Promise => { + try { + return await fsStat(adapter0.getFullRealPath(realPath)) + } catch { return null } + })() if (!stat) { return } await revealPrivateAsync(context, [stat], async stat0 => { const { type } = stat0 switch (type) { case "file": - adapter0.reconcileFileChanged( - realPath, - path, - stat, - ) + adapterRFC(realPath, path, stat) break case "directory": await adapter0.reconcileFolderCreation(realPath, path) @@ -340,7 +334,7 @@ async function showFile(context: PluginContext, path: string): Promise { } }, noop) } else { - throw new Error(CURRENT) + throw new Error() } }, noop,