Compare commits

...

18 Commits

Author SHA1 Message Date
493a235065 11.0.13 2024-04-12 15:00:56 +02:00
e4e48dc9db fix(core): update 2024-04-12 15:00:55 +02:00
7de647daa1 11.0.12 2024-04-12 14:51:23 +02:00
34bc92137b fix(core): update 2024-04-12 14:51:23 +02:00
954df5a0ee 11.0.11 2024-04-12 14:44:13 +02:00
b9c15b11ad fix(core): update 2024-04-12 14:44:12 +02:00
55e6e81957 11.0.10 2024-04-03 15:39:27 +02:00
f37956270c fix(core): update 2024-04-03 15:39:26 +02:00
0c985b9c00 11.0.9 2024-04-03 15:38:59 +02:00
85ff487809 fix(core): update 2024-04-03 15:38:58 +02:00
eb65c4e859 11.0.8 2024-04-02 21:37:32 +02:00
a1d6c37f18 fix(core): update 2024-04-02 21:37:31 +02:00
915ad00801 11.0.7 2024-04-02 20:58:34 +02:00
910671bfc6 fix(core): update 2024-04-02 20:58:33 +02:00
ae8835d430 11.0.6 2024-04-02 20:53:03 +02:00
d08cc0f350 fix(core): update 2024-04-02 20:53:02 +02:00
1311039127 update npmextra.json: githost 2024-04-01 21:35:01 +02:00
c267d2f226 update npmextra.json: githost 2024-04-01 19:58:14 +02:00
15 changed files with 1040 additions and 1730 deletions

View File

@ -10,9 +10,33 @@
"githost": "code.foss.global", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartfile", "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", "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"
} }
} }

View File

