Skip to content

Commit

Permalink
Add file sourcePath and destinationPath to the progress event (#112)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
sinchang and sindresorhus committed May 10, 2023
1 parent bb7f7c4 commit 936710e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
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

0 comments on commit 936710e

Please sign in to comment.