MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / getDirectorySize

Function getDirectorySize

lib/models/files.ts:799–837  ·  view source on GitHub ↗
(path: string)

Source from the content-addressed store, hash-verified

797
798// NOTE: We're using `-h` (human readable) and parsing the output because that's more stable than `-B 1B` across different systems for a reliable byte size.
799async function getDirectorySize(path: string): Promise<number> {
800 try {
801 const controller = new AbortController();
802 const commandTimeout = setTimeout(() => controller.abort(), 5_000);
803
804 const command = new Deno.Command(`du`, {
805 args: [
806 `-sh`,
807 path,
808 ],
809 signal: controller.signal,
810 });
811
812 const { code, stdout, stderr } = await command.output();
813
814 if (commandTimeout) {
815 clearTimeout(commandTimeout);
816 }
817
818 if (code !== 0) {
819 if (stderr) {
820 throw new Error(new TextDecoder().decode(stderr));
821 }
822
823 throw new Error(`Unknown error running "du"`);
824 }
825
826 const output = new TextDecoder().decode(stdout);
827
828 const value = output.split('\t')[0].trim();
829
830 const number = Number.parseFloat(value.match(/\d+(\.\d+)?/)?.[0] || '0');
831 const unit = value.match(/[A-Z]+/)?.[0] || 'B'.toUpperCase();
832
833 return bytesFromHumanFileSize(`${number} ${unit}B`);
834 } catch {
835 return getDirectorySizeFallback(path);
836 }
837}

Callers 2

listMethod · 0.85
searchNamesMethod · 0.85

Calls 2

bytesFromHumanFileSizeFunction · 0.90
getDirectorySizeFallbackFunction · 0.85

Tested by

no test coverage detected