smartfile/readme.md

279 lines
12 KiB
Markdown
Raw Normal View History

2024-04-02 18:53:02 +00:00
# @push.rocks/smartfile
2024-04-03 13:39:26 +00:00
> Provides a robust suite of tools for managing files in Node.js using TypeScript.
2024-04-01 15:46:40 +00:00
## Install
2024-04-02 19:37:31 +00:00
To integrate `@push.rocks/smartfile` into your project, run:
2024-04-02 18:53:02 +00:00
```bash
npm install @push.rocks/smartfile
2024-04-01 15:46:40 +00:00
```
2020-02-07 21:00:54 +00:00
## Usage
2024-04-03 13:39:26 +00:00
`@push.rocks/smartfile` offers a comprehensive set of utilities for handling files in Node.js projects using modern TypeScript and ESM syntax. It simplifies various file operations such as reading, writing, copying, and streaming files, as well as working with directories and virtual file systems.
2024-04-01 15:46:40 +00:00
2024-04-03 13:39:26 +00:00
### Key Features and Classes
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
- **`SmartFile`**: Facilitates reading from and writing to individual files, managing metadata.
- **`StreamFile`**: Optimizes memory usage by enabling efficient file streaming.
- **`VirtualDirectory`**: Allows manipulation of a group of files or directories as a virtual file system.
2024-04-01 15:46:40 +00:00
2024-04-03 13:39:26 +00:00
### Getting Started with ESM and TypeScript
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
First, ensure your project supports ESM syntax and TypeScript. Then, begin by importing the desired features from `@push.rocks/smartfile`:
2024-04-01 15:46:40 +00:00
```typescript
2024-04-02 19:37:31 +00:00
import { SmartFile, StreamFile, VirtualDirectory, memory, fs as smartFs } from '@push.rocks/smartfile';
2024-04-01 15:46:40 +00:00
```
2024-04-03 13:39:26 +00:00
### Reading and Writing Files
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
#### Reading Files
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
Reading a JSON file:
2024-04-02 18:53:02 +00:00
```typescript
2024-04-02 19:37:31 +00:00
const myJsonFile: SmartFile = await SmartFile.fromFilePath('./data.json');
const jsonData = JSON.parse(myJsonFile.contents.toString());
console.log(jsonData);
2024-04-02 18:53:02 +00:00
```
2024-04-02 19:37:31 +00:00
#### Writing Files
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
Writing content to a file:
2024-04-01 15:46:40 +00:00
2024-04-02 18:53:02 +00:00
```typescript
2024-04-02 19:37:31 +00:00
const filePath: string = './output.txt';
const content: string = 'Hello, SmartFile!';
await memory.toFs(content, filePath);
console.log('File saved successfully.');
2024-04-01 15:46:40 +00:00
```
2024-04-03 13:39:26 +00:00
### Streaming Large Files
2024-04-02 18:53:02 +00:00
2024-04-02 19:37:31 +00:00
For large files, `StreamFile` provides a memory-efficient streaming solution:
2024-04-02 18:53:02 +00:00
2024-04-02 19:37:31 +00:00
```typescript
import { createReadStream } from 'fs';
const sourceStream = createReadStream('./large-video.mp4');
const myStreamFile = await StreamFile.fromStream(sourceStream, 'large-video.mp4');
await myStreamFile.writeToDir('./storage');
console.log('Large file streamed to disk successfully.');
2024-04-02 18:53:02 +00:00
```
2024-04-03 13:39:26 +00:00
### Working with Virtual Directories
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
`VirtualDirectory` abstracts a collection of files allowing operations to be performed as if they were on disk:
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
```typescript
const virtualDir = new VirtualDirectory();
virtualDir.addSmartfiles([smartFile1, smartFile2]); // Assuming these are SmartFile instances
await virtualDir.saveToDisk('./virtual-output');
console.log('Virtual directory saved to disk.');
2024-04-02 18:53:02 +00:00
```
2024-04-01 15:46:40 +00:00
2024-04-03 13:39:26 +00:00
### Advanced File Operations
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
`@push.rocks/smartfile` simplifies complex file operations, including:
- Copying directories and files
- Removing files or directories
- Listing files and directories with filters
- Reading file content directly into JavaScript objects
2024-04-01 15:46:40 +00:00
2024-04-03 13:39:26 +00:00
### Web File Handling
2020-02-07 21:00:54 +00:00
2024-04-02 19:37:31 +00:00
Handling files from HTTP requests:
`@push.rocks/smartfile` offers utilities to work with files from web sources, making it simpler to manage downloads and uploads.
2020-02-07 21:00:54 +00:00
2024-04-03 13:39:26 +00:00
### Comprehensive File Management
2024-04-02 18:53:02 +00:00
2024-04-02 19:37:31 +00:00
Whether you're dealing with local files, directories, or files over the internet, `@push.rocks/smartfile` provides a comprehensive set of tools to streamline your workflow and reduce the complexity of file management in your Node.js projects.
2024-04-02 18:53:02 +00:00
2024-04-03 13:39:26 +00:00
## API Reference
### `SmartFile` Class
#### Static Methods
- `SmartFile.fromFilePath(filePath: string, baseArg?: string): Promise<SmartFile>`
- Creates a `SmartFile` instance from a file path.
- `SmartFile.fromBuffer(filePath: string, contentBufferArg: Buffer, baseArg?: string): Promise<SmartFile>`
- Creates a `SmartFile` instance from a `Buffer`.
- `SmartFile.fromString(filePath: string, contentStringArg: string, encodingArg: 'utf8' | 'binary', baseArg?: string): Promise<SmartFile>`
- Creates a `SmartFile` instance from a string.
- `SmartFile.fromFoldedJson(foldedJsonArg: string): Promise<SmartFile>`
- Creates a `SmartFile` instance from a folded JSON string.
- `SmartFile.fromStream(stream: Readable, filePath: string, baseArg?: string): Promise<SmartFile>`
- Creates a `SmartFile` instance from a `Readable` stream.
- `SmartFile.fromUrl(urlArg: string): Promise<SmartFile>`
- Creates a `SmartFile` instance from a URL.
#### Instance Properties
- `path: string`
- The relative path of the file.
- `parsedPath: path.ParsedPath`
- A parsed path object.
- `absolutePath: string`
- The absolute path of the file.
- `absoluteParsedPath: path.ParsedPath`
- A parsed absolute path object.
- `contentBuffer: Buffer`
- The content of the file as a `Buffer`.
- `base: string`
- The current working directory of the file.
- `sync: boolean`
- Indicates whether the file is synced with the disk.
#### Instance Methods
- `setContentsFromString(contentString: string, encodingArg?: 'utf8' | 'binary'): void`
- Sets the contents of the file from a string.
- `write(): Promise<void>`
- Writes the file to disk at its original location.
- `writeToDiskAtPath(filePathArg: string): Promise<void>`
- Writes the file to the specified path on disk.
- `writeToDir(dirPathArg: string): Promise<string>`
- Writes the file to a directory combined with the relative path portion.
- `read(): Promise<void>`
- Reads the file from disk.
- `delete(): Promise<void>`
- Deletes the file from disk at its original location.
- `getHash(typeArg?: 'path' | 'content' | 'all'): Promise<string>`
- Returns a hash of the file based on the specified type.
- `updateFileName(fileNameArg: string): void`
- Updates the file name of the `SmartFile` instance.
- `editContentAsString(editFuncArg: (fileStringArg: string) => Promise<string>): Promise<void>`
- Edits the content of the file as a string using the provided edit function.
- `getStream(): Readable`
- Returns a `Readable` stream from the file's content buffer.
### `StreamFile` Class
#### Static Methods
- `StreamFile.fromPath(filePath: string): Promise<StreamFile>`
- Creates a `StreamFile` instance from a file path.
- `StreamFile.fromUrl(url: string): Promise<StreamFile>`
- Creates a `StreamFile` instance from a URL.
- `StreamFile.fromBuffer(buffer: Buffer, relativeFilePath?: string): StreamFile`
- Creates a `StreamFile` instance from a `Buffer`.
- `StreamFile.fromStream(stream: Readable, relativeFilePath?: string, multiUse?: boolean): StreamFile`
- Creates a `StreamFile` instance from a `Readable` stream.
#### Instance Properties
- `relativeFilePath?: string`
- The relative file path of the `StreamFile`.
- `multiUse: boolean`
- Indicates whether the `StreamFile` can be used multiple times.
- `used: boolean`
- Indicates whether the `StreamFile` has been used.
#### Instance Methods
- `createReadStream(): Promise<Readable>`
- Creates a new `Readable` stream from the source.
- `writeToDisk(filePathArg: string): Promise<void>`
- Writes the stream to the disk at the specified path.
- `writeToDir(dirPathArg: string): Promise<void>`
- Writes the stream to a directory combined with the relative path portion.
- `getContentAsBuffer(): Promise<Buffer>`
- Returns the content of the `StreamFile` as a `Buffer`.
- `getContentAsString(formatArg?: 'utf8' | 'binary'): Promise<string>`
- Returns the content of the `StreamFile` as a string.
### `VirtualDirectory` Class
#### Static Methods
- `VirtualDirectory.fromFsDirPath(pathArg: string): Promise<VirtualDirectory>`
- Creates a `VirtualDirectory` instance from a file system directory path.
- `VirtualDirectory.fromVirtualDirTransferableObject(virtualDirTransferableObjectArg: VirtualDirTransferableObject): Promise<VirtualDirectory>`
- Creates a `VirtualDirectory` instance from a `VirtualDirTransferableObject`.
#### Instance Properties
- `smartfileArray: SmartFile[]`
- An array of `SmartFile` instances in the `VirtualDirectory`.
#### Instance Methods
- `addSmartfiles(smartfileArrayArg: SmartFile[]): void`
- Adds an array of `SmartFile` instances to the `VirtualDirectory`.
- `getFileByPath(pathArg: string): Promise<SmartFile | undefined>`
- Retrieves a `SmartFile` instance from the `VirtualDirectory` by its path.
- `toVirtualDirTransferableObject(): Promise<VirtualDirTransferableObject>`
- Converts the `VirtualDirectory` to a `VirtualDirTransferableObject`.
- `saveToDisk(dirArg: string): Promise<void>`
- Saves the `VirtualDirectory` to the disk at the specified directory.
- `shiftToSubdirectory(subDir: string): Promise<VirtualDirectory>`
- Shifts the `VirtualDirectory` to a subdirectory.
- `addVirtualDirectory(virtualDir: VirtualDirectory, newRoot: string): Promise<void>`
- Adds another `VirtualDirectory` to the current `VirtualDirectory` with a new root path.
### `fs` Module
The `fs` module provides various file system utility functions. Some notable functions include:
- `fileExistsSync(filePath: string): boolean`
- Checks if a file exists synchronously.
- `fileExists(filePath: string): Promise<boolean>`
- Checks if a file exists asynchronously.
- `listFoldersSync(pathArg: string, regexFilter?: RegExp): string[]`
- Lists folders in a directory synchronously.
- `listFolders(pathArg: string, regexFilter?: RegExp): Promise<string[]>`
- Lists folders in a directory asynchronously.
- `listFilesSync(pathArg: string, regexFilter?: RegExp): string[]`
- Lists files in a directory synchronously.
- `listFiles(pathArg: string, regexFilter?: RegExp): Promise<string[]>`
- Lists files in a directory asynchronously.
- `listFileTree(dirPathArg: string, miniMatchFilter: string, absolutePathsBool?: boolean): Promise<string[]>`
- Lists a file tree using a minimatch filter.
- `waitForFileToBeReady(filePathArg: string): Promise<void>`
- Waits for a file to be ready (exists and is not empty).
- `toFs(filePathArg: string, fileContentArg: string | Buffer | SmartFile | StreamFile, optionsArg?: { respectRelative?: boolean }): Promise<void>`
- Writes a string, `Buffer`, `SmartFile`, or `StreamFile` to the file system.
### `memory` Module
The `memory` module provides utility functions for working with files in memory. Some notable functions include:
- `toObject(fileStringArg: string, fileTypeArg: string): any`
- Converts a file string to an object based on the file type.
- `toFs(fileContentArg: string | Buffer | SmartFile | StreamFile, filePathArg: string, optionsArg?: IToFsOptions): Promise<void>`
- Writes a string, `Buffer`, `SmartFile`, or `StreamFile` to the file system.
- `toFsSync(fileArg: string, filePathArg: string): void`
- Writes a string to the file system synchronously.
- `smartfileArrayToFs(smartfileArrayArg: SmartFile[], dirArg: string): Promise<void>`
- Writes an array of `SmartFile` instances to the file system.
### `interpreter` Module
The `interpreter` module provides utility functions for interpreting file types and contents. Some notable functions include:
- `filetype(pathArg: string): string`
- Determines the file type based on the file path.
- `objectFile(fileStringArg: string, fileTypeArg: any): any`
- Converts a file string to an object based on the file type.
2024-04-02 18:53:02 +00:00
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2020-02-07 21:00:54 +00:00
2024-04-02 18:53:02 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2020-02-07 21:00:54 +00:00
2024-04-03 13:39:26 +00:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.