Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
493a235065 | |||
e4e48dc9db | |||
7de647daa1 | |||
34bc92137b | |||
954df5a0ee | |||
b9c15b11ad | |||
55e6e81957 | |||
f37956270c | |||
0c985b9c00 | |||
85ff487809 | |||
eb65c4e859 | |||
a1d6c37f18 | |||
915ad00801 | |||
910671bfc6 | |||
ae8835d430 | |||
d08cc0f350 | |||
1311039127 | |||
c267d2f226 |
@ -10,9 +10,33 @@
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartfile",
|
||||
"description": "smart ways to work with files in nodejs",
|
||||
"description": "provides a robust suite of tools for managing files in Node.js using TypeScript.",
|
||||
"npmPackagename": "@push.rocks/smartfile",
|
||||
"license": "MIT"
|
||||
}
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"files management",
|
||||
"TypeScript",
|
||||
"Node.js",
|
||||
"read files",
|
||||
"write files",
|
||||
"copy files",
|
||||
"file streaming",
|
||||
"directories manipulation",
|
||||
"virtual file system",
|
||||
"filesystem utilities",
|
||||
"ESM syntax",
|
||||
"memory-efficient streaming"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tsdoc": {
|
||||
"classes": [
|
||||
"SmartFile",
|
||||
"StreamFile"
|
||||
],
|
||||
"descriptions": [
|
||||
"the purpose of the StreamFile class is to provide a hybrid interface between streaming files and simple handling when writing and reading those files multiple times."
|
||||
],
|
||||
"legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
|
||||
}
|
||||
}
|
38
package.json
38
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@push.rocks/smartfile",
|
||||
"private": false,
|
||||
"version": "11.0.5",
|
||||
"description": "offers smart ways to work with files in nodejs",
|
||||
"version": "11.0.13",
|
||||
"description": "provides a robust suite of tools for managing files in Node.js using TypeScript.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
@ -16,8 +16,18 @@
|
||||
"url": "git+https://gitlab.com/push.rocks/smartfile.git"
|
||||
},
|
||||
"keywords": [
|
||||
"filesystem",
|
||||
"json"
|
||||
"files management",
|
||||
"TypeScript",
|
||||
"Node.js",
|
||||
"read files",
|
||||
"write files",
|
||||
"copy files",
|
||||
"file streaming",
|
||||
"directories manipulation",
|
||||
"virtual file system",
|
||||
"filesystem utilities",
|
||||
"ESM syntax",
|
||||
"memory-efficient streaming"
|
||||
],
|
||||
"author": "Lossless GmbH <hello@lossless.com> (https://lossless.com)",
|
||||
"license": "MIT",
|
||||
@ -26,29 +36,29 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/push.rocks/smartfile#readme",
|
||||
"dependencies": {
|
||||
"@push.rocks/lik": "^6.0.12",
|
||||
"@push.rocks/lik": "^6.0.14",
|
||||
"@push.rocks/smartdelay": "^3.0.5",
|
||||
"@push.rocks/smartfile-interfaces": "^1.0.7",
|
||||
"@push.rocks/smarthash": "^3.0.4",
|
||||
"@push.rocks/smartjson": "^5.0.10",
|
||||
"@push.rocks/smartjson": "^5.0.16",
|
||||
"@push.rocks/smartmime": "^1.0.5",
|
||||
"@push.rocks/smartpath": "^5.0.11",
|
||||
"@push.rocks/smartpath": "^5.0.13",
|
||||
"@push.rocks/smartpromise": "^4.0.2",
|
||||
"@push.rocks/smartrequest": "^2.0.21",
|
||||
"@push.rocks/smartstream": "^3.0.30",
|
||||
"@push.rocks/smartstream": "^3.0.34",
|
||||
"@types/fs-extra": "^11.0.4",
|
||||
"@types/glob": "^8.1.0",
|
||||
"@types/js-yaml": "^4.0.9",
|
||||
"fs-extra": "^11.1.1",
|
||||
"glob": "^10.3.10",
|
||||
"fs-extra": "^11.2.0",
|
||||
"glob": "^10.3.12",
|
||||
"js-yaml": "^4.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.1.70",
|
||||
"@git.zone/tsbuild": "^2.1.72",
|
||||
"@git.zone/tsrun": "^1.2.46",
|
||||
"@git.zone/tstest": "^1.0.84",
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.10.0"
|
||||
"@git.zone/tstest": "^1.0.88",
|
||||
"@push.rocks/tapbundle": "^5.0.22",
|
||||
"@types/node": "^20.12.7"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
|
2297
pnpm-lock.yaml
generated
2297
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
325
readme.md
325
readme.md
@ -1,126 +1,279 @@
|
||||
# SmartFile
|
||||
> SmartFile offers smart ways to work with files in nodejs.
|
||||
# @push.rocks/smartfile
|
||||
|
||||
> Provides a robust suite of tools for managing files in Node.js using TypeScript.
|
||||
|
||||
## Install
|
||||
To install SmartFile, use npm or Yarn as follows:
|
||||
|
||||
```
|
||||
npm install @push.rocks/smartfile --save
|
||||
```
|
||||
Or:
|
||||
```
|
||||
yarn add @push.rocks/smartfile
|
||||
To integrate `@push.rocks/smartfile` into your project, run:
|
||||
|
||||
```bash
|
||||
npm install @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.
|
||||
`@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.
|
||||
|
||||
### Basic File Operations
|
||||
### Key Features and Classes
|
||||
|
||||
For reading and writing files, SmartFile provides synchronous and asynchronous methods. Here’s how you can use them:
|
||||
- **`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.
|
||||
|
||||
#### Async Write to File
|
||||
### Getting Started with ESM and TypeScript
|
||||
|
||||
First, ensure your project supports ESM syntax and TypeScript. Then, begin by importing the desired features from `@push.rocks/smartfile`:
|
||||
|
||||
```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');
|
||||
import { SmartFile, StreamFile, VirtualDirectory, memory, fs as smartFs } from '@push.rocks/smartfile';
|
||||
```
|
||||
|
||||
#### Sync Write to File
|
||||
### Reading and Writing Files
|
||||
|
||||
#### Reading Files
|
||||
|
||||
Reading a JSON 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');
|
||||
const myJsonFile: SmartFile = await SmartFile.fromFilePath('./data.json');
|
||||
const jsonData = JSON.parse(myJsonFile.contents.toString());
|
||||
console.log(jsonData);
|
||||
```
|
||||
|
||||
### Working with Streams
|
||||
#### Writing Files
|
||||
|
||||
Streaming files to and from the filesystem is made easy with SmartFile. Here’s an example:
|
||||
|
||||
#### Creating Read and Write Streams
|
||||
Writing content to a file:
|
||||
|
||||
```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);
|
||||
const filePath: string = './output.txt';
|
||||
const content: string = 'Hello, SmartFile!';
|
||||
await memory.toFs(content, filePath);
|
||||
console.log('File saved successfully.');
|
||||
```
|
||||
|
||||
### Dealing with Virtual Directories
|
||||
### Streaming Large Files
|
||||
|
||||
Virtual directories allow you to group and manipulate files as if they were in a filesystem structure without actually writing them to disk.
|
||||
For large files, `StreamFile` provides a memory-efficient streaming solution:
|
||||
|
||||
```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');
|
||||
})();
|
||||
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.');
|
||||
```
|
||||
|
||||
### Advanced File Manipulation
|
||||
### Working with Virtual Directories
|
||||
|
||||
SmartFile also allows for more advanced file manipulation techniques through the `SmartFile` class.
|
||||
`VirtualDirectory` abstracts a collection of files allowing operations to be performed as if they were on disk:
|
||||
|
||||
```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();
|
||||
})();
|
||||
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.');
|
||||
```
|
||||
|
||||
### Conversion and Interpretation
|
||||
### Advanced File Operations
|
||||
|
||||
You can easily convert file contents to objects or interpret file types for further processing:
|
||||
`@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
|
||||
|
||||
```typescript
|
||||
import { memory } from '@push.rocks/smartfile';
|
||||
### Web File Handling
|
||||
|
||||
(async () => {
|
||||
const fileString: string = await fs.promises.readFile('./data/example.json', 'utf8');
|
||||
const fileObject = memory.toObject(fileString, 'json');
|
||||
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.
|
||||
|
||||
console.log(fileObject);
|
||||
// Proceed with the object...
|
||||
})();
|
||||
```
|
||||
### Comprehensive File Management
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
## Information on Licensing
|
||||
## API Reference
|
||||
|
||||
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 don’t hold you liable.
|
||||
### `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.
|
||||
|
||||
## 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
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
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.
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartfile',
|
||||
version: '11.0.5',
|
||||
description: 'offers smart ways to work with files in nodejs'
|
||||
version: '11.0.13',
|
||||
description: 'provides a robust suite of tools for managing files in Node.js using TypeScript.'
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import * as fs from './fs.js';
|
||||
import * as memory from './memory.js';
|
||||
|
||||
@ -169,7 +169,9 @@ export class SmartFile extends plugins.smartjson.Smartjson {
|
||||
* - no argument write to exactly where the file was picked up
|
||||
*/
|
||||
public async write() {
|
||||
await memory.toFs(this.contentBuffer, plugins.path.join(this.base, this.path));
|
||||
let writePath = plugins.smartpath.transform.makeAbsolute(this.path, this.base);
|
||||
console.log(`writing to ${writePath}`);
|
||||
await memory.toFs(this.contentBuffer, writePath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import * as smartfileFs from './fs.js';
|
||||
import * as smartfileFsStream from './fsstream.js';
|
||||
import { Readable } from 'stream';
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { SmartFile } from './classes.smartfile.js';
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import * as fs from './fs.js';
|
||||
|
||||
|
||||
|
47
ts/fs.ts
47
ts/fs.ts
@ -1,9 +1,10 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import * as interpreter from './interpreter.js';
|
||||
|
||||
import { SmartFile } from './classes.smartfile.js';
|
||||
|
||||
import * as memory from './memory.js';
|
||||
import type { StreamFile } from './classes.streamfile.js';
|
||||
/*===============================================================
|
||||
============================ Checks =============================
|
||||
===============================================================*/
|
||||
@ -439,6 +440,50 @@ export const waitForFileToBeReady = (filePathArg: string): Promise<void> => {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* writes string or Smartfile to disk.
|
||||
* @param fileArg
|
||||
* @param fileNameArg
|
||||
* @param fileBaseArg
|
||||
*/
|
||||
export let toFs = async (
|
||||
filePathArg: string,
|
||||
fileContentArg: string | Buffer | SmartFile | StreamFile,
|
||||
optionsArg: {
|
||||
respectRelative?: boolean;
|
||||
} = {}
|
||||
) => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
|
||||
// check args
|
||||
if (!fileContentArg || !filePathArg) {
|
||||
throw new Error('expected valid arguments');
|
||||
}
|
||||
|
||||
// prepare actual write action
|
||||
let fileContent: string | Buffer;
|
||||
let fileEncoding: 'utf8' | 'binary' = 'utf8';
|
||||
let filePath: string = filePathArg;
|
||||
|
||||
// handle Smartfile
|
||||
if (fileContentArg instanceof SmartFile) {
|
||||
fileContent = fileContentArg.contentBuffer;
|
||||
// handle options
|
||||
if (optionsArg.respectRelative) {
|
||||
filePath = plugins.path.join(filePath, fileContentArg.path);
|
||||
}
|
||||
} else if (Buffer.isBuffer(fileContentArg)) {
|
||||
fileContent = fileContentArg;
|
||||
fileEncoding = 'binary';
|
||||
} else if (typeof fileContentArg === 'string') {
|
||||
fileContent = fileContentArg;
|
||||
} else {
|
||||
throw new Error('fileContent is neither string nor Smartfile');
|
||||
}
|
||||
await ensureDir(plugins.path.parse(filePath).dir);
|
||||
plugins.fsExtra.writeFile(filePath, fileContent, { encoding: fileEncoding }, done.resolve);
|
||||
return await done.promise;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
/*
|
||||
This file contains logic for streaming things from and to the filesystem
|
||||
*/
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export const createReadStream = (pathArg: string) => {
|
||||
return plugins.fs.createReadStream(pathArg);
|
||||
@ -97,7 +97,7 @@ export const waitForFileToBeReadyForStreaming = (filePathArg: string): Promise<v
|
||||
});
|
||||
};
|
||||
|
||||
class SmartReadStream extends plugins.stream.Readable {
|
||||
export class SmartReadStream extends plugins.stream.Readable {
|
||||
private watcher: plugins.fs.FSWatcher | null = null;
|
||||
private lastReadSize: number = 0;
|
||||
private endTimeout: NodeJS.Timeout | null = null;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import * as fsMod from './fs.js';
|
||||
import * as fsStreamMod from './fsstream.js';
|
||||
import * as interpreterMod from './interpreter.js';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export let filetype = (pathArg: string): string => {
|
||||
const extName = plugins.path.extname(pathArg);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import { SmartFile } from './classes.smartfile.js';
|
||||
import * as smartfileFs from './fs.js';
|
||||
import * as interpreter from './interpreter.js';
|
||||
|
@ -3,9 +3,12 @@
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
}
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user