Compare commits

..

7 Commits

Author SHA1 Message Date
5a9e1b5798 11.0.5 2024-04-01 17:46:40 +02:00
b1ec86ee2d fix(core): update 2024-04-01 17:46:40 +02:00
a1353170f6 update npmextra.json: githost 2024-03-30 21:47:12 +01:00
3ff6de201d 11.0.4 2023-11-24 19:29:00 +01:00
f5c106b2ca fix(core): update 2023-11-24 19:28:59 +01:00
d3c26d0d46 11.0.3 2023-11-24 19:15:41 +01:00
9935fe2d3c fix(core): update 2023-11-24 19:15:41 +01:00
7 changed files with 605 additions and 364 deletions

View File

@ -7,7 +7,7 @@
"gitzone": { "gitzone": {
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "gitlab.com", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartfile", "gitrepo": "smartfile",
"description": "smart ways to work with files in nodejs", "description": "smart ways to work with files in nodejs",

View File

@ -1,7 +1,7 @@
{ {
"name": "@push.rocks/smartfile", "name": "@push.rocks/smartfile",
"private": false, "private": false,
"version": "11.0.2", "version": "11.0.5",
"description": "offers smart ways to work with files in nodejs", "description": "offers smart ways to work with files in nodejs",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
@ -26,7 +26,7 @@
}, },
"homepage": "https://gitlab.com/push.rocks/smartfile#readme", "homepage": "https://gitlab.com/push.rocks/smartfile#readme",
"dependencies": { "dependencies": {
"@push.rocks/lik": "^6.0.5", "@push.rocks/lik": "^6.0.12",
"@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",
@ -34,11 +34,11 @@
"@push.rocks/smartmime": "^1.0.5", "@push.rocks/smartmime": "^1.0.5",
"@push.rocks/smartpath": "^5.0.11", "@push.rocks/smartpath": "^5.0.11",
"@push.rocks/smartpromise": "^4.0.2", "@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartrequest": "^2.0.20", "@push.rocks/smartrequest": "^2.0.21",
"@push.rocks/smartstream": "^3.0.7", "@push.rocks/smartstream": "^3.0.30",
"@types/fs-extra": "^11.0.3", "@types/fs-extra": "^11.0.4",
"@types/glob": "^8.1.0", "@types/glob": "^8.1.0",
"@types/js-yaml": "^4.0.8", "@types/js-yaml": "^4.0.9",
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"glob": "^10.3.10", "glob": "^10.3.10",
"js-yaml": "^4.1.0" "js-yaml": "^4.1.0"
@ -46,9 +46,9 @@
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.1.70", "@git.zone/tsbuild": "^2.1.70",
"@git.zone/tsrun": "^1.2.46", "@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.81", "@git.zone/tstest": "^1.0.84",
"@push.rocks/tapbundle": "^5.0.15", "@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.8.10" "@types/node": "^20.10.0"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

736
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

151
readme.md
View File

@ -1,49 +1,126 @@
# @push.rocks/smartfile # SmartFile
smart ways to work with files in nodejs > SmartFile offers smart ways to work with files in nodejs.
## Availabililty and Links ## Install
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartfile) To install SmartFile, use npm or Yarn as follows:
* [gitlab.com (source)](https://gitlab.com/push.rocks/smartfile)
* [github.com (source mirror)](https://github.com/push.rocks/smartfile)
* [docs (typedoc)](https://push.rocks.gitlab.io/smartfile/)
## Status for master ```
npm install @push.rocks/smartfile --save
Status Category | Status Badge ```
-- | -- Or:
GitLab Pipelines | [![pipeline status](https://gitlab.com/push.rocks/smartfile/badges/master/pipeline.svg)](https://lossless.cloud) ```
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/push.rocks/smartfile/badges/master/coverage.svg)](https://lossless.cloud) yarn add @push.rocks/smartfile
npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartfile)](https://lossless.cloud) ```
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/push.rocks/smartfile)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartfile)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartfile)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartfile)](https://lossless.cloud)
## Usage ## Usage
smartfile is an approach of being one tool to handle files in diverse environments. 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.
### Smartfile Sections ### Basic File Operations
smartfile thinks in sections: For reading and writing files, SmartFile provides synchronous and asynchronous methods. Heres how you can use them:
| section | description | #### Async Write to File
| ----------- | ---------------------------------------------------------------------------- |
| fs | (object) gets data from fs to somewhere |
| memory | gets data from memory to somewhere |
| remote | gets data from remote locations to somewhere |
| interpreter | (object) handles yaml and json |
| smartfile | (class) a virtual representation of a file, alternative to vinyl file format |
## Contribution ```typescript
import { memory } from '@push.rocks/smartfile';
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) const myData: string = 'Hello, SmartFile!';
For further information read the linked docs at the top of this readme. // Writing string data to a file asynchronously
memory.toFs(myData, './data/hello.txt');
```
## Legal #### Sync Write to File
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) ```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.

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', name: '@push.rocks/smartfile',
version: '11.0.2', version: '11.0.5',
description: 'offers smart ways to work with files in nodejs' description: 'offers smart ways to work with files in nodejs'
} }

