From e1e821efec0506f66a8d5294c8bf148897d2b81b Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Wed, 23 Aug 2023 09:38:49 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartfile.fsstream.ts | 27 ++++++++++++++++++++++++++- ts/smartfile.plugins.ts | 3 ++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index b7e2213..8c22214 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartfile', - version: '10.0.28', + version: '10.0.29', description: 'offers smart ways to work with files in nodejs' } diff --git a/ts/smartfile.fsstream.ts b/ts/smartfile.fsstream.ts index 02d04cf..5abaa56 100644 --- a/ts/smartfile.fsstream.ts +++ b/ts/smartfile.fsstream.ts @@ -11,4 +11,29 @@ export const createWriteStream = (pathArg: string) => { return plugins.fs.createWriteStream(pathArg); }; -export const streamDirectory = async (dirPathArg: string) => {}; +export const processFile = async ( + filePath: string, + asyncFunc: (fileStream: plugins.stream.Readable) => Promise +): Promise => { + return new Promise((resolve, reject) => { + const fileStream = createReadStream(filePath); + asyncFunc(fileStream).then(resolve).catch(reject); + }); +} + +export const processDirectory = async ( + directoryPath: string, + asyncFunc: (fileStream: plugins.stream.Readable) => Promise +): Promise => { + const files = plugins.fs.readdirSync(directoryPath, { withFileTypes: true }); + + for (const file of files) { + const fullPath = plugins.path.join(directoryPath, file.name); + + if (file.isDirectory()) { + await processDirectory(fullPath, asyncFunc); // Recursively call processDirectory for directories + } else if (file.isFile()) { + await processFile(fullPath, asyncFunc); // Call async function with the file stream and wait for it + } + } +}; diff --git a/ts/smartfile.plugins.ts b/ts/smartfile.plugins.ts index c86c3b2..244a594 100644 --- a/ts/smartfile.plugins.ts +++ b/ts/smartfile.plugins.ts @@ -1,8 +1,9 @@ // node native scope import * as fs from 'fs'; import * as path from 'path'; +import * as stream from 'stream'; -export { fs, path }; +export { fs, path, stream }; // @pushrocks scope import * as lik from '@push.rocks/lik';