Skip to content

Commit

Permalink
Replace platform detection with feature detection
Browse files Browse the repository at this point in the history
This is more robust against [GH#5](#5).

Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Jun 6, 2024
1 parent 57436d0 commit e77456a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-cows-itch.md
Original file line number Diff line number Diff line change
@@ -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).
31 changes: 13 additions & 18 deletions sources/@types/obsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -50,16 +46,15 @@ interface $DataAdapter {
realPath: string,
path: string,
) => PromiseLike<void>
readonly reconcileFileChanged: <T extends Platform.Current>(
realPath: Deopaque<T> extends Platform.Mobile ? string : never,
path: Deopaque<T> extends Platform.Mobile ? string : never,
stat: Deopaque<T> extends Platform.Mobile ? MobileStat : never,
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
) => Deopaque<T> extends Platform.Mobile ? void : never
readonly reconcileFileInternal: <T extends Platform.Current>(
realPath: Deopaque<T> extends Platform.Desktop ? string : never,
path: Deopaque<T> extends Platform.Desktop ? string : never,
) => Deopaque<T> extends Platform.Desktop ? PromiseLike<void> : never
readonly reconcileFileChanged?: (
realPath: string,
path: string,
stat: MobileStat,
) => void
readonly reconcileFileInternal?: (
realPath: string,
path: string,
) => PromiseLike<void>
readonly reconcileFolderCreation: (
realPath: string,
path: string,
Expand All @@ -81,9 +76,9 @@ interface $FileItem {
}

interface $Filesystem {
readonly stat: <T extends Platform.Current>(
fullRealPath: Deopaque<T> extends Platform.Mobile ? string : never,
) => Deopaque<T> extends Platform.Mobile ? PromiseLike<MobileStat> : never
readonly stat?: (
fullRealPath: string,
) => PromiseLike<MobileStat>
}

interface $MobileStat extends Omit<Stat, "type"> {
Expand Down
32 changes: 13 additions & 19 deletions sources/show-hidden-files.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { Command, MobileStat } from "obsidian"
import {
Platform,
type PluginContext,
Rules,
SettingRules,
addCommand,
anyToError,
deepFreeze,
inSet,
printError,
revealPrivate,
revealPrivateAsync,
Expand Down Expand Up @@ -307,30 +305,26 @@ async function showFile(context: PluginContext, path: string): Promise<void> {
[context.app.vault.adapter],
async adapter0 => {
const realPath = adapter0.getRealPath(path),
{ CURRENT, DESKTOP, MOBILE } = Platform
if (inSet(DESKTOP, CURRENT)) {
await adapter0.reconcileFileInternal<typeof CURRENT>(
{ fs } = adapter0
if ("reconcileFileInternal" in adapter0) {
await adapter0.reconcileFileInternal(
realPath,
path,
)
} else if (inSet(MOBILE, CURRENT)) {
const stat = await (async (): Promise<MobileStat | null> => {
try {
return await adapter0.fs.stat<typeof CURRENT>(
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<MobileStat | null> => {
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<typeof CURRENT>(
realPath,
path,
stat,
)
adapterRFC(realPath, path, stat)
break
case "directory":
await adapter0.reconcileFolderCreation(realPath, path)
Expand All @@ -340,7 +334,7 @@ async function showFile(context: PluginContext, path: string): Promise<void> {
}
}, noop)
} else {
throw new Error(CURRENT)
throw new Error()
}
},
noop,
Expand Down

0 comments on commit e77456a

Please sign in to comment.