diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..b3fc4706 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=crlf \ No newline at end of file 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..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 @@ -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" + } } 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 4c68b25c..3322b034 100644 --- a/src/virtual-drive.ts +++ b/src/virtual-drive.ts @@ -1,11 +1,10 @@ -import { logger } from "examples/drive"; import fs from "fs"; import path, { join, win32 } from "path"; import winston from "winston"; import { Addon } from "./addon-wrapper"; import { createLogger } from "./logger"; -import { IQueueManager } from "./queue/queueManager"; +import { QueueManager } from "./queue/queue-manager"; import { Callbacks } from "./types/callbacks.type"; import { Watcher } from "./watcher/watcher"; @@ -165,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;