From 558c587f874cc4b3684984530ef9a7728eed63de Mon Sep 17 00:00:00 2001 From: linonetwo Date: Fri, 26 Dec 2025 16:59:07 +0800 Subject: [PATCH] Refactor external attachment utilities to module exports Refactored externalAttachmentUtilities to use ES module exports instead of attaching functions to $tw.utils. Updated imports and mocks accordingly, removed related type definitions from ExtendedUtilities, and cleaned up obsolete meta file. --- scripts/compilePlugins.mjs | 1 - .../plugin/watchFileSystemAdaptor/FileSystemAdaptor.ts | 3 ++- .../__tests__/FileSystemAdaptor.save.test.ts | 7 ++++++- .../externalAttachmentUtilities.js.meta | 3 --- .../externalAttachmentUtilities.ts | 9 ++------- .../watchFileSystemAdaptor/routingUtilities.type.ts | 10 ---------- 6 files changed, 10 insertions(+), 23 deletions(-) delete mode 100644 src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.js.meta diff --git a/scripts/compilePlugins.mjs b/scripts/compilePlugins.mjs index 7676c5cc..814c462e 100644 --- a/scripts/compilePlugins.mjs +++ b/scripts/compilePlugins.mjs @@ -77,7 +77,6 @@ const PLUGINS = [ 'in-tagtree-of.ts', 'WatchFileSystemAdaptor.ts', 'routingUtilities.ts', - 'externalAttachmentUtilities.ts', ], }, ]; diff --git a/src/services/wiki/plugin/watchFileSystemAdaptor/FileSystemAdaptor.ts b/src/services/wiki/plugin/watchFileSystemAdaptor/FileSystemAdaptor.ts index c2fcc0a3..21ae98fd 100644 --- a/src/services/wiki/plugin/watchFileSystemAdaptor/FileSystemAdaptor.ts +++ b/src/services/wiki/plugin/watchFileSystemAdaptor/FileSystemAdaptor.ts @@ -7,6 +7,7 @@ import fs from 'fs'; import path from 'path'; import type { IFileInfo } from 'tiddlywiki'; import type { Tiddler, Wiki } from 'tiddlywiki'; +import { moveExternalAttachmentIfNeeded } from './externalAttachmentUtilities'; import type { ExtendedUtilities } from './routingUtilities.type'; import { isFileLockError } from './utilities'; @@ -321,7 +322,7 @@ export class FileSystemAdaptor { const oldFileInfo = this.boot.files[tiddler.fields.title]; // Move external attachment file if tiddler has _canonical_uri and location changed - await ($tw.utils as unknown as ExtendedUtilities).moveExternalAttachmentIfNeeded( + await moveExternalAttachmentIfNeeded( tiddler.fields._canonical_uri as string | undefined, oldFileInfo, savedFileInfo, diff --git a/src/services/wiki/plugin/watchFileSystemAdaptor/__tests__/FileSystemAdaptor.save.test.ts b/src/services/wiki/plugin/watchFileSystemAdaptor/__tests__/FileSystemAdaptor.save.test.ts index fd6b2ad7..4c187e8d 100644 --- a/src/services/wiki/plugin/watchFileSystemAdaptor/__tests__/FileSystemAdaptor.save.test.ts +++ b/src/services/wiki/plugin/watchFileSystemAdaptor/__tests__/FileSystemAdaptor.save.test.ts @@ -3,6 +3,12 @@ import type { IFileInfo, Tiddler, Wiki } from 'tiddlywiki'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { FileSystemAdaptor } from '../FileSystemAdaptor'; +// Mock external attachment utilities +vi.mock('../externalAttachmentUtilities', () => ({ + moveExternalAttachmentIfNeeded: vi.fn().mockResolvedValue(undefined), + getWikiRootFromTiddlerPath: vi.fn(), +})); + // Mock the workspace service vi.mock('@services/wiki/wikiWorker/services', () => ({ workspace: { @@ -25,7 +31,6 @@ const mockUtils = { deleteTiddlerFile: vi.fn(), cleanupTiddlerFiles: vi.fn(), getFileExtensionInfo: vi.fn(() => ({ type: 'application/x-tiddler' })), - moveExternalAttachmentIfNeeded: vi.fn().mockResolvedValue(undefined), }; // Setup TiddlyWiki global diff --git a/src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.js.meta b/src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.js.meta deleted file mode 100644 index c2c7a393..00000000 --- a/src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.js.meta +++ /dev/null @@ -1,3 +0,0 @@ -title: $:/plugins/linonetwo/watch-filesystem-adaptor/externalAttachmentUtilities.js -type: application/javascript -module-type: utils diff --git a/src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.ts b/src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.ts index ac98ec7f..e39707cd 100644 --- a/src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.ts +++ b/src/services/wiki/plugin/watchFileSystemAdaptor/externalAttachmentUtilities.ts @@ -5,14 +5,13 @@ import type { IFileInfo } from 'tiddlywiki'; /** * External attachment utilities for moving files when tiddlers are routed between workspaces. - * These utilities are exposed as $tw.utils functions. */ /** * Determine wiki root folder from a tiddler file path. * Main wiki stores tiddlers in /tiddlers subfolder, sub-wikis store directly in root. */ -function getWikiRootFromTiddlerPath( +export function getWikiRootFromTiddlerPath( tiddlerDirectory: string, wikisWithRouting: IWikiWorkspace[], ): string | undefined { @@ -51,7 +50,7 @@ function getWikiRootFromTiddlerPath( * @param newFileInfo - The new file info (after save) * @param wikisWithRouting - List of workspaces with routing configuration */ -async function moveExternalAttachmentIfNeeded( +export async function moveExternalAttachmentIfNeeded( canonicalUri: string | undefined, oldFileInfo: IFileInfo | undefined, newFileInfo: IFileInfo, @@ -140,7 +139,3 @@ async function moveExternalAttachmentIfNeeded( } } } - -declare const exports: Record; -exports.getWikiRootFromTiddlerPath = getWikiRootFromTiddlerPath; -exports.moveExternalAttachmentIfNeeded = moveExternalAttachmentIfNeeded; diff --git a/src/services/wiki/plugin/watchFileSystemAdaptor/routingUtilities.type.ts b/src/services/wiki/plugin/watchFileSystemAdaptor/routingUtilities.type.ts index 17717bae..397c74ee 100644 --- a/src/services/wiki/plugin/watchFileSystemAdaptor/routingUtilities.type.ts +++ b/src/services/wiki/plugin/watchFileSystemAdaptor/routingUtilities.type.ts @@ -17,14 +17,4 @@ export interface ExtendedUtilities { wiki: typeof $tw.wiki, rootWidget: typeof $tw.rootWidget, ): IWikiWorkspace | undefined; - moveExternalAttachmentIfNeeded( - canonicalUri: string | undefined, - oldFileInfo: IFileInfo | undefined, - newFileInfo: IFileInfo, - wikisWithRouting: IWikiWorkspace[], - ): Promise; - getWikiRootFromTiddlerPath( - tiddlerDirectory: string, - wikisWithRouting: IWikiWorkspace[], - ): string | undefined; }