Skip to content

Commit

Permalink
doc: use shorter fs.copyFile examples
Browse files Browse the repository at this point in the history
This consolidates some examples to concentrate the reader on the
important aspects and to reduce reading overhead.

PR-URL: #27044
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
BridgeAR committed Jan 12, 2020
1 parent a13500f commit 4455f60
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1588,19 +1588,15 @@ then the operation will fail.

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;

// destination.txt will be created or overwritten by default.
fs.copyFile('source.txt', 'destination.txt', (err) => {
function callback(err) {
if (err) throw err;
console.log('source.txt was copied to destination.txt');
});
```

If the third argument is a number, then it specifies `mode`:
}

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;
// destination.txt will be created or overwritten by default.
fs.copyFile('source.txt', 'destination.txt', callback);

// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fs.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL, callback);
Expand Down Expand Up @@ -1636,17 +1632,11 @@ then the operation will fail.

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;

// destination.txt will be created or overwritten by default.
fs.copyFileSync('source.txt', 'destination.txt');
console.log('source.txt was copied to destination.txt');
```

If the third argument is a number, then it specifies `mode`:

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;

// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);
Expand Down Expand Up @@ -4718,20 +4708,17 @@ create a copy-on-write reflink. If the platform does not support copy-on-write,
then the operation will fail.

```js
const fsPromises = require('fs').promises;
const {
promises: fsPromises,
constants: {
COPYFILE_EXCL
}
} = require('fs');

// destination.txt will be created or overwritten by default.
fsPromises.copyFile('source.txt', 'destination.txt')
.then(() => console.log('source.txt was copied to destination.txt'))
.catch(() => console.log('The file could not be copied'));
```

If the third argument is a number, then it specifies `mode`:

```js
const fs = require('fs');
const fsPromises = fs.promises;
const { COPYFILE_EXCL } = fs.constants;

// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)
Expand Down

0 comments on commit 4455f60

Please sign in to comment.