Skip to content

Commit 1b5e072

Browse files
committedFeb 24, 2020
Rename some ProgressData properties
1 parent 6d92d5c commit 1b5e072

File tree

6 files changed

+46
-45
lines changed

6 files changed

+46
-45
lines changed
 

‎index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ declare namespace cpFile {
1212
/**
1313
Absolute path to source.
1414
*/
15-
src: string;
15+
sourcePath: string;
1616

1717
/**
1818
Absolute path to destination.
1919
*/
20-
dest: string;
20+
destinationPath: string;
2121

2222
/**
2323
File size in bytes.
@@ -27,7 +27,7 @@ declare namespace cpFile {
2727
/**
2828
Copied size in bytes.
2929
*/
30-
written: number;
30+
writtenBytes: number;
3131

3232
/**
3333
Copied percentage, a value between `0` and `1`.

‎index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
1616
const writeStream = fs.createWriteStream(destination, {flags: options.overwrite ? 'w' : 'wx'});
1717

1818
readStream.on('data', () => {
19-
progressEmitter.written = writeStream.bytesWritten;
19+
progressEmitter.writtenBytes = writeStream.bytesWritten;
2020
});
2121

2222
readStream.once('error', error => {
@@ -30,7 +30,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
3030
const writePromise = pEvent(writeStream, 'close');
3131
readStream.pipe(writeStream);
3232
await writePromise;
33-
progressEmitter.written = progressEmitter.size;
33+
progressEmitter.writtenBytes = progressEmitter.size;
3434
shouldUpdateStats = true;
3535
} catch (error) {
3636
throw new CpFileError(`Cannot write to \`${destination}\`: ${error.message}`, error);
@@ -51,8 +51,8 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
5151
}
5252
};
5353

54-
const cpFile = (source, destination, options) => {
55-
if (!source || !destination) {
54+
const cpFile = (sourcePath, destinationPath, options) => {
55+
if (!sourcePath || !destinationPath) {
5656
return Promise.reject(new CpFileError('`source` and `destination` required'));
5757
}
5858

@@ -61,8 +61,9 @@ const cpFile = (source, destination, options) => {
6161
...options
6262
};
6363

64-
const progressEmitter = new ProgressEmitter(path.resolve(source), path.resolve(destination));
65-
const promise = cpFileAsync(source, destination, options, progressEmitter);
64+
const progressEmitter = new ProgressEmitter(path.resolve(sourcePath), path.resolve(destinationPath));
65+
const promise = cpFileAsync(sourcePath, destinationPath, options, progressEmitter);
66+
6667
promise.on = (...arguments_) => {
6768
progressEmitter.on(...arguments_);
6869
return promise;

‎index.test-d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ expectType<Promise<void>>(
1414
data => {
1515
expectType<ProgressData>(data);
1616

17-
expectType<string>(data.src);
18-
expectType<string>(data.dest);
17+
expectType<string>(data.sourcePath);
18+
expectType<string>(data.destinationPath);
1919
expectType<number>(data.size);
20-
expectType<number>(data.written);
20+
expectType<number>(data.writtenBytes);
2121
expectType<number>(data.percent);
2222
}
2323
)

‎progress-emitter.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
'use strict';
22
const EventEmitter = require('events');
33

4-
const written = new WeakMap();
4+
const writtenBytes = new WeakMap();
55

66
class ProgressEmitter extends EventEmitter {
7-
constructor(source, destination) {
7+
constructor(sourcePath, destinationPath) {
88
super();
9-
this._source = source;
10-
this._destination = destination;
9+
this._sourcePath = sourcePath;
10+
this._destinationPath = destinationPath;
1111
}
1212

13-
get written() {
14-
return written.get(this);
13+
get writtenBytes() {
14+
return writtenBytes.get(this);
1515
}
1616

17-
set written(value) {
18-
written.set(this, value);
17+
set writtenBytes(value) {
18+
writtenBytes.set(this, value);
1919
this.emitProgress();
2020
}
2121

2222
emitProgress() {
23-
const {size, written} = this;
23+
const {size, writtenBytes} = this;
2424

2525
this.emit('progress', {
26-
src: this._source,
27-
dest: this._destination,
26+
sourcePath: this._sourcePath,
27+
destinationPath: this._destinationPath,
2828
size,
29-
written,
30-
percent: written === size ? 1 : written / size
29+
writtenBytes,
30+
percent: writtenBytes === size ? 1 : writtenBytes / size
3131
});
3232
}
3333
}

‎readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ Type: `Function`
7070

7171
```js
7272
{
73-
src: string,
74-
dest: string,
73+
sourcePath: string,
74+
destinationPath: string,
7575
size: number,
76-
written: number,
76+
writtenBytes: number,
7777
percent: number
7878
}
7979
```
8080

81-
- `src` and `dest` are absolute paths.
82-
- `size` and `written` are in bytes.
81+
- `source` and `destination` are absolute paths.
82+
- `size` and `writtenBytes` are in bytes.
8383
- `percent` is a value between `0` and `1`.
8484

8585
###### Notes

‎test/progress.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,46 @@ test.afterEach.always(t => {
2323
});
2424

2525
test('report progress', async t => {
26-
const buf = crypto.randomBytes(THREE_HUNDRED_KILO);
27-
fs.writeFileSync(t.context.source, buf);
26+
const buffer = crypto.randomBytes(THREE_HUNDRED_KILO);
27+
fs.writeFileSync(t.context.source, buffer);
2828

29-
let calls = 0;
29+
let callCount = 0;
3030
await cpFile(t.context.source, t.context.destination).on('progress', progress => {
31-
calls++;
32-
t.is(typeof progress.src, 'string');
33-
t.is(typeof progress.dest, 'string');
31+
callCount++;
32+
t.is(typeof progress.sourcePath, 'string');
33+
t.is(typeof progress.destinationPath, 'string');
3434
t.is(typeof progress.size, 'number');
35-
t.is(typeof progress.written, 'number');
35+
t.is(typeof progress.writtenBytes, 'number');
3636
t.is(typeof progress.percent, 'number');
3737
t.is(progress.size, THREE_HUNDRED_KILO);
3838
});
3939

40-
t.true(calls > 0);
40+
t.true(callCount > 0);
4141
});
4242

4343
test('report progress of 100% on end', async t => {
44-
const buf = crypto.randomBytes(THREE_HUNDRED_KILO);
45-
fs.writeFileSync(t.context.source, buf);
44+
const buffer = crypto.randomBytes(THREE_HUNDRED_KILO);
45+
fs.writeFileSync(t.context.source, buffer);
4646

4747
let lastEvent;
4848
await cpFile(t.context.source, t.context.destination).on('progress', progress => {
4949
lastEvent = progress;
5050
});
5151

5252
t.is(lastEvent.percent, 1);
53-
t.is(lastEvent.written, THREE_HUNDRED_KILO);
53+
t.is(lastEvent.writtenBytes, THREE_HUNDRED_KILO);
5454
});
5555

5656
test('report progress for empty files once', async t => {
5757
fs.writeFileSync(t.context.source, '');
5858

59-
let calls = 0;
59+
let callCount = 0;
6060
await cpFile(t.context.source, t.context.destination).on('progress', progress => {
61-
calls++;
61+
callCount++;
6262
t.is(progress.size, 0);
63-
t.is(progress.written, 0);
63+
t.is(progress.writtenBytes, 0);
6464
t.is(progress.percent, 1);
6565
});
6666

67-
t.is(calls, 1);
67+
t.is(callCount, 1);
6868
});

0 commit comments

Comments
 (0)
Please sign in to comment.