Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ jobs:
- name: Run Prettier
run: npm run format

- name: Run Eslint
run: npm run lint

- name: Run tests
run: npm run test:once
17 changes: 17 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import js from "@eslint/js";
import { defineConfig, globalIgnores } from "eslint/config";
import globals from "globals";
import tseslint from "typescript-eslint";

export default defineConfig([
{ files: ["**/*.{js,mjs,cjs,ts}"], plugins: { js }, extends: ["js/recommended"] },
{ files: ["**/*.{js,mjs,cjs,ts}"], languageOptions: { globals: globals.node } },
tseslint.configs.recommended,
globalIgnores(["dist"]),
{
rules: {
"@typescript-eslint/no-explicit-any": "warn",
"object-shorthand": "error",
},
},
]);
2 changes: 1 addition & 1 deletion examples/callbacks/cancel-fetch-data.callback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logger } from "examples/drive";

export const cancelFetchDataCallback = (fileId: string) => {
logger.info({ event: "cancelFetchDataCallback", fileId });
logger.debug({ msg: "cancelFetchDataCallback", fileId });
};
2 changes: 1 addition & 1 deletion examples/callbacks/notify-delete.callback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from "examples/drive";

export const notifyDeleteCallback = (fileId: string, callback: (response: boolean) => void) => {
logger.info({ event: "notifyDeleteCallback", fileId });
logger.debug({ msg: "notifyDeleteCallback", fileId });
callback(true);
};
2 changes: 1 addition & 1 deletion examples/callbacks/notify-fetch-data.callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TFetchDataCallback } from "@/types/callbacks.type";
import { sleep } from "@/utils";

