From c2a297608070aea36f5df11769f8582d855c8388 Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Tue, 11 Feb 2025 20:49:30 -0300 Subject: [PATCH 01/25] Refactor sync root management to use drive instance for unregistering and disconnecting --- examples/register.ts | 2 +- examples/unregister.ts | 4 +- include/sync_root_interface/SyncRoot.h | 6 +- native-src/sync_root_interface/SyncRoot.cpp | 49 ++++----- native-src/virtual_drive/Wrappers.cpp | 4 +- src/addon-wrapper.ts | 6 +- src/virtual-drive.ts | 109 +++++++++++--------- 7 files changed, 96 insertions(+), 84 deletions(-) diff --git a/examples/register.ts b/examples/register.ts index 5ed00fbe..c0c0d3fc 100644 --- a/examples/register.ts +++ b/examples/register.ts @@ -29,5 +29,5 @@ try { } catch (error) { logger.error(error); drive.disconnectSyncRoot(); - VirtualDrive.unregisterSyncRoot(settings.syncRootPath); + drive.unregisterSyncRoot(); } diff --git a/examples/unregister.ts b/examples/unregister.ts index 6c57ca89..b5446fcd 100644 --- a/examples/unregister.ts +++ b/examples/unregister.ts @@ -1,7 +1,7 @@ import VirtualDrive from "@/virtual-drive"; +import { drive } from "./drive"; import { deleteInfoItems } from "./info-items-manager"; -import settings from "./settings"; -VirtualDrive.unregisterSyncRoot(settings.syncRootPath); +drive.unregisterSyncRoot(); deleteInfoItems(); diff --git a/include/sync_root_interface/SyncRoot.h b/include/sync_root_interface/SyncRoot.h index 25238e3c..d8842999 100644 --- a/include/sync_root_interface/SyncRoot.h +++ b/include/sync_root_interface/SyncRoot.h @@ -16,11 +16,13 @@ class SyncRoot public: static HRESULT RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *providerName, const wchar_t *providerVersion, const GUID &providerId, const wchar_t *logoPath); static HRESULT ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallbacks syncCallbacks, napi_env env, CF_CONNECTION_KEY *connectionKey); - static HRESULT DisconnectSyncRoot(); + static HRESULT DisconnectSyncRoot(const wchar_t *syncRootPath); static HRESULT UnregisterSyncRoot(); - static std::list GetItemsSyncRoot(const wchar_t *syncRootPath); static std::string GetFileIdentity(const wchar_t *path); static void HydrateFile(const wchar_t *filePath); static void DehydrateFile(const wchar_t *filePath); static void DeleteFileSyncRoot(const wchar_t *path); + +private: + CF_CONNECTION_KEY connectionKey; }; \ No newline at end of file diff --git a/native-src/sync_root_interface/SyncRoot.cpp b/native-src/sync_root_interface/SyncRoot.cpp index 04125741..f7a62e6a 100644 --- a/native-src/sync_root_interface/SyncRoot.cpp +++ b/native-src/sync_root_interface/SyncRoot.cpp @@ -1,6 +1,6 @@ -#include "stdafx.h" -#include "SyncRoot.h" #include "Callbacks.h" +#include "SyncRoot.h" +#include "stdafx.h" #include #include #include @@ -9,6 +9,7 @@ namespace fs = std::filesystem; // variable to disconect CF_CONNECTION_KEY gloablConnectionKey; +std::map connectionMap; void TransformInputCallbacksToSyncCallbacks(napi_env env, InputSyncCallbacks input) { @@ -115,16 +116,17 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p { try { - auto syncRootID = providerId; + // Convert GUID to string for syncRootID + wchar_t syncRootID[39]; + StringFromGUID2(providerId, syncRootID, 39); winrt::StorageProviderSyncRootInfo info; - info.Id(L"syncRootID"); + info.Id(syncRootID); auto folder = winrt::StorageFolder::GetFolderFromPathAsync(syncRootPath).get(); info.Path(folder); // The string can be in any form acceptable to SHLoadIndirectString. - info.DisplayNameResource(providerName); std::wstring completeIconResource = std::wstring(logoPath) + L",0"; @@ -135,7 +137,7 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p info.HydrationPolicyModifier(winrt::StorageProviderHydrationPolicyModifier::None); info.PopulationPolicy(winrt::StorageProviderPopulationPolicy::AlwaysFull); info.InSyncPolicy(winrt::StorageProviderInSyncPolicy::FileCreationTime | winrt::StorageProviderInSyncPolicy::DirectoryCreationTime); - info.Version(L"1.0.0"); + info.Version(providerVersion); info.ShowSiblingsAsGroup(false); info.HardlinkPolicy(winrt::StorageProviderHardlinkPolicy::None); @@ -145,9 +147,8 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p // Context std::wstring syncRootIdentity(syncRootPath); syncRootIdentity.append(L"->"); - syncRootIdentity.append(L"TestProvider"); + syncRootIdentity.append(providerName); - wchar_t const contextString[] = L"TestProviderContextString"; winrt::IBuffer contextBuffer = winrt::CryptographicBuffer::ConvertStringToBinary(syncRootIdentity.data(), winrt::BinaryStringEncoding::Utf8); info.Context(contextBuffer); @@ -165,6 +166,7 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p catch (...) { wprintf(L"Could not register the sync root, hr %08x\n", static_cast(winrt::to_hresult())); + return E_FAIL; } } @@ -179,6 +181,7 @@ HRESULT SyncRoot::UnregisterSyncRoot() catch (...) { wprintf(L"Could not unregister the sync root, hr %08x\n", static_cast(winrt::to_hresult())); + return E_FAIL; } } @@ -205,38 +208,38 @@ HRESULT SyncRoot::ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallback CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO | CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH, connectionKey); wprintf(L"Connection key: %llu\n", *connectionKey); - gloablConnectionKey = *connectionKey; + if (SUCCEEDED(hr)) + { + connectionMap[syncRootPath] = *connectionKey; + } return hr; } catch (const std::exception &e) { wprintf(L"Excepción capturada: %hs\n", e.what()); - // Aquí puedes decidir si retornar un código de error específico o mantener el E_FAIL. + return E_FAIL; } catch (...) { wprintf(L"Excepción desconocida capturada\n"); - // Igualmente, puedes decidir el código de error a retornar. + return E_FAIL; } } // disconection sync root -HRESULT SyncRoot::DisconnectSyncRoot() +HRESULT SyncRoot::DisconnectSyncRoot(const wchar_t *syncRootPath) { - Logger::getInstance().log("Disconnecting sync root.", LogLevel::INFO); - try + auto it = connectionMap.find(syncRootPath); + if (it != connectionMap.end()) { - HRESULT hr = CfDisconnectSyncRoot(gloablConnectionKey); + HRESULT hr = CfDisconnectSyncRoot(it->second); + if (SUCCEEDED(hr)) + { + connectionMap.erase(it); + } return hr; } - catch (const std::exception &e) - { - Logger::getInstance().log("Exception caught: " + std::string(e.what()), LogLevel::ERROR); - } - catch (...) - { - Logger::getInstance().log("Unknown exception caught.", LogLevel::ERROR); - } + return E_FAIL; } // struct diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index a81e57e1..d75d6383 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -429,8 +429,8 @@ napi_value DisconnectSyncRootWrapper(napi_env env, napi_callback_info args) syncRootPath = new WCHAR[pathLength + 1]; napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(syncRootPath)), pathLength + 1, nullptr); - HRESULT result = SyncRoot::DisconnectSyncRoot(); - // wprintf(L"DisconnectSyncRootWrapper: %08x\n", static_cast(result)); + HRESULT result = SyncRoot::DisconnectSyncRoot(syncRootPath); + delete[] syncRootPath; napi_value napiResult; diff --git a/src/addon-wrapper.ts b/src/addon-wrapper.ts index 5e39f33a..5b55017a 100644 --- a/src/addon-wrapper.ts +++ b/src/addon-wrapper.ts @@ -27,7 +27,7 @@ export class Addon { return this.parseAddonZod("registerSyncRoot", result); } - connectSyncRoot({ callbacks }: { callbacks: Callbacks }) { + connectSyncRoot({ callbacks }: { callbacks: Callbacks; syncRootPath: string }) { const result = addon.connectSyncRoot(this.syncRootPath, callbacks); return this.parseAddonZod("connectSyncRoot", result); } @@ -37,8 +37,8 @@ export class Addon { return this.parseAddonZod("unregisterSyncRoot", result); } - disconnectSyncRoot() { - return addon.disconnectSyncRoot(this.syncRootPath); + disconnectSyncRoot({ syncRootPath }: { syncRootPath: string }) { + return addon.disconnectSyncRoot(syncRootPath); } addLogger({ logPath }: { logPath: string }) { diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 3b881b24..17f96e2f 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -1,14 +1,12 @@ -import path, { join, win32 } from "path"; import fs from "fs"; -import { Watcher } from "./watcher/watcher"; -import { Callbacks } from "./types/callbacks.type"; -import { IQueueManager } from "./queue/queueManager"; - -import { createLogger } from "./logger"; -import { Addon } from "./addon-wrapper"; +import path, { join, win32 } from "path"; import winston from "winston"; -const addon = new Addon(); +import { Addon } from "./addon-wrapper"; +import { createLogger } from "./logger"; +import { IQueueManager } from "./queue/queueManager"; +import { Callbacks } from "./types/callbacks.type"; +import { Watcher } from "./watcher/watcher"; const PLACEHOLDER_ATTRIBUTES = { FILE_ATTRIBUTE_READONLY: 0x1, @@ -22,12 +20,16 @@ class VirtualDrive { callbacks?: Callbacks; watcher = new Watcher(); logger: winston.Logger; + connections: Map = new Map(); + + addon: Addon; constructor(syncRootPath: string, loggerPath: string) { + this.addon = new Addon(); this.syncRootPath = this.convertToWindowsPath(syncRootPath); loggerPath = this.convertToWindowsPath(loggerPath); - addon.syncRootPath = this.syncRootPath; + this.addon.syncRootPath = this.syncRootPath; this.createSyncRootFolder(); this.addLoggerPath(loggerPath); @@ -52,15 +54,15 @@ class VirtualDrive { } addLoggerPath(logPath: string) { - addon.addLogger({ logPath }); + this.addon.addLogger({ logPath }); } getPlaceholderState(path: string) { - return addon.getPlaceholderState({ path: this.fixPath(path) }); + return this.addon.getPlaceholderState({ path: this.fixPath(path) }); } getPlaceholderWithStatePending() { - return addon.getPlaceholderWithStatePending(); + return this.addon.getPlaceholderWithStatePending(); } createSyncRootFolder() { @@ -70,11 +72,11 @@ class VirtualDrive { } getFileIdentity(relativePath: string) { - return addon.getFileIdentity({ path: this.fixPath(relativePath) }); + return this.addon.getFileIdentity({ path: this.fixPath(relativePath) }); } async deleteFileSyncRoot(relativePath: string) { - return addon.deleteFileSyncRoot({ path: this.fixPath(relativePath) }); + return this.addon.deleteFileSyncRoot({ path: this.fixPath(relativePath) }); } connectSyncRoot() { @@ -82,7 +84,23 @@ class VirtualDrive { throw new Error("Callbacks are not defined"); } - return addon.connectSyncRoot({ callbacks: this.callbacks }); + console.log("Connecting to sync root: ", this.syncRootPath); + + const connectionKey = this.addon.connectSyncRoot({ syncRootPath: this.syncRootPath, callbacks: this.callbacks }); + + console.log("Connection key: ", connectionKey); + this.connections.set(this.syncRootPath, connectionKey); + return connectionKey; + } + + disconnectSyncRoot() { + const connectionKey = this.connections.get(this.syncRootPath); + if (connectionKey) { + this.addon.disconnectSyncRoot({ syncRootPath: this.syncRootPath }); + this.connections.delete(this.syncRootPath); + } else { + throw new Error(`No connection found for syncRootPath: ${this.syncRootPath}`); + } } createPlaceholderFile( @@ -93,13 +111,13 @@ class VirtualDrive { creationTime: number, lastWriteTime: number, lastAccessTime: number, - basePath: string + basePath: string, ): any { const creationTimeStr = this.convertToWindowsTime(creationTime).toString(); const lastWriteTimeStr = this.convertToWindowsTime(lastWriteTime).toString(); const lastAccessTimeStr = this.convertToWindowsTime(lastAccessTime).toString(); - return addon.createPlaceholderFile({ + return this.addon.createPlaceholderFile({ fileName, fileId, fileSize, @@ -107,7 +125,7 @@ class VirtualDrive { creationTime: creationTimeStr, lastWriteTime: lastWriteTimeStr, lastAccessTime: lastAccessTimeStr, - basePath + basePath, }); } @@ -120,13 +138,13 @@ class VirtualDrive { creationTime: number, lastWriteTime: number, lastAccessTime: number, - path: string + path: string, ) { const creationTimeStr = this.convertToWindowsTime(creationTime).toString(); const lastWriteTimeStr = this.convertToWindowsTime(lastWriteTime).toString(); const lastAccessTimeStr = this.convertToWindowsTime(lastAccessTime).toString(); - - return addon.createPlaceholderDirectory({ + + return this.addon.createPlaceholderDirectory({ itemName, itemId, isDirectory, @@ -135,7 +153,7 @@ class VirtualDrive { creationTime: creationTimeStr, lastWriteTime: lastWriteTimeStr, lastAccessTime: lastAccessTimeStr, - path + path, }); } @@ -144,27 +162,24 @@ class VirtualDrive { providerVersion: string, providerId: string, callbacks: Callbacks, - logoPath: string + logoPath: string, ): Promise { this.callbacks = callbacks; - return addon.registerSyncRoot({ + console.log("Registering sync root: ", this.syncRootPath); + return this.addon.registerSyncRoot({ providerName, providerVersion, providerId, - logoPath + logoPath, }); } - static unregisterSyncRoot(syncRootPath: string) { - return addon.unregisterSyncRoot({ syncRootPath }); + unregisterSyncRoot() { + return this.addon.unregisterSyncRoot({ syncRootPath: this.syncRootPath }); } - watchAndWait( - path: string, - queueManager: IQueueManager, - loggerPath: string - ): void { - this.watcher.addon = addon; + watchAndWait(path: string, queueManager: IQueueManager, loggerPath: string): void { + this.watcher.addon = this.addon; this.watcher.queueManager = queueManager; this.watcher.logger = this.logger; this.watcher.syncRootPath = this.syncRootPath; @@ -189,7 +204,7 @@ class VirtualDrive { itemId: string, size: number = 0, creationTime: number = Date.now(), - lastWriteTime: number = Date.now() + lastWriteTime: number = Date.now(), ) { const fullPath = path.join(this.syncRootPath, relativePath); const splitPath = relativePath.split("/").filter((p) => p); @@ -211,7 +226,7 @@ class VirtualDrive { creationTime, lastWriteTime, Date.now(), - currentPath + currentPath, ); } catch (error) { //@ts-ignore @@ -224,7 +239,7 @@ class VirtualDrive { itemId: string, size: number = 0, creationTime: number = Date.now(), - lastWriteTime: number = Date.now() + lastWriteTime: number = Date.now(), ) { const splitPath = relativePath.split("/").filter((p) => p); const directoryPath = path.resolve(this.syncRootPath); @@ -244,7 +259,7 @@ class VirtualDrive { creationTime, lastWriteTime, Date.now(), - currentPath + currentPath, ); } } @@ -252,31 +267,23 @@ class VirtualDrive { } } - disconnectSyncRoot() { - return addon.disconnectSyncRoot(); - } - - updateSyncStatus( - itemPath: string, - isDirectory: boolean, - sync: boolean = true - ) { - return addon.updateSyncStatus({ path: this.fixPath(itemPath), isDirectory, sync }); + updateSyncStatus(itemPath: string, isDirectory: boolean, sync: boolean = true) { + return this.addon.updateSyncStatus({ path: this.fixPath(itemPath), isDirectory, sync }); } convertToPlaceholder(itemPath: string, id: string) { - return addon.convertToPlaceholder({ path: this.fixPath(itemPath), id }); + return this.addon.convertToPlaceholder({ path: this.fixPath(itemPath), id }); } updateFileIdentity(itemPath: string, id: string, isDirectory: boolean) { - return addon.updateFileIdentity({ path: this.fixPath(itemPath), id, isDirectory }); + return this.addon.updateFileIdentity({ path: this.fixPath(itemPath), id, isDirectory }); } dehydrateFile(itemPath: string) { - return addon.dehydrateFile({ path: this.fixPath(itemPath) }); + return this.addon.dehydrateFile({ path: this.fixPath(itemPath) }); } hydrateFile(itemPath: string) { - return addon.hydrateFile({ path: this.fixPath(itemPath) }); + return this.addon.hydrateFile({ path: this.fixPath(itemPath) }); } } From 53167e67cbd33d83527eb7e46187598475c5c547 Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Tue, 11 Feb 2025 21:10:37 -0300 Subject: [PATCH 02/25] Update unregisterSyncRoot to use providerId instead of syncRootPath --- examples/drive.ts | 2 +- include/sync_root_interface/SyncRoot.h | 2 +- native-src/sync_root_interface/SyncRoot.cpp | 8 +++++-- native-src/virtual_drive/Wrappers.cpp | 24 ++++++++++++++------- src/addon-wrapper.ts | 4 ++-- src/virtual-drive.ts | 6 ++++-- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/examples/drive.ts b/examples/drive.ts index 3322737f..5d47137e 100644 --- a/examples/drive.ts +++ b/examples/drive.ts @@ -3,5 +3,5 @@ import VirtualDrive from "@/virtual-drive"; import settings from "./settings"; -export const drive = new VirtualDrive(settings.syncRootPath, settings.defaultLogPath); +export const drive = new VirtualDrive(settings.syncRootPath, settings.providerid, settings.defaultLogPath); export const logger = createLogger(settings.defaultLogPath); diff --git a/include/sync_root_interface/SyncRoot.h b/include/sync_root_interface/SyncRoot.h index d8842999..6d74f738 100644 --- a/include/sync_root_interface/SyncRoot.h +++ b/include/sync_root_interface/SyncRoot.h @@ -17,7 +17,7 @@ class SyncRoot static HRESULT RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *providerName, const wchar_t *providerVersion, const GUID &providerId, const wchar_t *logoPath); static HRESULT ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallbacks syncCallbacks, napi_env env, CF_CONNECTION_KEY *connectionKey); static HRESULT DisconnectSyncRoot(const wchar_t *syncRootPath); - static HRESULT UnregisterSyncRoot(); + static HRESULT UnregisterSyncRoot(const GUID &providerId); static std::string GetFileIdentity(const wchar_t *path); static void HydrateFile(const wchar_t *filePath); static void DehydrateFile(const wchar_t *filePath); diff --git a/native-src/sync_root_interface/SyncRoot.cpp b/native-src/sync_root_interface/SyncRoot.cpp index f7a62e6a..3cb6c7b6 100644 --- a/native-src/sync_root_interface/SyncRoot.cpp +++ b/native-src/sync_root_interface/SyncRoot.cpp @@ -170,12 +170,16 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p } } -HRESULT SyncRoot::UnregisterSyncRoot() +HRESULT SyncRoot::UnregisterSyncRoot(const GUID &providerId) { try { + // Convert GUID to string for syncRootID + wchar_t syncRootID[39]; + StringFromGUID2(providerId, syncRootID, 39); + Logger::getInstance().log("Unregistering sync root.", LogLevel::INFO); - winrt::StorageProviderSyncRootManager::Unregister(L"syncRootID"); + winrt::StorageProviderSyncRootManager::Unregister(syncRootID); return S_OK; } catch (...) diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index d75d6383..e8ba0e6e 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -112,19 +112,27 @@ napi_value UnregisterSyncRootWrapper(napi_env env, napi_callback_info args) if (argc < 1) { - napi_throw_error(env, nullptr, "The sync root path is required for UnregisterSyncRoot"); + napi_throw_error(env, nullptr, "The provider ID is required for UnregisterSyncRoot"); return nullptr; } - LPCWSTR syncRootPath; - size_t pathLength; - napi_get_value_string_utf16(env, argv[0], nullptr, 0, &pathLength); - syncRootPath = new WCHAR[pathLength + 1]; - napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(syncRootPath)), pathLength + 1, nullptr); + GUID providerId; + LPCWSTR providerIdStr; + size_t providerIdStrLength; + napi_get_value_string_utf16(env, argv[0], nullptr, 0, &providerIdStrLength); + providerIdStr = new WCHAR[providerIdStrLength + 1]; + napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(providerIdStr)), providerIdStrLength + 1, nullptr); - HRESULT result = SyncRoot::UnregisterSyncRoot(); + if (FAILED(CLSIDFromString(providerIdStr, &providerId))) + { + napi_throw_error(env, nullptr, "Invalid GUID format"); + delete[] providerIdStr; + return nullptr; + } - delete[] syncRootPath; + HRESULT result = SyncRoot::UnregisterSyncRoot(providerId); + + delete[] providerIdStr; napi_value napiResult; napi_create_int32(env, static_cast(result), &napiResult); diff --git a/src/addon-wrapper.ts b/src/addon-wrapper.ts index 5b55017a..7c77c4a2 100644 --- a/src/addon-wrapper.ts +++ b/src/addon-wrapper.ts @@ -32,8 +32,8 @@ export class Addon { return this.parseAddonZod("connectSyncRoot", result); } - unregisterSyncRoot({ syncRootPath }: { syncRootPath: string }) { - const result = addon.unregisterSyncRoot(syncRootPath); + unregisterSyncRoot({ providerId }: { providerId: string }) { + const result = addon.unregisterSyncRoot(providerId); return this.parseAddonZod("unregisterSyncRoot", result); } diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 17f96e2f..153ef649 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -17,6 +17,7 @@ const PLACEHOLDER_ATTRIBUTES = { class VirtualDrive { syncRootPath: string; + providerId: string; callbacks?: Callbacks; watcher = new Watcher(); logger: winston.Logger; @@ -24,10 +25,11 @@ class VirtualDrive { addon: Addon; - constructor(syncRootPath: string, loggerPath: string) { + constructor(syncRootPath: string, providerId: string, loggerPath: string) { this.addon = new Addon(); this.syncRootPath = this.convertToWindowsPath(syncRootPath); loggerPath = this.convertToWindowsPath(loggerPath); + this.providerId = providerId; this.addon.syncRootPath = this.syncRootPath; @@ -175,7 +177,7 @@ class VirtualDrive { } unregisterSyncRoot() { - return this.addon.unregisterSyncRoot({ syncRootPath: this.syncRootPath }); + return this.addon.unregisterSyncRoot({ providerId: this.providerId }); } watchAndWait(path: string, queueManager: IQueueManager, loggerPath: string): void { From 976468b44d85a36fd544f9efa0e7b55a6061b0fa Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Tue, 11 Feb 2025 21:16:22 -0300 Subject: [PATCH 03/25] Refactor registerSyncRoot to use providerId from instance and update related tests --- 3758a9e6-de8b-4e1e-a4fe-53300d078e99 | 0 examples/register.ts | 2 +- src/virtual-drive.ts | 10 ++-------- src/virtual-drive.unit.test.ts | 30 +++++++++++++++++++--------- 4 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 3758a9e6-de8b-4e1e-a4fe-53300d078e99 diff --git a/3758a9e6-de8b-4e1e-a4fe-53300d078e99 b/3758a9e6-de8b-4e1e-a4fe-53300d078e99 new file mode 100644 index 00000000..e69de29b diff --git a/examples/register.ts b/examples/register.ts index c0c0d3fc..108bff7d 100644 --- a/examples/register.ts +++ b/examples/register.ts @@ -20,7 +20,7 @@ const handlers = { handleAdd, handleHydrate, handleDehydrate, handleChangeSize } const notify = { onTaskSuccess: async () => undefined, onTaskProcessing: async () => undefined }; const queueManager = new QueueManager(handlers, notify, settings.queuePersistPath); -drive.registerSyncRoot(settings.driveName, settings.driveVersion, settings.providerid, callbacks, settings.iconPath); +drive.registerSyncRoot(settings.driveName, settings.driveVersion, callbacks, settings.iconPath); drive.connectSyncRoot(); try { diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 153ef649..b0029d1a 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -159,19 +159,13 @@ class VirtualDrive { }); } - async registerSyncRoot( - providerName: string, - providerVersion: string, - providerId: string, - callbacks: Callbacks, - logoPath: string, - ): Promise { + async registerSyncRoot(providerName: string, providerVersion: string, callbacks: Callbacks, logoPath: string): Promise { this.callbacks = callbacks; console.log("Registering sync root: ", this.syncRootPath); return this.addon.registerSyncRoot({ providerName, providerVersion, - providerId, + providerId: this.providerId, logoPath, }); } diff --git a/src/virtual-drive.unit.test.ts b/src/virtual-drive.unit.test.ts index 14c9c711..af736ca6 100644 --- a/src/virtual-drive.unit.test.ts +++ b/src/virtual-drive.unit.test.ts @@ -29,8 +29,10 @@ describe("VirtualDrive", () => { }); describe("When convertToWindowsPath is called", () => { + const providerId = v4(); + // Arrange - const drive = new VirtualDrive(syncRootPath, logPath); + const drive = new VirtualDrive(syncRootPath, providerId, logPath); it("When unix path, then convert to windows path", () => { // Assert @@ -46,8 +48,10 @@ describe("VirtualDrive", () => { }); describe("When fixPath is called", () => { + const providerId = v4(); + // Arrange - const drive = new VirtualDrive(syncRootPath, logPath); + const drive = new VirtualDrive(syncRootPath, providerId, logPath); it("When absolute windows path, then do not modify it", () => { // Assert @@ -80,8 +84,10 @@ describe("VirtualDrive", () => { // Arrange mockExistsSync.mockReturnValue(false); + const providerId = v4(); + // Act - new VirtualDrive(syncRootPath, logPath); + new VirtualDrive(syncRootPath, providerId, logPath); // Assert expect(fs.mkdirSync).toHaveBeenCalledWith(syncRootPath, { @@ -93,8 +99,10 @@ describe("VirtualDrive", () => { // Arrange mockExistsSync.mockReturnValue(true); + const providerId = v4(); + // Act - new VirtualDrive(syncRootPath, logPath); + new VirtualDrive(syncRootPath, providerId, logPath); // Assert expect(fs.mkdirSync).not.toHaveBeenCalled(); @@ -102,7 +110,9 @@ describe("VirtualDrive", () => { it("Then it calls addon.addLoggerPath with logPath provided", () => { // Act - new VirtualDrive(syncRootPath, logPath); + const providerId = v4(); + + new VirtualDrive(syncRootPath, providerId, logPath); // Assert expect(addon.addLoggerPath).toHaveBeenCalledWith(logPath); @@ -113,7 +123,9 @@ describe("VirtualDrive", () => { it("Then it calls addon.createPlaceholderFile", () => { // Arrange mockExistsSync.mockReturnValue(true); - const drive = new VirtualDrive(syncRootPath, logPath); + const providerId = v4(); + + const drive = new VirtualDrive(syncRootPath, logPath, providerId); // Act drive.createFileByPath("folder/subfolder/file.txt", "file-id", 1234, 1660000000000, 1660000001000); @@ -135,16 +147,16 @@ describe("VirtualDrive", () => { describe("When call registerSyncRoot", () => { it("Then it assigns callbacks and calls addon.registerSyncRoot", async () => { // Arrange - const drive = new VirtualDrive(syncRootPath, logPath); + const providerId = v4(); + const drive = new VirtualDrive(syncRootPath, providerId, logPath); const providerName = "MyProvider"; const providerVersion = "1.0.0"; - const providerId = v4(); const logoPath = "C:\\iconPath"; const callbacks = mockDeep(); // Act expect(drive.callbacks).toBe(undefined); - await drive.registerSyncRoot(providerName, providerVersion, providerId, callbacks, logoPath); + await drive.registerSyncRoot(providerName, providerVersion, callbacks, logoPath); // Assert expect(drive.callbacks).not.toBe(undefined); From 850e4c32eda5051c014655a520689d970e050539 Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Wed, 12 Feb 2025 09:37:57 -0300 Subject: [PATCH 04/25] Remove connections map from VirtualDrive and simplify disconnectSyncRoot method --- src/virtual-drive.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index b0029d1a..1ca3564c 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -21,7 +21,6 @@ class VirtualDrive { callbacks?: Callbacks; watcher = new Watcher(); logger: winston.Logger; - connections: Map = new Map(); addon: Addon; @@ -91,18 +90,11 @@ class VirtualDrive { const connectionKey = this.addon.connectSyncRoot({ syncRootPath: this.syncRootPath, callbacks: this.callbacks }); console.log("Connection key: ", connectionKey); - this.connections.set(this.syncRootPath, connectionKey); return connectionKey; } disconnectSyncRoot() { - const connectionKey = this.connections.get(this.syncRootPath); - if (connectionKey) { - this.addon.disconnectSyncRoot({ syncRootPath: this.syncRootPath }); - this.connections.delete(this.syncRootPath); - } else { - throw new Error(`No connection found for syncRootPath: ${this.syncRootPath}`); - } + this.addon.disconnectSyncRoot({ syncRootPath: this.syncRootPath }); } createPlaceholderFile( From 693be0ca9842ceea43706ea389f8597dbf93a9dd Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Wed, 12 Feb 2025 12:30:46 -0300 Subject: [PATCH 05/25] Add FilePlaceholderId type for improved type safety in TFetchDataCallback --- src/types/callbacks.type.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/types/callbacks.type.ts b/src/types/callbacks.type.ts index 96b09a5d..7e3be7d4 100644 --- a/src/types/callbacks.type.ts +++ b/src/types/callbacks.type.ts @@ -1,7 +1,11 @@ export type NapiCallbackFunction = (...args: any[]) => any; +export type FilePlaceholderIdPrefixType = "FILE:"; + +export type FilePlaceholderId = `${FilePlaceholderIdPrefixType}${string}`; + export type TFetchDataCallback = ( - id: string, + id: FilePlaceholderId, callback: (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>, ) => void; From 326ad37366b6deb323f8b5f305a68317929c58c1 Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Thu, 13 Feb 2025 12:22:03 -0300 Subject: [PATCH 06/25] Remove syncRootPath parameter from connectSyncRoot method for cleaner API usage --- src/addon-wrapper.ts | 2 +- src/virtual-drive.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/addon-wrapper.ts b/src/addon-wrapper.ts index 7c77c4a2..14a2097b 100644 --- a/src/addon-wrapper.ts +++ b/src/addon-wrapper.ts @@ -27,7 +27,7 @@ export class Addon { return this.parseAddonZod("registerSyncRoot", result); } - connectSyncRoot({ callbacks }: { callbacks: Callbacks; syncRootPath: string }) { + connectSyncRoot({ callbacks }: { callbacks: Callbacks }) { const result = addon.connectSyncRoot(this.syncRootPath, callbacks); return this.parseAddonZod("connectSyncRoot", result); } diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 1ca3564c..86fadb42 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -87,7 +87,7 @@ class VirtualDrive { console.log("Connecting to sync root: ", this.syncRootPath); - const connectionKey = this.addon.connectSyncRoot({ syncRootPath: this.syncRootPath, callbacks: this.callbacks }); + const connectionKey = this.addon.connectSyncRoot({ callbacks: this.callbacks }); console.log("Connection key: ", connectionKey); return connectionKey; From 5d68993c44dcb0497b9d60de58dd13a33ef3e5d4 Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Thu, 13 Feb 2025 14:45:21 -0300 Subject: [PATCH 07/25] Refactor VirtualDrive to use logger for connection debug information and update constructor parameter order --- src/virtual-drive.ts | 5 ++--- src/virtual-drive.unit.test.ts | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 86fadb42..4c68b25c 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -1,3 +1,4 @@ +import { logger } from "examples/drive"; import fs from "fs"; import path, { join, win32 } from "path"; import winston from "winston"; @@ -85,11 +86,9 @@ class VirtualDrive { throw new Error("Callbacks are not defined"); } - console.log("Connecting to sync root: ", this.syncRootPath); - const connectionKey = this.addon.connectSyncRoot({ callbacks: this.callbacks }); - console.log("Connection key: ", connectionKey); + this.logger.debug({ fn: "connectSyncRoot", connectionKey }); return connectionKey; } diff --git a/src/virtual-drive.unit.test.ts b/src/virtual-drive.unit.test.ts index af736ca6..712d6366 100644 --- a/src/virtual-drive.unit.test.ts +++ b/src/virtual-drive.unit.test.ts @@ -125,7 +125,7 @@ describe("VirtualDrive", () => { mockExistsSync.mockReturnValue(true); const providerId = v4(); - const drive = new VirtualDrive(syncRootPath, logPath, providerId); + const drive = new VirtualDrive(syncRootPath, providerId, logPath); // Act drive.createFileByPath("folder/subfolder/file.txt", "file-id", 1234, 1660000000000, 1660000001000); From 43e9c786a2ba3ec6263efcccb8f68a0e7fe9601d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 18:49:09 +0100 Subject: [PATCH 08/25] Delete 3758a9e6-de8b-4e1e-a4fe-53300d078e99 --- 3758a9e6-de8b-4e1e-a4fe-53300d078e99 | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 3758a9e6-de8b-4e1e-a4fe-53300d078e99 diff --git a/3758a9e6-de8b-4e1e-a4fe-53300d078e99 b/3758a9e6-de8b-4e1e-a4fe-53300d078e99 deleted file mode 100644 index e69de29b..00000000 From bbc1c095d3196455658834248a47faf245a15608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 19:18:26 +0100 Subject: [PATCH 09/25] Add prettier --- .github/workflows/publish.yml | 10 +- .github/workflows/pull-request.yml | 7 +- examples/populate.ts | 4 +- examples/utils.ts | 65 ++++++------- examples/utils/generate-random-file-tree.ts | 53 +++------- gyp.config.json | 12 +-- package.json | 102 ++++++++++---------- 7 files changed, 113 insertions(+), 140 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index a62f77f4..b8f9ad64 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -3,7 +3,7 @@ on: release: types: [created] workflow_dispatch: - + jobs: build-windows: runs-on: windows-latest @@ -12,7 +12,7 @@ jobs: packages: write steps: - uses: actions/checkout@v2 - - run: yarn build:gyp + - run: yarn build:gyp - uses: actions/upload-artifact@v2 with: name: windows-binaries @@ -32,11 +32,11 @@ jobs: path: build/Release - uses: actions/setup-node@v2 with: - node-version: '16.x' - registry-url: 'https://npm.pkg.github.com' + node-version: "16.x" + registry-url: "https://npm.pkg.github.com" - run: yarn env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: yarn publish env: NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 988992fd..5f4311a3 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -22,5 +22,8 @@ jobs: - name: Run TypeScript compiler run: yarn tsc - # - name: Run Vitest - # run: yarn vitest:once + - name: Run Prettier + run: yarn prettier . --check + + # - name: Run tests + # run: yarn test:once diff --git a/examples/populate.ts b/examples/populate.ts index 0843a9d7..99cc31f1 100644 --- a/examples/populate.ts +++ b/examples/populate.ts @@ -1,9 +1,9 @@ -import VirtualDrive from "@/virtual-drive"; - import { execSync } from "child_process"; import { join } from "path"; import { v4 } from "uuid"; +import VirtualDrive from "@/virtual-drive"; + import settings from "./settings"; const rootFileName1 = v4(); diff --git a/examples/utils.ts b/examples/utils.ts index 5636667d..fc0dd972 100644 --- a/examples/utils.ts +++ b/examples/utils.ts @@ -1,47 +1,46 @@ -import fs from 'fs'; -import path from 'path'; +import fs from "fs"; +import path from "path"; interface FileDetail { - path: string; - size: number; - baseDir: string; + path: string; + size: number; + baseDir: string; } function readFilesRecursively(dir: string, fileList: FileDetail[] = []): FileDetail[] { - fs.readdirSync(dir).forEach(file => { - const filePath = path.join(dir, file); - if (fs.statSync(filePath).isDirectory()) { - readFilesRecursively(filePath, fileList); - } else { - fileList.push({ - path: filePath, - size: fs.statSync(filePath).size, - baseDir: dir - }); - } - }); - return fileList; + fs.readdirSync(dir).forEach((file) => { + const filePath = path.join(dir, file); + if (fs.statSync(filePath).isDirectory()) { + readFilesRecursively(filePath, fileList); + } else { + fileList.push({ + path: filePath, + size: fs.statSync(filePath).size, + baseDir: dir, + }); + } + }); + return fileList; } function createFilesWithSize(sourceFolder: string, destFolder: string): void { - const files: FileDetail[] = readFilesRecursively(sourceFolder); + const files: FileDetail[] = readFilesRecursively(sourceFolder); - if (!fs.existsSync(destFolder)) { - fs.mkdirSync(destFolder, { recursive: true }); - } - - files.forEach(file => { - const relativePath = path.relative(file.baseDir, file.path); - const destFilePath = path.join(file.baseDir.replace(sourceFolder, destFolder), relativePath);//path.join(destFolder, relativePath); - const destFileDir = file.baseDir.replace(sourceFolder, destFolder);//path.dirname(destFilePath); + if (!fs.existsSync(destFolder)) { + fs.mkdirSync(destFolder, { recursive: true }); + } - if (!fs.existsSync(destFileDir)){ - fs.mkdirSync(destFileDir, { recursive: true }); - } + files.forEach((file) => { + const relativePath = path.relative(file.baseDir, file.path); + const destFilePath = path.join(file.baseDir.replace(sourceFolder, destFolder), relativePath); //path.join(destFolder, relativePath); + const destFileDir = file.baseDir.replace(sourceFolder, destFolder); //path.dirname(destFilePath); - fs.writeFileSync(destFilePath, Buffer.alloc(file.size)); - }); + if (!fs.existsSync(destFileDir)) { + fs.mkdirSync(destFileDir, { recursive: true }); + } + fs.writeFileSync(destFilePath, Buffer.alloc(file.size)); + }); } -export { createFilesWithSize }; \ No newline at end of file +export { createFilesWithSize }; diff --git a/examples/utils/generate-random-file-tree.ts b/examples/utils/generate-random-file-tree.ts index f5cebba8..42b7fc94 100644 --- a/examples/utils/generate-random-file-tree.ts +++ b/examples/utils/generate-random-file-tree.ts @@ -1,8 +1,9 @@ -import VirtualDrive from '@/virtual-drive'; -import { v4 as uuidv4 } from 'uuid'; +import { v4 as uuidv4 } from "uuid"; + +import VirtualDrive from "@/virtual-drive"; interface GenerateOptions { - rootPath: string + rootPath: string; depth: number; filesPerFolder: number; foldersPerLevel: number; @@ -26,18 +27,7 @@ function randomNormal(mean: number, stdDev: number): number { } function getRandomExtension(): string { - const extensions = [ - ".txt", - ".pdf", - ".rar", - ".jpg", - ".docx", - ".xlsx", - ".mp4", - ".mkv", - ".json", - "" - ]; + const extensions = [".txt", ".pdf", ".rar", ".jpg", ".docx", ".xlsx", ".mp4", ".mkv", ".json", ""]; const index = Math.floor(Math.random() * extensions.length); return extensions[index]; } @@ -47,17 +37,11 @@ async function createStructureRecursively( currentPath: string, level: number, options: GenerateOptions, - result: Record + result: Record, ): Promise { if (level < 0) return; - const { - filesPerFolder, - foldersPerLevel, - meanSize, - stdDev, - timeOffset - } = options; + const { filesPerFolder, foldersPerLevel, meanSize, stdDev, timeOffset } = options; for (let i = 0; i < filesPerFolder; i++) { const fileName = `file_${generateRandomId()}${getRandomExtension()}`; @@ -69,13 +53,7 @@ async function createStructureRecursively( const createdAt = Date.now() - (timeOffset || 0); const updatedAt = Date.now() - (timeOffset || 0) + 2000; - drive.createFileByPath( - fullPath, - fileId, - fileSize, - createdAt, - updatedAt - ); + drive.createFileByPath(fullPath, fileId, fileSize, createdAt, updatedAt); result[fileId] = fullPath; } @@ -88,22 +66,13 @@ async function createStructureRecursively( const createdAt = Date.now() - (timeOffset || 0) - 10000; // Ejemplo const updatedAt = Date.now() - (timeOffset || 0); - drive.createFolderByPath( - newFolderPath, - folderId, - 1000, - createdAt, - updatedAt - ); + drive.createFolderByPath(newFolderPath, folderId, 1000, createdAt, updatedAt); await createStructureRecursively(drive, newFolderPath, level - 1, options, result); } } -async function generateRandomFilesAndFolders( - drive: VirtualDrive, - options: GenerateOptions -): Promise> { +async function generateRandomFilesAndFolders(drive: VirtualDrive, options: GenerateOptions): Promise> { const { rootPath } = options; const result: Record = {}; @@ -114,4 +83,4 @@ async function generateRandomFilesAndFolders( } export { generateRandomFilesAndFolders }; -export type { GenerateOptions } \ No newline at end of file +export type { GenerateOptions }; diff --git a/gyp.config.json b/gyp.config.json index c62dde82..66c437f2 100644 --- a/gyp.config.json +++ b/gyp.config.json @@ -1,7 +1,7 @@ { - "gyp_file": "binding.gyp", - "source_dirs": ["native-src/**/*.cpp"], - "ignored_source_dirs": [], - "include_dirs": ["include"], - "ignored_include_dirs": [] -} \ No newline at end of file + "gyp_file": "binding.gyp", + "source_dirs": ["native-src/**/*.cpp"], + "ignored_source_dirs": [], + "include_dirs": ["include"], + "ignored_include_dirs": [] +} diff --git a/package.json b/package.json index dbc7e550..7ef35f1e 100644 --- a/package.json +++ b/package.json @@ -1,52 +1,54 @@ { - "name": "virtual-drive", - "version": "1.0.1", - "description": "", - "main": "dist/index.ts", - "types": "dist/index.d.ts", - "scripts": { - "========== Testing ==========": "", - "test": "vitest", - "test:once": "yarn vitest --run", - "test:one": "yarn vitest related x", - "========== Build ==========": "", - "clean": "node-gyp clean", - "build:gyp": "node-gyp configure build", - "build:ts": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json", - "config:gyp": "python gyp.config.py", - "build": "python gyp.config.py && node-gyp clean && node-gyp configure build && yarn build:ts", - "========== Examples ==========": "", - "prod:register": "node ./dist/examples/register.js", - "register": "nodemon", - "populate": "ts-node -r tsconfig-paths/register ./examples/populate.ts", - "get-state": "ts-node -r tsconfig-paths/register ./examples/get-state.ts", - "unregister": "ts-node -r tsconfig-paths/register ./examples/unregister.ts", - "disconnect": "ts-node -r tsconfig-paths/register ./examples/disconnect.ts" - }, - "author": "", - "license": "ISC", - "gypfile": true, - "devDependencies": { - "@trivago/prettier-plugin-sort-imports": "^5.2.1", - "@types/lodash.chunk": "^4.2.9", - "@types/node": "^20.5.0", - "@types/yargs": "^17.0.32", - "nodemon": "^3.1.9", - "prettier": "^3.4.2", - "ts-node": "^10.9.2", - "tsc-alias": "^1.8.10", - "typescript": "^5.1.6", - "vite-tsconfig-paths": "^5.1.4", - "vitest": "^3.0.3", - "vitest-mock-extended": "^2.0.2" - }, - "dependencies": { - "chokidar": "^3.6.0", - "lodash.chunk": "^4.2.0", - "tsconfig-paths": "^4.2.0", - "uuid": "^11.0.3", - "winston": "^3.17.0", - "yargs": "^17.7.2", - "zod": "^3.24.1" - } + "name": "virtual-drive", + "version": "1.0.1", + "description": "", + "main": "dist/index.ts", + "types": "dist/index.d.ts", + "scripts": { + "========== Testing ==========": "", + "test": "vitest", + "test:once": "yarn vitest --run", + "test:one": "yarn vitest related x", + "========== Build ==========": "", + "clean": "node-gyp clean", + "build:gyp": "node-gyp configure build", + "build:ts": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json", + "config:gyp": "python gyp.config.py", + "build": "python gyp.config.py && node-gyp clean && node-gyp configure build && yarn build:ts", + "========== Linter ==========": "", + "format": "prettier . --write", + "========== Examples ==========": "", + "prod:register": "node ./dist/examples/register.js", + "register": "nodemon", + "populate": "ts-node -r tsconfig-paths/register ./examples/populate.ts", + "get-state": "ts-node -r tsconfig-paths/register ./examples/get-state.ts", + "unregister": "ts-node -r tsconfig-paths/register ./examples/unregister.ts", + "disconnect": "ts-node -r tsconfig-paths/register ./examples/disconnect.ts" + }, + "author": "", + "license": "ISC", + "gypfile": true, + "devDependencies": { + "@trivago/prettier-plugin-sort-imports": "^5.2.1", + "@types/lodash.chunk": "^4.2.9", + "@types/node": "^20.5.0", + "@types/yargs": "^17.0.32", + "nodemon": "^3.1.9", + "prettier": "^3.4.2", + "ts-node": "^10.9.2", + "tsc-alias": "^1.8.10", + "typescript": "^5.1.6", + "vite-tsconfig-paths": "^5.1.4", + "vitest": "^3.0.3", + "vitest-mock-extended": "^2.0.2" + }, + "dependencies": { + "chokidar": "^3.6.0", + "lodash.chunk": "^4.2.0", + "tsconfig-paths": "^4.2.0", + "uuid": "^11.0.3", + "winston": "^3.17.0", + "yargs": "^17.7.2", + "zod": "^3.24.1" + } } From cf10e3c66a40b6bfb58afefd93692fae7502c606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 19:25:57 +0100 Subject: [PATCH 10/25] Remove prettier --- .github/workflows/pull-request.yml | 4 ++-- src/virtual-drive.ts | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 5f4311a3..91e96963 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -22,8 +22,8 @@ jobs: - name: Run TypeScript compiler run: yarn tsc - - name: Run Prettier - run: yarn prettier . --check + # - name: Run Prettier + # run: yarn prettier . --check # - name: Run tests # run: yarn test:once diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 4c68b25c..3aa94b52 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -1,4 +1,3 @@ -import { logger } from "examples/drive"; import fs from "fs"; import path, { join, win32 } from "path"; import winston from "winston"; From 1ec350ce424a149d7923cca7d64bb4e5e21a8753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 19:26:48 +0100 Subject: [PATCH 11/25] Update pull-request.yml --- .github/workflows/pull-request.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 91e96963..5f4311a3 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -22,8 +22,8 @@ jobs: - name: Run TypeScript compiler run: yarn tsc - # - name: Run Prettier - # run: yarn prettier . --check + - name: Run Prettier + run: yarn prettier . --check # - name: Run tests # run: yarn test:once From 4874fa21547917994b48d39f9f1fed3956b19d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 19:28:41 +0100 Subject: [PATCH 12/25] Create .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..94f480de --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file From 04c244762f8c30677ad3630d56dd71a9423a9c2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 19:29:24 +0100 Subject: [PATCH 13/25] Update .gitattributes --- .gitattributes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index 94f480de..21256661 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* text=auto eol=lf \ No newline at end of file +* text=auto \ No newline at end of file From 82ec8238c6dde39354b46c1fcedcf875dbf52548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 19:30:30 +0100 Subject: [PATCH 14/25] Update .gitattributes --- .gitattributes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index 21256661..b3fc4706 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* text=auto \ No newline at end of file +* text=auto eol=crlf \ No newline at end of file From b990df3c0c28be89388b02c24aaf83d585e1eb33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 21:43:49 +0100 Subject: [PATCH 15/25] Update pull-request.yml --- .github/workflows/pull-request.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 5f4311a3..7f341dae 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -25,5 +25,5 @@ jobs: - name: Run Prettier run: yarn prettier . --check - # - name: Run tests - # run: yarn test:once + - name: Run tests + run: yarn test:once From 0ee614d3efb91375818b99a49982f7de8f54a676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 21:47:14 +0100 Subject: [PATCH 16/25] Update pull-request.yml --- .github/workflows/pull-request.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 7f341dae..0a1f9247 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -5,7 +5,7 @@ on: jobs: type-check: - runs-on: ubuntu-latest + runs-on: windows-latest steps: - name: Checkout code From 8813dddd520a4213068ec801f4d8bf13790261af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 21:54:12 +0100 Subject: [PATCH 17/25] Remove IQueueManager --- src/queue/queueManager.ts | 6 ------ src/virtual-drive.ts | 4 ++-- src/watcher/watcher.ts | 9 ++------- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/queue/queueManager.ts b/src/queue/queueManager.ts index ada25d93..920db88a 100644 --- a/src/queue/queueManager.ts +++ b/src/queue/queueManager.ts @@ -18,9 +18,3 @@ export type HandleAction = (task: QueueItem) => Promise; export type HandleActions = { [key in typeQueue]: HandleAction; }; - -export interface IQueueManager { - actions: HandleActions; - - enqueue(task: QueueItem): void; -} diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index 3aa94b52..c40c3299 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -4,9 +4,9 @@ import winston from "winston"; import { Addon } from "./addon-wrapper"; import { createLogger } from "./logger"; -import { IQueueManager } from "./queue/queueManager"; import { Callbacks } from "./types/callbacks.type"; import { Watcher } from "./watcher/watcher"; +import { QueueManager } from "./queue/queue-manager"; const PLACEHOLDER_ATTRIBUTES = { FILE_ATTRIBUTE_READONLY: 0x1, @@ -164,7 +164,7 @@ class VirtualDrive { return this.addon.unregisterSyncRoot({ providerId: this.providerId }); } - watchAndWait(path: string, queueManager: IQueueManager, loggerPath: string): void { + watchAndWait(path: string, queueManager: QueueManager, loggerPath: string): void { this.watcher.addon = this.addon; this.watcher.queueManager = queueManager; this.watcher.logger = this.logger; diff --git a/src/watcher/watcher.ts b/src/watcher/watcher.ts index 2695b968..c185dc8a 100644 --- a/src/watcher/watcher.ts +++ b/src/watcher/watcher.ts @@ -4,21 +4,16 @@ import { Logger } from "winston"; import { Addon } from "@/addon-wrapper"; import { QueueManager } from "@/queue/queue-manager"; -import { IQueueManager } from "@/queue/queueManager"; import { OnAddDirService } from "./events/on-add-dir.service"; import { OnAddService } from "./events/on-add.service"; import { OnRawService } from "./events/on-raw.service"; -export namespace Watcher { - export type TOptions = WatchOptions; -} - export class Watcher { syncRootPath!: string; - options!: Watcher.TOptions; + options!: WatchOptions; addon!: Addon; - queueManager!: IQueueManager; + queueManager!: QueueManager; logger!: Logger; fileInDevice = new Set(); chokidar?: FSWatcher; From 61d0df6db2c0e730122c372659f68a15b2b5f814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Thu, 13 Feb 2025 22:00:00 +0100 Subject: [PATCH 18/25] Update virtual-drive.ts --- src/virtual-drive.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/virtual-drive.ts b/src/virtual-drive.ts index c40c3299..3322b034 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -4,9 +4,9 @@ import winston from "winston"; import { Addon } from "./addon-wrapper"; import { createLogger } from "./logger"; +import { QueueManager } from "./queue/queue-manager"; import { Callbacks } from "./types/callbacks.type"; import { Watcher } from "./watcher/watcher"; -import { QueueManager } from "./queue/queue-manager"; const PLACEHOLDER_ATTRIBUTES = { FILE_ATTRIBUTE_READONLY: 0x1, From 6eae75d3115095d0519885229773f5ba0e3324cd Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Fri, 21 Feb 2025 10:13:44 -0300 Subject: [PATCH 19/25] Remove unnecessary debug print statements from PlaceHolderInfo and Wrappers --- .../PlaceHolderInfo.cpp | 1 - .../placeholders_interface/Planceholders.cpp | 39 +------------------ native-src/virtual_drive/Wrappers.cpp | 3 -- 3 files changed, 2 insertions(+), 41 deletions(-) diff --git a/native-src/placeholders_interface/PlaceHolderInfo.cpp b/native-src/placeholders_interface/PlaceHolderInfo.cpp index bd726ab5..2c77c2b7 100644 --- a/native-src/placeholders_interface/PlaceHolderInfo.cpp +++ b/native-src/placeholders_interface/PlaceHolderInfo.cpp @@ -167,7 +167,6 @@ FileHandle handleForPath(const std::wstring &wPath) std::wstring_convert> converter; std::string path = converter.to_bytes(wPath); - printf("path IN HANDLERCREATOR: %s\n", path.c_str()); LPCSTR pPath = path.c_str(); std::filesystem::path pathFs(path); diff --git a/native-src/placeholders_interface/Planceholders.cpp b/native-src/placeholders_interface/Planceholders.cpp index 415401fd..1fbee5e8 100644 --- a/native-src/placeholders_interface/Planceholders.cpp +++ b/native-src/placeholders_interface/Planceholders.cpp @@ -45,9 +45,6 @@ void Placeholders::CreateOne( wstring fullPath = std::wstring(destPath) + L'\\' + fileName; - wprintf(L"[CreatePlaceholder] Full path: %s", fullPath.c_str()); - wprintf(L"\n"); - if (std::filesystem::exists(fullPath)) { Placeholders::ConvertToPlaceholder(fullPath, fileIdentity); @@ -85,7 +82,6 @@ void Placeholders::CreateOne( prop.Value(L"Value1"); prop.IconResource(L"shell32.dll,-44"); - wprintf(L"[CreatePlaceholder] Successfully created placeholder file\n"); // UpdateSyncStatus(fullDestPath, true, false); } catch (...) @@ -112,7 +108,6 @@ void Placeholders::MaintainIdentity(std::wstring &fullPath, PCWSTR itemIdentity, std::string identity = Placeholders::GetFileIdentity(fullPath); if (!identity.empty()) { - wprintf(L"[MaintainIdentity] Identity is not empty\n"); int len = WideCharToMultiByte(CP_UTF8, 0, itemIdentity, -1, NULL, 0, NULL, NULL); if (len > 0) { @@ -120,13 +115,7 @@ void Placeholders::MaintainIdentity(std::wstring &fullPath, PCWSTR itemIdentity, WideCharToMultiByte(CP_UTF8, 0, itemIdentity, -1, &itemIdentityStr[0], len, NULL, NULL); std::string cleanIdentity = cleanString(identity); std::string cleanItemIdentity = cleanString(itemIdentityStr); - printf("[MaintainIdentity] cleanCurrentIdentity: %s\n", cleanIdentity.c_str()); - printf("[MaintainIdentity] cleanItemIdentity: %s\n", cleanItemIdentity.c_str()); - if (cleanIdentity == cleanItemIdentity) - { - wprintf(L"[MaintainIdentity] Identity is corret not need to update\n"); - } - else + if (cleanIdentity != cleanItemIdentity) { wprintf(L"[MaintainIdentity] Identity is incorrect, updating...\n"); std::wstring itemIdentityStrW(itemIdentity); @@ -138,7 +127,6 @@ void Placeholders::MaintainIdentity(std::wstring &fullPath, PCWSTR itemIdentity, // Handle error as needed } } - printf("[MaintainIdentity] End Maintain Identity\n"); } void Placeholders::CreateEntry( @@ -185,17 +173,11 @@ void Placeholders::CreateEntry( wprintf(L"[CreatePlaceholder] Failed to create placeholder directory with HRESULT 0x%08x\n", hr); throw winrt::hresult_error(hr); } - else - { - wprintf(L"[CreatePlaceholder] Successfully created placeholder directory\n"); - } std::wstring finalPath = std::wstring(destPath) + L"\\" + std::wstring(itemName); Placeholders::UpdatePinState(finalPath, PinState::OnlineOnly); UpdateSyncStatus(finalPath, true, true); } - - wprintf(L"[CreatePlaceholder] Successfully created %s at %s\n", isDirectory ? L"directory" : L"file", fullDestPath.c_str()); } catch (const winrt::hresult_error &error) { @@ -230,7 +212,6 @@ bool Placeholders::ConvertToPlaceholder(const std::wstring &fullPath, const std: if (fileHandle == INVALID_HANDLE_VALUE) { // Manejar el error al abrir el archivo - wprintf(L"[ConvertToPlaceholder] Error opening file: %d\n", GetLastError()); return false; } @@ -260,8 +241,6 @@ bool Placeholders::ConvertToPlaceholder(const std::wstring &fullPath, const std: 0, NULL); - wprintf(L"[ConvertToPlaceholder] Error details: %s\n", errorMsg); - // Liberar el buffer de mensaje de error LocalFree(errorMsg); @@ -328,7 +307,6 @@ void Placeholders::UpdateSyncStatus(const std::wstring &filePath, bool inputSync void Placeholders::UpdateFileIdentity(const std::wstring &filePath, const std::wstring &fileIdentity, bool isDirectory) { - wprintf(L"[UpdateFileIdentity] Path: %ls\n", filePath.c_str()); HANDLE fileHandle = CreateFileW( filePath.c_str(), FILE_WRITE_ATTRIBUTES, // permisson needed to change the state @@ -452,7 +430,6 @@ CF_PLACEHOLDER_STATE GetPlaceholderStateMock(const std::wstring &filePath) } } - FileState Placeholders::GetPlaceholderInfo(const std::wstring &directoryPath) { @@ -488,24 +465,12 @@ FileState Placeholders::GetPlaceholderInfo(const std::wstring &directoryPath) { SyncState syncState = syncStateOpt.value(); - - printf("placeholderInfo.SyncState: %s\n", syncStateToString(syncState).c_str()); - } - else - { - printf("placeholderInfo.SyncState: No value\n"); } if (pinStateOpt.has_value()) { PinState pinState = pinStateOpt.value(); - - printf("placeholderInfo.PinState: %s\n", pinStateToString(pinState).c_str()); - } - else - { - printf("placeholderInfo.PinState: No value\n"); } fileState.pinstate = pinStateOpt.value_or(PinState::Unspecified); @@ -607,7 +572,7 @@ HRESULT Placeholders::UpdatePinState(const std::wstring &path, const PinState st const auto cfState = pinStateToCfPinState(state); HRESULT result = CfSetPinState(handleForPath(path).get(), cfState, CF_SET_PIN_FLAG_NONE, nullptr); - + // ForceShellRefresh(path); if (result != S_OK) diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index e8ba0e6e..572d99b6 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -448,7 +448,6 @@ napi_value DisconnectSyncRootWrapper(napi_env env, napi_callback_info args) napi_value GetFileIdentityWrapper(napi_env env, napi_callback_info args) { - printf("GetFileIdentityWrapper\n"); size_t argc = 1; napi_value argv[1]; napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr); @@ -466,7 +465,6 @@ napi_value GetFileIdentityWrapper(napi_env env, napi_callback_info args) napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(fullPath)), pathLength + 1, nullptr); std::string fileIdentity = Placeholders::GetFileIdentity(fullPath); - printf("fileIdentity got\n"); fileIdentity.erase(std::remove(fileIdentity.begin(), fileIdentity.end(), '\0'), fileIdentity.end()); fileIdentity.erase(std::remove(fileIdentity.begin(), fileIdentity.end(), ' '), fileIdentity.end()); @@ -497,7 +495,6 @@ napi_value DeleteFileSyncRootWrapper(napi_env env, napi_callback_info args) napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(fullPath)), pathLength + 1, nullptr); SyncRoot::DeleteFileSyncRoot(fullPath); - printf("fileIdentity got\n"); delete[] fullPath; return nullptr; From 53fccf6aa342b03836489888c6a3df7e13df58c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Tue, 25 Feb 2025 12:35:00 +0100 Subject: [PATCH 20/25] Check PR size --- .github/workflows/pull-request.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 0a1f9247..214aa806 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -1,10 +1,10 @@ -name: Some pull request checks +name: Pull request checks on: pull_request: jobs: - type-check: + checks: runs-on: windows-latest steps: @@ -16,6 +16,11 @@ jobs: with: node-version: 18 + - name: Check PR size + - uses: maidsafe/pr_size_checker@v2 + with: + max_lines_changed: 500 + - name: Install dependencies run: yarn install --ignore-scripts From 406087baec88978b007e749cd1d948721abb1df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Tue, 25 Feb 2025 12:43:03 +0100 Subject: [PATCH 21/25] Update pull-request.yml --- .github/workflows/pull-request.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 214aa806..f46b430b 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -17,7 +17,7 @@ jobs: node-version: 18 - name: Check PR size - - uses: maidsafe/pr_size_checker@v2 + uses: maidsafe/pr_size_checker@v2 with: max_lines_changed: 500 From fb5df581aa0dcf8e2d2970497ed542a4f5ce158a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Tue, 25 Feb 2025 12:44:54 +0100 Subject: [PATCH 22/25] Split check PR size --- .github/workflows/check-pr-size.yml | 17 +++++++++++++++++ .github/workflows/pull-request.yml | 5 ----- 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/check-pr-size.yml diff --git a/.github/workflows/check-pr-size.yml b/.github/workflows/check-pr-size.yml new file mode 100644 index 00000000..dcac5602 --- /dev/null +++ b/.github/workflows/check-pr-size.yml @@ -0,0 +1,17 @@ +name: Check PR size + +on: + pull_request: + +jobs: + checks: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Check PR size + uses: maidsafe/pr_size_checker@v2 + with: + max_lines_changed: 500 diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index f46b430b..7e9a6ad6 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -16,11 +16,6 @@ jobs: with: node-version: 18 - - name: Check PR size - uses: maidsafe/pr_size_checker@v2 - with: - max_lines_changed: 500 - - name: Install dependencies run: yarn install --ignore-scripts From 147428a6629d454f37e28804e5fdd477373c9232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Tue, 25 Feb 2025 12:47:59 +0100 Subject: [PATCH 23/25] Update check-pr-size.yml --- .github/workflows/check-pr-size.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-pr-size.yml b/.github/workflows/check-pr-size.yml index dcac5602..d5d0d376 100644 --- a/.github/workflows/check-pr-size.yml +++ b/.github/workflows/check-pr-size.yml @@ -4,12 +4,14 @@ on: pull_request: jobs: - checks: + check_pr_size: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: Check PR size uses: maidsafe/pr_size_checker@v2 From 26d998e253dcd32ef77192b4ba19c4fb680375d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Tue, 25 Feb 2025 13:07:15 +0100 Subject: [PATCH 24/25] Update check-pr-size.yml --- .github/workflows/check-pr-size.yml | 37 +++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/.github/workflows/check-pr-size.yml b/.github/workflows/check-pr-size.yml index d5d0d376..d19b4d0c 100644 --- a/.github/workflows/check-pr-size.yml +++ b/.github/workflows/check-pr-size.yml @@ -9,11 +9,38 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Check PR size - uses: maidsafe/pr_size_checker@v2 - with: - max_lines_changed: 500 + - name: Calculate changed lines + id: diff_check + run: | + # Get the target branch commit (base) and the PR branch commit (head) + BASE_SHA="${{ github.event.pull_request.base.sha }}" + HEAD_SHA="${{ github.event.pull_request.head.sha }}" + echo "Base SHA: $BASE_SHA" + echo "Head SHA: $HEAD_SHA" + + # Compute the merge base between the two branches. + MERGE_BASE=$(git merge-base "$HEAD_SHA" "$BASE_SHA") + echo "Merge Base: $MERGE_BASE" + + # Calculate added and deleted lines between the merge base and the head commit. + TOTAL_CHANGED=$(git diff --numstat "$MERGE_BASE" "$HEAD_SHA" \ + | awk '{ added += $1; deleted += $2 } END { print added + deleted }') + + # Default to 0 if nothing is output. + TOTAL_CHANGED=${TOTAL_CHANGED:-0} + echo "Total changed lines: $TOTAL_CHANGED" + + # Make the total available for later steps. + echo "total=$TOTAL_CHANGED" >> "$GITHUB_OUTPUT" + + - name: Fail if too many changes + if: ${{ steps.diff_check.outputs.total > 500 }} + run: | + echo "PR has ${{ steps.diff_check.outputs.total }} changed lines, which exceeds the 500-line limit." + echo "Please reduce the size of this PR." + exit 1 + From 374d011e2c4cb726c7dee893858a0008507cd1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Jim=C3=A9nez=20Rivera?= Date: Tue, 25 Feb 2025 13:09:18 +0100 Subject: [PATCH 25/25] Update check-pr-size.yml --- .github/workflows/check-pr-size.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/check-pr-size.yml b/.github/workflows/check-pr-size.yml index d19b4d0c..6289183a 100644 --- a/.github/workflows/check-pr-size.yml +++ b/.github/workflows/check-pr-size.yml @@ -22,19 +22,20 @@ jobs: echo "Base SHA: $BASE_SHA" echo "Head SHA: $HEAD_SHA" - # Compute the merge base between the two branches. + # Compute the merge base between the two branches MERGE_BASE=$(git merge-base "$HEAD_SHA" "$BASE_SHA") echo "Merge Base: $MERGE_BASE" - # Calculate added and deleted lines between the merge base and the head commit. + # Calculate added and deleted lines between the merge base and the head commit TOTAL_CHANGED=$(git diff --numstat "$MERGE_BASE" "$HEAD_SHA" \ + | grep -v "yarn.lock" \ | awk '{ added += $1; deleted += $2 } END { print added + deleted }') - # Default to 0 if nothing is output. + # Default to 0 if nothing is output TOTAL_CHANGED=${TOTAL_CHANGED:-0} echo "Total changed lines: $TOTAL_CHANGED" - # Make the total available for later steps. + # Make the total available for later steps echo "total=$TOTAL_CHANGED" >> "$GITHUB_OUTPUT" - name: Fail if too many changes @@ -43,4 +44,3 @@ jobs: echo "PR has ${{ steps.diff_check.outputs.total }} changed lines, which exceeds the 500-line limit." echo "Please reduce the size of this PR." exit 1 -