Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file sourcePath and destinationPath to the progress #112

Merged
merged 3 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions index.d.ts
Expand Up @@ -125,6 +125,16 @@ export type ProgressData = {
Completed percentage. A value between `0` and `1`.
*/
percent: number;

/**
The absolute source path of the current file being copied.
*/
sourcePath: string;

/**
The absolute destination path of the current file being copied.
*/
destinationPath: string;
};

export type ProgressEmitter = {
Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -248,6 +248,8 @@ export default function cpy(
percent: completedFiles / entries.length,
completedFiles,
completedSize,
sourcePath: event.sourcePath,
destinationPath: event.destinationPath,
});
}
};
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Expand Up @@ -50,5 +50,7 @@ expectType<Promise<string[]>>(
expectType<number>(progress.totalFiles);
expectType<number>(progress.completedSize);
expectType<number>(progress.percent);
expectType<string>(progress.sourcePath);
expectType<string>(progress.destinationPath);
}),
);
6 changes: 5 additions & 1 deletion readme.md
Expand Up @@ -201,12 +201,16 @@ Type: `Function`
completedFiles: number,
totalFiles: number,
completedSize: number,
percent: number
percent: number,
sourcePath: string,
destinationPath: string,
}
```

- `completedSize` is in bytes
- `percent` is a value between `0` and `1`
- `sourcePath` is the absolute source path of the current file being copied.
- `destinationPath` is The absolute destination path of the current file being copied.

Note that the `.on()` method is available only right after the initial `cpy` call, so make sure you add a `handler` before awaiting the promise:

Expand Down
12 changes: 12 additions & 0 deletions test.js
Expand Up @@ -334,6 +334,8 @@ test('junk files are ignored', async t => {
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/foo'));
t.is(read(report.destinationPath), read(t.context.tmp + '/cwd/foo'));
});

test('junk files are copied', async t => {
Expand All @@ -356,6 +358,8 @@ test('junk files are copied', async t => {
t.is(report.completedFiles, 2);
t.is(report.completedSize, 22);
t.is(report.percent, 1);
t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/foo'));
t.is(read(report.destinationPath), read(t.context.tmp + '/cwd/foo'));
});

test('nested junk files are ignored', async t => {
Expand All @@ -378,6 +382,8 @@ test('nested junk files are ignored', async t => {
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/test'));
t.is(read(report.destinationPath), read(t.context.tmp + '/test'));
});

test('reports copy progress of single file', async t => {
Expand All @@ -398,6 +404,8 @@ test('reports copy progress of single file', async t => {
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/foo'));
t.is(read(report.destinationPath), read(t.context.tmp + '/foo'));
});

test('reports copy progress of multiple files', async t => {
Expand All @@ -419,6 +427,8 @@ test('reports copy progress of multiple files', async t => {
t.is(report.completedFiles, 2);
t.is(report.completedSize, 25);
t.is(report.percent, 1);
t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/bar'));
t.is(read(report.destinationPath), read(t.context.tmp + '/bar'));
});

test('reports correct completedSize', async t => {
Expand All @@ -443,6 +453,8 @@ test('reports correct completedSize', async t => {
t.is(report.totalFiles, 1);
t.is(report.completedFiles, 1);
t.is(report.completedSize, ONE_MEGABYTE);
t.is(read(report.sourcePath), read(t.context.tmp, 'cwd/fatfile'));
t.is(read(report.destinationPath), read(t.context.tmp, 'fatfile'));
t.true(chunkCount > 1);
t.is(report.percent, 1);
});
Expand Down