Skip to content

Commit 936710e

Browse files
sinchangsindresorhus
andauthoredMay 10, 2023
Add file sourcePath and destinationPath to the progress event (#112)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent bb7f7c4 commit 936710e

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed
 

Diff for: ‎index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ export type ProgressData = {
125125
Completed percentage. A value between `0` and `1`.
126126
*/
127127
percent: number;
128+
129+
/**
130+
The absolute source path of the current file being copied.
131+
*/
132+
sourcePath: string;
133+
134+
/**
135+
The absolute destination path of the current file being copied.
136+
*/
137+
destinationPath: string;
128138
};
129139

130140
export type ProgressEmitter = {

Diff for: ‎index.js

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ export default function cpy(
248248
percent: completedFiles / entries.length,
249249
completedFiles,
250250
completedSize,
251+
sourcePath: event.sourcePath,
252+
destinationPath: event.destinationPath,
251253
});
252254
}
253255
};

Diff for: ‎index.test-d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@ expectType<Promise<string[]>>(
5050
expectType<number>(progress.totalFiles);
5151
expectType<number>(progress.completedSize);
5252
expectType<number>(progress.percent);
53+
expectType<string>(progress.sourcePath);
54+
expectType<string>(progress.destinationPath);
5355
}),
5456
);

Diff for: ‎readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ Type: `Function`
201201
completedFiles: number,
202202
totalFiles: number,
203203
completedSize: number,
204-
percent: number
204+
percent: number,
205+
sourcePath: string,
206+
destinationPath: string,
205207
}
206208
```
207209

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

211215
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:
212216

Diff for: ‎test.js

+12
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ test('junk files are ignored', async t => {
334334
t.is(report.completedFiles, 1);
335335
t.is(report.completedSize, 11);
336336
t.is(report.percent, 1);
337+
t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/foo'));
338+
t.is(read(report.destinationPath), read(t.context.tmp + '/cwd/foo'));
337339
});
338340

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

361365
test('nested junk files are ignored', async t => {
@@ -378,6 +382,8 @@ test('nested junk files are ignored', async t => {
378382
t.is(report.completedFiles, 1);
379383
t.is(report.completedSize, 11);
380384
t.is(report.percent, 1);
385+
t.is(read(report.sourcePath), read(t.context.tmp + '/cwd/test'));
386+
t.is(read(report.destinationPath), read(t.context.tmp + '/test'));
381387
});
382388

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

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

424434
test('reports correct completedSize', async t => {
@@ -443,6 +453,8 @@ test('reports correct completedSize', async t => {
443453
t.is(report.totalFiles, 1);
444454
t.is(report.completedFiles, 1);
445455
t.is(report.completedSize, ONE_MEGABYTE);
456+
t.is(read(report.sourcePath), read(t.context.tmp, 'cwd/fatfile'));
457+
t.is(read(report.destinationPath), read(t.context.tmp, 'fatfile'));
446458
t.true(chunkCount > 1);
447459
t.is(report.percent, 1);
448460
});

0 commit comments

Comments
 (0)
Please sign in to comment.