From c61af9815b7bbf72b5ec712b1f5fa2aeef7c9e86 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Tue, 23 Apr 2024 20:15:48 +0530 Subject: [PATCH 01/13] Added CopyFile utility to Stdlib/fs --- .../@stdlib/fs/copy-file/README.md | 222 ++++++++++++++++ .../fs/copy-file/benchmark/benchmark.js | 80 ++++++ .../fs/copy-file/benchmark/fixtures/dest.txt | 1 + .../fs/copy-file/benchmark/fixtures/src.txt | 1 + lib/node_modules/@stdlib/fs/copy-file/bin/cli | 101 +++++++ .../@stdlib/fs/copy-file/docs/repl.txt | 55 ++++ .../fs/copy-file/docs/types/index.d.ts | 114 ++++++++ .../@stdlib/fs/copy-file/docs/types/test.ts | 135 ++++++++++ .../@stdlib/fs/copy-file/docs/usage.txt | 10 + .../@stdlib/fs/copy-file/etc/cli_opts.json | 30 +++ .../fs/copy-file/examples/fixtures/dest.txt | 1 + .../fs/copy-file/examples/fixtures/src.txt | 1 + .../@stdlib/fs/copy-file/examples/index.js | 45 ++++ .../@stdlib/fs/copy-file/lib/async.js | 103 +++++++ .../@stdlib/fs/copy-file/lib/index.js | 61 +++++ .../@stdlib/fs/copy-file/lib/sync.js | 67 +++++ .../@stdlib/fs/copy-file/package.json | 68 +++++ .../fs/copy-file/test/fixtures/dest.txt | 0 .../fs/copy-file/test/fixtures/src.txt | 1 + .../@stdlib/fs/copy-file/test/test.async.js | 153 +++++++++++ .../@stdlib/fs/copy-file/test/test.cli.js | 251 ++++++++++++++++++ .../@stdlib/fs/copy-file/test/test.js | 38 +++ .../@stdlib/fs/copy-file/test/test.sync.js | 144 ++++++++++ 23 files changed, 1682 insertions(+) create mode 100644 lib/node_modules/@stdlib/fs/copy-file/README.md create mode 100644 lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/dest.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/benchmark/fixtures/src.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/bin/cli create mode 100644 lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/fs/copy-file/docs/usage.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/etc/cli_opts.json create mode 100644 lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/dest.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/examples/fixtures/src.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/examples/index.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/lib/async.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/lib/index.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/lib/sync.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/package.json create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/fixtures/dest.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/fixtures/src.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/test.async.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/test.js create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js 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..327d289d2c63 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/README.md @@ -0,0 +1,222 @@ + + +# 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 ); + // => 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]. + +
+ + + +
+ +## 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 join = require( 'path' ).join; +var copyFile = require( '@stdlib/fs/copy-file' ); +var constants = require( 'constants' ); + +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!!!' ); +} +``` + +
+ + + +* * * + +
+ +## 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..eacedda849f3 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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..25c45f5ff712 --- /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) 2018 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 src; + var dest; + var args; + var mode; + var cli; + + // 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 ) ) { + 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..c0f28c6782b6 --- /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 ) { + ... throw err; + ... } + ... 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..b1222d1086e1 --- /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) 2021 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..3154f261704d --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts @@ -0,0 +1,135 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 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..37f92d82f584 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 ); +// returns null + +console.log( err instanceof Error ); +// => false + +// Asynchronously copy data from src.txt to dest.txt: + +copyFile( src, dest, onCopy ); + +function onCopy( error ) { + if ( error ) { + console.error( 'Error: %s', error.message ); + } + 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..513ec7004734 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 copy = require( 'fs' ).copyFile; +var createReadStream = require( 'fs' ).createReadStream; +var createWriteStream = require( 'fs' ).createWriteStream; +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isFunction = require( '@stdlib/assert/is-function' ); +var semver = require( 'semver' ); +var VERSION = require( '@stdlib/process/node-version' ); +var format = require( '@stdlib/string/format' ); + + +// 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} src argument must be either a string or an instance of Buffer +* @throws {TypeError} dest argument must be either a string or an instance of Buffer +* @throws {TypeError} callback argument must be a function +* +* @example +* function onCopy( error ) { +* if ( error ) { +* throw error; +* } +* console.log( 'src.txt has been copied to dest.txt' ); +* } +* +* copyFile( 'src.txt', 'dest.txt', onCopy ); +*/ +function copyFile() { + var src, dest, mode, 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 ) ) { + throw new TypeError( format( 'invalid argument. The "src" argument must be of type string or an instance of Buffer. Received type: %s.', typeof src ) ); + } + if ( !isString( dest ) ) { + throw new TypeError( format( 'invalid argument. The "dest" argument must be of type string or an instance of Buffer. Received type: %s.', typeof dest ) ); + } + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. The "cb" argument must be of type function. Received : `%s`.', cb ) ); + } + if ( semver.gte( VERSION, '8.5.0' ) ) { + copy( src, dest, mode, cb ); + } else { + var readStream = createReadStream( src ); + var writeStream = createWriteStream( dest ); + readStream.on( 'error', ( err ) => { + cb( err ); + readStream.destroy(); + writeStream.end(); + }); + writeStream.on( 'error', ( err ) => { + cb( err ); + readStream.destroy(); + writeStream.end(); + }); + writeStream.on( 'finish', () => { + cb( null ); // Success + readStream.close(); + writeStream.close(); + }); + readStream.pipe( writeStream ); + } +} + + +// 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..19d67e898835 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/index.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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'; + +/** +* Copies 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..81f5dbf9c4ab --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 copyFile = require( 'fs' ).copyFileSync; // eslint-disable-line node/no-sync +var readFile = require( '@stdlib/fs/read-file' ).sync; // eslint-disable-line node/no-sync +var writeFile = require( '@stdlib/fs/write-file' ).sync; // eslint-disable-line node/no-sync +var VERSION = require( '@stdlib/process/node-version' ); +var semver = require( 'semver' ); + + +// 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. +* @returns {(Error|null)} error object or null +* +* @example +* var err = copyFileSync( 'src.txt', 'dest.txt' ); +* if ( err instanceof Error ) { +* throw err; +* } +*/ +function copyFileSync( src, dest, mode ) { + try { + if ( semver.gte( VERSION, '8.5.0' ) ) { + if ( arguments.length > 2 ) { + copyFile( src, dest, mode ); + } else { + copyFile( src, dest ); + } + } else { + var 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..229c4025ffa7 --- /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": "Copies data from one file to another file.", + "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/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/test.async.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js new file mode 100644 index 000000000000..d761d4a0949f --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js @@ -0,0 +1,153 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 tape = require( 'tape' ); +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' ); +var constants = require( 'constants' ); + + +// 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 = 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 = 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 = 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(); + } +}); 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..fe241ff3a082 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -0,0 +1,251 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 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; +var constants = require( 'constants' ); + + +// 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: beep\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 cmd; + + expected = readFileSync( SRC, 'utf8' ); + var 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 ) { + 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`' ); + } + var 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..b28e15f6dc9e --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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..21ced738cf73 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js @@ -0,0 +1,144 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 tape = require( 'tape' ); +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' ); +var constants = require( 'constants' ); + + +// 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 = readFile( SRC, 'utf8' ); + var actual; + + 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 = readFile( SRC, 'utf8' ); + var actual; + + 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(); +}); From 20f004da948ce808b649b45faa16f6f24a134814 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Tue, 23 Apr 2024 21:03:02 +0530 Subject: [PATCH 02/13] fixed a cli test --- lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index fe241ff3a082..f43ec3936acb 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -222,7 +222,7 @@ tape( 'the command-line interface copies data from a source file to a destinatio var expected; var cmd; - expected = readFileSync( SRC, 'utf8' ); + expected = readFileSync( DEST, 'utf8' ); var mode = constants.COPYFILE_EXCL; // The copy operation will fail if dest already exists. cmd = [ @@ -238,7 +238,7 @@ tape( 'the command-line interface copies data from a source file to a destinatio function done( error, stdout, stderr ) { if ( error ) { - t.fail( error.message ); + t.strictEqual( error.code, 'EEXIST', 'expected error occurred' ); } else { t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); From 107afd0ed85ac39e6cd6abafc155a614c3e8b4c5 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Tue, 23 Apr 2024 21:28:40 +0530 Subject: [PATCH 03/13] some CI checks fixed --- lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index f43ec3936acb..35ba79e42cac 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -238,7 +238,7 @@ tape( 'the command-line interface copies data from a source file to a destinatio function done( error, stdout, stderr ) { if ( error ) { - t.strictEqual( error.code, 'EEXIST', 'expected error occurred' ); + 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`' ); From d3e0ab39828b42ee3a08a1f71831ac3efd31e940 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Tue, 23 Apr 2024 21:42:38 +0530 Subject: [PATCH 04/13] some CI errors fixed --- .../test/fixtures/stdin_error.js.txt | 34 +++++++++++++++ .../test/fixtures/write_error.js.txt | 43 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/fixtures/write_error.js.txt 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..3f0c46576ec6 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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', 'file.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/fixtures/write_error.js.txt b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/write_error.js.txt new file mode 100644 index 000000000000..5d7db91ca283 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/write_error.js.txt @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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', 'file.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' ) ); + } +} From 29c68b8bdc24897b3d564ae8031b5c5623698bd9 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Tue, 23 Apr 2024 22:02:49 +0530 Subject: [PATCH 05/13] some CI checks in Run changed examples solved --- lib/node_modules/@stdlib/fs/copy-file/README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/fs/copy-file/README.md b/lib/node_modules/@stdlib/fs/copy-file/README.md index 327d289d2c63..0f474b474d8b 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/README.md +++ b/lib/node_modules/@stdlib/fs/copy-file/README.md @@ -80,7 +80,7 @@ The function accepts the same `mode` and has the same defaults as [`fs.copyFileS - 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' ); @@ -93,7 +93,7 @@ The function accepts the same `mode` and has the same defaults as [`fs.copyFileS can be replaced by an approach which addresses existence via `error` handling. - + ```javascript var copyFile = require( '@stdlib/fs/copy-file' ); @@ -117,9 +117,9 @@ The function accepts the same `mode` and has the same defaults as [`fs.copyFileS ```javascript +var constants = require( 'constants' ); var join = require( 'path' ).join; var copyFile = require( '@stdlib/fs/copy-file' ); -var constants = require( 'constants' ); var src = join( __dirname, 'examples', 'fixtures', 'src.txt' ); var dest = join( __dirname, 'examples', 'fixtures', 'dest.txt' ); @@ -213,10 +213,6 @@ $ copy-file ./examples/fixtures/src.txt ./examples/fixtures/dest.txt [node-fs]: https://nodejs.org/api/fs.html -[@stdlib/buffer/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/buffer/ctor - -[standard-stream]: https://en.wikipedia.org/wiki/Pipeline_%28Unix%29 - From bcf23dd9dce5f46f6d3d7c2c8db6fa54c4408a15 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Tue, 23 Apr 2024 22:10:15 +0530 Subject: [PATCH 06/13] some CI checks in Run changed examples solved_attempt(2) --- lib/node_modules/@stdlib/fs/copy-file/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/fs/copy-file/README.md b/lib/node_modules/@stdlib/fs/copy-file/README.md index 0f474b474d8b..37ef078de5d3 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/README.md +++ b/lib/node_modules/@stdlib/fs/copy-file/README.md @@ -80,7 +80,7 @@ The function accepts the same `mode` and has the same defaults as [`fs.copyFileS - 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' ); @@ -93,7 +93,7 @@ The function accepts the same `mode` and has the same defaults as [`fs.copyFileS can be replaced by an approach which addresses existence via `error` handling. - + ```javascript var copyFile = require( '@stdlib/fs/copy-file' ); From 9fec68f2f1ce2c19fab2a311485ecafe8164e306 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Wed, 24 Apr 2024 12:46:51 +0530 Subject: [PATCH 07/13] Some validation improvements and ci errors solved. --- .../@stdlib/fs/copy-file/lib/async.js | 10 +++++++-- .../@stdlib/fs/copy-file/lib/sync.js | 22 ++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js index 513ec7004734..4b18e7ea07aa 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js @@ -24,6 +24,8 @@ var copy = require( 'fs' ).copyFile; 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 semver = require( 'semver' ); var VERSION = require( '@stdlib/process/node-version' ); @@ -41,6 +43,7 @@ var format = require( '@stdlib/string/format' ); * @param {Function} clbk - callback to invoke after copying src to dest * @throws {TypeError} src argument must be either a string or an instance of Buffer * @throws {TypeError} dest argument must be either a string or an instance of Buffer +* @throws {TypeError} mode argument must be a number * @throws {TypeError} callback argument must be a function * * @example @@ -64,12 +67,15 @@ function copyFile() { mode = arguments[2]; cb = arguments[3]; } - if ( !isString( src ) ) { + if ( !isString( src ) && !isBuffer( src ) ) { throw new TypeError( format( 'invalid argument. The "src" argument must be of type string or an instance of Buffer. Received type: %s.', typeof src ) ); } - if ( !isString( dest ) ) { + if ( !isString( dest ) && !isBuffer( dest ) ) { throw new TypeError( format( 'invalid argument. The "dest" argument must be of type string or an instance of Buffer. Received type: %s.', typeof dest ) ); } + if ( !isNumber( mode ) && mode !== null ) { + throw new TypeError( format( 'invalid argument. The "mode" argument must be of type number. Received type: %s.', typeof mode ) ); + } if ( !isFunction( cb ) ) { throw new TypeError( format( 'invalid argument. The "cb" argument must be of type function. Received : `%s`.', cb ) ); } diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js index 81f5dbf9c4ab..93d28d467fff 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js @@ -23,8 +23,12 @@ var copyFile = require( 'fs' ).copyFileSync; // eslint-disable-line node/no-sync var readFile = require( '@stdlib/fs/read-file' ).sync; // eslint-disable-line node/no-sync var writeFile = require( '@stdlib/fs/write-file' ).sync; // eslint-disable-line node/no-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 VERSION = require( '@stdlib/process/node-version' ); var semver = require( 'semver' ); +var format = require( '@stdlib/string/format' ); // MAIN // @@ -45,12 +49,20 @@ var semver = require( 'semver' ); */ function copyFileSync( src, dest, mode ) { try { + if ( arguments.length < 3 ) { + mode = null; + } + if ( !isString( src ) && !isBuffer( src ) ) { + throw new TypeError( format( 'invalid argument. The "src" argument must be of type string or an instance of Buffer. Received type: %s.', typeof src ) ); + } + if ( !isString( dest ) && !isBuffer( dest ) ) { + throw new TypeError( format( 'invalid argument. The "dest" argument must be of type string or an instance of Buffer. Received type: %s.', typeof dest ) ); + } + if ( !isNumber( mode ) && mode !== null ) { + throw new TypeError( format( 'invalid argument. The "mode" argument must be of type number. Received type: %s.', typeof mode ) ); + } if ( semver.gte( VERSION, '8.5.0' ) ) { - if ( arguments.length > 2 ) { - copyFile( src, dest, mode ); - } else { - copyFile( src, dest ); - } + copyFile( src, dest, mode ); } else { var data = readFile( src ); writeFile( dest, data ); From f57b5555e56ea1b1e74e9e67c3f20633e8429af5 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Wed, 24 Apr 2024 16:38:58 +0530 Subject: [PATCH 08/13] Some CI checks fixed --- .../@stdlib/fs/copy-file/docs/repl.txt | 2 +- .../fs/copy-file/docs/types/index.d.ts | 2 +- .../@stdlib/fs/copy-file/lib/async.js | 23 ++++++++----- .../@stdlib/fs/copy-file/lib/sync.js | 6 ++-- .../{write_error.js.txt => copy_error.js.txt} | 3 +- .../test/fixtures/stdin_error.js.txt | 34 ------------------- .../@stdlib/fs/copy-file/test/test.async.js | 9 +++-- .../@stdlib/fs/copy-file/test/test.cli.js | 6 ++-- .../@stdlib/fs/copy-file/test/test.sync.js | 6 ++-- 9 files changed, 35 insertions(+), 56 deletions(-) rename lib/node_modules/@stdlib/fs/copy-file/test/fixtures/{write_error.js.txt => copy_error.js.txt} (89%) delete mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt diff --git a/lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt b/lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt index c0f28c6782b6..01304957b6f2 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt +++ b/lib/node_modules/@stdlib/fs/copy-file/docs/repl.txt @@ -20,7 +20,7 @@ -------- > function onCopy( err ) { ... if ( err ) { - ... throw err; + ... console.error( err.message ); ... } ... console.log('src.txt has been copied to dest.txt'); ... }; 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 index b1222d1086e1..14b1cf17ac91 100644 --- 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 @@ -27,7 +27,7 @@ import { Buffer } from 'buffer'; * * @param err - error object */ -type Callback = (err: Error | null) => void; +type Callback = ( err: Error | null ) => void; /** * Interface for copying files. diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js index 4b18e7ea07aa..19f03b0c84aa 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js @@ -20,14 +20,14 @@ // MODULES // -var copy = require( 'fs' ).copyFile; +var copy = require( 'fs' ).copyFile; // eslint-disable-line node/no-unsupported-features/node-builtins var createReadStream = require( 'fs' ).createReadStream; var createWriteStream = require( 'fs' ).createWriteStream; +var semver = require( 'semver' ); 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 semver = require( 'semver' ); var VERSION = require( '@stdlib/process/node-version' ); var format = require( '@stdlib/string/format' ); @@ -49,7 +49,7 @@ var format = require( '@stdlib/string/format' ); * @example * function onCopy( error ) { * if ( error ) { -* throw error; +* console.error( error.message ); * } * console.log( 'src.txt has been copied to dest.txt' ); * } @@ -57,7 +57,12 @@ var format = require( '@stdlib/string/format' ); * copyFile( 'src.txt', 'dest.txt', onCopy ); */ function copyFile() { - var src, dest, mode, cb; + var src; + var dest; + var mode; + var cb; + var readStream; + var writeStream; src = arguments[0] || null; dest = arguments[1] || null; if ( arguments.length < 4 ) { @@ -82,19 +87,19 @@ function copyFile() { if ( semver.gte( VERSION, '8.5.0' ) ) { copy( src, dest, mode, cb ); } else { - var readStream = createReadStream( src ); - var writeStream = createWriteStream( dest ); - readStream.on( 'error', ( err ) => { + readStream = createReadStream( src ); + writeStream = createWriteStream( dest ); + readStream.on( 'error', function ( err ) { cb( err ); readStream.destroy(); writeStream.end(); }); - writeStream.on( 'error', ( err ) => { + writeStream.on( 'error', function ( err ) { cb( err ); readStream.destroy(); writeStream.end(); }); - writeStream.on( 'finish', () => { + writeStream.on( 'finish', function () { cb( null ); // Success readStream.close(); writeStream.close(); diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js index 93d28d467fff..c92973e19543 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js @@ -20,14 +20,14 @@ // MODULES // -var copyFile = require( 'fs' ).copyFileSync; // eslint-disable-line node/no-sync +var copyFile = require( 'fs' ).copyFileSync; // eslint-disable-line node/no-sync, node/no-unsupported-features/node-builtins +var semver = require( 'semver' ); var readFile = require( '@stdlib/fs/read-file' ).sync; // eslint-disable-line node/no-sync var writeFile = require( '@stdlib/fs/write-file' ).sync; // eslint-disable-line node/no-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 VERSION = require( '@stdlib/process/node-version' ); -var semver = require( 'semver' ); var format = require( '@stdlib/string/format' ); @@ -44,7 +44,7 @@ var format = require( '@stdlib/string/format' ); * @example * var err = copyFileSync( 'src.txt', 'dest.txt' ); * if ( err instanceof Error ) { -* throw err; +* console.error( err.message ); * } */ function copyFileSync( src, dest, mode ) { diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/write_error.js.txt b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/copy_error.js.txt similarity index 89% rename from lib/node_modules/@stdlib/fs/copy-file/test/fixtures/write_error.js.txt rename to lib/node_modules/@stdlib/fs/copy-file/test/fixtures/copy_error.js.txt index 5d7db91ca283..c5943b76f122 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/write_error.js.txt +++ b/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/copy_error.js.txt @@ -23,7 +23,8 @@ var proxyquire = require( 'proxyquire' ); var fpath = resolve( __dirname, '..', 'bin', 'cli' ); proc.stdin.isTTY = false; -proc.argv[ 2 ] = resolve( __dirname, 'fixtures', 'file.txt' ); +proc.argv[ 2 ] = resolve( __dirname, 'fixtures', 'src.txt' ); +proc.argv[ 3 ] = resolve( __dirname, 'fixtures', 'dest.txt' ); proxyquire( fpath, { '@stdlib/process/read-stdin': stdin, 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 deleted file mode 100644 index 3f0c46576ec6..000000000000 --- a/lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt +++ /dev/null @@ -1,34 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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', 'file.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 index d761d4a0949f..259ecf7b851a 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js @@ -64,7 +64,8 @@ tape( 'main export is a function', function test( t ) { }); tape( 'the function copies data from src file to dest file (string)', opts, function test( t ) { - var expected = readFile( SRC, 'utf8' ); + var expected; + expected = readFile( SRC, 'utf8' ); copyFile( SRC, DEST, onCopy ); function onCopy( error ) { @@ -81,7 +82,8 @@ tape( 'the function copies data from src file to dest file (string)', opts, func }); tape( 'the function copies data from src file to dest file (buffer)', opts, function test( t ) { - var expected = readFile( SRC, 'utf8' ); + var expected; + expected = readFile( SRC, 'utf8' ); copyFile( SRC, DEST, onCopy ); function onCopy( error ) { @@ -98,7 +100,8 @@ tape( 'the function copies data from src file to dest file (buffer)', opts, func }); tape( 'the function copies data from src file to dest file using provided mode', opts, function test( t ) { - var expected = readFile( DEST, 'utf8' ); + var expected; + expected = readFile( DEST, 'utf8' ); copyFile( SRC, DEST, constants.COPYFILE_EXCL, onCopy ); function onCopy( error ) { 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 index 35ba79e42cac..382b7cae9a7e 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -221,9 +221,10 @@ tape( 'if an error is encountered when copying file due to missing source file, 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 cmd; + var mode; expected = readFileSync( DEST, 'utf8' ); - var mode = constants.COPYFILE_EXCL; // The copy operation will fail if dest already exists. + mode = constants.COPYFILE_EXCL; // The copy operation will fail if dest already exists. cmd = [ EXEC_PATH, @@ -237,13 +238,14 @@ tape( 'the command-line interface copies data from a source file to a destinatio 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`' ); } - var actual = readFileSync( DEST, 'utf8' ); + 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.sync.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js index 21ced738cf73..318f174cad39 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js @@ -64,9 +64,10 @@ tape( 'main export is a function', function test( t ) { }); tape( 'this function copies data from src file to dest file (string)', opts, function test( t ) { - var expected = readFile( SRC, 'utf8' ); + var expected; var actual; + expected = readFile( SRC, 'utf8' ); copyFile( SRC, DEST ); actual = readFile( DEST, 'utf8' ); @@ -77,9 +78,10 @@ tape( 'this function copies data from src file to dest file (string)', opts, fun }); tape( 'this function copies data from src file to dest file (buffer)', opts, function test( t ) { - var expected = readFile( SRC, 'utf8' ); + var expected; var actual; + expected = readFile( SRC, 'utf8' ); copyFile( SRC, DEST ); actual = readFile( DEST, 'utf8' ); From dce41a7c88829fe827d813284bdac22d962cef23 Mon Sep 17 00:00:00 2001 From: Hridyanshu7 Date: Wed, 24 Apr 2024 16:52:22 +0530 Subject: [PATCH 09/13] CI errors solved --- .../test/fixtures/stdin_error.js.txt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/node_modules/@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt 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..2922960d0873 --- /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) 2018 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' ) ); +} From 447fbc6344a155d469156c6e4f6a04905e15f14d Mon Sep 17 00:00:00 2001 From: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Date: Sun, 19 May 2024 22:13:36 +0000 Subject: [PATCH 10/13] chore: update copyright years --- lib/node_modules/@stdlib/fs/copy-file/README.md | 2 +- lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js | 2 +- lib/node_modules/@stdlib/fs/copy-file/bin/cli | 2 +- lib/node_modules/@stdlib/fs/copy-file/docs/types/index.d.ts | 2 +- lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts | 2 +- lib/node_modules/@stdlib/fs/copy-file/examples/index.js | 2 +- lib/node_modules/@stdlib/fs/copy-file/lib/async.js | 2 +- lib/node_modules/@stdlib/fs/copy-file/lib/index.js | 2 +- lib/node_modules/@stdlib/fs/copy-file/lib/sync.js | 2 +- .../@stdlib/fs/copy-file/test/fixtures/copy_error.js.txt | 2 +- .../@stdlib/fs/copy-file/test/fixtures/stdin_error.js.txt | 2 +- lib/node_modules/@stdlib/fs/copy-file/test/test.async.js | 2 +- lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js | 2 +- lib/node_modules/@stdlib/fs/copy-file/test/test.js | 2 +- lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/node_modules/@stdlib/fs/copy-file/README.md b/lib/node_modules/@stdlib/fs/copy-file/README.md index 37ef078de5d3..7f16f5e56034 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/README.md +++ b/lib/node_modules/@stdlib/fs/copy-file/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2018 The Stdlib Authors. +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. diff --git a/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js b/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js index eacedda849f3..822728ed54aa 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/fs/copy-file/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/fs/copy-file/bin/cli b/lib/node_modules/@stdlib/fs/copy-file/bin/cli index 25c45f5ff712..987a2ebeb60c 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/bin/cli +++ b/lib/node_modules/@stdlib/fs/copy-file/bin/cli @@ -3,7 +3,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. 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 index 14b1cf17ac91..1075a8dc8a6c 100644 --- 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 @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2021 The Stdlib Authors. +* 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. 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 index 3154f261704d..82c92d5a917c 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts +++ b/lib/node_modules/@stdlib/fs/copy-file/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2021 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/fs/copy-file/examples/index.js b/lib/node_modules/@stdlib/fs/copy-file/examples/index.js index 37f92d82f584..057a3ae496bf 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/examples/index.js +++ b/lib/node_modules/@stdlib/fs/copy-file/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js index 19f03b0c84aa..2192cb9b357a 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/index.js b/lib/node_modules/@stdlib/fs/copy-file/lib/index.js index 19d67e898835..197bcee22ffd 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/index.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js index c92973e19543..da5ed5fb232f 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. 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 index c5943b76f122..f07f2856e8a6 100644 --- 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 @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. 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 index 2922960d0873..7504051b5de6 100644 --- 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 @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. 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 index 259ecf7b851a..097643aa730b 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. 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 index 382b7cae9a7e..c32cd5c03e25 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/fs/copy-file/test/test.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.js index b28e15f6dc9e..192cada50448 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. 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 index 318f174cad39..8f6ef5725f85 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. From 5968203cb4e95217bc9d89985f2d118a0d9ca43b Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 14 Oct 2024 09:27:06 -0400 Subject: [PATCH 11/13] chore: minor clean-up and use feature detection --- .../@stdlib/fs/copy-file/README.md | 12 +-- lib/node_modules/@stdlib/fs/copy-file/bin/cli | 12 +-- .../@stdlib/fs/copy-file/examples/index.js | 11 ++- .../@stdlib/fs/copy-file/lib/async.js | 88 +++++++++++++------ .../@stdlib/fs/copy-file/lib/index.js | 4 +- .../@stdlib/fs/copy-file/lib/sync.js | 54 +++++++----- .../@stdlib/fs/copy-file/package.json | 2 +- .../@stdlib/fs/copy-file/test/test.async.js | 22 ++--- .../@stdlib/fs/copy-file/test/test.cli.js | 6 +- .../@stdlib/fs/copy-file/test/test.sync.js | 4 +- 10 files changed, 129 insertions(+), 86 deletions(-) diff --git a/lib/node_modules/@stdlib/fs/copy-file/README.md b/lib/node_modules/@stdlib/fs/copy-file/README.md index 7f16f5e56034..23de9d4a2304 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/README.md +++ b/lib/node_modules/@stdlib/fs/copy-file/README.md @@ -45,7 +45,7 @@ copyFile( src, dest, onCopy ); function onCopy( error ) { if ( error ) { console.log( error instanceof Error ); - // => false + // => true } } ``` @@ -126,10 +126,9 @@ 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 +if ( err instanceof Error ) { + console.error( 'Error: %s', err.message ); +} // Asynchronously copies data from one file to another file: copyFile( src, dest, onCopy ); @@ -137,8 +136,9 @@ copyFile( src, dest, onCopy ); function onCopy( error ) { if ( error ) { console.error( 'Error: %s', error.message ); + } else { + console.log( 'Success!!!' ); } - console.log( 'Success!!!' ); } ``` diff --git a/lib/node_modules/@stdlib/fs/copy-file/bin/cli b/lib/node_modules/@stdlib/fs/copy-file/bin/cli index 987a2ebeb60c..063a0b4c4137 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/bin/cli +++ b/lib/node_modules/@stdlib/fs/copy-file/bin/cli @@ -39,18 +39,18 @@ var copyFile = require( './../lib' ); */ function main() { var flags; - var src; 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' + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync(resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' }) }); @@ -65,7 +65,7 @@ function main() { mode = 0; if ( args.length < 2 && ( !flags.source || !flags.destination ) ) { - cli.error( new Error( 'Both source and destination files must be provided' ) ); + return cli.error( new Error( 'Both source and destination files must be provided' ) ); } if ( flags.source ) { src = flags.source; diff --git a/lib/node_modules/@stdlib/fs/copy-file/examples/index.js b/lib/node_modules/@stdlib/fs/copy-file/examples/index.js index 057a3ae496bf..eb04452484c4 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/examples/index.js +++ b/lib/node_modules/@stdlib/fs/copy-file/examples/index.js @@ -26,12 +26,10 @@ 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 ); -// returns null - -console.log( err instanceof Error ); -// => false +if ( err instanceof Error ) { + console.error( 'Error: %s', err.message ); +} // Asynchronously copy data from src.txt to dest.txt: @@ -40,6 +38,7 @@ copyFile( src, dest, onCopy ); function onCopy( error ) { if ( error ) { console.error( 'Error: %s', error.message ); + } else { + console.log( 'Success!' ); } - 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 index 2192cb9b357a..5f461915480a 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/async.js @@ -20,18 +20,26 @@ // MODULES // -var copy = require( 'fs' ).copyFile; // eslint-disable-line node/no-unsupported-features/node-builtins +var fs = require( 'fs' ); var createReadStream = require( 'fs' ).createReadStream; var createWriteStream = require( 'fs' ).createWriteStream; -var semver = require( 'semver' ); 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 VERSION = require( '@stdlib/process/node-version' ); 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 // /** @@ -41,8 +49,8 @@ var format = require( '@stdlib/string/format' ); * @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} src argument must be either a string or an instance of Buffer -* @throws {TypeError} dest argument must be either a string or an instance of Buffer +* @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 * @@ -57,12 +65,13 @@ var format = require( '@stdlib/string/format' ); * copyFile( 'src.txt', 'dest.txt', onCopy ); */ function copyFile() { - var src; + var writeStream; + var readStream; var dest; var mode; + var src; var cb; - var readStream; - var writeStream; + src = arguments[0] || null; dest = arguments[1] || null; if ( arguments.length < 4 ) { @@ -73,39 +82,62 @@ function copyFile() { cb = arguments[3]; } if ( !isString( src ) && !isBuffer( src ) ) { - throw new TypeError( format( 'invalid argument. The "src" argument must be of type string or an instance of Buffer. Received type: %s.', typeof 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. The "dest" argument must be of type string or an instance of Buffer. Received type: %s.', typeof 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. The "mode" argument must be of type number. Received type: %s.', typeof mode ) ); + throw new TypeError( format( 'invalid argument. Mode argument must be a number. Value: `%s`.', mode ) ); } if ( !isFunction( cb ) ) { - throw new TypeError( format( 'invalid argument. The "cb" argument must be of type function. Received : `%s`.', cb ) ); + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); } - if ( semver.gte( VERSION, '8.5.0' ) ) { + if ( copy ) { copy( src, dest, mode, cb ); } else { readStream = createReadStream( src ); writeStream = createWriteStream( dest ); - readStream.on( 'error', function ( err ) { - cb( err ); - readStream.destroy(); - writeStream.end(); - }); - writeStream.on( 'error', function ( err ) { - cb( err ); - readStream.destroy(); - writeStream.end(); - }); - writeStream.on( 'finish', function () { - cb( null ); // Success - readStream.close(); - writeStream.close(); - }); + 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(); + } } diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/index.js b/lib/node_modules/@stdlib/fs/copy-file/lib/index.js index 197bcee22ffd..9cfff1340c9e 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/index.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Copies data from one file to another +* Copy data from one file to another. * * @module @stdlib/fs/copy-file * @@ -30,7 +30,7 @@ * if ( error ) { * throw error; * } -* console.log('src.txt has been copied to dest.txt'); +* console.log( 'src.txt has been copied to dest.txt' ); * } * * copyFile( 'src.txt', 'dest.txt', onCopy ); diff --git a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js index da5ed5fb232f..eac0d91470e3 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/lib/sync.js @@ -20,25 +20,36 @@ // MODULES // -var copyFile = require( 'fs' ).copyFileSync; // eslint-disable-line node/no-sync, node/no-unsupported-features/node-builtins -var semver = require( 'semver' ); -var readFile = require( '@stdlib/fs/read-file' ).sync; // eslint-disable-line node/no-sync -var writeFile = require( '@stdlib/fs/write-file' ).sync; // eslint-disable-line node/no-sync +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 VERSION = require( '@stdlib/process/node-version' ); 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. +* 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. +* @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 @@ -48,23 +59,24 @@ var format = require( '@stdlib/string/format' ); * } */ 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 ( arguments.length < 3 ) { - mode = null; - } - if ( !isString( src ) && !isBuffer( src ) ) { - throw new TypeError( format( 'invalid argument. The "src" argument must be of type string or an instance of Buffer. Received type: %s.', typeof src ) ); - } - if ( !isString( dest ) && !isBuffer( dest ) ) { - throw new TypeError( format( 'invalid argument. The "dest" argument must be of type string or an instance of Buffer. Received type: %s.', typeof dest ) ); - } - if ( !isNumber( mode ) && mode !== null ) { - throw new TypeError( format( 'invalid argument. The "mode" argument must be of type number. Received type: %s.', typeof mode ) ); - } - if ( semver.gte( VERSION, '8.5.0' ) ) { + if ( copyFile ) { copyFile( src, dest, mode ); } else { - var data = readFile( src ); + data = readFile( src ); writeFile( dest, data ); } } catch ( error ) { diff --git a/lib/node_modules/@stdlib/fs/copy-file/package.json b/lib/node_modules/@stdlib/fs/copy-file/package.json index 229c4025ffa7..6707bd9a91b6 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/package.json +++ b/lib/node_modules/@stdlib/fs/copy-file/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/fs/copy-file", "version": "0.0.0", - "description": "Copies data from one file to another file.", + "description": "Copy data from one file to another.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", 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 index 097643aa730b..dcca0360007a 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js @@ -20,13 +20,13 @@ // MODULES // +var constants = require( 'constants' ); var join = require( 'path' ).join; var tape = require( 'tape' ); 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' ); -var constants = require( 'constants' ); +var copyFile = require( './../lib/async.js' ); // VARIABLES // @@ -42,16 +42,16 @@ 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 - */ +* 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, ''); + writeFile( DEST, '' ); } 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 index c32cd5c03e25..736c2230d9e6 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -20,6 +20,7 @@ // MODULES // +var constants = require( 'constants' ); var resolve = require( 'path' ).resolve; var join = require( 'path' ).join; var exec = require( 'child_process' ).exec; @@ -29,7 +30,6 @@ 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; -var constants = require( 'constants' ); // VARIABLES // @@ -211,7 +211,7 @@ tape( 'if an error is encountered when copying file due to missing source file, t.strictEqual( error.code, 1, 'expected exit code' ); } t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); - t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.strictEqual( stderr.toString(), 'Error: Both source and destination files must be provided\n', 'expected value' ); restore(); t.end(); @@ -220,8 +220,8 @@ tape( 'if an error is encountered when copying file due to missing source file, 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 cmd; var mode; + var cmd; expected = readFileSync( DEST, 'utf8' ); mode = constants.COPYFILE_EXCL; // The copy operation will fail if dest already exists. 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 index 8f6ef5725f85..170a72704bab 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js @@ -20,13 +20,13 @@ // MODULES // +var constants = require( 'constants' ); var join = require( 'path' ).join; var tape = require( 'tape' ); 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' ); -var constants = require( 'constants' ); // VARIABLES // @@ -96,7 +96,7 @@ tape( 'the function copies data to a file using provided mode', opts, function t var actual; copyFile( SRC, DEST, constants.COPYFILE_EXCL ); - actual = readFile( DEST, 'utf8' ); //destination file won't be modified if it exists + actual = readFile( DEST, 'utf8' ); // destination file won't be modified if it exists t.strictEqual( actual, expected, 'file contains expected data' ); From a0441128ed83f8e8eab7a1e353c040a245bbc6e2 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 14 Oct 2024 10:18:18 -0400 Subject: [PATCH 12/13] test: add tests for when native functions are unavailable --- .../@stdlib/fs/copy-file/test/test.async.js | 31 ++++++++++++++++++- .../@stdlib/fs/copy-file/test/test.sync.js | 26 +++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) 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 index dcca0360007a..466405742594 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.async.js @@ -20,9 +20,11 @@ // MODULES // -var constants = require( 'constants' ); +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' ); @@ -154,3 +156,30 @@ tape( 'if the function encounters an error, the function returns the error (mode 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.sync.js b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js index 170a72704bab..0e4b5b544ba6 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.sync.js @@ -20,9 +20,10 @@ // MODULES // -var constants = require( 'constants' ); +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' ); @@ -144,3 +145,26 @@ tape( 'if the function encounters an error, the function returns the error (mode 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(); +}); From 1d42f327cdfc432b3059cbaa7bfcd14628b19dd3 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 14 Oct 2024 10:19:01 -0400 Subject: [PATCH 13/13] chore: use fs.constants consistently --- lib/node_modules/@stdlib/fs/copy-file/README.md | 2 +- lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/fs/copy-file/README.md b/lib/node_modules/@stdlib/fs/copy-file/README.md index 23de9d4a2304..f18681278f86 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/README.md +++ b/lib/node_modules/@stdlib/fs/copy-file/README.md @@ -117,7 +117,7 @@ The function accepts the same `mode` and has the same defaults as [`fs.copyFileS ```javascript -var constants = require( 'constants' ); +var constants = require( 'fs' ).constants; var join = require( 'path' ).join; var copyFile = require( '@stdlib/fs/copy-file' ); 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 index 736c2230d9e6..0a4f6bc6306a 100644 --- a/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js +++ b/lib/node_modules/@stdlib/fs/copy-file/test/test.cli.js @@ -20,7 +20,7 @@ // MODULES // -var constants = require( 'constants' ); +var constants = require( 'fs' ).constants; var resolve = require( 'path' ).resolve; var join = require( 'path' ).join; var exec = require( 'child_process' ).exec;