smartfile/readme.md
2024-04-01 17:46:40 +02:00

126 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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
```typescript
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
```typescript
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
```typescript
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.
```typescript
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.
```typescript
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:
```typescript
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.