fix(core): update

This commit is contained in:
2024-04-02 20:53:02 +02:00
parent 1311039127
commit d08cc0f350
12 changed files with 188 additions and 92 deletions

View File

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

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 memory from './memory.js';

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

View File

@ -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';

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 { 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;
};

View File

@ -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;

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 fsStreamMod from './fsstream.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 => {
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 * as smartfileFs from './fs.js';
import * as interpreter from './interpreter.js';