export const fetchDataCallback = async (id: string, callback: Parameters<TFetchDataCallback>[1]) => {
logger.info({ fn: "fetchDataCallback", id });
logger.debug({ msg: "fetchDataCallback", id });
const path = await getInfoItem(id);

let finish = false;
Expand Down
2 changes: 1 addition & 1 deletion examples/callbacks/notify-message.callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const notifyMessageCallback = (
errorName: string,
callback: (response: boolean) => void,
) => {
logger.info({ event: "notifyMessageCallback", message, action, errorName });
logger.debug({ msg: "notifyMessageCallback", message, action, errorName });
callback(true);
};
2 changes: 1 addition & 1 deletion examples/callbacks/notify-rename.callback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from "examples/drive";

export const notifyRenameCallback = (newName: string, fileId: string, callback: (response: boolean) => void) => {
logger.info({ event: "notifyRenameCallback", newName, fileId });
logger.debug({ msg: "notifyRenameCallback", newName, fileId });
callback(true);
};
25 changes: 22 additions & 3 deletions examples/drive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import { createLogger } from "@/logger";
import { TBody } from "@/logger";
import VirtualDrive from "@/virtual-drive";

import settings from "./settings";

export const drive = new VirtualDrive(settings.syncRootPath, settings.providerid, settings.defaultLogPath);
export const logger = createLogger(settings.defaultLogPath);
export const logger = {
debug(body: TBody) {
console.debug(body);
},
info(body: TBody) {
console.info(body);
},
warn(body: TBody) {
console.warn(body);
},
error(body: TBody) {
console.error(body);
},
};

export const drive = new VirtualDrive({
syncRootPath: settings.syncRootPath,
providerId: settings.providerid,
loggerPath: settings.defaultLogPath,
logger,
});
4 changes: 2 additions & 2 deletions examples/get-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (data) {
const state = drive.getPlaceholderState({
path,
});
logger.info({ state });
logger.debug({ msg: "state", state });
} else {
logger.error("Por favor especifica un archivo con --file <path>");
logger.error({ msg: "Please specify a file with --file <path>" });
}
4 changes: 2 additions & 2 deletions examples/handlers/handle-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { QueueItem } from "@/queue/queueManager";

export const handleAdd = async (task: QueueItem) => {
try {
logger.info({ fn: "handleAdd", task });
logger.debug({ msg: "handleAdd", task });
const id = task.isFolder ? v4() : addInfoItem(task.path);
drive.convertToPlaceholder({
itemPath: task.path,
id,
});
} catch (error) {
logger.error("handleAdd", error);
logger.error({ msg: "handleAdd", error });
}
};
4 changes: 2 additions & 2 deletions examples/handlers/handle-change-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { QueueItem } from "@/queue/queueManager";

export const handleChangeSize = async (task: QueueItem) => {
try {
logger.info({ fn: "handleChangeSize", path: task.path });
logger.debug({ msg: "handleChangeSize", path: task.path });
const id = v4();
drive.convertToPlaceholder({
itemPath: task.path,
Expand All @@ -17,6 +17,6 @@ export const handleChangeSize = async (task: QueueItem) => {
isDirectory: task.isFolder,
});
} catch (error) {
logger.error("handleChangeSize", error);
logger.error({ msg: "handleChangeSize", error });
}
};
4 changes: 2 additions & 2 deletions examples/handlers/handle-dehydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { QueueItem } from "@/queue/queueManager";

export const handleDehydrate = async (task: QueueItem) => {
try {
logger.info({ fn: "handleDehydrate", path: task.path });
logger.debug({ msg: "handleDehydrate", path: task.path });
drive.dehydrateFile({
itemPath: task.path,
});
} catch (error) {
logger.error("handleDehydrate", error);
logger.error({ msg: "handleDehydrate", error });
}
};
4 changes: 2 additions & 2 deletions examples/handlers/handle-hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { QueueItem } from "@/queue/queueManager";

export const handleHydrate = async (task: QueueItem) => {
try {
logger.info({ fn: "handleHydrate", path: task.path });
logger.debug({ msg: "handleHydrate", path: task.path });
await drive.hydrateFile({
itemPath: task.path,
});
} catch (error) {
logger.error("handleHydrate", error);
logger.error({ msg: "handleHydrate", error });
}
};
2 changes: 1 addition & 1 deletion examples/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execSync } from "child_process";
import { join } from "path";
import { v4 } from "uuid";

import VirtualDrive from "@/virtual-drive";
import "@/virtual-drive";

import settings from "./settings";

Expand Down
2 changes: 1 addition & 1 deletion examples/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ try {
initInfoItems();
drive.watchAndWait({ queueManager });
} catch (error) {
logger.error(error);
logger.error({ msg: "Error when register", error });
drive.disconnectSyncRoot();
drive.unregisterSyncRoot();
}
2 changes: 0 additions & 2 deletions examples/unregister.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import VirtualDrive from "@/virtual-drive";

import { drive } from "./drive";
import { deleteInfoItems } from "./info-items-manager";

Expand Down
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,42 @@
"config:gyp": "python gyp.config.py",
"build": "python gyp.config.py && node-gyp clean && node-gyp configure build && npm run build:ts",
"========== Linter ==========": "",
"lint": "eslint . --ext .ts,.tsx --max-warnings 13",
"lint:fix": "npm run run lint --fix",
"format": "prettier . --check",
"format:fix": "prettier . --write",
"type-check": "tsc",
"========== 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"
},
"devDependencies": {
"@eslint/js": "^9.24.0",
"@trivago/prettier-plugin-sort-imports": "^5.2.1",
"@types/lodash.chunk": "^4.2.9",
"@types/node": "^20.5.0",
"@types/yargs": "^17.0.32",
"eslint": "^9.24.0",
"globals": "^16.0.0",
"nodemon": "^3.1.9",
"prettier": "^3.4.2",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.10",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.6",
"typescript-eslint": "^8.30.1",
"uuid": "^11.1.0",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.0.3",
"vitest-mock-extended": "^2.0.2"
"vitest": "^3.0.5",
"vitest-mock-extended": "^2.0.2",
"yargs": "^17.7.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"
}
}
1 change: 1 addition & 0 deletions src/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { addonZod } from "./addon/addon-zod";
import { InputSyncCallbacks } from "./types/callbacks.type";

// eslint-disable-next-line @typescript-eslint/no-require-imports
export const addon: TAddon = require("../addon.node");

export type TAddon = {
Expand All @@ -16,7 +17,7 @@
lastWriteTime: string,
lastAccessTime: string,
path: string,
): any;

Check warning on line 20 in src/addon.ts

View workflow job for this annotation

GitHub Actions / checks

Unexpected any. Specify a different type
createEntry(
itemName: string,
itemId: string,
Expand All @@ -27,12 +28,12 @@
lastWriteTime: string,
lastAccessTime: string,
path: string,
): any;

Check warning on line 31 in src/addon.ts

View workflow job for this annotation

GitHub Actions / checks

Unexpected any. Specify a different type
hydrateFile(path: string): Promise<z.infer<typeof addonZod.hydrateFile>>;
dehydrateFile(path: string): z.infer<typeof addonZod.dehydrateFile>;
connectSyncRoot(path: string, callbacks: InputSyncCallbacks): z.infer<typeof addonZod.connectSyncRoot>;
convertToPlaceholder(path: string, id: string): z.infer<typeof addonZod.convertToPlaceholder>;
deleteFileSyncRoot(path: string): any;

Check warning on line 36 in src/addon.ts

View workflow job for this annotation

GitHub Actions / checks

