Skip to content

Commit

Permalink
fix(cli): show download progress
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Jan 16, 2024
1 parent bd8ee7b commit 6dfd61d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/cli/src/repositories/DatatruckRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,14 @@ export class DatatruckRepository extends RepositoryAbstract<DatatruckRepositoryC
entry,
);
tempEntry = `${tempDir}/${entry}`;
await fs.download(sourceEntry, tempEntry);
await fs.download(sourceEntry, tempEntry, {
onProgress: (stats) => {
progress.updateRelative("Downloading", entry, {
...stats,
format: "size",
});
},
});
}
const stats = tarStats[entry];
if (data.options.verbose)
Expand Down
25 changes: 23 additions & 2 deletions packages/cli/src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { pkg } from "../pkg";
import { formatBytes, parseSize } from "./bytes";
import { AppError } from "./error";
import { progressPercent } from "./math";
import { Progress } from "./progress";
import { Progress, ProgressStats } from "./progress";
import { waitForClose } from "./stream";
import { endsWith } from "./string";
import { mkTmpDir } from "./temp";
import { eachLimit } from "async";
import fastFolderSize from "fast-folder-size";
import FastGlob, { Entry, Options } from "fast-glob";
import { createReadStream, Dirent, ReadStream, Stats } from "fs";
import { createReadStream, Dirent, Stats } from "fs";
import { createWriteStream, WriteStream } from "fs";
import {
cp,
Expand Down Expand Up @@ -471,6 +471,11 @@ type ProgressObject = {
total: number;
current: number;
update: (description: string, path?: string, increment?: boolean) => void;
updateRelative: (
description: string,
path: string,
stats: ProgressStats,
) => void;
};

export function createProgress(options: {
Expand All @@ -480,9 +485,25 @@ export function createProgress(options: {
disposed: false,
total: 0,
current: 0,
updateRelative(description, path, stats) {
if (progress.disposed) return;
options.onProgress({
relative: {
...stats,
description,
payload: path,
},
absolute: {
total: progress.total,
current: progress.current,
percent: progressPercent(progress.total, progress.current),
},
});
},
update: (description, path, increment = true) => {
if (progress.disposed) return;
if (path && increment) progress.current++;
if (path === "." || path === "./") return;
options.onProgress({
relative: {
description,
Expand Down

0 comments on commit 6dfd61d

Please sign in to comment.