Skip to content

Commit

Permalink
Rename some ProgressData properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 24, 2020
1 parent 6d92d5c commit 1b5e072
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 45 deletions.
6 changes: 3 additions & 3 deletions index.d.ts
Expand Up @@ -12,12 +12,12 @@ declare namespace cpFile {
/**
Absolute path to source.
*/
src: string;
sourcePath: string;

/**
Absolute path to destination.
*/
dest: string;
destinationPath: string;

/**
File size in bytes.
Expand All @@ -27,7 +27,7 @@ declare namespace cpFile {
/**
Copied size in bytes.
*/
written: number;
writtenBytes: number;

/**
Copied percentage, a value between `0` and `1`.
Expand Down
13 changes: 7 additions & 6 deletions index.js
Expand Up @@ -16,7 +16,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
const writeStream = fs.createWriteStream(destination, {flags: options.overwrite ? 'w' : 'wx'});

readStream.on('data', () => {
progressEmitter.written = writeStream.bytesWritten;
progressEmitter.writtenBytes = writeStream.bytesWritten;
});

readStream.once('error', error => {
Expand All @@ -30,7 +30,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
const writePromise = pEvent(writeStream, 'close');
readStream.pipe(writeStream);
await writePromise;
progressEmitter.written = progressEmitter.size;
progressEmitter.writtenBytes = progressEmitter.size;
shouldUpdateStats = true;
} catch (error) {
throw new CpFileError(`Cannot write to \`${destination}\`: ${error.message}`, error);
Expand All @@ -51,8 +51,8 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
}
};

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

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

const progressEmitter = new ProgressEmitter(path.resolve(source), path.resolve(destination));
const promise = cpFileAsync(source, destination, options, progressEmitter);
const progressEmitter = new ProgressEmitter(path.resolve(sourcePath), path.resolve(destinationPath));
const promise = cpFileAsync(sourcePath, destinationPath, options, progressEmitter);

promise.on = (...arguments_) => {
progressEmitter.on(...arguments_);
return promise;
Expand Down
6 changes: 3 additions & 3 deletions index.test-d.ts
Expand Up @@ -14,10 +14,10 @@ expectType<Promise<void>>(
data => {
expectType<ProgressData>(data);

expectType<string>(data.src);
expectType<string>(data.dest);
expectType<string>(data.sourcePath);
expectType<string>(data.destinationPath);
expectType<number>(data.size);
expectType<number>(data.written);
expectType<number>(data.writtenBytes);
expectType<number>(data.percent);
}
)
Expand Down
26 changes: 13 additions & 13 deletions progress-emitter.js
@@ -1,33 +1,33 @@
'use strict';
const EventEmitter = require('events');

const written = new WeakMap();
const writtenBytes = new WeakMap();

class ProgressEmitter extends EventEmitter {
constructor(source, destination) {
constructor(sourcePath, destinationPath) {
super();
this._source = source;
this._destination = destination;
this._sourcePath = sourcePath;
this._destinationPath = destinationPath;
}

get written() {
return written.get(this);
get writtenBytes() {
return writtenBytes.get(this);
}

set written(value) {
written.set(this, value);
set writtenBytes(value) {
writtenBytes.set(this, value);
this.emitProgress();
}

emitProgress() {
const {size, written} = this;
const {size, writtenBytes} = this;

this.emit('progress', {
src: this._source,
dest: this._destination,
sourcePath: this._sourcePath,
destinationPath: this._destinationPath,
size,
written,
percent: written === size ? 1 : written / size
writtenBytes,
percent: writtenBytes === size ? 1 : writtenBytes / size
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Expand Up @@ -70,16 +70,16 @@ Type: `Function`

```js
{
src: string,
dest: string,
sourcePath: string,
destinationPath: string,
size: number,
written: number,
writtenBytes: number,
percent: number
}
```

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

###### Notes
Expand Down
30 changes: 15 additions & 15 deletions test/progress.js
Expand Up @@ -23,46 +23,46 @@ test.afterEach.always(t => {
});

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

let calls = 0;
let callCount = 0;
await cpFile(t.context.source, t.context.destination).on('progress', progress => {
calls++;
t.is(typeof progress.src, 'string');
t.is(typeof progress.dest, 'string');
callCount++;
t.is(typeof progress.sourcePath, 'string');
t.is(typeof progress.destinationPath, 'string');
t.is(typeof progress.size, 'number');
t.is(typeof progress.written, 'number');
t.is(typeof progress.writtenBytes, 'number');
t.is(typeof progress.percent, 'number');
t.is(progress.size, THREE_HUNDRED_KILO);
});

t.true(calls > 0);
t.true(callCount > 0);
});

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

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

t.is(lastEvent.percent, 1);
t.is(lastEvent.written, THREE_HUNDRED_KILO);
t.is(lastEvent.writtenBytes, THREE_HUNDRED_KILO);
});

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

let calls = 0;
let callCount = 0;
await cpFile(t.context.source, t.context.destination).on('progress', progress => {
calls++;
callCount++;
t.is(progress.size, 0);
t.is(progress.written, 0);
t.is(progress.writtenBytes, 0);
t.is(progress.percent, 1);
});

t.is(calls, 1);
t.is(callCount, 1);
});

0 comments on commit 1b5e072

Please sign in to comment.