Unexpected any. Specify a different type
getFileIdentity(path: string): z.infer<typeof addonZod.getFileIdentity>;
/**
* TODO: Not all paths return value
Expand All @@ -44,7 +45,7 @@
providerName: string,
providerVersion: string,
providerId: string,
callbacks: any,

Check warning on line 48 in src/addon.ts

View workflow job for this annotation

GitHub Actions / checks

Unexpected any. Specify a different type
logoPath: string,
): z.infer<typeof addonZod.registerSyncRoot>;
unregisterSyncRoot(path: string): z.infer<typeof addonZod.unregisterSyncRoot>;
Expand All @@ -52,6 +53,6 @@
/**
* TODO: Returns a type in c++ that is not initialized
*/
updateFileIdentity(itemPath: string, id: string, isDirectory: boolean): any;

Check warning on line 56 in src/addon.ts

View workflow job for this annotation

GitHub Actions / checks

Unexpected any. Specify a different type
getRegisteredSyncRoots(): z.infer<typeof addonZod.getRegisteredSyncRoots>;
};
33 changes: 9 additions & 24 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import { resolve } from "path";
import { inspect } from "util";
import winston from "winston";

const { format, transports } = winston;
export type TBody = {
msg: string;
[key: string]: unknown;
};

export const createLogger = (path: string) => {
return winston.createLogger({
format: format.errors({ stack: true }),
transports: [
new transports.File({
filename: resolve(path),
format: format.combine(format.timestamp(), format.json()),
}),
new transports.Console({
format: format.combine(
format.printf(({ level, message, stack }) => {
const object: any = { level, message };
if (stack) object.stack = stack;
return inspect(object, { colors: true, depth: Infinity, breakLength: Infinity });
}),
),
}),
],
});
export type TLogger = {
debug: (body: TBody) => void;
info: (body: TBody) => void;
warn: (body: TBody) => void;
error: (body: TBody) => void;
};
17 changes: 9 additions & 8 deletions src/queue/queue-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";
import lodashChunk from "lodash.chunk";
import { Logger } from "winston";

import { TLogger } from "@/logger";

import { HandleAction, HandleActions, QueueItem, typeQueue } from "./queueManager";

Expand Down Expand Up @@ -43,7 +44,7 @@ export class QueueManager {
// private readonly notify: QueueManagerCallback;
private readonly persistPath: string;

logger?: Logger;
logger?: TLogger;
actions: HandleActions;

constructor({ handlers, persistPath }: { handlers: QueueHandler; persistPath: string }) {
Expand Down Expand Up @@ -82,7 +83,7 @@ export class QueueManager {
}

private loadQueueStateFromFile(): void {
this.logger?.debug("Loading queue state from file:" + this.persistPath);
this.logger?.debug({ msg: "Loading queue state from file:" + this.persistPath });
if (this.persistPath) {
if (!fs.existsSync(this.persistPath)) {
this.saveQueueStateToFile();
Expand All @@ -108,11 +109,11 @@ export class QueueManager {
}

public enqueue(task: QueueItem): void {
this.logger?.debug({ fn: "enqueue", task });
this.logger?.debug({ msg: "enqueue", task });
const existingTask = this.queues[task.type].find((item) => item.path === task.path && item.type === task.type);

if (existingTask) {
this.logger?.info("Task already exists in queue. Skipping.");
this.logger?.info({ msg: "Task already exists in queue. Skipping." });
return;
}

Expand Down Expand Up @@ -186,17 +187,17 @@ export class QueueManager {
}

private async processTask(type: typeQueue, task: QueueItem) {
this.logger?.debug({ fn: "processTask", task });
this.logger?.debug({ msg: "processTask", task });

try {
await this.actions[task.type](task);
} catch (error) {
this.logger?.error(`Failed to process ${type} task`, error);
this.logger?.error({ msg: `Failed to process ${type} task`, error });
}
}

public async processAll(): Promise<void> {
this.logger?.debug({ fn: "processAll" });
this.logger?.debug({ msg: "processAll" });
const taskTypes = Object.keys(this.queues) as typeQueue[];
// await this.notify.onTaskProcessing();
await Promise.all(taskTypes.map((type: typeQueue) => this.processQueue(type)));
Expand Down
3 changes: 1 addition & 2 deletions src/queue/queue-manager.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { v4 } from "uuid";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { mockDeep } from "vitest-mock-extended";

import { QueueHandler, QueueManager, QueueManagerCallback } from "./queue-manager";
import { QueueHandler, QueueManager } from "./queue-manager";
import { QueueItem, typeQueue } from "./queueManager";

describe("QueueManager", () => {
const mockHandlers = mockDeep<QueueHandler>();
const mockCallbacks = mockDeep<QueueManagerCallback>();
let queueManager: QueueManager;

const persistPath = join(TEST_FILES, v4());
Expand Down
Loading
Loading