diff --git a/lib/node_modules/@stdlib/fs/copy-file/README.md b/lib/node_modules/@stdlib/fs/copy-file/README.md new file mode 100644 index 000000000000..f18681278f86 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/README.md @@ -0,0 +1,218 @@ + + +# Copy File + +> Copy data from one file to another. + +
+ +## 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 ); + // => true + } +} +``` + +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]. + +
+ + + +
+ +## 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 + + + + ```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. + + + + ```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; + } + ``` + +
+ + + +
+ +## Examples + + + +```javascript +var constants = require( 'fs' ).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 ); +if ( err instanceof Error ) { + console.error( 'Error: %s', err.message ); +} + +// Asynchronously copies data from one file to another file: +copyFile( src, dest, onCopy ); + +function onCopy( error ) { + if ( error ) { + console.error( 'Error: %s', error.message ); + } else { + console.log( 'Success!!!' ); + } +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: copy-file [mode] + +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. +``` + +
+ + + +
+ +### Notes + +- Relative output file paths are resolved relative to the current working directory. +- Errors are written to `stderr`. + +
+ + + +
+ +### Examples + +```bash +$ copy-file ./examples/fixtures/src.txt ./examples/fixtures/dest.txt +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js b/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js new file mode 100644 index 000000000000..822728ed54aa --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js @@ -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(); +}); diff --git a/lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/dest.txt b/lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/dest.txt new file mode 100644 index 000000000000..e92ac7a4a98c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/dest.txt @@ -0,0 +1 @@ +beep boop \ No newline at end of file diff --git a/lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/src.txt b/lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/src.txt new file mode 100644 index 000000000000..e92ac7a4a98c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/src.txt @@ -0,0 +1 @@ +beep boop \ No newline at end of file diff --git a/lib/node_modules/@stdlib/fs/copy-file/bin/cli b/lib/node_modules/@stdlib/fs/copy-file/bin/cli new file mode 100644 index 000000000000..063a0b4c4137 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/bin/cli @@ -0,0 +1,101 @@ +#!/usr/bin/env node + +/** +* @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 resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/cli/ctor' ); +var cwd = require( '@stdlib/process/cwd' ); +var copyFile = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var dest; + var args; + var mode; + var cli; + var src; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync(resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + mode = 0; + if ( args.length < 2 && ( !flags.source || !flags.destination ) ) { + return cli.error( new Error( 'Both source and destination files must be provided' ) ); + } + if ( flags.source ) { + src = flags.source; + } + if ( flags.destination ) { + dest = flags.destination; + } + if ( flags.mode ) { + mode = parseInt( flags.mode, 8 ); + } + if ( args.length >= 2 ) { + src = resolve( cwd(), args[0] ); + dest = resolve( cwd(), args[1] ); + } + + // Perform copying... + copyFile( src, dest, mode, onCopy ); + + /** + * Callback invoked upon copying the file. + * + * @private + * @param {Error} [err] - error object + * @returns {void} + */ + function onCopy( err ) { + if ( err ) { + return cli.error( err ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt b/lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt new file mode 100644 index 000000000000..01304957b6f2 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt @@ -0,0 +1,55 @@ + +{{alias}}( src, dest[, mode], clbk ) + Asynchronously copies src to dest. + + Parameters + ---------- + src: string|Buffer + Source filename to copy. + + dest: string|Buffer + Destination filename of the copy operation. + + mode: integer (optional) + Modifiers for copy operation. Default: 0. + + clbk: Function + Callback to invoke upon copying src file to dest file. + + Examples + -------- + > function onCopy( err ) { + ... if ( err ) { + ... console.error( err.message ); + ... } + ... console.log('src.txt has been copied to dest.txt'); + ... }; + > {{alias}}( 'src.txt', 'dest.txt', onCopy ); + + +{{alias}}.sync( src, dest[, mode] ) + Synchronously copies src to dest. + + Parameters + ---------- + src: string|Buffer + Source filename to copy. + + dest: string|Buffer + Destination filename of the copy operation. + + mode: integer (optional) + Modifiers for copy operation. Default: 0. + + Returns + ------- + err: Error|null + Error object or null. + + Examples + -------- + > var err = {{alias}}.sync( 'src.txt', 'dest.txt' ); + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/fs/copy-file/docs/types/index.d.ts b/lib/node_modules/@stdlib/fs/copy-file/docs/types/index.d.ts new file mode 100644 index 000000000000..1075a8dc8a6c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/docs/types/index.d.ts @@ -0,0 +1,114 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Buffer } from 'buffer'; + +/** +* Callback invoked upon copying src file to dest file. +* +* @param err - error object +*/ +type Callback = ( err: Error | null ) => void; + +/** +* Interface for copying files. +*/ +interface CopyFile { + /** + * Asynchronously copies src to dest. + * + * @param src - source filename to copy + * @param dest - destination filename of the copy operation + * @param mode - modifiers for copy operation (default: 0) + * @param clbk - callback to invoke after copying src file to dest file + * + * @example + * var constants = require( 'fs' ).constants; + * function onCopy( error ) { + * if ( error ) { + * throw error; + * } + * } + * + * var mode = constants.COPYFILE_EXCL; + * copyFile( 'src.txt', 'dest.txt', mode, onCopy ); + */ + ( src: string | Buffer, dest: string | Buffer, mode: number, clbk: Callback ): void; + + /** + * Asynchronously copies src to dest. + * + * @param src - source filename to copy + * @param dest - destination filename of the copy operation + * @param clbk - callback to invoke after copying src file to dest file + * + * @example + * function onCopy( error ) { + * if ( error ) { + * throw error; + * } + * } + * + * copyFile( 'src.txt', 'dest.txt', onCopy ); + */ + ( src: string | Buffer, dest: string | Buffer, clbk: Callback ): void; + + /** + * Synchronously copies src to dest. + * + * @param src - source filename to copy + * @param dest - destination filename of the copy operation + * @param mode - modifiers for copy operation (default: 0) + * @returns error object or null + * + * @example + * var err = copyFileSync( 'src.txt', 'dest.txt' ); + * if ( err instanceof Error ) { + * throw err; + * } + */ + sync( src: string | Buffer, dest: string | Buffer, mode?: number ): Error | null; +} + +/** +* Asynchronously copies src to dest. +* +* @param src - source filename to copy +* @param dest - destination filename of the copy operation +* @param mode - modifiers for copy operation (default: 0) +* @param clbk - callback to invoke after copying src file to dest file +* +* @example +* function onCopy( error ) { +* if ( error ) { +* throw error; +* } +* } +* +* copyFile( 'src.txt', 'dest.txt', onCopy ); +*/ +declare var copyFile: CopyFile; + + +// EXPORTS // + +export = copyFile; diff --git a/lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts b/lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts new file mode 100644 index 000000000000..82c92d5a917c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts @@ -0,0 +1,135 @@ +/* +* @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. +*/ + +import copyFile = require( './index' ); + +const onCopy = ( error: Error | null ): void => { + if ( error ) { + throw error; + } +}; + + +// TESTS // + +// The function does not have a return value... +{ + copyFile( 'src.txt', 'dest.txt', onCopy ); // $ExpectType void +} + +// The compiler throws an error if the function is provided a first argument which is not a string or buffer... +{ + copyFile( 22, 'dest.txt', onCopy ); // $ExpectError + copyFile( false, 'dest.txt', onCopy ); // $ExpectError + copyFile( true, 'dest.txt', onCopy ); // $ExpectError + copyFile( null, 'dest.txt', onCopy ); // $ExpectError + copyFile( undefined, 'dest.txt', onCopy ); // $ExpectError + copyFile( [], 'dest.txt', onCopy ); // $ExpectError + copyFile( {}, 'dest.txt', onCopy ); // $ExpectError + copyFile( ( x: number ): number => x, 'dest.txt', onCopy ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a string or buffer... +{ + copyFile( 'src.txt', 22, onCopy ); // $ExpectError + copyFile( 'src.txt', false, onCopy ); // $ExpectError + copyFile( 'src.txt', true, onCopy ); // $ExpectError + copyFile( 'src.txt', null, onCopy ); // $ExpectError + copyFile( 'src.txt', undefined, onCopy ); // $ExpectError + copyFile( 'src.txt', [], onCopy ); // $ExpectError + copyFile( 'src.txt', {}, onCopy ); // $ExpectError + copyFile( 'src.txt', ( x: number ): number => x, onCopy ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature... +{ + copyFile( 'src.txt', 'dest.txt', 'abc' ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', 22 ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', false ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', true ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', null ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', undefined ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', [] ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', {} ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a mode argument which is not a number... +{ + copyFile( 'src.txt', 'dest.txt', false, onCopy ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', true, onCopy ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', null, onCopy ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', undefined, onCopy ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', 'mode', onCopy ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', {}, onCopy ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', [], onCopy ); // $ExpectError + copyFile( 'src.txt', 'dest.txt', ( x: number ): number => x, onCopy ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + copyFile( ); // $ExpectError + copyFile( 'C:\\foo\\bar\\baz\\srrc.txt' ); // $ExpectError + copyFile( 'C:\\foo\\bar\\baz\\src.txt', 'dest.txt' ); // $ExpectError +} + +// Attached to main export is a `sync` method which returns an error or null... +{ + copyFile.sync( 'src.txt', 'dest.txt' ); // $ExpectType Error | null +} + +// The compiler throws an error if the `sync` method is provided a first argument which is not a string or buffer... +{ + copyFile.sync( false, 'dest.txt' ); // $ExpectError + copyFile.sync( 232, 'dest.txt' ); // $ExpectError + copyFile.sync( true, 'dest.txt' ); // $ExpectError + copyFile.sync( null, 'dest.txt' ); // $ExpectError + copyFile.sync( undefined, 'dest.txt' ); // $ExpectError + copyFile.sync( [], 'dest.txt' ); // $ExpectError + copyFile.sync( {}, 'dest.txt' ); // $ExpectError + copyFile.sync( ( x: number ): number => x, 'dest.txt' ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided a second argument which is not a string or buffer... +{ + copyFile.sync( 'src.txt', false ); // $ExpectError + copyFile.sync( 'src.txt', 2322 ); // $ExpectError + copyFile.sync( 'src.txt', true ); // $ExpectError + copyFile.sync( 'src.txt', null ); // $ExpectError + copyFile.sync( 'src.txt', undefined ); // $ExpectError + copyFile.sync( 'src.txt', [] ); // $ExpectError + copyFile.sync( 'src.txt', {} ); // $ExpectError + copyFile.sync( 'src.txt', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided a mode argument which is not a number... +{ + copyFile.sync( 'src.txt', 'dest.txt', null ); // $ExpectError + copyFile.sync( 'src.txt', 'dest.txt', true ); // $ExpectError + copyFile.sync( 'src.txt', 'dest.txt', false ); // $ExpectError + copyFile.sync( 'src.txt', 'dest.txt', 'mode' ); // $ExpectError + copyFile.sync( 'src.txt', 'dest.txt', [] ); // $ExpectError + copyFile.sync( 'src.txt', 'dest.txt', {} ); // $ExpectError + copyFile.sync( 'src.txt', 'dest.txt', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an unsupported number of arguments... +{ + copyFile.sync(); // $ExpectError + copyFile.sync( 'src.txt' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/fs/copy-file/docs/usage.txt b/lib/node_modules/@stdlib/fs/copy-file/docs/usage.txt new file mode 100644 index 000000000000..e5aa984d3472 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/docs/usage.txt @@ -0,0 +1,10 @@ + +Usage: copy-file [mode] + +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. \ No newline at end of file diff --git a/lib/node_modules/@stdlib/fs/copy-file/etc/cli_opts.json b/lib/node_modules/@stdlib/fs/copy-file/etc/cli_opts.json new file mode 100644 index 000000000000..0c94ac2ae2b7 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/etc/cli_opts.json @@ -0,0 +1,30 @@ +{ + "string": [ + "source", + "destination" + ], + "integer": [ + "mode" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ], + "mode": [ + "m" + ], + "source": [ + "s" + ], + "destination": [ + "d" + ] + } +} diff --git a/lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/dest.txt b/lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/dest.txt new file mode 100644 index 000000000000..e92ac7a4a98c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/dest.txt @@ -0,0 +1 @@ +beep boop \ No newline at end of file diff --git a/lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/src.txt b/lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/src.txt new file mode 100644 index 000000000000..e92ac7a4a98c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/src.txt @@ -0,0 +1 @@ +beep boop \ No newline at end of file diff --git a/lib/node_modules/@stdlib/fs/copy-file/examples/index.js b/lib/node_modules/@stdlib/fs/copy-file/examples/index.js new file mode 100644 index 000000000000..eb04452484c4 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/examples/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +var join = require( 'path' ).join; +var constants = require( 'fs' ).constants; +var copyFile = require( './../lib' ); + +var src = join( __dirname, 'fixtures', 'src.txt' ); +var dest = join( __dirname, 'fixtures', 'dest.txt' ); + +// Synchronously copy data from src.txt to dest.txt: +var err = copyFile.sync( src, dest, constants.COPYFILE_EXCL ); +if ( err instanceof Error ) { + console.error( 'Error: %s', err.message ); +} + +// Asynchronously copy data from src.txt to dest.txt: + +copyFile( src, dest, onCopy ); + +function onCopy( error ) { + if ( error ) { + console.error( 'Error: %s', error.message ); + } else { + console.log( 'Success!' ); + } +} diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js new file mode 100644 index 000000000000..5f461915480a --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js @@ -0,0 +1,146 @@ +/** +* @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 fs = require( 'fs' ); +var createReadStream = require( 'fs' ).createReadStream; +var createWriteStream = require( 'fs' ).createWriteStream; +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isBuffer = require( '@stdlib/assert/is-buffer' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +/* eslint-disable node/no-unsupported-features/node-builtins */ +var copy; +if ( typeof fs.copyFile === 'function' ) { + copy = fs.copyFile; +} +/* eslint-enable node/no-unsupported-features/node-builtins */ + + +// MAIN // + +/** +* Asynchronously copies src to dest.By default, dest is overwritten if it already exists. +* +* @param {(string|Buffer)} src - source filename to copy +* @param {(string|Buffer)} dest - destination filename of the copy operation +* @param {(integer)} [mode] - modifiers for copy operation. +* @param {Function} clbk - callback to invoke after copying src to dest +* @throws {TypeError} first argument must be either a string or a Buffer +* @throws {TypeError} second argument must be either a string or a Buffer +* @throws {TypeError} mode argument must be a number +* @throws {TypeError} callback argument must be a function +* +* @example +* function onCopy( error ) { +* if ( error ) { +* console.error( error.message ); +* } +* console.log( 'src.txt has been copied to dest.txt' ); +* } +* +* copyFile( 'src.txt', 'dest.txt', onCopy ); +*/ +function copyFile() { + var writeStream; + var readStream; + var dest; + var mode; + var src; + var cb; + + src = arguments[0] || null; + dest = arguments[1] || null; + if ( arguments.length < 4 ) { + mode = null; + cb = arguments[2] || null; + } else { + mode = arguments[2]; + cb = arguments[3]; + } + if ( !isString( src ) && !isBuffer( src ) ) { + throw new TypeError( format( 'invalid argument. First argument must be either a string or a Buffer. Value: `%s`.', src ) ); + } + if ( !isString( dest ) && !isBuffer( dest ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be either a string or a Buffer. Value: `%s`.', dest ) ); + } + if ( !isNumber( mode ) && mode !== null ) { + throw new TypeError( format( 'invalid argument. Mode argument must be a number. Value: `%s`.', mode ) ); + } + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); + } + if ( copy ) { + copy( src, dest, mode, cb ); + } else { + readStream = createReadStream( src ); + writeStream = createWriteStream( dest ); + readStream.on( 'error', onReadError ); + writeStream.on( 'error', onWriteError ); + writeStream.on( 'finish', onFinish); + readStream.pipe( writeStream ); + } + + /** + * Callback invoked upon encountering a read error. + * + * @private + * @param {Error} err - error object + */ + function onReadError( err ) { + cb( err ); + readStream.destroy(); + writeStream.end(); + } + + /** + * Callback invoked upon encountering a write error. + * + * @private + * @param {Error} err - error object + */ + function onWriteError( err ) { + cb( err ); + readStream.destroy(); + writeStream.end(); + } + + /** + * Callback invoked upon copying a file. + * + * @private + */ + function onFinish() { + cb( null ); // Success + readStream.close(); + writeStream.close(); + } +} + + +// EXPORTS // + +module.exports = copyFile; diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/index.js b/lib/node_modules/@stdlib/fs/copy-file/lib/index.js new file mode 100644 index 000000000000..9cfff1340c9e --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/index.js @@ -0,0 +1,61 @@ +/** +* @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'; + +/** +* Copy data from one file to another. +* +* @module @stdlib/fs/copy-file +* +* @example +* var copyFile = require( '@stdlib/fs/copy-file' ); +* +* function onCopy( error ) { +* if ( error ) { +* throw error; +* } +* console.log( 'src.txt has been copied to dest.txt' ); +* } +* +* copyFile( 'src.txt', 'dest.txt', onCopy ); +* +* @example +* var copyFileSync = require( '@stdlib/fs/copy-file' ).sync; +* +* var err = copyFileSync( 'src.txt', 'dest.txt' ); +* if ( err instanceof Error ) { +* throw err; +* } +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var async = require( './async.js' ); +var sync = require( './sync.js' ); + + +// MAIN // + +setReadOnly( async, 'sync', sync ); + + +// EXPORTS // + +module.exports = async; diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js new file mode 100644 index 000000000000..eac0d91470e3 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js @@ -0,0 +1,91 @@ +/** +* @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 fs = require( 'fs' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var writeFile = require( '@stdlib/fs/write-file' ).sync; +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isBuffer = require( '@stdlib/assert/is-buffer' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +/* eslint-disable node/no-unsupported-features/node-builtins, node/no-sync */ +var copyFile; +if ( typeof fs.copyFileSync === 'function' ) { + copyFile = fs.copyFileSync; +} +/* eslint-enable node/no-unsupported-features/node-builtins, node/no-sync */ + + +// MAIN // + +/** +* Synchronously copies `src` to `dest`. By default, `dest` is overwritten if it already exists. +* +* @param {(string|Buffer)} src - source filename to copy +* @param {(string|Buffer)} dest - destination filename of the copy operation +* @param {integer} [mode] - modifiers for copy operation +* @throws {TypeError} first argument must be either a string or a Buffer +* @throws {TypeError} second argument must be either a string or a Buffer +* @throws {TypeError} third argument must be a number +* @returns {(Error|null)} error object or null +* +* @example +* var err = copyFileSync( 'src.txt', 'dest.txt' ); +* if ( err instanceof Error ) { +* console.error( err.message ); +* } +*/ +function copyFileSync( src, dest, mode ) { + var data; + if ( arguments.length < 3 ) { + mode = null; + } + if ( !isString( src ) && !isBuffer( src ) ) { + throw new TypeError( format( 'invalid argument. First argument must be either a string or a Buffer. Value: `%s`.', src ) ); + } + if ( !isString( dest ) && !isBuffer( dest ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be either a string or a Buffer. Value: `%s`.', dest ) ); + } + if ( !isNumber( mode ) && mode !== null ) { + throw new TypeError( format( 'invalid argument. Third argument must be a number. Value: `%s`.', mode ) ); + } + try { + if ( copyFile ) { + copyFile( src, dest, mode ); + } else { + data = readFile( src ); + writeFile( dest, data ); + } + } catch ( error ) { + return error; + } + return null; +} + + +// EXPORTS // + +module.exports = copyFileSync; diff --git a/lib/node_modules/@stdlib/fs/copy-file/package.json b/lib/node_modules/@stdlib/fs/copy-file/package.json new file mode 100644 index 000000000000..6707bd9a91b6 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/fs/copy-file", + "version": "0.0.0", + "description": "Copy data from one file to another.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "copy-file": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdfs", + "fs", + "copyfile", + "copyfilesync", + "mode", + "async", + "sync", + "files", + "copy", + "open", + "filesystem" + ] +} diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/copy_error.js.txt b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/copy_error.js.txt new file mode 100644 index 000000000000..f07f2856e8a6 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/copy_error.js.txt @@ -0,0 +1,44 @@ +/** +* @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. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; +proc.argv[ 2 ] = resolve( __dirname, 'fixtures', 'src.txt' ); +proc.argv[ 3 ] = resolve( __dirname, 'fixtures', 'dest.txt' ); + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin, + './../lib': copyFile +}); + +function stdin( clbk ) { + clbk( null, 'beep boop foo' ); +} + +function copyFile() { + var clbk = arguments[ arguments.length-1 ]; + setTimeout( onTimeout, 0 ); + function onTimeout() { + clbk( new Error( 'beep' ) ); + } +} diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/dest.txt b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/dest.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/src.txt b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/src.txt new file mode 100644 index 000000000000..e92ac7a4a98c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/src.txt @@ -0,0 +1 @@ +beep boop \ No newline at end of file diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt new file mode 100644 index 000000000000..7504051b5de6 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt @@ -0,0 +1,35 @@ +/** +* @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. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; +proc.argv[ 2 ] = resolve( __dirname, 'fixtures', 'src.txt' ); +proc.argv[ 3 ] = resolve( __dirname, 'fixtures', 'dest.txt' ); + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js new file mode 100644 index 000000000000..466405742594 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js @@ -0,0 +1,185 @@ +/** +* @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 fs = require( 'fs' ); +var constants = require( 'fs' ).constants; +var join = require( 'path' ).join; +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var writeFile = require( '@stdlib/fs/write-file' ).sync; +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var copyFile = require( './../lib/async.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; +var SRC = join( __dirname, 'fixtures', 'src.txt' ); +var DEST = join( __dirname, 'fixtures', 'dest.txt' ); + + +// FUNCTIONS // + +/** +* Restores a fixture file. +* +* ## Notes +* +* - Every function which has the **potential** to affect the fixture file should invoke this function immediately before calling `t.end()`. +* +* @private +*/ +function restore() { + writeFile( DEST, '' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof copyFile, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function copies data from src file to dest file (string)', opts, function test( t ) { + var expected; + expected = readFile( SRC, 'utf8' ); + copyFile( SRC, DEST, onCopy ); + + function onCopy( error ) { + var actual; + if ( error ) { + t.fail( error.message ); + } + actual = readFile( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'the function copies data from src file to dest file (buffer)', opts, function test( t ) { + var expected; + expected = readFile( SRC, 'utf8' ); + copyFile( SRC, DEST, onCopy ); + + function onCopy( error ) { + var actual; + if ( error ) { + t.fail( error.message ); + } + actual = readFile( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'the function copies data from src file to dest file using provided mode', opts, function test( t ) { + var expected; + expected = readFile( DEST, 'utf8' ); + copyFile( SRC, DEST, constants.COPYFILE_EXCL, onCopy ); + + function onCopy( error ) { + var actual; + if ( error ) { + t.strictEqual( error.code, 'EEXIST', 'expected error occurred' ); + } + actual = readFile( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'if the function encounters an error, the function returns the error', opts, function test( t ) { + var file = 'beepboopbapbop/dkfjldjHRDfakhlrsdjdf/bdlfalfas/bkldflakfHRDjas'; // non-existent directory path + copyFile( file, DEST, onCopy ); + + function onCopy( error ) { + var expected; + var actual; + + t.strictEqual( error instanceof Error, true, error.message ); + + expected = ''; + actual = readFile( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'if the function encounters an error, the function returns the error (mode provided)', opts, function test(t) { + var file = 'beepboopbapbop/dkfjldjHRDfaklsjf/bdlfalfas/bkldflakfjas'; // non-existent directory path + copyFile( file, 'beepboopbapbop', constants.COPYFILE_EXCL, onCopy ); + + function onCopy( error ) { + var expected; + var actual; + + t.strictEqual( error instanceof Error, true, error.message ); + + expected = ''; + actual = readFile( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'the function falls back to streams when `fs.copyFile` is unavailable', function test( t ) { + var copyFile; + var fsMock; + + // Mock fs without copyFile: + fsMock = { + 'createReadStream': fs.createReadStream, + 'createWriteStream': fs.createWriteStream + }; + copyFile = proxyquire( './../lib/async.js', { + 'fs': fsMock + }); + + copyFile( SRC, DEST, onCopy ); + + function onCopy( error ) { + var expected = readFile( SRC, 'utf8' ); + var actual = readFile( DEST, 'utf8' ); + + t.strictEqual( error, null, 'returns expected value' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js new file mode 100644 index 000000000000..0a4f6bc6306a --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -0,0 +1,253 @@ +/** +* @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 constants = require( 'fs' ).constants; +var resolve = require( 'path' ).resolve; +var join = require( 'path' ).join; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var writeFileSync = require( '@stdlib/fs/write-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; +var SRC = join( __dirname, 'fixtures', 'src.txt' ); +var DEST = join( __dirname, 'fixtures', 'dest.txt' ); + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// FUNCTIONS // + +/** +* Restores a fixture file. +* +* ## Notes +* +* - Every function which has the **potential** to affect the fixture file should invoke this function immediately before calling `t.end()`. +* +* @private +*/ +function restore() { + writeFileSync( DEST, '' ); +} + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected + '\n', 'prints expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected + '\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION + '\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION + '\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface copies data from a source file to a destination file', opts, function test( t ) { + var expected; + var opts; + var cmd; + + cmd = [ + EXEC_PATH, + fpath, + SRC, + DEST + ]; + opts = {}; + + expected = readFileSync( SRC, 'utf8' ); + exec( cmd.join(' '), opts, done ); + + function done( error, stdout, stderr ) { + var actual; + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + actual = readFileSync( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file has been successfully copied' ); + + restore(); + t.end(); + } +}); + +tape( 'if an error is encountered when copying file due to missing source file, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var cmd; + + cmd = [ + EXEC_PATH, + fpath, + DEST + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.ok( error instanceof Error, 'throws an error' ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: Both source and destination files must be provided\n', 'expected value' ); + + restore(); + t.end(); + } +}); + +tape( 'the command-line interface copies data from a source file to a destination file with the specified mode', opts, function test( t ) { + var expected; + var mode; + var cmd; + + expected = readFileSync( DEST, 'utf8' ); + mode = constants.COPYFILE_EXCL; // The copy operation will fail if dest already exists. + + cmd = [ + EXEC_PATH, + fpath, + SRC, + DEST, + '-m', + mode + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + var actual; + if ( error ) { + t.strictEqual( error instanceof Error, true, error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + actual = readFileSync( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'destination file contains expected contents' ); + restore(); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/test.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.js new file mode 100644 index 000000000000..192cada50448 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.js @@ -0,0 +1,38 @@ +/** +* @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 tape = require( 'tape' ); +var copyFile = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof copyFile, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to synchronously write data to a file', function test( t ) { + t.strictEqual( typeof copyFile.sync, 'function', 'has method' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js new file mode 100644 index 000000000000..0e4b5b544ba6 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js @@ -0,0 +1,170 @@ +/** +* @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 constants = require( 'fs' ).constants; +var join = require( 'path' ).join; +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var writeFile = require( '@stdlib/fs/write-file' ).sync; +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var copyFile = require( './../lib/sync.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; +var SRC = join( __dirname, 'fixtures', 'src.txt' ); +var DEST = join( __dirname, 'fixtures', 'dest.txt' ); + + +// FUNCTIONS // + +/** +* Restores a fixture file. +* +* ## Notes +* +* - Every function which has the **potential** to affect the fixture file should invoke this function immediately before calling `t.end()`. +* +* @private +*/ +function restore() { + writeFile( DEST, '' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof copyFile, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'this function copies data from src file to dest file (string)', opts, function test( t ) { + var expected; + var actual; + + expected = readFile( SRC, 'utf8' ); + copyFile( SRC, DEST ); + actual = readFile( DEST, 'utf8' ); + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +}); + +tape( 'this function copies data from src file to dest file (buffer)', opts, function test( t ) { + var expected; + var actual; + + expected = readFile( SRC, 'utf8' ); + copyFile( SRC, DEST ); + actual = readFile( DEST, 'utf8' ); + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +}); + +tape( 'the function copies data to a file using provided mode', opts, function test( t ) { + var expected = ''; + var actual; + + copyFile( SRC, DEST, constants.COPYFILE_EXCL ); + actual = readFile( DEST, 'utf8' ); // destination file won't be modified if it exists + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +}); + +tape( 'if the function encounters an error, the function returns the error', opts, function test( t ) { + var expected; + var actual; + var file; + var err; + + file = 'beepboopbapbop/dkfjldjHRDfaklsjf/bdlfalfas/bkldflakfjas'; // non-existent directory path + err = copyFile( file, DEST ); + if ( err ) { + t.strictEqual( err instanceof Error, true, err.message ); + } + + expected = ''; + actual = readFile( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); +}); + +tape( 'if the function encounters an error, the function returns the error (mode provided)', opts, function test( t ) { + var expected; + var actual; + var file; + var err; + + file = 'beepboopbapbop/dkfjldjHRDfaklsjf/bdlfalfas/bkldflakfjas'; // non-existent directory path + + err = writeFile( file, DEST, constants.COPYFILE_EXCL ); + if ( err ) { + t.strictEqual( err instanceof Error, true, err.message ); + } + + expected = ''; + actual = readFile( DEST, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); +}); + +tape( 'the function falls back to read/write streams when fs.copyFileSync is unavailable', function test( t ) { + var copyFileSync; + var expected; + var actual; + var err; + + copyFileSync = proxyquire( './../lib/sync.js', { + 'fs': {} // Intentionally omit copyFileSync + }); + + err = copyFileSync( SRC, DEST ); + + t.strictEqual( err, null, 'returns expected value' ); + + expected = readFile( SRC, 'utf8' ); + actual = readFile( DEST, 'utf8' ); + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +});