(chore): split utils

This commit is contained in:
vorotamoroz
2026-06-10 11:22:34 +01:00
parent 36827d4799
commit e4b36602ec
25 changed files with 358 additions and 136 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
import { REMOTE_COUCHDB, REMOTE_MINIO } from "@lib/common/models/setting.const"; import { REMOTE_COUCHDB, REMOTE_MINIO } from "@lib/common/models/setting.const";
import type { ObsidianLiveSyncSettings } from "@lib/common/models/setting.type"; import type { ObsidianLiveSyncSettings } from "@lib/common/models/setting.type";
import { generateCredentialObject } from "@lib/replication/httplib"; import { generateCredentialObject } from "@lib/replication/httplib";
import { parseHeaderValues } from "@lib/common/utils"; import { parseHeaderValues } from "@lib/common/utils.misc";
import { requestToCouchDBWithCredentials } from "./utils"; import { requestToCouchDBWithCredentials } from "./utils";
import { LOG_LEVEL_VERBOSE, Logger } from "@lib/common/logger"; import { LOG_LEVEL_VERBOSE, Logger } from "@lib/common/logger";
import { DEFAULT_SETTINGS } from "@lib/common/models/setting.const.defaults"; import { DEFAULT_SETTINGS } from "@lib/common/models/setting.const.defaults";
+1 -1
View File
@@ -204,7 +204,7 @@ export function requestToCouchDBWithCredentials(
import { BASE_IS_NEW, EVEN, TARGET_IS_NEW } from "@lib/common/models/shared.const.symbols.ts"; import { BASE_IS_NEW, EVEN, TARGET_IS_NEW } from "@lib/common/models/shared.const.symbols.ts";
export { BASE_IS_NEW, EVEN, TARGET_IS_NEW }; export { BASE_IS_NEW, EVEN, TARGET_IS_NEW };
// Why 2000? : ZIP FILE Does not have enough resolution. // Why 2000? : ZIP FILE Does not have enough resolution.
import { compareMTime } from "@lib/common/utils.ts"; import { compareMTime } from "@lib/common/utils.database.ts";
import { _fetch } from "@/lib/src/common/coreEnvFunctions.ts"; import { _fetch } from "@/lib/src/common/coreEnvFunctions.ts";
export { compareMTime }; export { compareMTime };
function getKey(file: AnyEntry | string | UXFileInfoStub) { function getKey(file: AnyEntry | string | UXFileInfoStub) {
+10 -5
View File
@@ -10,7 +10,14 @@ import {
addIcon, addIcon,
} from "@/deps.ts"; } from "@/deps.ts";
import type { EntryDoc } from "@lib/common/models/db.definition"; import type { EntryDoc } from "@lib/common/models/db.definition";
import type { LoadedEntry, InternalFileEntry, FilePathWithPrefix, FilePath, AnyEntry, SavingEntry } from "@lib/common/models/db.type"; import type {
LoadedEntry,
InternalFileEntry,
FilePathWithPrefix,
FilePath,
AnyEntry,
SavingEntry,
} from "@lib/common/models/db.type";
import type { diff_result } from "@lib/common/models/diff.definition"; import type { diff_result } from "@lib/common/models/diff.definition";
import { CANCELLED, LEAVE_TO_SUBSEQUENT } from "@lib/common/models/shared.const.symbols"; import { CANCELLED, LEAVE_TO_SUBSEQUENT } from "@lib/common/models/shared.const.symbols";
import { MODE_SELECTIVE, MODE_SHINY } from "@lib/common/models/setting.const"; import { MODE_SELECTIVE, MODE_SHINY } from "@lib/common/models/setting.const";
@@ -21,14 +28,12 @@ import {
createBlob, createBlob,
createSavingEntryFromLoadedEntry, createSavingEntryFromLoadedEntry,
createTextBlob, createTextBlob,
delay,
fireAndForget,
getDocData, getDocData,
getDocDataAsArray, getDocDataAsArray,
isDocContentSame, isDocContentSame,
isLoadedEntry, isLoadedEntry,
isObjectDifferent, } from "@lib/common/utils.database.ts";
} from "@lib/common/utils.ts"; import { delay, fireAndForget, isObjectDifferent } from "@lib/common/utils.ts";
import { digestHash } from "@lib/string_and_binary/hash.ts"; import { digestHash } from "@lib/string_and_binary/hash.ts";
import { arrayBufferToBase64, decodeBinary, readString } from "@lib/string_and_binary/convert.ts"; import { arrayBufferToBase64, decodeBinary, readString } from "@lib/string_and_binary/convert.ts";
import { serialized, shareRunningResult } from "octagonal-wheels/concurrency/lock"; import { serialized, shareRunningResult } from "octagonal-wheels/concurrency/lock";
@@ -1,22 +1,24 @@
import { type PluginManifest, type ListedFiles } from "@/deps.ts"; import { type PluginManifest, type ListedFiles } from "@/deps.ts";
import type { LoadedEntry, FilePathWithPrefix, FilePath, SavingEntry, DocumentID, MetaEntry } from "@lib/common/models/db.type"; import type {
LoadedEntry,
FilePathWithPrefix,
FilePath,
SavingEntry,
DocumentID,
MetaEntry,
} from "@lib/common/models/db.type";
import { MODE_SELECTIVE, MODE_PAUSED } from "@lib/common/models/setting.const"; import { MODE_SELECTIVE, MODE_PAUSED } from "@lib/common/models/setting.const";
import type { UXFileInfo, UXStat, UXDataWriteOptions } from "@lib/common/models/fileaccess.type"; import type { UXFileInfo, UXStat, UXDataWriteOptions } from "@lib/common/models/fileaccess.type";
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, LOG_LEVEL_DEBUG } from "@lib/common/logger"; import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, LOG_LEVEL_DEBUG } from "@lib/common/logger";
import { ICHeader, ICHeaderEnd } from "@lib/common/models/fileaccess.const"; import { ICHeader, ICHeaderEnd } from "@lib/common/models/fileaccess.const";
import { type InternalFileInfo } from "@/common/types.ts"; import { type InternalFileInfo } from "@/common/types.ts";
import { readAsBlob, isDocContentSame, readContent, createBlob } from "@lib/common/utils.database.ts";
import type { CustomRegExp } from "@lib/common/utils.regexp.ts";
import { getFileRegExp } from "@lib/common/utils.regexp.ts";
import { sendSignal, fireAndForget } from "@lib/common/utils.ts";
import { compareMTime } from "@lib/common/utils.database.ts";
import { displayRev } from "@lib/common/utils.notations.ts";
import { import {
readAsBlob,
isDocContentSame,
sendSignal,
readContent,
createBlob,
fireAndForget,
type CustomRegExp,
getFileRegExp,
} from "@lib/common/utils.ts";
import {
compareMTime,
isInternalMetadata, isInternalMetadata,
TARGET_IS_NEW, TARGET_IS_NEW,
scheduleTask, scheduleTask,
@@ -26,7 +28,6 @@ import {
onlyInNTimes, onlyInNTimes,
BASE_IS_NEW, BASE_IS_NEW,
EVEN, EVEN,
displayRev,
} from "@/common/utils.ts"; } from "@/common/utils.ts";
import { PeriodicProcessor } from "@/common/PeriodicProcessor.ts"; import { PeriodicProcessor } from "@/common/PeriodicProcessor.ts";
import { serialized, skipIfDuplicated } from "octagonal-wheels/concurrency/lock"; import { serialized, skipIfDuplicated } from "octagonal-wheels/concurrency/lock";
+1 -1
Submodule src/lib updated: 0b5947a3fc...d82503b665
+2 -1
View File
@@ -17,7 +17,8 @@ import {
Logger, Logger,
type LOG_LEVEL, type LOG_LEVEL,
} from "@lib/common/logger"; } from "@lib/common/logger";
import { fireAndForget, isAnyNote, throttle } from "@lib/common/utils"; import { isAnyNote } from "@lib/common/utils.database";
import { fireAndForget, throttle } from "@lib/common/utils";
import { isNotFoundError } from "@lib/common/utils.doc.ts"; import { isNotFoundError } from "@lib/common/utils.doc.ts";
import { Semaphore } from "octagonal-wheels/concurrency/semaphore_v2"; import { Semaphore } from "octagonal-wheels/concurrency/semaphore_v2";
import { serialized } from "octagonal-wheels/concurrency/lock"; import { serialized } from "octagonal-wheels/concurrency/lock";
@@ -13,7 +13,8 @@ import {
} from "@lib/common/types"; } from "@lib/common/types";
import { isCustomisationSyncMetadata, isPluginMetadata } from "@lib/common/typeUtils.ts"; import { isCustomisationSyncMetadata, isPluginMetadata } from "@lib/common/typeUtils.ts";
import { TARGET_IS_NEW } from "@lib/common/models/shared.const.symbols.ts"; import { TARGET_IS_NEW } from "@lib/common/models/shared.const.symbols.ts";
import { compareMTime, displayRev } from "@lib/common/utils.ts"; import { compareMTime } from "@lib/common/utils.database.ts";
import { displayRev } from "@lib/common/utils.notations.ts";
import diff_match_patch from "diff-match-patch"; import diff_match_patch from "diff-match-patch";
import { stripAllPrefixes, isPlainText } from "@lib/string_and_binary/path"; import { stripAllPrefixes, isPlainText } from "@lib/string_and_binary/path";
import { eventHub } from "@/common/events.ts"; import { eventHub } from "@/common/events.ts";
@@ -1,10 +1,16 @@
import { Logger, LOG_LEVEL_NOTICE } from "octagonal-wheels/common/logger"; import { Logger, LOG_LEVEL_NOTICE } from "octagonal-wheels/common/logger";
import { extractObject } from "octagonal-wheels/object"; import { extractObject } from "octagonal-wheels/object";
import type { TweakValues } from "@lib/common/models/tweak.definition"; import type { TweakValues } from "@lib/common/models/tweak.definition";
import { TweakValuesShouldMatchedTemplate, TweakValuesTemplate, IncompatibleChanges, IncompatibleChangesInSpecificPattern, CompatibleButLossyChanges } from "@lib/common/models/tweak.definition"; import {
TweakValuesShouldMatchedTemplate,
TweakValuesTemplate,
IncompatibleChanges,
IncompatibleChangesInSpecificPattern,
CompatibleButLossyChanges,
} from "@lib/common/models/tweak.definition";
import { confName } from "@lib/common/models/shared.definition.configNames"; import { confName } from "@lib/common/models/shared.definition.configNames";
import type { ObsidianLiveSyncSettings, RemoteDBSettings } from "@lib/common/models/setting.type"; import type { ObsidianLiveSyncSettings, RemoteDBSettings } from "@lib/common/models/setting.type";
import { escapeMarkdownValue } from "@lib/common/utils.ts"; import { escapeMarkdownValue } from "@lib/common/utils.notations.ts";
import { AbstractModule } from "@/modules/AbstractModule.ts"; import { AbstractModule } from "@/modules/AbstractModule.ts";
import { $msg } from "@lib/common/i18n.ts"; import { $msg } from "@lib/common/i18n.ts";
import type { InjectableServiceHub } from "@lib/services/InjectableServices.ts"; import type { InjectableServiceHub } from "@lib/services/InjectableServices.ts";
@@ -4,9 +4,14 @@ import { TFile, type TAbstractFile, type TFolder } from "@/deps.ts";
import { ICHeader } from "@lib/common/models/fileaccess.const"; import { ICHeader } from "@lib/common/models/fileaccess.const";
import { addPrefix, isPlainText } from "@lib/string_and_binary/path.ts"; import { addPrefix, isPlainText } from "@lib/string_and_binary/path.ts";
import { LOG_LEVEL_VERBOSE, Logger } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_VERBOSE, Logger } from "octagonal-wheels/common/logger";
import { createBlob } from "@lib/common/utils.ts"; import { createBlob } from "@lib/common/utils.database.ts";
import type { FilePath, FilePathWithPrefix } from "@lib/common/models/db.type"; import type { FilePath, FilePathWithPrefix } from "@lib/common/models/db.type";
import type { UXFileInfo, UXFileInfoStub, UXFolderInfo, UXInternalFileInfoStub } from "@lib/common/models/fileaccess.type"; import type {
UXFileInfo,
UXFileInfoStub,
UXFolderInfo,
UXInternalFileInfoStub,
} from "@lib/common/models/fileaccess.type";
import type { LiveSyncCore } from "@/main.ts"; import type { LiveSyncCore } from "@/main.ts";
import type { FileAccessObsidian } from "@/serviceModules/FileAccessObsidian.ts"; import type { FileAccessObsidian } from "@/serviceModules/FileAccessObsidian.ts";
+1 -1
View File
@@ -12,7 +12,7 @@ import { $msg } from "@lib/common/i18n.ts";
import { performDoctorConsultation, RebuildOptions } from "@lib/common/configForDoc.ts"; import { performDoctorConsultation, RebuildOptions } from "@lib/common/configForDoc.ts";
import { isValidPath } from "@/common/utils.ts"; import { isValidPath } from "@/common/utils.ts";
import { isMetaEntry } from "@lib/common/models/db.definition"; import { isMetaEntry } from "@lib/common/models/db.definition";
import { isDeletedEntry, isDocContentSame, isLoadedEntry, readAsBlob } from "@lib/common/utils.ts"; import { isDeletedEntry, isDocContentSame, isLoadedEntry, readAsBlob } from "@lib/common/utils.database.ts";
import { countCompromisedChunks } from "@lib/pouchdb/negotiation.ts"; import { countCompromisedChunks } from "@lib/pouchdb/negotiation.ts";
import type { LiveSyncCore } from "@/main.ts"; import type { LiveSyncCore } from "@/main.ts";
import { SetupManager } from "@/modules/features/SetupManager.ts"; import { SetupManager } from "@/modules/features/SetupManager.ts";
@@ -6,7 +6,8 @@ import type { DocumentID, FilePathWithPrefix, LoadedEntry } from "@lib/common/mo
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger"; import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger";
import { Logger } from "@lib/common/logger.ts"; import { Logger } from "@lib/common/logger.ts";
import { isErrorOfMissingDoc } from "@lib/pouchdb/utils_couchdb.ts"; import { isErrorOfMissingDoc } from "@lib/pouchdb/utils_couchdb.ts";
import { fireAndForget, getDocData, readContent } from "@lib/common/utils.ts"; import { getDocData, readContent } from "@lib/common/utils.database.ts";
import { fireAndForget } from "@lib/common/utils.ts";
import { isPlainText, stripPrefix } from "@lib/string_and_binary/path.ts"; import { isPlainText, stripPrefix } from "@lib/string_and_binary/path.ts";
import { scheduleOnceIfDuplicated } from "octagonal-wheels/concurrency/lock"; import { scheduleOnceIfDuplicated } from "octagonal-wheels/concurrency/lock";
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore.ts"; import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore.ts";
@@ -4,7 +4,7 @@ import type { diff_result } from "@lib/common/models/diff.definition";
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger"; import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger";
import { ConflictResolveModal } from "./InteractiveConflictResolving/ConflictResolveModal.ts"; import { ConflictResolveModal } from "./InteractiveConflictResolving/ConflictResolveModal.ts";
import { AbstractObsidianModule } from "@/modules/AbstractObsidianModule.ts"; import { AbstractObsidianModule } from "@/modules/AbstractObsidianModule.ts";
import { displayRev } from "@/common/utils.ts"; import { displayRev } from "@lib/common/utils.notations.ts";
import { fireAndForget } from "octagonal-wheels/promises"; import { fireAndForget } from "octagonal-wheels/promises";
import { serialized } from "octagonal-wheels/concurrency/lock"; import { serialized } from "octagonal-wheels/concurrency/lock";
import type { LiveSyncCore } from "@/main.ts"; import type { LiveSyncCore } from "@/main.ts";
+2 -1
View File
@@ -4,7 +4,8 @@ import type { DatabaseConnectingStatus } from "@lib/common/models/shared.definit
import { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE } from "@lib/common/logger"; import { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE } from "@lib/common/logger";
import type { LOG_LEVEL } from "@lib/common/logger"; import type { LOG_LEVEL } from "@lib/common/logger";
import { cancelTask, scheduleTask } from "octagonal-wheels/concurrency/task"; import { cancelTask, scheduleTask } from "octagonal-wheels/concurrency/task";
import { fireAndForget, isDirty, throttle } from "@lib/common/utils.ts"; import { isDirty } from "@lib/common/utils.misc.ts";
import { fireAndForget, throttle } from "@lib/common/utils.ts";
import { import {
collectingChunks, collectingChunks,
pluginScanningCount, pluginScanningCount,
@@ -1,6 +1,7 @@
import type { FilePathWithPrefix, DocumentID, LoadedEntry, MetaEntry, FilePath } from "@lib/common/models/db.type"; import type { FilePathWithPrefix, DocumentID, LoadedEntry, MetaEntry, FilePath } from "@lib/common/models/db.type";
import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger"; import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger";
import { createBlob, getFileRegExp, isDocContentSame, readAsBlob } from "@lib/common/utils.ts"; import { createBlob, isDocContentSame, readAsBlob } from "@lib/common/utils.database.ts";
import { getFileRegExp } from "@lib/common/utils.regexp.ts";
import { Logger } from "@lib/common/logger.ts"; import { Logger } from "@lib/common/logger.ts";
import { addPrefix, shouldBeIgnored, stripAllPrefixes } from "@lib/string_and_binary/path.ts"; import { addPrefix, shouldBeIgnored, stripAllPrefixes } from "@lib/string_and_binary/path.ts";
import { $msg } from "@lib/common/i18n.ts"; import { $msg } from "@lib/common/i18n.ts";
@@ -1,6 +1,6 @@
import { LEVEL_ADVANCED } from "@lib/common/models/shared.definition.configNames"; import { LEVEL_ADVANCED } from "@lib/common/models/shared.definition.configNames";
import type { CustomRegExpSource } from "@lib/common/models/shared.type.util"; import type { CustomRegExpSource } from "@lib/common/models/shared.type.util";
import { constructCustomRegExpList, splitCustomRegExpList } from "@lib/common/utils.ts"; import { constructCustomRegExpList, splitCustomRegExpList } from "@lib/common/utils.regexp.ts";
import MultipleRegExpControl from "./MultipleRegExpControl.svelte"; import MultipleRegExpControl from "./MultipleRegExpControl.svelte";
import { LiveSyncSetting as Setting } from "./LiveSyncSetting.ts"; import { LiveSyncSetting as Setting } from "./LiveSyncSetting.ts";
import { mount } from "svelte"; import { mount } from "svelte";
@@ -1,4 +1,4 @@
import { pickBucketSyncSettings, pickCouchDBSyncSettings, pickP2PSyncSettings } from "@lib/common/utils.ts"; import { pickBucketSyncSettings, pickCouchDBSyncSettings, pickP2PSyncSettings } from "@lib/common/utils.settings.ts";
import type { ObsidianLiveSyncSettings } from "@lib/common/models/setting.type"; import type { ObsidianLiveSyncSettings } from "@lib/common/models/setting.type";
// Keep the setting dialogue buffer aligned with the current core settings before persisting other dirty keys. // Keep the setting dialogue buffer aligned with the current core settings before persisting other dirty keys.
// This also clears stale dirty values left from editing a different remote type before switching active remotes. // This also clears stale dirty values left from editing a different remote type before switching active remotes.
@@ -5,7 +5,7 @@ import {
pickBucketSyncSettings, pickBucketSyncSettings,
pickP2PSyncSettings, pickP2PSyncSettings,
pickEncryptionSettings, pickEncryptionSettings,
} from "@lib/common/utils"; } from "@lib/common/utils.settings";
import { getConfig, type AllSettingItemKey } from "./settingConstants"; import { getConfig, type AllSettingItemKey } from "./settingConstants";
import { LOG_LEVEL_NOTICE, Logger } from "octagonal-wheels/common/logger"; import { LOG_LEVEL_NOTICE, Logger } from "octagonal-wheels/common/logger";
import { isNotFoundError } from "@lib/common/utils.doc.ts"; import { isNotFoundError } from "@lib/common/utils.doc.ts";
@@ -2,7 +2,8 @@ import { requestToCouchDBWithCredentials } from "@/common/utils";
import { $msg } from "@lib/common/i18n"; import { $msg } from "@lib/common/i18n";
import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, Logger } from "@lib/common/logger"; import { LOG_LEVEL_INFO, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE, Logger } from "@lib/common/logger";
import type { ObsidianLiveSyncSettings } from "@lib/common/types"; import type { ObsidianLiveSyncSettings } from "@lib/common/types";
import { fireAndForget, parseHeaderValues } from "@lib/common/utils"; import { parseHeaderValues } from "@lib/common/utils.misc";
import { fireAndForget } from "@lib/common/utils";
import { isCloudantURI } from "@lib/pouchdb/utils_couchdb"; import { isCloudantURI } from "@lib/pouchdb/utils_couchdb";
import { generateCredentialObject } from "@lib/replication/httplib"; import { generateCredentialObject } from "@lib/replication/httplib";
+7 -1
View File
@@ -1,4 +1,10 @@
import type { BucketSyncSetting, CouchDBConnection, EncryptionSettings, ObsidianLiveSyncSettings, P2PSyncSetting } from "@lib/common/models/setting.type"; import type {
BucketSyncSetting,
CouchDBConnection,
EncryptionSettings,
ObsidianLiveSyncSettings,
P2PSyncSetting,
} from "@lib/common/models/setting.type";
import { DEFAULT_SETTINGS } from "@lib/common/models/setting.const.defaults"; import { DEFAULT_SETTINGS } from "@lib/common/models/setting.const.defaults";
import { REMOTE_COUCHDB, REMOTE_MINIO, REMOTE_P2P } from "@lib/common/models/setting.const"; import { REMOTE_COUCHDB, REMOTE_MINIO, REMOTE_P2P } from "@lib/common/models/setting.const";
import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger"; import { LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "@lib/common/logger";
@@ -2,7 +2,7 @@ import { requestToCouchDBWithCredentials } from "@/common/utils";
import { $msg } from "@lib/common/i18n"; import { $msg } from "@lib/common/i18n";
import { Logger } from "@lib/common/logger"; import { Logger } from "@lib/common/logger";
import type { ObsidianLiveSyncSettings } from "@lib/common/types"; import type { ObsidianLiveSyncSettings } from "@lib/common/types";
import { parseHeaderValues } from "@lib/common/utils"; import { parseHeaderValues } from "@lib/common/utils.misc";
import { isCloudantURI } from "@lib/pouchdb/utils_couchdb"; import { isCloudantURI } from "@lib/pouchdb/utils_couchdb";
import { generateCredentialObject } from "@lib/replication/httplib"; import { generateCredentialObject } from "@lib/replication/httplib";
export type ResultMessage = { message: string; classes: string[] }; export type ResultMessage = { message: string; classes: string[] };
+3 -2
View File
@@ -1,7 +1,8 @@
import { compareMTime, EVEN } from "@/common/utils"; import { compareMTime } from "@lib/common/utils.database";
import { EVEN } from "@/common/utils";
import { TFile, type DataWriteOptions } from "@/deps"; import { TFile, type DataWriteOptions } from "@/deps";
import type { FilePath } from "@/lib/src/common/types"; import type { FilePath } from "@/lib/src/common/types";
import { isDocContentSame, readContent } from "@/lib/src/common/utils"; import { isDocContentSame, readContent } from "@lib/common/utils.database";
import { waitForIdle, type LiveSyncHarness } from "../harness/harness"; import { waitForIdle, type LiveSyncHarness } from "../harness/harness";
import { expect } from "vitest"; import { expect } from "vitest";
+1 -1
View File
@@ -2,7 +2,7 @@ import { beforeAll, describe, expect, it, test } from "vitest";
import { generateHarness, waitForIdle, waitForReady, type LiveSyncHarness } from "../harness/harness"; import { generateHarness, waitForIdle, waitForReady, type LiveSyncHarness } from "../harness/harness";
import { TFile } from "@/deps.ts"; import { TFile } from "@/deps.ts";
import { DEFAULT_SETTINGS, type FilePath, type ObsidianLiveSyncSettings } from "@/lib/src/common/types"; import { DEFAULT_SETTINGS, type FilePath, type ObsidianLiveSyncSettings } from "@/lib/src/common/types";
import { isDocContentSame, readContent } from "@/lib/src/common/utils"; import { isDocContentSame, readContent } from "@lib/common/utils.database";
import { DummyFileSourceInisialised, generateBinaryFile, generateFile, init } from "../utils/dummyfile"; import { DummyFileSourceInisialised, generateBinaryFile, generateFile, init } from "../utils/dummyfile";
const localdb_test_setting = { const localdb_test_setting = {
+1 -1
View File
@@ -1,4 +1,4 @@
import { DEFAULT_SETTINGS } from "@/lib/src/common/types.ts"; import { DEFAULT_SETTINGS } from "@lib/common/models/setting.const.defaults";
import { readFile } from "../utils/fileapi.vite.ts"; import { readFile } from "../utils/fileapi.vite.ts";
let charset = ""; let charset = "";
export async function init() { export async function init() {
+186
View File
@@ -0,0 +1,186 @@
// Delete references to utils.ts and replace them with new imports based on the importMap.
// Use this script by running `deno run --allow-read --allow-write --allow-run refactor-import-utils.ts` from the utilsdeno directory.
import { Project } from "npm:ts-morph";
const isDryRun = !Deno.args.includes("--run");
if (isDryRun) {
console.log("=== DRY RUN MODE ===");
console.log(
"To apply changes, run with: deno run --allow-read --allow-write --allow-run refactor-import-utils.ts --run\n"
);
}
const project = new Project({ tsConfigFilePath: "../tsconfig.json" });
const importMap = new Map<string, string>();
const targetFiles = [
"utils.concurrency.ts",
"utils.timer.ts",
"utils.notations.ts",
"utils.database.ts",
"utils.regexp.ts",
"utils.settings.ts",
"utils.patch.ts",
"utils.misc.ts",
];
// 1. Map exports from our newly created subfiles
for (const sourceFile of project.getSourceFiles()) {
const filePath = sourceFile.getFilePath();
const fileName = sourceFile.getBaseName();
if (filePath.includes("src/lib/src/common/") && targetFiles.includes(fileName)) {
const exports = sourceFile.getExportedDeclarations();
for (const [name] of exports) {
const relativePath = filePath.split("src/lib/src/")[1].replace(/\.ts$/, "");
importMap.set(name, `@lib/${relativePath}`);
}
}
}
// 2. Map exports/imports of octagonal-wheels in utils.ts
const utilsFile = project.getSourceFile("src/lib/src/common/utils.ts");
if (utilsFile) {
// Parse imports from octagonal-wheels
for (const imp of utilsFile.getImportDeclarations()) {
const moduleSpec = imp.getModuleSpecifierValue();
if (moduleSpec.startsWith("octagonal-wheels")) {
for (const namedImport of imp.getNamedImports()) {
importMap.set(namedImport.getName(), moduleSpec);
}
}
}
// Parse export declarations from octagonal-wheels
for (const exp of utilsFile.getExportDeclarations()) {
const moduleSpec = exp.getModuleSpecifierValue();
if (moduleSpec && moduleSpec.startsWith("octagonal-wheels")) {
for (const namedExport of exp.getNamedExports()) {
importMap.set(namedExport.getName(), moduleSpec);
}
}
}
}
console.log(`Built importMap with ${importMap.size} mappings.\n`);
let modifiedFilesCount = 0;
// 3. Loop through all source files and replace imports
for (const sourceFile of project.getSourceFiles()) {
let fileModified = false;
const imports = sourceFile.getImportDeclarations();
for (const imp of imports) {
const moduleSpec = imp.getModuleSpecifierValue();
const isUtilsImport =
moduleSpec === "@lib/common/utils" ||
moduleSpec === "@lib/common/utils.ts" ||
moduleSpec.endsWith("/common/utils") ||
moduleSpec.endsWith("/common/utils.ts");
if (isUtilsImport) {
const namedImports = imp.getNamedImports();
const defaultImport = imp.getDefaultImport();
const importsToReplace: Record<string, { name: string; newPath: string; isTypeOnly: boolean }[]> = {};
for (const namedImport of namedImports) {
const name = namedImport.getName();
let newPath = importMap.get(name);
if (newPath) {
// If original ended with .ts and the new path starts with @lib, keep .ts
if (moduleSpec.endsWith(".ts") && newPath.startsWith("@lib/")) {
newPath = newPath + ".ts";
}
if (!importsToReplace[newPath]) {
importsToReplace[newPath] = [];
}
importsToReplace[newPath].push({
name,
newPath,
isTypeOnly: namedImport.isTypeOnly() || imp.isTypeOnly(),
});
}
}
if (Object.keys(importsToReplace).length > 0 || (defaultImport && importMap.has(defaultImport.getText()))) {
fileModified = true;
console.log(`File: ${sourceFile.getFilePath().split("obsidian-livesync/")[1]}`);
console.log(` Old: ${imp.getText()}`);
}
if (!isDryRun) {
// Apply replacements
for (const newPath in importsToReplace) {
const isTypeOnly = importsToReplace[newPath].filter((i) => i.isTypeOnly);
if (isTypeOnly.length > 0) {
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
namedImports: isTypeOnly.map((i) => i.name),
moduleSpecifier: newPath,
isTypeOnly: true,
});
}
const isValueImport = importsToReplace[newPath].filter((i) => !i.isTypeOnly);
if (isValueImport.length > 0) {
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
namedImports: isValueImport.map((i) => i.name),
moduleSpecifier: newPath,
isTypeOnly: false,
});
}
for (const { name } of importsToReplace[newPath]) {
const namedImport = imp.getNamedImports().find((ni) => ni.getName() === name);
if (namedImport) {
namedImport.remove();
}
}
}
} else {
// In dry run, just print what it would do
for (const newPath in importsToReplace) {
const names = importsToReplace[newPath].map((i) => i.name).join(", ");
console.log(` -> Would import { ${names} } from "${newPath}"`);
}
}
if (defaultImport) {
const name = defaultImport.getText();
let newPath = importMap.get(name);
if (newPath) {
if (moduleSpec.endsWith(".ts") && newPath.startsWith("@lib/")) {
newPath = newPath + ".ts";
}
if (!isDryRun) {
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
defaultImport: name,
moduleSpecifier: newPath,
isTypeOnly: imp.isTypeOnly(),
});
imp.removeDefaultImport();
} else {
console.log(` -> Would import default ${name} from "${newPath}"`);
}
}
}
if (!isDryRun) {
if (imp.getNamedImports().length === 0 && !imp.getDefaultImport()) {
imp.remove();
}
}
}
}
if (fileModified) {
modifiedFilesCount++;
}
}
console.log(`\nTotal files to modify: ${modifiedFilesCount}`);
if (!isDryRun) {
project.saveSync();
console.log("All changes successfully saved.");
} else {
console.log("Dry run complete. No changes were written to files.");
}
+100 -94
View File
@@ -8,23 +8,23 @@ const importMap = new Map<string, string>();
// Build a map of types moved out of Models. // Build a map of types moved out of Models.
// Under src/lib/src/common/models. // Under src/lib/src/common/models.
for (const sourceFile of project.getSourceFiles()) { for (const sourceFile of project.getSourceFiles()) {
if (sourceFile.getFilePath().includes("src/lib/src/common/models")) { if (sourceFile.getFilePath().includes("src/lib/src/common/models")) {
const exports = sourceFile.getExportedDeclarations(); const exports = sourceFile.getExportedDeclarations();
for (const [name, declarations] of exports) { for (const [name, declarations] of exports) {
for (const declaration of declarations) { for (const declaration of declarations) {
if ( if (
// declaration.getKindName() === "TypeAliasDeclaration" || // declaration.getKindName() === "TypeAliasDeclaration" ||
// declaration.getKindName() === "InterfaceDeclaration" || // declaration.getKindName() === "InterfaceDeclaration" ||
// declaration.getKindName() === "EnumDeclaration" || // declaration.getKindName() === "EnumDeclaration" ||
true true
) { ) {
console.log(`Found type export in ${sourceFile.getFilePath()}:`, name); console.log(`Found type export in ${sourceFile.getFilePath()}:`, name);
const relativePath = sourceFile.getFilePath().split("src/lib/src/")[1].replace(/\.ts$/, ""); const relativePath = sourceFile.getFilePath().split("src/lib/src/")[1].replace(/\.ts$/, "");
importMap.set(name, `@lib/${relativePath}`); importMap.set(name, `@lib/${relativePath}`);
}
}
} }
}
} }
}
} }
// Extras // Extras
@@ -40,91 +40,97 @@ console.log("Import map:", importMap);
// Loop through all files that import from types.ts. // Loop through all files that import from types.ts.
for (const sourceFile of project.getSourceFiles()) { for (const sourceFile of project.getSourceFiles()) {
const imports = sourceFile.getImportDeclarations(); const imports = sourceFile.getImportDeclarations();
for (const imp of imports) { for (const imp of imports) {
if (imp.getModuleSpecifierValue().includes("types.ts") && imp.getModuleSpecifierValue().startsWith("@lib/common/")) { if (
// Collect imports from types.ts. imp.getModuleSpecifierValue().includes("types.ts") &&
const namedImports = imp.getNamedImports(); imp.getModuleSpecifierValue().startsWith("@lib/common/")
const defaultImport = imp.getDefaultImport(); ) {
console.log(`Found import in ${sourceFile.getFilePath()}:`, { // Collect imports from types.ts.
namedImports: namedImports.map((ni) => ni.getText()), const namedImports = imp.getNamedImports();
defaultImport: defaultImport ? defaultImport.getText() : null, const defaultImport = imp.getDefaultImport();
}); console.log(`Found import in ${sourceFile.getFilePath()}:`, {
// Group imports by their names and generate new import paths based on the importMap namedImports: namedImports.map((ni) => ni.getText()),
const importsToReplace: Record<string, { name: string; newPath: string; isTypeOnly: boolean }[]> = {}; defaultImport: defaultImport ? defaultImport.getText() : null,
for (const namedImport of namedImports) { });
const name = namedImport.getName(); // Group imports by their names and generate new import paths based on the importMap
const newPath = importMap.get(name); const importsToReplace: Record<string, { name: string; newPath: string; isTypeOnly: boolean }[]> = {};
if (newPath) { for (const namedImport of namedImports) {
console.log( const name = namedImport.getName();
`Will replace import of ${name} in ${sourceFile.getFilePath()} with new path:`, const newPath = importMap.get(name);
newPath if (newPath) {
); console.log(
if (!importsToReplace[newPath]) { `Will replace import of ${name} in ${sourceFile.getFilePath()} with new path:`,
importsToReplace[newPath] = []; newPath
} );
importsToReplace[newPath].push({ name, newPath, isTypeOnly: namedImport.isTypeOnly() || imp.isTypeOnly() }); if (!importsToReplace[newPath]) {
} importsToReplace[newPath] = [];
} }
importsToReplace[newPath].push({
name,
newPath,
isTypeOnly: namedImport.isTypeOnly() || imp.isTypeOnly(),
});
}
}
// For each import, generate a new path from importMap and replace it. // For each import, generate a new path from importMap and replace it.
// Split the import when it needs to become multiple imports. // Split the import when it needs to become multiple imports.
for (const newPath in importsToReplace) { for (const newPath in importsToReplace) {
// First, handle type-only imports. // First, handle type-only imports.
const isTypeOnly = importsToReplace[newPath].filter((i) => i.isTypeOnly); const isTypeOnly = importsToReplace[newPath].filter((i) => i.isTypeOnly);
if (isTypeOnly.length > 0) { if (isTypeOnly.length > 0) {
sourceFile.insertImportDeclaration(imp.getChildIndex(), { sourceFile.insertImportDeclaration(imp.getChildIndex(), {
namedImports: isTypeOnly.map((i) => i.name), namedImports: isTypeOnly.map((i) => i.name),
moduleSpecifier: newPath, moduleSpecifier: newPath,
isTypeOnly: true, isTypeOnly: true,
}); });
} }
// Then, handle non-type-only imports. // Then, handle non-type-only imports.
const isValueImport = importsToReplace[newPath].filter((i) => !i.isTypeOnly); const isValueImport = importsToReplace[newPath].filter((i) => !i.isTypeOnly);
if (isValueImport.length > 0) { if (isValueImport.length > 0) {
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
namedImports: isValueImport.map((i) => i.name),
moduleSpecifier: newPath,
isTypeOnly: false,
});
}
// Remove the replaced named imports from the old import.
for (const { name } of importsToReplace[newPath]) {
const namedImport = imp.getNamedImports().find((ni) => ni.getName() === name);
if (namedImport) {
namedImport.remove();
}
}
}
// If there is also a default import and it exists in importMap, replace it too.
if (defaultImport) {
const name = defaultImport.getText();
const newPath = importMap.get(name);
sourceFile.insertImportDeclaration(imp.getChildIndex(), { if (newPath) {
namedImports: isValueImport.map((i) => i.name), console.log(
moduleSpecifier: newPath, `Replacing default import of ${name} in ${sourceFile.getFilePath()} with new path:`,
isTypeOnly: false, newPath
}); );
// Add the new import statement.
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
defaultImport: name,
moduleSpecifier: newPath,
isTypeOnly: imp.isTypeOnly(),
});
// Remove the default import from the old import.
imp.removeDefaultImport();
}
}
if (imp.getNamedImports().length === 0 && !imp.getDefaultImport()) {
// Delete the entire import statement if nothing remains.
imp.remove();
}
} }
// Remove the replaced named imports from the old import.
for (const { name } of importsToReplace[newPath]) {
const namedImport = imp.getNamedImports().find((ni) => ni.getName() === name);
if (namedImport) {
namedImport.remove();
}
}
}
// If there is also a default import and it exists in importMap, replace it too.
if (defaultImport) {
const name = defaultImport.getText();
const newPath = importMap.get(name);
if (newPath) {
console.log(
`Replacing default import of ${name} in ${sourceFile.getFilePath()} with new path:`,
newPath
);
// Add the new import statement.
sourceFile.insertImportDeclaration(imp.getChildIndex(), {
defaultImport: name,
moduleSpecifier: newPath,
isTypeOnly: imp.isTypeOnly(),
});
// Remove the default import from the old import.
imp.removeDefaultImport();
}
}
if (imp.getNamedImports().length === 0 && !imp.getDefaultImport()) {
// Delete the entire import statement if nothing remains.
imp.remove();
}
} }
}
} }
// Save everything at the end. // Save everything at the end.