UNPKG

@guoyunhe/downloader

Version:

Download large files with minimum RAM usage. Support tar.gz and zip extraction.

31 lines (30 loc) 1.04 kB
import { mkdir, readdir, rename, rm } from 'fs/promises'; import { join } from 'path'; export async function stripDirectory(dir, strip = 1) { if (strip < 1) return; const items = await scanDirectory(dir, strip + 1); const tmpDir = dir + '__old__'; await rename(dir, tmpDir); await mkdir(dir); await Promise.all(Object.entries(items).map(async ([from, to]) => { rename(join(tmpDir, from), join(dir, to)); })); await rm(tmpDir, { recursive: true }); } async function scanDirectory(dir, depth = 1, prefix = '') { const result = {}; if (depth < 1) return result; const items = await readdir(dir, { withFileTypes: true }); for (let i = 0; i < items.length; i++) { const item = items[i]; if (item.isDirectory() && depth > 1) { Object.assign(result, await scanDirectory(join(dir, item.name), depth - 1, join(prefix, item.name))); } else { result[join(prefix, item.name)] = item.name; } } return result; }