mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-06-17 20:00:13 +00:00
barrel node specific modules to summarise the warnings
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import * as path from "path";
|
||||
import type { UXFileInfoStub, UXFolderInfo } from "@lib/common/types";
|
||||
import type { IConversionAdapter } from "@lib/serviceModules/adapters";
|
||||
import type { NodeFile, NodeFolder } from "./NodeTypes";
|
||||
import { path } from "../node-compat";
|
||||
|
||||
/**
|
||||
* Conversion adapter implementation for Node.js
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
import type { FilePath, UXStat } from "@lib/common/types";
|
||||
import type { IFileSystemAdapter } from "@lib/serviceModules/adapters";
|
||||
import { NodePathAdapter } from "./NodePathAdapter";
|
||||
@@ -8,6 +6,7 @@ import { NodeConversionAdapter } from "./NodeConversionAdapter";
|
||||
import { NodeStorageAdapter } from "./NodeStorageAdapter";
|
||||
import { NodeVaultAdapter } from "./NodeVaultAdapter";
|
||||
import type { NodeFile, NodeFolder, NodeStat } from "./NodeTypes";
|
||||
import { fsPromises as fs, path } from "../node-compat";
|
||||
|
||||
/**
|
||||
* Complete file system adapter implementation for Node.js
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as path from "path";
|
||||
import type { FilePath } from "@lib/common/types";
|
||||
import type { IPathAdapter } from "@lib/serviceModules/adapters";
|
||||
import type { NodeFile } from "./NodeTypes";
|
||||
import { path } from "../node-compat";
|
||||
|
||||
/**
|
||||
* Path adapter implementation for Node.js
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
import type { UXDataWriteOptions } from "@lib/common/types";
|
||||
import type { IStorageAdapter } from "@lib/serviceModules/adapters";
|
||||
import type { NodeStat } from "./NodeTypes";
|
||||
import { fsPromises as fs, path } from "../node-compat";
|
||||
|
||||
/**
|
||||
* Storage adapter implementation for Node.js
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
import type { UXDataWriteOptions } from "@lib/common/types";
|
||||
import type { IVaultAdapter } from "@lib/serviceModules/adapters";
|
||||
import type { NodeFile, NodeFolder } from "./NodeTypes";
|
||||
import { fsPromises as fs, path } from "../node-compat";
|
||||
|
||||
/**
|
||||
* Vault adapter implementation for Node.js
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
import { decodeSettingsFromSetupURI } from "@lib/API/processSetting";
|
||||
import { configURIBase } from "@lib/common/models/shared.const";
|
||||
import {
|
||||
@@ -19,6 +17,7 @@ import { collectPeers, openP2PHost, parseTimeoutSeconds, syncWithPeer } from "./
|
||||
import { performFullScan } from "@lib/serviceFeatures/offlineScanner";
|
||||
import { UnresolvedErrorManager } from "@lib/services/base/UnresolvedErrorManager";
|
||||
import { compatGlobal } from "@lib/common/coreEnvFunctions.ts";
|
||||
import { fsPromises as fs, path } from "../node-compat";
|
||||
|
||||
function redactConnectionString(uri: string): string {
|
||||
return uri.replace(/\/\/([^@/]+)@/u, "//***@");
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import * as path from "path";
|
||||
import * as readline from "node:readline/promises";
|
||||
import { path, readline } from "../node-compat";
|
||||
|
||||
export function toArrayBuffer(data: Buffer): ArrayBuffer {
|
||||
return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) as ArrayBuffer;
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
/**
|
||||
* Self-hosted LiveSync CLI
|
||||
* Command-line version of Self-hosted LiveSync plugin for syncing vaults without Obsidian
|
||||
*/
|
||||
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
import { NodeServiceContext, NodeServiceHub } from "./services/NodeServiceHub";
|
||||
import { configureNodeLocalStorage, ensureGlobalNodeLocalStorage } from "./services/NodeLocalStorage";
|
||||
import { LiveSyncBaseCore } from "@/LiveSyncBaseCore";
|
||||
@@ -27,6 +20,7 @@ import { getPathFromUXFileInfo } from "@lib/common/typeUtils";
|
||||
import { stripAllPrefixes } from "@lib/string_and_binary/path";
|
||||
import { IgnoreRules } from "./serviceModules/IgnoreRules";
|
||||
import { useP2PReplicatorFeature } from "@lib/replication/trystero/useP2PReplicatorFeature";
|
||||
import { fsPromises as fs, path } from "./node-compat";
|
||||
|
||||
const SETTINGS_FILE = ".livesync/settings.json";
|
||||
ensureGlobalNodeLocalStorage();
|
||||
|
||||
@@ -12,10 +12,9 @@ import type {
|
||||
import type { FileEventItemSentinel } from "@lib/managers/StorageEventManager";
|
||||
import type { NodeFile, NodeFolder } from "@/apps/cli/adapters/NodeTypes";
|
||||
import type { Stats } from "fs";
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
import { watch as chokidarWatch, type FSWatcher } from "chokidar";
|
||||
import type { IgnoreRules } from "@/apps/cli/serviceModules/IgnoreRules";
|
||||
import { fsPromises as fs, path } from "../node-compat";
|
||||
|
||||
/**
|
||||
* CLI-specific type guard adapter
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/* eslint-disable obsidianmd/no-nodejs-builtins */
|
||||
import * as nodeFs from "node:fs";
|
||||
import * as nodeFsPromises from "node:fs/promises";
|
||||
import * as nodePath from "node:path";
|
||||
import * as nodeReadlinePromises from "node:readline/promises";
|
||||
|
||||
export {
|
||||
nodeFs as fs,
|
||||
nodeFsPromises as fsPromises,
|
||||
nodePath as path,
|
||||
nodeReadlinePromises as readline,
|
||||
};
|
||||
@@ -1,7 +1,5 @@
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
|
||||
import { minimatch } from "minimatch";
|
||||
import { fsPromises as fs, path } from "../node-compat";
|
||||
|
||||
/**
|
||||
* Loads and evaluates ignore rules from `.livesync/ignore` inside the vault.
|
||||
|
||||
@@ -7,8 +7,7 @@ import type { InjectableDatabaseEventService } from "@lib/services/implements/in
|
||||
import type { IVaultService } from "@lib/services/base/IService";
|
||||
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
|
||||
import { createInstanceLogFunction } from "@lib/services/lib/logUtils";
|
||||
import * as nodeFs from "node:fs";
|
||||
import * as nodePath from "node:path";
|
||||
import { fs as nodeFs, path as nodePath } from "../node-compat";
|
||||
|
||||
const NODE_KV_TYPED_KEY = "__nodeKvType";
|
||||
const NODE_KV_VALUES_KEY = "values";
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import * as nodeFs from "node:fs";
|
||||
import * as nodePath from "node:path";
|
||||
import { fs as nodeFs, path as nodePath } from "../node-compat";
|
||||
|
||||
type LocalStorageShape = {
|
||||
getItem(key: string): string | null;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { AppLifecycleService, AppLifecycleServiceDependencies } from "@lib/services/base/AppLifecycleService";
|
||||
import { ServiceContext } from "@lib/services/base/ServiceBase";
|
||||
import * as nodePath from "node:path";
|
||||
import { ConfigServiceBrowserCompat } from "@lib/services/implements/browser/ConfigServiceBrowserCompat";
|
||||
import { SvelteDialogManagerBase, type ComponentHasResult } from "@lib/services/implements/base/SvelteDialog";
|
||||
import { UIService } from "@lib/services/implements/base/UIService";
|
||||
@@ -25,6 +24,7 @@ import { NodeKeyValueDBService } from "./NodeKeyValueDBService";
|
||||
import { NodeSettingService } from "./NodeSettingService";
|
||||
import { DatabaseService } from "@lib/services/base/DatabaseService";
|
||||
import type { ObsidianLiveSyncSettings } from "@lib/common/types";
|
||||
import { path as nodePath } from "../node-compat";
|
||||
|
||||
export class NodeServiceContext extends ServiceContext {
|
||||
databasePath: string;
|
||||
|
||||
@@ -93,6 +93,18 @@ Ensures that all import statements are standardised across the codebase, resolvi
|
||||
deno run --allow-read --allow-write --allow-env normalise-imports.ts
|
||||
```
|
||||
|
||||
### 7. CLI Node.js Import Redirection (`refactor-cli-node-imports.ts`)
|
||||
Redirects direct Node.js built-in module imports (like `fs` and `path`) within the CLI codebase to use a single barrel file (`src/apps/cli/node-compat.ts`).
|
||||
|
||||
* **Actions**:
|
||||
* Finds imports of Node.js built-in APIs (`fs`, `fs/promises`, `path`, and `readline/promises`) in CLI source files.
|
||||
* Replaces them with imports from the local `node-compat.ts` barrel file.
|
||||
* This eliminates duplicate browser-targeted linter warnings on Node.js built-ins in the CLI workspace, keeping linter ignores consolidated.
|
||||
* **Command**:
|
||||
```bash
|
||||
deno run --allow-read --allow-write --allow-env refactor-cli-node-imports.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Safety and Exclusions
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
// Refactor Node.js imports in the CLI application to use the barrel compatibility file.
|
||||
// Use this script by running `deno run --allow-read --allow-write --allow-env refactor-cli-node-imports.ts` from the utilsdeno directory.
|
||||
// Run with --run flag to apply changes.
|
||||
import { Project, SyntaxKind, Node } from "npm:ts-morph";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
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-env refactor-cli-node-imports.ts --run\n"
|
||||
);
|
||||
} else {
|
||||
console.log("=== RUN MODE: WILL MODIFY FILES ===");
|
||||
}
|
||||
|
||||
const project = new Project({ tsConfigFilePath: "../tsconfig.json" });
|
||||
project.addSourceFilesAtPaths("../src/apps/cli/**/*.ts");
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const projectRoot = path.resolve(__dirname, "..");
|
||||
const nodeCompatPath = path.resolve(projectRoot, "src", "apps", "cli", "node-compat.ts");
|
||||
|
||||
function toPosixPath(filePath: string): string {
|
||||
return filePath.replace(/\\/g, "/");
|
||||
}
|
||||
|
||||
const posixProjectRoot = toPosixPath(projectRoot);
|
||||
const posixSrc = `${posixProjectRoot}/src`;
|
||||
|
||||
function getRelativeImportPath(fromFile: string, toFile: string): string {
|
||||
let rel = path.relative(path.dirname(fromFile), toFile);
|
||||
rel = rel.replace(/\\/g, "/");
|
||||
if (!rel.startsWith(".") && !rel.startsWith("/")) {
|
||||
rel = "./" + rel;
|
||||
}
|
||||
if (rel.endsWith(".ts")) {
|
||||
rel = rel.slice(0, -3);
|
||||
}
|
||||
return rel;
|
||||
}
|
||||
|
||||
let modifiedFilesCount = 0;
|
||||
|
||||
for (const sourceFile of project.getSourceFiles()) {
|
||||
const filePath = sourceFile.getFilePath();
|
||||
const posixFilePath = toPosixPath(filePath);
|
||||
|
||||
// Only process CLI source files under src/apps/cli/
|
||||
if (!posixFilePath.includes("/src/apps/cli/")) continue;
|
||||
if (
|
||||
posixFilePath.endsWith("node-compat.ts") ||
|
||||
posixFilePath.endsWith("vite.config.ts") ||
|
||||
posixFilePath.endsWith(".spec.ts") ||
|
||||
posixFilePath.endsWith(".test.ts") ||
|
||||
posixFilePath.includes("/_test/") ||
|
||||
posixFilePath.includes("/testdeno/") ||
|
||||
posixFilePath.includes("/test/")
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const importDeclarations = sourceFile.getImportDeclarations();
|
||||
const targetImports: any[] = [];
|
||||
const namedImportsToAdd: string[] = [];
|
||||
|
||||
for (const impDecl of importDeclarations) {
|
||||
const specifier = impDecl.getModuleSpecifierValue();
|
||||
|
||||
// Check if it's a Node.js built-in module we want to redirect
|
||||
let exportedName = "";
|
||||
if (specifier === "fs/promises" || specifier === "node:fs/promises") {
|
||||
exportedName = "fsPromises";
|
||||
} else if (specifier === "fs" || specifier === "node:fs") {
|
||||
exportedName = "fs";
|
||||
} else if (specifier === "path" || specifier === "node:path") {
|
||||
exportedName = "path";
|
||||
} else if (specifier === "node:readline/promises") {
|
||||
exportedName = "readline";
|
||||
}
|
||||
|
||||
if (exportedName) {
|
||||
const localName = impDecl.getNamespaceImport()?.getText() || impDecl.getDefaultImport()?.getText();
|
||||
if (localName) {
|
||||
targetImports.push({ impDecl, exportedName, localName });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetImports.length > 0) {
|
||||
console.log(`File: ${posixFilePath.slice(posixProjectRoot.length + 1)}`);
|
||||
|
||||
for (const { impDecl, exportedName, localName } of targetImports) {
|
||||
const { line } = sourceFile.getLineAndColumnAtPos(impDecl.getStart());
|
||||
console.log(` Line ${line}: Redirecting "${impDecl.getText()}"`);
|
||||
|
||||
if (exportedName === localName) {
|
||||
namedImportsToAdd.push(exportedName);
|
||||
} else {
|
||||
namedImportsToAdd.push(`${exportedName} as ${localName}`);
|
||||
}
|
||||
|
||||
if (!isDryRun) {
|
||||
impDecl.remove();
|
||||
}
|
||||
}
|
||||
|
||||
const relImportPath = getRelativeImportPath(filePath, nodeCompatPath);
|
||||
console.log(` Adding: import { ${namedImportsToAdd.join(", ")} } from "${relImportPath}"`);
|
||||
|
||||
if (!isDryRun) {
|
||||
sourceFile.addImportDeclaration({
|
||||
namedImports: namedImportsToAdd,
|
||||
moduleSpecifier: relImportPath,
|
||||
});
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
Reference in New Issue
Block a user