40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
/*
|
|
This file contains logic for streaming things from and to the filesystem
|
|
*/
|
|
import * as plugins from './smartfile.plugins.js';
|
|
|
|
export const createReadStream = (pathArg: string) => {
|
|
return plugins.fs.createReadStream(pathArg);
|
|
};
|
|
|
|
export const createWriteStream = (pathArg: string) => {
|
|
return plugins.fs.createWriteStream(pathArg);
|
|
};
|
|
|
|
export const processFile = async (
|
|
filePath: string,
|
|
asyncFunc: (fileStream: plugins.stream.Readable) => Promise<void>
|
|
): Promise<void> => {
|
|
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<void>
|
|
): Promise<void> => {
|
|
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
|
|
}
|
|
}
|
|
};
|