Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.
Go to file
2024-04-01 21:35:01 +02:00
.gitea/workflows fix(core): update 2023-10-12 02:21:39 +02:00
.vscode fix(updated repo structure): update 2022-03-11 09:46:54 +01:00
test fix(core): update 2023-11-04 20:14:20 +01:00
ts fix(core): update 2024-04-01 17:46:40 +02:00
.gitignore fix(core): update 2020-03-15 18:58:46 +00:00
.npmignore add copy 2016-03-14 03:50:14 +01:00
license fix(core): update 2022-06-07 15:50:47 +02:00
npmextra.json update npmextra.json: githost 2024-04-01 21:35:01 +02:00
package.json 11.0.5 2024-04-01 17:46:40 +02:00
pnpm-lock.yaml fix(core): update 2023-11-24 19:15:41 +01:00
readme.md fix(core): update 2024-04-01 17:46:40 +02:00
tsconfig.json update npmextra.json: githost 2024-04-01 21:35:01 +02:00

SmartFile

SmartFile offers smart ways to work with files in nodejs.

Install

To install SmartFile, use npm or Yarn as follows:

npm install @push.rocks/smartfile --save

Or:

yarn add @push.rocks/smartfile

Usage

SmartFile is a comprehensive toolkit for file manipulation in Node.js. It provides functionalities for working with the filesystem, in-memory operations, streaming, and handling virtual directories. Below, you will find examples showcasing how to utilize these functionalities effectively.

Basic File Operations

For reading and writing files, SmartFile provides synchronous and asynchronous methods. Heres how you can use them:

Async Write to File

import { memory } from '@push.rocks/smartfile';

const myData: string = 'Hello, SmartFile!';

// Writing string data to a file asynchronously
memory.toFs(myData, './data/hello.txt');

Sync Write to File

import { memory } from '@push.rocks/smartfile';

const myData: string = 'Hello, World!';

// Writing string data to a file synchronously
memory.toFsSync(myData, './data/helloSync.txt');

Working with Streams

Streaming files to and from the filesystem is made easy with SmartFile. Heres an example:

Creating Read and Write Streams

import { fsStream } from '@push.rocks/smartfile';
import * as fs from 'fs';

// Creating a read stream
const readStream = fsStream.createReadStream('./data/readme.txt');

// Creating a write stream
const writeStream = fsStream.createWriteStream('./data/copy.txt');

// Piping the read stream to the write stream
readStream.pipe(writeStream);

Dealing with Virtual Directories

Virtual directories allow you to group and manipulate files as if they were in a filesystem structure without actually writing them to disk.

import { VirtualDirectory } from '@push.rocks/smartfile';

(async () => {
  // Creating a virtual directory from the file system
  const virtualDir = await VirtualDirectory.fromFsDirPath('./myDirectory');

  // Adding files from another virtual directory
  const anotherVirtualDir = await VirtualDirectory.fromFsDirPath('./anotherDirectory');
  await virtualDir.addVirtualDirectory(anotherVirtualDir, 'merged');

  // Saving the virtual directory to disk
  await virtualDir.saveToDisk('./outputDirectory');
})();

Advanced File Manipulation

SmartFile also allows for more advanced file manipulation techniques through the SmartFile class.

import { SmartFile } from '@push.rocks/smartfile';

(async () => {
  // Create a SmartFile instance from a file path
  const smartFile = await SmartFile.fromFilePath('./data/example.txt');

  // Edit the file content
  await smartFile.editContentAsString(async (currentContent: string) => {
    return currentContent.toUpperCase();
  });

  // Write the changes back to disk
  await smartFile.write();
})();

Conversion and Interpretation

You can easily convert file contents to objects or interpret file types for further processing:

import { memory } from '@push.rocks/smartfile';

(async () => {
  const fileString: string = await fs.promises.readFile('./data/example.json', 'utf8');
  const fileObject = memory.toObject(fileString, 'json');

  console.log(fileObject);
  // Proceed with the object...
})();

SmartFile simplifies handling files in a Node.js environment, providing a concise, promise-based API for various file operations, stream handling, and in-memory file manipulation. Whether you're dealing with physical files on the disk, manipulating file streams, or managing virtual files and directories, SmartFile has got you covered.

Information on Licensing

SmartFile is licensed under the MIT License. This permissive license is short and to the point. It lets people do anything they want with your code as long as they provide attribution back to you and dont hold you liable.