View File

@ -91,6 +91,12 @@ export class SmartFile extends plugins.smartjson.Smartjson {
}); });
} }
public static async fromUrl (urlArg: string) {
const response = await plugins.smartrequest.getBinary(urlArg);
const smartfile = await SmartFile.fromBuffer(urlArg, response.body);
return smartfile;
}
// ======== // ========
// INSTANCE // INSTANCE
// ======== // ========

View File

@ -392,44 +392,50 @@ export const listFileTree = async (
* Watches for file stability before resolving the promise. * Watches for file stability before resolving the promise.
*/ */
export const waitForFileToBeReady = (filePathArg: string): Promise<void> => { export const waitForFileToBeReady = (filePathArg: string): Promise<void> => {
return new Promise((resolve, reject) => { return new Promise(async (resolve, reject) => {
let lastSize = -1; let lastFileSize = -1;
let stableCheckTimeout: NodeJS.Timeout | null = null; let fileIsStable = false;
const clearStableCheckTimeout = () => { const checkFileStability = async () => {
if (stableCheckTimeout) { let currentFileSize: number;
clearTimeout(stableCheckTimeout); const deferred = plugins.smartpromise.defer();
stableCheckTimeout = null; plugins.fs.stat(filePathArg, (err, stats) => {
if (err) {
fileIsStable = true;
watcher.close();
reject(err);
return;
} }
currentFileSize = stats.size;
deferred.resolve();
});
await deferred.promise;
if (currentFileSize === lastFileSize) {
fileIsStable = true;
await plugins.smartdelay.delayFor(100);
resolve();
}
lastFileSize = currentFileSize;
}; };
const watcher = plugins.fs.watch(filePathArg, (eventType, filename) => { const watcher = plugins.fs.watch(filePathArg, (eventType, filename) => {
if (eventType === 'change') { if (eventType === 'change') {
plugins.fs.stat(filePathArg, (err, stats) => { checkFileStability();
if (err) {
watcher.close();
clearStableCheckTimeout();
reject(err);
return;
}
if (stats.size === lastSize) {
clearStableCheckTimeout();
stableCheckTimeout = setTimeout(() => {
watcher.close();
resolve();
}, 5000); // stability duration
} else {
lastSize = stats.size;
}
});
} }
}); });
watcher.on('error', (error) => { watcher.on('error', (error) => {
clearStableCheckTimeout();
watcher.close(); watcher.close();
reject(error); reject(error);
}); });
while (!fileIsStable) {
await checkFileStability();
if (!fileIsStable) {
await plugins.smartdelay.delayFor(5000);
}
}
watcher.close();
}); });
}; };