Skip to content
Snippets Groups Projects
Commit 0c9528be authored by michael.minelli's avatar michael.minelli
Browse files

Toolbox => Add some files functions

parent fc0c547b
No related branches found
No related tags found
No related merge requests found
import fs from 'fs/promises';
import path from 'path';
class Toolbox { class Toolbox {
public urlToPath(url: string): string { public urlToPath(url: string): string {
return url.replace(/^([a-z]{3,5}:\/{2})?[a-z.@]+(:[0-9]{1,5})?.(.*)/, '$3').replace('.git', ''); return url.replace(/^([a-z]{3,5}:\/{2})?[a-z.@]+(:[0-9]{1,5})?.(.*)/, '$3').replace('.git', '');
} }
/*
Source of getAllFiles and getTotalSize (modified for this project): https://coderrocketfuel.com/article/get-the-total-size-of-all-files-in-a-directory-using-node-js
*/
private async getAllFiles(dirPath: string, arrayOfFiles: Array<string> = []): Promise<Array<string>> {
let files = await fs.readdir(dirPath);
await Promise.all(files.map(async file => {
if ( (await fs.stat(dirPath + '/' + file)).isDirectory() ) {
arrayOfFiles = await this.getAllFiles(dirPath + '/' + file, arrayOfFiles);
} else {
arrayOfFiles.push(path.join(dirPath, file));
}
}));
return arrayOfFiles;
};
private async getTotalSize(directoryPath: string): Promise<number> {
const arrayOfFiles = await this.getAllFiles(directoryPath);
let totalSize = 0;
for ( const filePath of arrayOfFiles ) {
totalSize += (await fs.stat(filePath)).size;
}
return totalSize;
};
get fs() {
return {
getAllFiles : this.getAllFiles.bind(this),
getTotalSize: this.getTotalSize.bind(this)
};
}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment