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.
This commit is contained in:
linonetwo 2025-12-26 16:59:07 +08:00
parent 73f7667841
commit 558c587f87
6 changed files with 10 additions and 23 deletions

View file

@ -77,7 +77,6 @@ const PLUGINS = [
'in-tagtree-of.ts',
'WatchFileSystemAdaptor.ts',
'routingUtilities.ts',
'externalAttachmentUtilities.ts',
],
},
];

View file

@ -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,

View file

@ -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

View file

@ -1,3 +0,0 @@
title: $:/plugins/linonetwo/watch-filesystem-adaptor/externalAttachmentUtilities.js
type: application/javascript
module-type: utils

View file

@ -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<string, unknown>;
exports.getWikiRootFromTiddlerPath = getWikiRootFromTiddlerPath;
exports.moveExternalAttachmentIfNeeded = moveExternalAttachmentIfNeeded;

View file

@ -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<void>;
getWikiRootFromTiddlerPath(
tiddlerDirectory: string,
wikisWithRouting: IWikiWorkspace[],
): string | undefined;
}