@ -1,8 +1,8 @@
{ {
"name": "@push.rocks/smartfile", "name": "@push.rocks/smartfile",
"private": false, "private": false,
"version": "11.0.5", "version": "11.0.13",
"description": "offers smart ways to work with files in nodejs", "description": "provides a robust suite of tools for managing files in Node.js using TypeScript.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
@ -16,8 +16,18 @@
"url": "git+https://gitlab.com/push.rocks/smartfile.git" "url": "git+https://gitlab.com/push.rocks/smartfile.git"
}, },
"keywords": [ "keywords": [
"filesystem", "files management",
"json" "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)", "author": "Lossless GmbH <hello@lossless.com> (https://lossless.com)",
"license": "MIT", "license": "MIT",
@ -26,29 +36,29 @@
}, },
"homepage": "https://gitlab.com/push.rocks/smartfile#readme", "homepage": "https://gitlab.com/push.rocks/smartfile#readme",
"dependencies": { "dependencies": {
"@push.rocks/lik": "^6.0.12", "@push.rocks/lik": "^6.0.14",
"@push.rocks/smartdelay": "^3.0.5", "@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartfile-interfaces": "^1.0.7", "@push.rocks/smartfile-interfaces": "^1.0.7",
"@push.rocks/smarthash": "^3.0.4", "@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/smartmime": "^1.0.5",
"@push.rocks/smartpath": "^5.0.11", "@push.rocks/smartpath": "^5.0.13",
"@push.rocks/smartpromise": "^4.0.2", "@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartrequest": "^2.0.21", "@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/fs-extra": "^11.0.4",
"@types/glob": "^8.1.0", "@types/glob": "^8.1.0",
"@types/js-yaml": "^4.0.9", "@types/js-yaml": "^4.0.9",
"fs-extra": "^11.1.1", "fs-extra": "^11.2.0",
"glob": "^10.3.10", "glob": "^10.3.12",
"js-yaml": "^4.1.0" "js-yaml": "^4.1.0"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.1.70", "@git.zone/tsbuild": "^2.1.72",
"@git.zone/tsrun": "^1.2.46", "@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.84", "@git.zone/tstest": "^1.0.88",
"@push.rocks/tapbundle": "^5.0.15", "@push.rocks/tapbundle": "^5.0.22",
"@types/node": "^20.10.0" "@types/node": "^20.12.7"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

2297
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

325
readme.md
View File

@ -1,126 +1,279 @@
# SmartFile # @push.rocks/smartfile
> SmartFile offers smart ways to work with files in nodejs.
> Provides a robust suite of tools for managing files in Node.js using TypeScript.
## Install ## Install
To install SmartFile, use npm or Yarn as follows:
``` To integrate `@push.rocks/smartfile` into your project, run:
npm install @push.rocks/smartfile --save
``` ```bash
Or: npm install @push.rocks/smartfile
```
yarn add @push.rocks/smartfile
``` ```
## Usage ## 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. Heres 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 ```typescript
import { memory } from '@push.rocks/smartfile'; import { SmartFile, StreamFile, VirtualDirectory, memory, fs as smartFs } 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 ### Reading and Writing Files
#### Reading Files
Reading a JSON file:
```typescript ```typescript
import { memory } from '@push.rocks/smartfile'; const myJsonFile: SmartFile = await SmartFile.fromFilePath('./data.json');
const jsonData = JSON.parse(myJsonFile.contents.toString());
const myData: string = 'Hello, World!'; console.log(jsonData);
// Writing string data to a file synchronously
memory.toFsSync(myData, './data/helloSync.txt');
``` ```
### Working with Streams #### Writing Files
Streaming files to and from the filesystem is made easy with SmartFile. Heres an example: Writing content to a file:
#### Creating Read and Write Streams
```typescript ```typescript
import { fsStream } from '@push.rocks/smartfile'; const filePath: string = './output.txt';
import * as fs from 'fs'; const content: string = 'Hello, SmartFile!';
await memory.toFs(content, filePath);
// Creating a read stream console.log('File saved successfully.');
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 ### 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 ```typescript
import { VirtualDirectory } from '@push.rocks/smartfile'; import { createReadStream } from 'fs';
const sourceStream = createReadStream('./large-video.mp4');
(async () => { const myStreamFile = await StreamFile.fromStream(sourceStream, 'large-video.mp4');
// Creating a virtual directory from the file system await myStreamFile.writeToDir('./storage');
const virtualDir = await VirtualDirectory.fromFsDirPath('./myDirectory'); console.log('Large file streamed to disk successfully.');
// 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 ### 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 ```typescript
import { SmartFile } from '@push.rocks/smartfile'; const virtualDir = new VirtualDirectory();
virtualDir.addSmartfiles([smartFile1, smartFile2]); // Assuming these are SmartFile instances
(async () => { await virtualDir.saveToDisk('./virtual-output');
// Create a SmartFile instance from a file path console.log('Virtual directory saved to disk.');
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 ### 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 ### Web File Handling
import { memory } from '@push.rocks/smartfile';
(async () => { Handling files from HTTP requests:
const fileString: string = await fs.promises.readFile('./data/example.json', 'utf8'); `@push.rocks/smartfile` offers utilities to work with files from web sources, making it simpler to manage downloads and uploads.
const fileObject = memory.toObject(fileString, 'json');
console.log(fileObject); ### Comprehensive File Management
// 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. 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 dont 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.

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', name: '@push.rocks/smartfile',
version: '11.0.5', version: '11.0.13',
description: 'offers smart ways to work with files in nodejs' description: 'provides a robust suite of tools for managing files in Node.js using TypeScript.'
} }

View File

@ -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 fs from './fs.js';
import * as memory from './memory.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 * - no argument write to exactly where the file was picked up
*/ */
public async write() { 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);
} }
/** /**

View File

@ -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 smartfileFs from './fs.js';
import * as smartfileFsStream from './fsstream.js'; import * as smartfileFsStream from './fsstream.js';
import { Readable } from 'stream'; import { Readable } from 'stream';

View File

@ -1,5 +1,5 @@
import { SmartFile } from './classes.smartfile.js'; 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'; import * as fs from './fs.js';

View File

@ -1,9 +1,10 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import * as interpreter from './interpreter.js'; import * as interpreter from './interpreter.js';
import { SmartFile } from './classes.smartfile.js'; import { SmartFile } from './classes.smartfile.js';
import * as memory from './memory.js'; import * as memory from './memory.js';
import type { StreamFile } from './classes.streamfile.js';
/*=============================================================== /*===============================================================
============================ Checks ============================= ============================ 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;
};

View File

@ -1,7 +1,7 @@
/* /*
This file contains logic for streaming things from and to the filesystem 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) => { export const createReadStream = (pathArg: string) => {
return plugins.fs.createReadStream(pathArg); 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 watcher: plugins.fs.FSWatcher | null = null;
private lastReadSize: number = 0; private lastReadSize: number = 0;
private endTimeout: NodeJS.Timeout | null = null; private endTimeout: NodeJS.Timeout | null = null;

View File

@ -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 fsMod from './fs.js';
import * as fsStreamMod from './fsstream.js'; import * as fsStreamMod from './fsstream.js';
import * as interpreterMod from './interpreter.js'; import * as interpreterMod from './interpreter.js';

View File

@ -1,4 +1,4 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
export let filetype = (pathArg: string): string => { export let filetype = (pathArg: string): string => {
const extName = plugins.path.extname(pathArg); const extName = plugins.path.extname(pathArg);

View File

@ -1,4 +1,4 @@
import * as plugins from './smartfile.plugins.js'; import * as plugins from './plugins.js';
import { SmartFile } from './classes.smartfile.js'; import { SmartFile } from './classes.smartfile.js';
import * as smartfileFs from './fs.js'; import * as smartfileFs from './fs.js';
import * as interpreter from './interpreter.js'; import * as interpreter from './interpreter.js';

View File

@ -3,9 +3,12 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"useDefineForClassFields": false, "useDefineForClassFields": false,
"target": "ES2022", "target": "ES2022",
"module": "ES2022", "module": "NodeNext",
"moduleResolution": "nodenext", "moduleResolution": "NodeNext",
"esModuleInterop": true, "esModuleInterop": true,
"verbatimModuleSyntax": true, "verbatimModuleSyntax": true
} },
"exclude": [
"dist_*/**/*.d.ts"
]
} }