All files / src fileSystemFolder.ts

100% Statements 19/19
100% Branches 2/2
100% Functions 7/7
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 441x 1x 1x   1x 1x             1x     11x       290x       11x   11x 289x   10x         97x 375x 375x 375x   375x          
import util from "util";
import fs from "fs";
import path from "path";
 
const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
 
export interface IFileNameFormats {
    absolute: string;
    relative: string;
}
 
export default class FileSystemFolder {
    private name: string;
    constructor(name: string) {
        this.name = name;
    }
 
    public getName(): IFileNameFormats {
        return { relative: this.name, absolute: path.resolve(this.name) };
    }
 
    public async getFileNames(): Promise<IFileNameFormats[]> {
        const fileNames: IFileNameFormats[] = [];
 
        for (const absoluteFileName of await this.getFileNamesRecursively(this.name)) {
            fileNames.push({ absolute: absoluteFileName, relative: absoluteFileName.replace(path.resolve(this.getName().absolute), "").replace(/\\/g, "/") })
        }
        return (fileNames);
    };
 
    private async getFileNamesRecursively(name: string): Promise<string[]> {
 
        const subdirs: string[] = await readdir(name);
        const files = await Promise.all(subdirs.map(async (subdir: string) => {
            const res: string = path.resolve(name, subdir);
            return (await stat(res)).isDirectory() ? this.getFileNamesRecursively(res) : res;
        }));
        return files.reduce((a: any, f: any) => a.concat(f), []);
    }
 
 
}