Files
smartfile/ts/classes.virtualdirectory.ts

93 lines
2.7 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
import * as fs from './fs.js';
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
*/
export class VirtualDirectory {
consstructor(options = {}) {}
2023-11-04 20:07:43 +01:00
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,
2020-10-05 16:20:57 +00:00
): Promise<VirtualDirectory> {
const newVirtualDir = new VirtualDirectory();
for (const fileArg of virtualDirTransferableObjectArg.files) {
newVirtualDir.addSmartfiles([
SmartFile.enfoldFromJson(fileArg) as SmartFile,
]);
2020-10-05 16:20:57 +00:00
}
return newVirtualDir;
}
// INSTANCE
2023-11-04 20:07:43 +01:00
public smartfileArray: SmartFile[] = [];
2020-10-05 16:20:57 +00:00
2020-10-02 13:26:53 +00:00
constructor() {}
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 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 {
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();
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 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
}