Files
smartfile/ts/classes.virtualdirectory.ts

202 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-11-04 20:07:43 +01:00
import { SmartFile } from './classes.smartfile.js';
2024-04-02 20:53:02 +02:00
import * as plugins from './plugins.js';
2023-11-04 20:07:43 +01:00
export interface IVirtualDirectoryConstructorOptions {
mode: '';
2023-11-04 20:07:43 +01:00
}
2020-10-02 13:26:53 +00:00
/**
* a virtual directory exposes a fs api
* Use SmartFileFactory to create instances of this class
2020-10-02 13:26:53 +00:00
*/
export class VirtualDirectory {
2020-10-05 16:20:57 +00:00
// STATIC
public static async fromFsDirPath(
pathArg: string,
smartFs?: any,
factory?: any
): Promise<VirtualDirectory> {
if (!smartFs || !factory) {
throw new Error('No SmartFs/Factory instance available. Create VirtualDirectory through SmartFileFactory.');
}
const newVirtualDir = new VirtualDirectory(smartFs, factory);
// Use smartFs to list directory and factory to create SmartFiles
const entries = await smartFs.directory(pathArg).recursive().list();
const smartfiles = await Promise.all(
entries
.filter((entry: any) => entry.isFile)
.map((entry: any) => factory.fromFilePath(entry.path, pathArg))
);
newVirtualDir.addSmartfiles(smartfiles);
2020-10-02 14:34:09 +00:00
return newVirtualDir;
2020-10-02 13:29:40 +00:00
}
2020-10-02 13:26:53 +00:00
2020-10-05 16:20:57 +00:00
public static async fromVirtualDirTransferableObject(
virtualDirTransferableObjectArg: plugins.smartfileInterfaces.VirtualDirTransferableObject,
smartFs?: any,
factory?: any
2020-10-05 16:20:57 +00:00
): Promise<VirtualDirectory> {
const newVirtualDir = new VirtualDirectory(smartFs, factory);
2020-10-05 16:20:57 +00:00
for (const fileArg of virtualDirTransferableObjectArg.files) {
const smartFile = SmartFile.enfoldFromJson(fileArg) as SmartFile;
// Update smartFs reference if available
if (smartFs) {
(smartFile as any).smartFs = smartFs;
}
newVirtualDir.addSmartfiles([smartFile]);
2020-10-05 16:20:57 +00:00
}
return newVirtualDir;
}
public static fromFileArray(files: SmartFile[], smartFs?: any, factory?: any): VirtualDirectory {
const vdir = new VirtualDirectory(smartFs, factory);
vdir.addSmartfiles(files);
return vdir;
}
public static empty(smartFs?: any, factory?: any): VirtualDirectory {
return new VirtualDirectory(smartFs, factory);
}
2020-10-05 16:20:57 +00:00
// INSTANCE
2023-11-04 20:07:43 +01:00
public smartfileArray: SmartFile[] = [];
private smartFs?: any;
private factory?: any;
2020-10-05 16:20:57 +00:00
constructor(smartFs?: any, factory?: any) {
this.smartFs = smartFs;
this.factory = factory;
}
// ============================================
// Collection Mutations
// ============================================
2020-10-02 13:26:53 +00:00
2023-11-04 20:07:43 +01:00
public addSmartfiles(smartfileArrayArg: SmartFile[]) {
2020-10-11 15:34:24 +00:00
this.smartfileArray = this.smartfileArray.concat(smartfileArrayArg);
2020-10-02 13:26:53 +00:00
}
public addSmartfile(smartfileArg: SmartFile): void {
this.smartfileArray.push(smartfileArg);
}
public removeByPath(pathArg: string): boolean {
const initialLength = this.smartfileArray.length;
this.smartfileArray = this.smartfileArray.filter(f => f.path !== pathArg);
return this.smartfileArray.length < initialLength;
}
public clear(): void {
this.smartfileArray = [];
}
public merge(otherVDir: VirtualDirectory): void {
this.addSmartfiles(otherVDir.smartfileArray);
}
// ============================================
// Collection Queries
// ============================================
public exists(pathArg: string): boolean {
return this.smartfileArray.some(f => f.path === pathArg);
}
public has(pathArg: string): boolean {
return this.exists(pathArg);
}
public async getFileByPath(pathArg: string): Promise<SmartFile | undefined> {
return this.smartfileArray.find(f => f.path === pathArg);
}
public listFiles(): SmartFile[] {
return [...this.smartfileArray];
}
public listDirectories(): string[] {
const dirs = new Set<string>();
for (const file of this.smartfileArray) {
const dir = plugins.path.dirname(file.path);
if (dir !== '.') {
dirs.add(dir);
2020-10-02 13:26:53 +00:00
}
}
return Array.from(dirs).sort();
}
public filter(predicate: (file: SmartFile) => boolean): VirtualDirectory {
const newVDir = new VirtualDirectory(this.smartFs, this.factory);
newVDir.addSmartfiles(this.smartfileArray.filter(predicate));
return newVDir;
}
public map(fn: (file: SmartFile) => SmartFile): VirtualDirectory {
const newVDir = new VirtualDirectory(this.smartFs, this.factory);
newVDir.addSmartfiles(this.smartfileArray.map(fn));
return newVDir;
}
public find(predicate: (file: SmartFile) => boolean): SmartFile | undefined {
return this.smartfileArray.find(predicate);
}
public size(): number {
return this.smartfileArray.length;
}
public isEmpty(): boolean {
return this.smartfileArray.length === 0;
2020-10-02 13:26:53 +00:00
}
2020-10-05 16:20:57 +00:00
public async toVirtualDirTransferableObject(): Promise<plugins.smartfileInterfaces.VirtualDirTransferableObject> {
return {
files: this.smartfileArray.map((smartfileArg) =>
smartfileArg.foldToJson(),
),
2020-10-05 16:20:57 +00:00
};
}
2020-10-11 15:34:24 +00:00
public async saveToDisk(dirArg: string) {
2023-07-08 16:24:53 +02:00
console.log(`writing VirtualDirectory with ${this.smartfileArray.length} files to directory:
2020-10-11 15:34:24 +00:00
--> ${dirArg}`);
for (const smartfileArg of this.smartfileArray) {
const filePath = await smartfileArg.writeToDir(dirArg);
console.log(`wrote ${smartfileArg.relative} to
--> ${filePath}`);
2020-10-09 15:15:47 +00:00
}
}
2023-07-08 16:24:53 +02:00
public async shiftToSubdirectory(subDir: string): Promise<VirtualDirectory> {
const newVirtualDir = new VirtualDirectory(this.smartFs, this.factory);
2023-07-08 16:24:53 +02:00
for (const file of this.smartfileArray) {
if (file.path.startsWith(subDir)) {
const adjustedFilePath = plugins.path.relative(subDir, file.path);
file.path = adjustedFilePath;
newVirtualDir.addSmartfiles([file]);
}
}
return newVirtualDir;
}
public async loadFromDisk(dirArg: string): Promise<void> {
// Load from disk, replacing current collection
this.clear();
const loaded = await VirtualDirectory.fromFsDirPath(dirArg, this.smartFs, this.factory);
this.addSmartfiles(loaded.smartfileArray);
}
public async addVirtualDirectory(
virtualDir: VirtualDirectory,
newRoot: string,
): Promise<void> {
2023-07-08 16:24:53 +02:00
for (const file of virtualDir.smartfileArray) {
file.path = plugins.path.join(newRoot, file.path);
}
this.addSmartfiles(virtualDir.smartfileArray);
}
2020-10-02 13:29:40 +00:00
}