update
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as fs from './smartfile.fs.js';
|
||||
import * as memory from './smartfile.memory.js';
|
||||
import * as fs from './fs.js';
|
||||
import * as memory from './memory.js';
|
||||
|
||||
export interface ISmartfileConstructorOptions {
|
||||
path: string;
|
||||
@ -9,10 +9,9 @@ export interface ISmartfileConstructorOptions {
|
||||
}
|
||||
|
||||
/**
|
||||
* class Smartfile
|
||||
* -> is vinyl file compatible
|
||||
* an vinyl file compatible in memory file class
|
||||
*/
|
||||
export class Smartfile extends plugins.smartjson.Smartjson {
|
||||
export class SmartFile extends plugins.smartjson.Smartjson {
|
||||
// ======
|
||||
// STATIC
|
||||
// ======
|
||||
@ -24,7 +23,7 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
||||
public static async fromFilePath(filePath: string, baseArg: string = process.cwd()) {
|
||||
filePath = plugins.path.resolve(filePath);
|
||||
const fileBuffer = fs.toBufferSync(filePath);
|
||||
const smartfile = new Smartfile({
|
||||
const smartfile = new SmartFile({
|
||||
contentBuffer: fileBuffer,
|
||||
base: baseArg,
|
||||
path: plugins.path.relative(baseArg, filePath),
|
||||
@ -37,7 +36,7 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
||||
contentBufferArg: Buffer,
|
||||
baseArg: string = process.cwd()
|
||||
) {
|
||||
const smartfile = new Smartfile({
|
||||
const smartfile = new SmartFile({
|
||||
contentBuffer: contentBufferArg,
|
||||
base: baseArg,
|
||||
path: plugins.path.relative(baseArg, filePath),
|
||||
@ -52,7 +51,7 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
||||
encodingArg: 'utf8' | 'binary',
|
||||
baseArg = process.cwd()
|
||||
) {
|
||||
const smartfile = new Smartfile({
|
||||
const smartfile = new SmartFile({
|
||||
contentBuffer: Buffer.from(contentStringArg, encodingArg),
|
||||
base: baseArg,
|
||||
path: plugins.path.relative(baseArg, filePath),
|
||||
@ -62,7 +61,7 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
||||
}
|
||||
|
||||
public static async fromFoldedJson(foldedJsonArg: string) {
|
||||
return new Smartfile(plugins.smartjson.parse(foldedJsonArg));
|
||||
return new SmartFile(plugins.smartjson.parse(foldedJsonArg));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -75,14 +74,14 @@ export class Smartfile extends plugins.smartjson.Smartjson {
|
||||
stream: plugins.stream.Readable,
|
||||
filePath: string,
|
||||
baseArg: string = process.cwd()
|
||||
): Promise<Smartfile> {
|
||||
return new Promise<Smartfile>((resolve, reject) => {
|
||||
): Promise<SmartFile> {
|
||||
return new Promise<SmartFile>((resolve, reject) => {
|
||||
const chunks: Buffer[] = [];
|
||||
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
|
||||
stream.on('error', (error) => reject(error));
|
||||
stream.on('end', () => {
|
||||
const contentBuffer = Buffer.concat(chunks);
|
||||
const smartfile = new Smartfile({
|
||||
const smartfile = new SmartFile({
|
||||
contentBuffer: contentBuffer,
|
||||
base: baseArg,
|
||||
path: plugins.path.relative(baseArg, filePath),
|
90
ts/classes.streamfile.ts
Normal file
90
ts/classes.streamfile.ts
Normal file
@ -0,0 +1,90 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as fsStream from './fsstream.js';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
type StreamSource = () => Promise<Readable>;
|
||||
|
||||
/**
|
||||
* The StreamFile class represents a file as a stream.
|
||||
* It allows creating streams from a file path, a URL, or a buffer.
|
||||
*/
|
||||
export class StreamFile {
|
||||
// INSTANCE
|
||||
relativeFilePath?: string;
|
||||
private streamSource: StreamSource;
|
||||
|
||||
private constructor(streamSource: StreamSource, relativeFilePath?: string) {
|
||||
this.streamSource = streamSource;
|
||||
this.relativeFilePath = relativeFilePath;
|
||||
}
|
||||
|
||||
// STATIC
|
||||
|
||||
public static async fromPath(filePath: string): Promise<StreamFile> {
|
||||
const streamSource = () => Promise.resolve(fsStream.createReadStream(filePath));
|
||||
return new StreamFile(streamSource, filePath);
|
||||
}
|
||||
|
||||
public static async fromUrl(url: string): Promise<StreamFile> {
|
||||
const streamSource = async () => plugins.smartrequest.getStream(url); // Replace with actual plugin method
|
||||
return new StreamFile(streamSource);
|
||||
}
|
||||
|
||||
public static fromBuffer(buffer: Buffer, relativeFilePath?: string): StreamFile {
|
||||
const streamSource = () => {
|
||||
const stream = new Readable();
|
||||
stream.push(buffer);
|
||||
stream.push(null); // End of stream
|
||||
return Promise.resolve(stream);
|
||||
};
|
||||
return new StreamFile(streamSource, relativeFilePath);
|
||||
}
|
||||
|
||||
// METHODS
|
||||
|
||||
/**
|
||||
* Creates a new readable stream from the source.
|
||||
*/
|
||||
public async createReadStream(): Promise<Readable> {
|
||||
return this.streamSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the stream to the disk at the specified path.
|
||||
* @param filePathArg The file path where the stream should be written.
|
||||
*/
|
||||
public async writeToDisk(filePathArg: string): Promise<void> {
|
||||
const readStream = await this.createReadStream();
|
||||
const writeStream = fsStream.createWriteStream(filePathArg);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
readStream.pipe(writeStream);
|
||||
readStream.on('error', reject);
|
||||
writeStream.on('error', reject);
|
||||
writeStream.on('finish', resolve);
|
||||
});
|
||||
}
|
||||
|
||||
public async writeToDir(dirPathArg: string) {
|
||||
const filePath = plugins.path.join(dirPathArg, this.relativeFilePath);
|
||||
return this.writeToDisk(filePath);
|
||||
}
|
||||
|
||||
public async getContentAsBuffer() {
|
||||
const done = plugins.smartpromise.defer<Buffer>();
|
||||
const readStream = await this.createReadStream();
|
||||
const chunks: Buffer[] = [];
|
||||
readStream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
|
||||
readStream.on('error', done.reject);
|
||||
readStream.on('end', () => {
|
||||
const contentBuffer = Buffer.concat(chunks);
|
||||
done.resolve(contentBuffer);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
public async getContentAsString(formatArg: 'utf8' | 'binary' = 'utf8') {
|
||||
const contentBuffer = await this.getContentAsBuffer();
|
||||
return contentBuffer.toString(formatArg);
|
||||
}
|
||||
}
|
@ -1,11 +1,21 @@
|
||||
import { Smartfile } from './smartfile.classes.smartfile.js';
|
||||
import { SmartFile } from './classes.smartfile.js';
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as fs from './smartfile.fs.js';
|
||||
import * as fs from './fs.js';
|
||||
|
||||
|
||||
export interface IVirtualDirectoryConstructorOptions {
|
||||
mode: ''
|
||||
}
|
||||
|
||||
/**
|
||||
* a virtual directory exposes a fs api
|
||||
*/
|
||||
export class VirtualDirectory {
|
||||
|
||||
consstructor(options = {}) {
|
||||
|
||||
}
|
||||
|
||||
// STATIC
|
||||
public static async fromFsDirPath(pathArg: string): Promise<VirtualDirectory> {
|
||||
const newVirtualDir = new VirtualDirectory();
|
||||
@ -18,17 +28,17 @@ export class VirtualDirectory {
|
||||
): Promise<VirtualDirectory> {
|
||||
const newVirtualDir = new VirtualDirectory();
|
||||
for (const fileArg of virtualDirTransferableObjectArg.files) {
|
||||
newVirtualDir.addSmartfiles([Smartfile.enfoldFromJson(fileArg) as Smartfile]);
|
||||
newVirtualDir.addSmartfiles([SmartFile.enfoldFromJson(fileArg) as SmartFile]);
|
||||
}
|
||||
return newVirtualDir;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public smartfileArray: Smartfile[] = [];
|
||||
public smartfileArray: SmartFile[] = [];
|
||||
|
||||
constructor() {}
|
||||
|
||||
public addSmartfiles(smartfileArrayArg: Smartfile[]) {
|
||||
public addSmartfiles(smartfileArrayArg: SmartFile[]) {
|
||||
this.smartfileArray = this.smartfileArray.concat(smartfileArrayArg);
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as interpreter from './smartfile.interpreter.js';
|
||||
import * as interpreter from './interpreter.js';
|
||||
|
||||
import { Smartfile } from './smartfile.classes.smartfile.js';
|
||||
import { SmartFile } from './classes.smartfile.js';
|
||||
|
||||
import * as memory from './smartfile.memory.js';
|
||||
import * as memory from './memory.js';
|
||||
/*===============================================================
|
||||
============================ Checks =============================
|
||||
===============================================================*/
|
||||
@ -258,7 +258,7 @@ export const fileTreeToObject = async (dirPathArg: string, miniMatchFilter: stri
|
||||
}
|
||||
|
||||
const fileTree = await listFileTree(dirPath, miniMatchFilter);
|
||||
const smartfileArray: Smartfile[] = [];
|
||||
const smartfileArray: SmartFile[] = [];
|
||||
for (const filePath of fileTree) {
|
||||
const readPath = ((): string => {
|
||||
if (!plugins.path.isAbsolute(filePath)) {
|
||||
@ -271,7 +271,7 @@ export const fileTreeToObject = async (dirPathArg: string, miniMatchFilter: stri
|
||||
|
||||
// push a read file as Smartfile
|
||||
smartfileArray.push(
|
||||
new Smartfile({
|
||||
new SmartFile({
|
||||
contentBuffer: fileBuffer,
|
||||
base: dirPath,
|
||||
path: filePath,
|
12
ts/index.ts
12
ts/index.ts
@ -1,11 +1,11 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import * as fsMod from './smartfile.fs.js';
|
||||
import * as fsStreamMod from './smartfile.fsstream.js';
|
||||
import * as interpreterMod from './smartfile.interpreter.js';
|
||||
import * as memoryMod from './smartfile.memory.js';
|
||||
import * as fsMod from './fs.js';
|
||||
import * as fsStreamMod from './fsstream.js';
|
||||
import * as interpreterMod from './interpreter.js';
|
||||
import * as memoryMod from './memory.js';
|
||||
|
||||
export * from './smartfile.classes.smartfile.js';
|
||||
export * from './smartfile.classes.virtualdirectory.js';
|
||||
export * from './classes.smartfile.js';
|
||||
export * from './classes.virtualdirectory.js';
|
||||
|
||||
export const fs = fsMod;
|
||||
export const fsStream = fsStreamMod;
|
||||
|
@ -1,7 +1,8 @@
|
||||
import * as plugins from './smartfile.plugins.js';
|
||||
import { Smartfile } from './smartfile.classes.smartfile.js';
|
||||
import * as smartfileFs from './smartfile.fs.js';
|
||||
import * as interpreter from './smartfile.interpreter.js';
|
||||
import { SmartFile } from './classes.smartfile.js';
|
||||
import * as smartfileFs from './fs.js';
|
||||
import * as interpreter from './interpreter.js';
|
||||
import type { StreamFile } from './classes.streamfile.js';
|
||||
|
||||
/**
|
||||
* converts file to Object
|
||||
@ -24,7 +25,7 @@ export interface IToFsOptions {
|
||||
* @param fileBaseArg
|
||||
*/
|
||||
export let toFs = async (
|
||||
fileContentArg: string | Buffer | Smartfile,
|
||||
fileContentArg: string | Buffer | SmartFile | StreamFile,
|
||||
filePathArg: string,
|
||||
optionsArg: IToFsOptions = {}
|
||||
) => {
|
||||
@ -41,7 +42,7 @@ export let toFs = async (
|
||||
let filePath: string = filePathArg;
|
||||
|
||||
// handle Smartfile
|
||||
if (fileContentArg instanceof Smartfile) {
|
||||
if (fileContentArg instanceof SmartFile) {
|
||||
fileContent = fileContentArg.contentBuffer;
|
||||
// handle options
|
||||
if (optionsArg.respectRelative) {
|
||||
@ -83,7 +84,7 @@ export const toFsSync = (fileArg: string, filePathArg: string) => {
|
||||
plugins.fsExtra.writeFileSync(filePath, fileString, { encoding: 'utf8' });
|
||||
};
|
||||
|
||||
export let smartfileArrayToFs = async (smartfileArrayArg: Smartfile[], dirArg: string) => {
|
||||
export let smartfileArrayToFs = async (smartfileArrayArg: SmartFile[], dirArg: string) => {
|
||||
await smartfileFs.ensureDir(dirArg);
|
||||
for (const smartfile of smartfileArrayArg) {
|
||||
await toFs(smartfile, dirArg, {
|
Reference in New Issue
Block a user