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:
95
test/helpers/mock-smartfs.ts
Normal file
95
test/helpers/mock-smartfs.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Mock SmartFs implementation for testing until @push.rocks/smartfs is available
|
||||
* This wraps fs-extra to provide the SmartFs interface
|
||||
*/
|
||||
import { ensureDir, pathExists, remove, copy } from 'fs-extra';
|
||||
import { promises as fsPromises, createReadStream, createWriteStream } from 'fs';
|
||||
import * as path from 'path';
|
||||
import { Readable, Writable } from 'stream';
|
||||
|
||||
export class MockSmartFs {
|
||||
public file(filePath: string) {
|
||||
return {
|
||||
async read(): Promise<string | Buffer> {
|
||||
return await fsPromises.readFile(filePath);
|
||||
},
|
||||
async write(content: string | Buffer): Promise<void> {
|
||||
await ensureDir(path.dirname(filePath));
|
||||
await fsPromises.writeFile(filePath, content);
|
||||
},
|
||||
async exists(): Promise<boolean> {
|
||||
return await pathExists(filePath);
|
||||
},
|
||||
async delete(): Promise<void> {
|
||||
await remove(filePath);
|
||||
},
|
||||
async stat(): Promise<any> {
|
||||
return await fsPromises.stat(filePath);
|
||||
},
|
||||
async readStream(): Promise<Readable> {
|
||||
return Promise.resolve(createReadStream(filePath));
|
||||
},
|
||||
async writeStream(): Promise<Writable> {
|
||||
await ensureDir(path.dirname(filePath));
|
||||
return Promise.resolve(createWriteStream(filePath));
|
||||
},
|
||||
async copy(dest: string): Promise<void> {
|
||||
await copy(filePath, dest);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public directory(dirPath: string) {
|
||||
return {
|
||||
async list(options?: { recursive?: boolean }): Promise<Array<{ path: string; isFile: boolean; isDirectory: boolean }>> {
|
||||
const entries: Array<{ path: string; isFile: boolean; isDirectory: boolean }> = [];
|
||||
|
||||
if (options?.recursive) {
|
||||
// Recursive listing
|
||||
const walk = async (dir: string) => {
|
||||
const items = await fsPromises.readdir(dir);
|
||||
for (const item of items) {
|
||||
const fullPath = path.join(dir, item);
|
||||
const stats = await fsPromises.stat(fullPath);
|
||||
|
||||
if (stats.isFile()) {
|
||||
entries.push({ path: fullPath, isFile: true, isDirectory: false });
|
||||
} else if (stats.isDirectory()) {
|
||||
entries.push({ path: fullPath, isFile: false, isDirectory: true });
|
||||
await walk(fullPath);
|
||||
}
|
||||
}
|
||||
};
|
||||
await walk(dirPath);
|
||||
} else {
|
||||
// Non-recursive listing
|
||||
const items = await fsPromises.readdir(dirPath);
|
||||
for (const item of items) {
|
||||
const fullPath = path.join(dirPath, item);
|
||||
const stats = await fsPromises.stat(fullPath);
|
||||
entries.push({
|
||||
path: fullPath,
|
||||
isFile: stats.isFile(),
|
||||
isDirectory: stats.isDirectory(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return entries;
|
||||
},
|
||||
async create(options?: { recursive?: boolean }): Promise<void> {
|
||||
if (options?.recursive) {
|
||||
await ensureDir(dirPath);
|
||||
} else {
|
||||
await fsPromises.mkdir(dirPath);
|
||||
}
|
||||
},
|
||||
async exists(): Promise<boolean> {
|
||||
return await pathExists(dirPath);
|
||||
},
|
||||
async delete(): Promise<void> {
|
||||
await remove(dirPath);
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user