Skip to content

Commit

Permalink
Require Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 24, 2020
1 parent 4668c5a commit df050ac
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -6,6 +6,5 @@ language: node_js
node_js:
- '12'
- '10'
- '8'
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
28 changes: 15 additions & 13 deletions index.js
Expand Up @@ -11,24 +11,26 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
const stat = await fs.stat(source);
progressEmitter.size = stat.size;

const read = await fs.createReadStream(source);
const readStream = await fs.createReadStream(source);
await fs.makeDir(path.dirname(destination));
const write = fs.createWriteStream(destination, {flags: options.overwrite ? 'w' : 'wx'});
read.on('data', () => {
progressEmitter.written = write.bytesWritten;
const writeStream = fs.createWriteStream(destination, {flags: options.overwrite ? 'w' : 'wx'});

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

readStream.once('error', error => {
readError = new CpFileError(`Cannot read from \`${source}\`: ${error.message}`, error);
write.end();
writeStream.end();
});

let updateStats = false;
let shouldUpdateStats = false;
try {
const writePromise = pEvent(write, 'close');
read.pipe(write);
const writePromise = pEvent(writeStream, 'close');
readStream.pipe(writeStream);
await writePromise;
progressEmitter.written = progressEmitter.size;
updateStats = true;
shouldUpdateStats = true;
} catch (error) {
throw new CpFileError(`Cannot write to \`${destination}\`: ${error.message}`, error);
}
Expand All @@ -37,7 +39,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
throw readError;
}

if (updateStats) {
if (shouldUpdateStats) {
const stats = await fs.lstat(source);

return Promise.all([
Expand All @@ -60,8 +62,8 @@ const cpFile = (source, destination, options) => {

const progressEmitter = new ProgressEmitter(path.resolve(source), path.resolve(destination));
const promise = cpFileAsync(source, destination, options, progressEmitter);
promise.on = (...args) => {
progressEmitter.on(...args);
promise.on = (...arguments_) => {
progressEmitter.on(...arguments_);
return promise;
};

Expand Down
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
15 changes: 8 additions & 7 deletions package.json
Expand Up @@ -4,13 +4,14 @@
"description": "Copy a file",
"license": "MIT",
"repository": "sindresorhus/cp-file",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && nyc ava && tsd"
Expand Down Expand Up @@ -47,12 +48,12 @@
"ava": "^2.1.0",
"clear-module": "^3.1.0",
"coveralls": "^3.0.4",
"del": "^4.1.1",
"del": "^5.1.0",
"import-fresh": "^3.0.0",
"nyc": "^14.1.1",
"sinon": "^7.3.1",
"tsd": "^0.7.3",
"nyc": "^15.0.0",
"sinon": "^9.0.0",
"tsd": "^0.11.0",
"uuid": "^3.3.2",
"xo": "^0.24.0"
"xo": "^0.26.1"
}
}
9 changes: 5 additions & 4 deletions progress-emitter.js
Expand Up @@ -10,17 +10,18 @@ class ProgressEmitter extends EventEmitter {
this._destination = destination;
}

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

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

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

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

this.emit('progress', {
src: this._source,
dest: this._destination,
Expand Down
7 changes: 1 addition & 6 deletions readme.md
Expand Up @@ -2,7 +2,6 @@

> Copy a file

## Highlights

- Fast by using streams in the async version and [`fs.copyFileSync()`](https://nodejs.org/api/fs.html#fs_fs_copyfilesync_src_dest_flags) in the synchronous version.
Expand All @@ -11,14 +10,12 @@
- Can be safe by turning off [overwriting](#optionsoverwrite).
- User-friendly errors.


## Install

```
$ npm install cp-file
```


## Usage

```js
Expand All @@ -30,7 +27,6 @@ const cpFile = require('cp-file');
})();
```


## API

### cpFile(source, destination, options?)
Expand All @@ -57,7 +53,7 @@ Type: `object`

##### overwrite

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Overwrite existing destination file.
Expand Down Expand Up @@ -102,7 +98,6 @@ const cpFile = require('cp-file');
})();
```


## Related

- [cpy](https://github.com/sindresorhus/cpy) - Copy files
Expand Down

0 comments on commit df050ac

Please sign in to comment.