BREAKING CHANGE(SmartFileFactory): Refactor to in-memory file API and introduce SmartFileFactory; delegate filesystem operations to @push.rocks/smartfs; bump to 12.0.0

This commit is contained in:
2025-11-22 13:18:32 +00:00
parent 16d47ea348
commit ad33cb6d73
24 changed files with 1552 additions and 1789 deletions

View File

@@ -1,6 +1,4 @@
import * as plugins from './plugins.js';
import * as fs from './fs.js';
import * as memory from './memory.js';
export interface ISmartfileConstructorOptions {
path: string;
@@ -10,103 +8,17 @@ export interface ISmartfileConstructorOptions {
/**
* an vinyl file compatible in memory file class
* Use SmartFileFactory to create instances of this class
*/
export class SmartFile extends plugins.smartjson.Smartjson {
// ======
// STATIC
// ======
/**
* creates a Smartfile from a filePath
* @param filePath
*/
public static async fromFilePath(
filePath: string,
baseArg: string = process.cwd(),
) {
filePath = plugins.path.resolve(filePath);
const fileBuffer = fs.toBufferSync(filePath);
const smartfile = new SmartFile({
contentBuffer: fileBuffer,
base: baseArg,
path: plugins.path.relative(baseArg, filePath),
});
return smartfile;
}
public static async fromBuffer(
filePath: string,
contentBufferArg: Buffer,
baseArg: string = process.cwd(),
) {
const smartfile = new SmartFile({
contentBuffer: contentBufferArg,
base: baseArg,
path: plugins.path.relative(baseArg, filePath),
});
return smartfile;
}
public static async fromString(
filePath: string,
contentStringArg: string,
encodingArg: 'utf8' | 'binary',
baseArg = process.cwd(),
) {
const smartfile = new SmartFile({
contentBuffer: Buffer.from(contentStringArg, encodingArg),
base: baseArg,
path: plugins.path.relative(baseArg, filePath),
});
return smartfile;
}
public static async fromFoldedJson(foldedJsonArg: string) {
return new SmartFile(plugins.smartjson.parse(foldedJsonArg));
}
/**
* creates a Smartfile from a ReadableStream
* @param stream a readable stream that provides file content
* @param filePath the file path to associate with the content
* @param baseArg the base path to use for the file
*/
public static async fromStream(
stream: plugins.stream.Readable,
filePath: string,
baseArg: string = process.cwd(),
): 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({
contentBuffer: contentBuffer,
base: baseArg,
path: plugins.path.relative(baseArg, filePath),
});
resolve(smartfile);
});
});
}
public static async fromUrl(urlArg: string) {
const response = await plugins.smartrequest.SmartRequest.create()
.url(urlArg)
.accept('binary')
.get();
const buffer = Buffer.from(await response.arrayBuffer());
const smartfile = await SmartFile.fromBuffer(urlArg, buffer);
return smartfile;
}
// ========
// INSTANCE
// ========
/**
* Reference to the SmartFs instance for filesystem operations
*/
private smartFs?: any;
/**
* the relative path of the file
*/
@@ -149,9 +61,10 @@ export class SmartFile extends plugins.smartjson.Smartjson {
/**
* the constructor of Smartfile
* @param optionsArg
* @param smartFs optional SmartFs instance for filesystem operations
*/
constructor(optionsArg: ISmartfileConstructorOptions) {
constructor(optionsArg: ISmartfileConstructorOptions, smartFs?: any) {
super();
if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer;
@@ -160,6 +73,7 @@ export class SmartFile extends plugins.smartjson.Smartjson {
}
this.path = optionsArg.path;
this.base = optionsArg.base;
this.smartFs = smartFs;
}
/**
@@ -177,14 +91,19 @@ export class SmartFile extends plugins.smartjson.Smartjson {
* write file to disk at its original location
* Behaviours:
* - no argument write to exactly where the file was picked up
* - Requires SmartFs instance (create via SmartFileFactory)
*/
public async write() {
let writePath = plugins.smartpath.transform.makeAbsolute(
if (!this.smartFs) {
throw new Error('No SmartFs instance available. Create SmartFile through SmartFileFactory.');
}
const writePath = plugins.smartpath.transform.makeAbsolute(
this.path,
this.base,
);
console.log(`writing to ${writePath}`);
await memory.toFs(this.contentBuffer, writePath);
await this.smartFs.file(writePath).write(this.contentBuffer);
}
/**
@@ -193,10 +112,15 @@ export class SmartFile extends plugins.smartjson.Smartjson {
* @param filePathArg
*/
public async writeToDiskAtPath(filePathArg: string) {
if (!this.smartFs) {
throw new Error('No SmartFs instance available. Create SmartFile through SmartFileFactory.');
}
if (!plugins.path.isAbsolute(filePathArg)) {
filePathArg = plugins.path.join(process.cwd(), filePathArg);
}
await memory.toFs(this.contentBuffer, filePathArg);
await this.smartFs.file(filePathArg).write(this.contentBuffer);
}
/**
@@ -205,9 +129,13 @@ export class SmartFile extends plugins.smartjson.Smartjson {
* @returns
*/
public async writeToDir(dirPathArg: string) {
if (!this.smartFs) {
throw new Error('No SmartFs instance available. Create SmartFile through SmartFileFactory.');
}
dirPathArg = plugins.smartpath.transform.toAbsolute(dirPathArg) as string;
const filePath = plugins.path.join(dirPathArg, this.path);
await memory.toFs(this.contentBuffer, filePath);
await this.smartFs.file(filePath).write(this.contentBuffer);
return filePath;
}
@@ -215,16 +143,25 @@ export class SmartFile extends plugins.smartjson.Smartjson {
* read file from disk
*/
public async read() {
this.contentBuffer = await fs.toBuffer(
plugins.path.join(this.base, this.path),
);
if (!this.smartFs) {
throw new Error('No SmartFs instance available. Create SmartFile through SmartFileFactory.');
}
const filePath = plugins.path.join(this.base, this.path);
const content = await this.smartFs.file(filePath).read();
this.contentBuffer = Buffer.from(content);
}
/**
* deletes the file from disk at its original location
*/
public async delete() {
await fs.remove(plugins.path.join(this.base, this.path));
if (!this.smartFs) {
throw new Error('No SmartFs instance available. Create SmartFile through SmartFileFactory.');
}
const filePath = plugins.path.join(this.base, this.path);
await this.smartFs.file(filePath).delete();
}
/**
@@ -380,4 +317,18 @@ export class SmartFile extends plugins.smartjson.Smartjson {
public async getSize(): Promise<number> {
return this.contentBuffer.length;
}
/**
* Parse content as string with specified encoding
*/
public parseContentAsString(encodingArg: BufferEncoding = 'utf8'): string {
return this.contentBuffer.toString(encodingArg);
}
/**
* Parse content as buffer
*/
public parseContentAsBuffer(): Buffer {
return this.contentBuffer;
}
}