From 091b5971b99367bdf3286ea0cc5b1b2eb6c93024 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Thu, 12 Dec 2024 08:57:39 -0300 Subject: [PATCH] fix: replace Duplex with Transform (#282) * fix: replace Duplex with Transform * chore: apply prettier --- .eslintrc.json | 7 +------ README.md | 2 +- package.json | 34 ++++++++++++++++---------------- src/actions/commit-file-async.ts | 2 +- src/transform.ts | 17 +++++++++------- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 9a4cf93..8a2d359 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,10 +1,5 @@ { - "extends": [ - "xo", - "prettier", - "eslint:recommended", - "plugin:@typescript-eslint/recommended" - ], + "extends": ["xo", "prettier", "eslint:recommended", "plugin:@typescript-eslint/recommended"], "parser": "@typescript-eslint/parser", "env": { "node": true diff --git a/README.md b/README.md index 2d9420f..303e516 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ const store = createMemFs(); const fs = createEditor(store); fs.write('somefile.js', 'var a = 1;'); -await fs.commit() +await fs.commit(); ``` ### `#read(filepath, [options])` diff --git a/package.json b/package.json index fd12232..636aec7 100644 --- a/package.json +++ b/package.json @@ -2,19 +2,10 @@ "name": "mem-fs-editor", "version": "11.1.3", "description": "File edition helpers working on top of mem-fs", - "type": "module", - "scripts": { - "build": "tsc", - "fix": "eslint . --fix", - "prepare": "npm run build", - "pretest": "eslint .", - "test": "vitest --run" - }, "repository": "SBoudrias/mem-fs-editor", - "author": "Simon Boudrias", "license": "MIT", - "main": "./dist/index.js", - "types": "./dist/index.d.ts", + "author": "Simon Boudrias", + "type": "module", "exports": { ".": { "types": "./dist/index.d.ts", @@ -29,9 +20,18 @@ "default": "./dist/transform.js" } }, + "main": "./dist/index.js", + "types": "./dist/index.d.ts", "files": [ "dist" ], + "scripts": { + "build": "tsc", + "fix": "eslint . --fix", + "prepare": "npm run build", + "pretest": "eslint .", + "test": "vitest --run" + }, "dependencies": { "@types/ejs": "^3.1.4", "@types/node": ">=18", @@ -47,12 +47,6 @@ "textextensions": "^6.11.0", "vinyl": "^3.0.0" }, - "peerDependencies": { - "mem-fs": "^4.0.0" - }, - "acceptDependencies": { - "isbinaryfile": "^5.0.3" - }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^7.18.0", "@typescript-eslint/parser": "^7.18.0", @@ -69,7 +63,13 @@ "typescript": "^5.2.2", "vitest": "^2.0.5" }, + "peerDependencies": { + "mem-fs": "^4.0.0" + }, "engines": { "node": ">=18.0.0" + }, + "acceptDependencies": { + "isbinaryfile": "^5.0.3" } } diff --git a/src/actions/commit-file-async.ts b/src/actions/commit-file-async.ts index 0c204dd..031f17b 100644 --- a/src/actions/commit-file-async.ts +++ b/src/actions/commit-file-async.ts @@ -10,7 +10,7 @@ async function write(file: MemFsEditorFile) { const dir = path.dirname(file.path); try { if (!(await fs.stat(dir)).isDirectory()) { - throw new Error(`${file.path} is not a directory`); + throw new Error(`${dir} is not a directory`); } } catch (error) { if ((error as any).code === 'ENOENT') { diff --git a/src/transform.ts b/src/transform.ts index bcae8ce..495d0ef 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -1,11 +1,14 @@ -import { Duplex } from 'stream'; +import { Transform } from 'stream'; import commitFileAsync from './actions/commit-file-async.js'; import type { MemFsEditorFile } from './index.js'; -export const createCommitTransform = () => - Duplex.from(async function* (generator: AsyncGenerator) { - for await (const file of generator) { - await commitFileAsync(file); - yield file; - } +export const createCommitTransform = () => + new Transform({ + objectMode: true, + transform(file: MemFsEditorFile, _encoding, callback) { + commitFileAsync(file).then( + () => callback(null, file), + (error) => callback(error), + ); + }, });