smartfile/ts/smartfile.classes.virtualdirectory.ts

62 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-06-07 13:43:28 +00:00
import { Smartfile } from './smartfile.classes.smartfile.js';
import * as plugins from './smartfile.plugins.js';
import * as fs from './smartfile.fs.js';
2020-10-02 13:26:53 +00:00
/**
* a virtual directory exposes a fs api
*/
export class VirtualDirectory {
2020-10-05 16:20:57 +00:00
// STATIC
public static async fromFsDirPath(pathArg: string): Promise<VirtualDirectory> {
2020-10-02 13:26:53 +00:00
const newVirtualDir = new VirtualDirectory();
newVirtualDir.addSmartfiles(await fs.fileTreeToObject(pathArg, '**/*'));
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
): Promise<VirtualDirectory> {
const newVirtualDir = new VirtualDirectory();
for (const fileArg of virtualDirTransferableObjectArg.files) {
newVirtualDir.addSmartfiles([Smartfile.enfoldFromJson(fileArg) as Smartfile]);
}
return newVirtualDir;
}
// INSTANCE
2020-10-11 15:34:24 +00:00
public smartfileArray: Smartfile[] = [];
2020-10-05 16:20:57 +00:00
2020-10-02 13:26:53 +00:00
constructor() {}
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 async getFileByPath(pathArg: string) {
2020-10-11 15:34:24 +00:00
for (const smartfile of this.smartfileArray) {
2020-10-02 13:26:53 +00:00
if (smartfile.path === pathArg) {
return smartfile;
}
}
}
2020-10-05 16:20:57 +00:00
public async toVirtualDirTransferableObject(): Promise<plugins.smartfileInterfaces.VirtualDirTransferableObject> {
return {
2022-03-11 08:46:54 +00:00
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) {
console.log(`writing VirtualDirectory with ${this.smartfileArray.length} to directory:
--> ${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
}
}
2020-10-02 13:26:53 +00:00
// TODO implement root shifting to get subdirectories as new virtual directories
// TODO implement root shifting to combine VirtualDirecotries in a parent virtual directory
2020-10-02 13:29:40 +00:00
}