Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fs/copy-file #2201

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 218 additions & 0 deletions lib/node_modules/@stdlib/fs/copy-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# Copy File

> Copy data from one file to another.

<section class="usage">

## Usage

```javascript
var copyFile = require( '@stdlib/fs/copy-file' );
```

#### copyFile( src, dest\[, mode], clbk )

Asynchronously copies data from `src` file to `dest` file.

```javascript
var join = require( 'path' ).join;

var src = join( __dirname, 'examples', 'fixtures', 'src.txt' );
var dest = join( __dirname, 'examples', 'fixtures', 'dest.txt' );

copyFile( src, dest, onCopy );

function onCopy( error ) {
if ( error ) {
console.log( error instanceof Error );
// => false
}
}
```

The function accepts the same `mode` and has the same defaults as [`fs.copyFile()`][node-fs].

#### copyFile.sync( src, dest\[, mode] )

Synchronously copies data from `src` file to `dest` file.

```javascript
var join = require( 'path' ).join;

var src = join( __dirname, 'examples', 'fixtures', 'src.txt' );
var dest = join( __dirname, 'examples', 'fixtures', 'dest.txt' );

var err = copyFile.sync( src, dest );
if ( err instanceof Error ) {
throw err;
}
```

The function accepts the same `mode` and has the same defaults as [`fs.copyFileSync()`][node-fs].

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The difference between this `copyFile.sync` and [`fs.copyFileSync()`][node-fs] is that [`fs.copyFileSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a non-existent directory path) and this API will return an `error`. Hence, the following anti-pattern

<!-- eslint-disable node/no-unsupported-features/node-builtins, node/no-sync -->

```javascript
var fs = require( 'fs' );

// Check for src file and directory path existence to prevent an error being thrown...
if ( fs.existsSync( '/path/from/src.txt' ) && fs.existsSync( '/path/to' ) ) {
fs.copyFileSync( '/path/from/src.txt', '/path/to/dest.txt' );
}
```

can be replaced by an approach which addresses existence via `error` handling.

<!-- eslint-disable node/no-sync -->

```javascript
var copyFile = require( '@stdlib/fs/copy-file' );

// Explicitly handle the error...
var err = copyFile.sync( '/path/from/src.txt', 'beep boop' );
if ( err instanceof Error ) {
// You choose what to do...
throw err;
}
```

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var constants = require( 'constants' );
var join = require( 'path' ).join;
var copyFile = require( '@stdlib/fs/copy-file' );

var src = join( __dirname, 'examples', 'fixtures', 'src.txt' );
var dest = join( __dirname, 'examples', 'fixtures', 'dest.txt' );

// Synchronously copies data from one file to another file:
var err = copyFile.sync( src, dest, constants.COPYFILE_EXCL );
// Function successfully executes and returns null

console.log( err instanceof Error );
// => false

// Asynchronously copies data from one file to another file:
copyFile( src, dest, onCopy );

function onCopy( error ) {
if ( error ) {
console.error( 'Error: %s', error.message );
}
console.log( 'Success!!!' );
}
```

</section>

<!-- /.examples -->

* * *

<section class="cli">

## CLI

<section class="usage">

### Usage

```text
Usage: copy-file [mode] <source> <destination>

Options:

-h, --help Print this message.
-V, --version Print the package version.
-m --mode mode Mode. Default: 0.
-s --source Source file path.
-d --destination Destination file path.
```

</section>

<!-- /.usage -->

<section class="notes">

### Notes

- Relative output file paths are resolved relative to the current working directory.
- Errors are written to `stderr`.

</section>

<!-- /.notes -->

<section class="examples">

### Examples

```bash
$ copy-file ./examples/fixtures/src.txt ./examples/fixtures/dest.txt
```

</section>

<!-- /.examples -->

</section>

<!-- /.cli -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[node-fs]: https://nodejs.org/api/fs.html

</section>

<!-- /.links -->
80 changes: 80 additions & 0 deletions lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var join = require( 'path' ).join;
var bench = require( '@stdlib/bench' );
var pkg = require( './../package.json' ).name;
var copyFile = require( './../lib' );


// FIXTURES //

var SRC = join( __dirname, 'fixtures', 'src.txt' );
var DEST = join( __dirname, 'fixtures', 'dest.txt' );


// MAIN //

bench( pkg, function benchmark( b ) {
var i;

i = 0;
b.tic();

return next();

function next() {
i += 1;
if ( i <= b.iterations ) {
return copyFile( SRC, DEST, done );
}
b.toc();
b.pass( 'benchmark finished' );
b.end();
}

function done( error ) {
if ( error ) {
b.fail( error.message );
}
next();
}
});

bench( pkg + ':sync', function benchmark( b ) {
var out;
var i;

b.tic();
for (i = 0; i < b.iterations; i++) {
out = copyFile.sync( SRC, DEST );
if ( out instanceof Error ) {
b.fail( out.message );
}
}
b.toc();
if ( out instanceof Error ) {
b.fail( out.message );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
beep boop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
beep boop
Loading
Loading